Skip to content

Commit 41380f7

Browse files
added one misc question assign cookies
1 parent c43111a commit 41380f7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Miscellaneous/AssignCookies.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
2+
3+
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.
4+
5+
6+
7+
Input: g = [1,2,3], s = [1,1]
8+
Output: 1
9+
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
10+
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
11+
You need to output 1.
12+
13+
14+
15+
sort(s.begin(), s.end());
16+
sort(g.begin(), g.end());
17+
int i=0, j=0;
18+
while(i<g.size() && j<s.size()){
19+
if(s[j]>=g[i]){
20+
count++;
21+
i++;
22+
j++;
23+
}
24+
else{
25+
j++;
26+
}
27+
}
28+
29+
return count;

0 commit comments

Comments
 (0)