问题描述
问题分析
- 贪心问题,对两个数组进行排序,然后分配
经验教训
- 贪心问题常和排序搭配使用
代码实现
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;
}
}