Problem:
Assume you is an awesome the parent and want to give your children some cookies. But, you should give a cookie at the most one. Each child I have a greed factor gi, which is the minimum size of a cookie that the child would be content with; And each cookie J has a size SJ. If sj >= GI, we can assign the cookie J to the child I, and the child I'll be content. Your goal is to maximize the number of Your content children and output the maximum number.
Note:
You may assume the greed factor are always positive.
You cannot assign more than one cookies to one child.
Example 1:
Input: [1,1]output:1explanation:you], [3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you has 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 cont Ent. You need to output 1.
Example 2:
Input: [+], [1,2,3]output:2explanation:you has 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes is big enough to gratify all of the children, you need to output 2.
Analysis:
The M biscuits to the N children, each child has a greedy value, ask the child to get the size of the cookie is greater than or equal to the size of the greedy value.
Ask for a maximum number of children can be divided into biscuits.
Summary:
The two arrays are sorted from small to large, and can be compared to each other by pointers.
1 classSolution {2 Public:3 intFindcontentchildren (vector<int>& G, vector<int>&s) {4 intCNT =0, Glen = G.size (), Slen =s.size ();5 sort (G.begin (), G.end ());6 sort (S.begin (), S.end ());7 8 inti =0, j =0;9 while(I < Glen && J <Slen) {Ten if(G[i] <=S[j]) { Onei++; AJ + +; -cnt++; - } the Else if(G[i] >S[j]) { -J + +; - } - } + - returnCNT; + } A};
Leetcode 455 Assign Cookies