455. Assign Cookies

我写的有点丑,两个for,其实复杂度也是O(n),遍历s[]。

    //  Input: [1,2], [1,2,3]
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int count = 0;
        int index = 0;
        for (int i = 0; i < g.length; i++) {
            for (int j = index; j < s.length; j++) {
                if (g[i] <= s[j]) {
                    count++;
                    index = j + 1;
                    break;
                }
            }
        }
        return count;
    }

好的写法:
greedy

Arrays.sort(g);
Arrays.sort(s);
int i = 0;
for(int j=0;i

你可能感兴趣的:(455. Assign Cookies)