File tree Expand file tree Collapse file tree 5 files changed +91
-2
lines changed
Course Code (C++)/01-Assign-Cookies
Course Code (Java)/01-Assign-Cookies/src Expand file tree Collapse file tree 5 files changed +91
-2
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ project(01_Assign_Cookies)
3
3
4
4
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
5
6
- set (SOURCE_FILES main .cpp )
6
+ set (SOURCE_FILES main2 .cpp )
7
7
add_executable (01_Assign_Cookies ${SOURCE_FILES} )
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ using namespace std;
6
6
7
7
// / 455. Assign Cookies
8
8
// / https://leetcode.com/problems/assign-cookies/description/
9
+ // / 先尝试满足最贪心的小朋友
9
10
// / 时间复杂度: O(nlogn)
10
11
// / 空间复杂度: O(1)
11
12
class Solution {
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < algorithm>
4
+
5
+ using namespace std ;
6
+
7
+ // / 455. Assign Cookies
8
+ // / https://leetcode.com/problems/assign-cookies/description/
9
+ // / 先尝试满足最不贪心的小朋友
10
+ // / 时间复杂度: O(nlogn)
11
+ // / 空间复杂度: O(1)
12
+ class Solution {
13
+ public:
14
+ int findContentChildren (vector<int >& g, vector<int >& s) {
15
+
16
+ sort (g.begin (), g.end ());
17
+ sort (s.begin (), s.end ());
18
+
19
+ int gi = 0 , si = 0 ;
20
+ int res = 0 ;
21
+ while (gi < g.size () && si < s.size ()){
22
+ if (s[si] >= g[gi]){
23
+ res ++;
24
+ si ++;
25
+ gi ++;
26
+ }
27
+ else
28
+ si ++;
29
+ }
30
+
31
+ return res;
32
+ }
33
+ };
34
+
35
+ int main () {
36
+
37
+ int g1[] = {1 , 2 , 3 };
38
+ vector<int > gv1 (g1, g1 + sizeof (g1)/sizeof (int ));
39
+ int s1[] = {1 , 1 };
40
+ vector<int > sv1 (s1, s1 + sizeof (s1)/sizeof (int ));
41
+ cout << Solution ().findContentChildren (gv1, sv1) << endl;
42
+
43
+ int g2[] = {1 , 2 };
44
+ vector<int > gv2 (g2, g2 + sizeof (g2)/sizeof (int ));
45
+ int s2[] = {1 , 2 , 3 };
46
+ vector<int > sv2 (s2, s2 + sizeof (s2)/sizeof (int ));
47
+ cout << Solution ().findContentChildren (gv2, sv2) << endl;
48
+
49
+ return 0 ;
50
+ }
Original file line number Diff line number Diff line change 2
2
3
3
/// 455. Assign Cookies
4
4
/// https://leetcode.com/problems/assign-cookies/description/
5
+ /// 先尝试满足最贪心的小朋友
5
6
/// 时间复杂度: O(nlogn)
6
7
/// 空间复杂度: O(1)
7
-
8
8
public class Solution {
9
9
10
10
public int findContentChildren (int [] g , int [] s ) {
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
3
+ /// 455. Assign Cookies
4
+ /// https://leetcode.com/problems/assign-cookies/description/
5
+ /// 先尝试满足最不贪心的小朋友
6
+ /// 时间复杂度: O(nlogn)
7
+ /// 空间复杂度: O(1)
8
+ public class Solution2 {
9
+
10
+ public int findContentChildren (int [] g , int [] s ) {
11
+
12
+ Arrays .sort (g );
13
+ Arrays .sort (s );
14
+
15
+ int gi = 0 , si = 0 ;
16
+ int res = 0 ;
17
+ while (gi < g .length && si < s .length ){
18
+ if (s [si ] >= g [gi ]){
19
+ res ++;
20
+ gi ++;
21
+ }
22
+ si ++;
23
+ }
24
+
25
+ return res ;
26
+ }
27
+
28
+ public static void main (String [] args ) {
29
+
30
+ int g1 [] = {1 , 2 , 3 };
31
+ int s1 [] = {1 , 1 };
32
+ System .out .println ((new Solution2 ()).findContentChildren (g1 , s1 ));
33
+
34
+ int g2 [] = {1 , 2 };
35
+ int s2 [] = {1 , 2 , 3 };
36
+ System .out .println ((new Solution2 ()).findContentChildren (g2 , s2 ));
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments