LeetCode 455. Assign Cookies

问题描述

这里写图片描述

问题分析

  • 贪心问题,对两个数组进行排序,然后分配

经验教训

  • 贪心问题常和排序搭配使用

代码实现

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int count = 0;
        int gIndex = 0;
        int sIndex = 0;
        while (gIndex < g.length && sIndex < s.length) {
            if (g[gIndex] <= s[sIndex]) {
                ++count;
                ++gIndex;
            }
            ++sIndex;
        }
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/zjxxyz123/article/details/80283185