class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort();
s.sort();
child = 0;
cookies = 0;
while (child<len(g) and cookies<len(s)):
if g[child]<=s[cookies]:
child +=1;
cookies +=1;
return child;
贪心算法系列的起始篇。
本来想着用for循环来做,但是child和cookies不是遍历就能做的,因为之前是排过序的,一一比较的话会浪费很多不必要的算力,因为如果胃口值比饼干值小,这样的确满足了,但是需要做下一个胃口值和饼干值的判断的时候,胃口值和饼干值要同时后移,这样如果用两层for循环就很难做到,不如用while来直接做到终止条件的判断,每次判断成功就换一个child来判断,要是饼干被吃掉,就换一块饼干来比较。