455.分发饼干
455.分发饼干
思路:贪心算法,先满足要求最小的孩子的需求
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
i = 0
j = 0
#进行排序
g.sort()
s.sort()
#记录满足需求的孩子的个数
count = 0
while i < len(g) and j < len(s):
#满足需求,指针右移
if g[i] <= s[j]:
i += 1
j += 1
count += 1
#如果饼干无法满足孩子需求,饼干指针右移
else:
j += 1
return count