25.07.2021
小白一枚,文章仅当日记本记录学习进度 ;)
如被浏览,请多多指教,非常感谢大家纠错与建议!
(因留学顺便练习英语,所以部分用英文笔记,并无他意)
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int child = 0, cookie = 0;
while (child < g.length && cookie < s.length) {
if (s[cookie] >= g[child]) {
child ++;
}
cookie ++;
}
return child;
}
}
Greedy Algorithm 贪心算法
Greedy Algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. 保证每次操作都是局部最优解,从而使得最后结果为全局最优。
1. 对Array或String排序是常见操作,为了方便比较大小
2. 数组和字符串都是在连续空间上的有序变量合集