Assign Cookies (split cake) _assign

Source: Internet
Author: User

Assume you are a awesome parent and want to give your cookies. But, you should give the each child at most one cookie. Each child I has a greed factor gi, which are the minimum size a cookie that's the child'll 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 I'll be content. Your goal is to maximize the number of Your content children and output the maximum number.

Note:
Your may assume the greed factor is always positive.
You are cannot assign more than one cookie to one child.

Example 1:

Input: [1,2,3], [1,1]

output:1

explanation:you have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size are both 1, you could only make the child whose greed factor are 1 cont Ent.
You are need to output 1.

Example 2:

Input: [1,2], [1,2,3]

output:2

explanation:you have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
You are have 3 cookies and their sizes are big enough to gratify all of the children, you are 
need to output 2.
public class Solution {
    //give everyone as much as possible to accept the smallest cake on the good, direct sort, two arrays from scratch on the good public
    int Findcontentchildren (int[] g, int[] s) {
        Arrays.sort (g);
        Arrays.sort (s);
        int res = 0;
        int i = 0;
        int j = 0;
        while (I < g.length && J < s.length) {
            if (G[i]<=s[j]) {
                i++;
                j + +;
                res++;
            } else{
                j + +;
            }
        }
        return res;

    }






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.