贪心策略,大饼干既可以满足胃口大的孩子,也可以满足胃口小的孩子,那么应该优先满足胃口大的。局部最优就是大饼干喂给胃口大的,全局最优就是喂饱尽可能多的小孩。
class Solution:
def findContentChildren(self, g, s):
g.sort()
s.sort()
res = 0
begin = 0
for i in range(0, len(g)):
for j in range(begin, len(s)):
if s[j] >= g[i]:
res += 1
begin = j + 1
break
return res
if __name__ == "__main__":
a = Solution()
print(a.findContentChildren([1,2,3],[1,1]))
print(a.findContentChildren([1, 2], [1, 2,3]))