Leetcode 455 Assign Cookies

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.