-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.txt
34 lines (30 loc) · 1.11 KB
/
cookies.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies.
But, you should give each child at most one cookie.
Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with;
and each cookie j has a size s[j].
If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content.
Your goal is to maximize the number of your content children and output the maximum number.
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int cookiesNums = s.size();
if(cookiesNums == 0) return 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int maxNum = 0;
int cookieIndex = cookiesNums - 1;
int childIndex = g.size() - 1;
while(cookieIndex >= 0 && childIndex >=0){
if(s[cookieIndex] >= g[childIndex]){
maxNum++;
cookieIndex--;
childIndex--;
}
else{
childIndex--;
}
}
return maxNum;
}
};