美文网首页
Assign Cookies

Assign Cookies

作者: 第六象限 | 来源:发表于2018-05-13 18:52 被阅读0次

    描述

    You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.You have 3 cookies and their sizes are big enough to gratify all of the children,You need to output 2.

    Example

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

    代码

    import java.util.Arrays;
    
    public class AssignCookies {
        public static int findContentChildren(int[] g, int[] s) {
            Arrays.sort(g);
            Arrays.sort(s);
            int gIndex = 0, sIndex = 0;
            while (gIndex < g.length && sIndex < s.length) {
                if (g[gIndex] <= s[sIndex])
                    gIndex++;
                sIndex++;
            }
            return gIndex;
        }
    
        public static void main(String[] args) {
            int[] g={2,3};
            int[] s={1,2,3};
            System.out.println(findContentChildren(g,s));
        }
    }
    

    相关文章

      网友评论

          本文标题:Assign Cookies

          本文链接:https://www.haomeiwen.com/subject/ufoldftx.html