Assign Cookies

2018-05-13  本文已影响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));
    }
}
上一篇下一篇

猜你喜欢

热点阅读