[leetcode greedy]455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

题意:把m块饼干分给n个孩子,每块饼干的尺寸为mi,每个孩子的要求尺寸为ni,最多可以使几个孩子满足?

思路:

排序,目标只分饼干,从小到大遍历饼干,只要有某个饼干可以满足某个孩子,就立马把饼干分出去,如果某块饼干不能满足最小的需求尺寸,立马舍弃这块饼干

 1 class Solution(object):
 2     def findContentChildren(self, g, s):
 3         g.sort()
 4         s.sort()
 5         n,i = 0,0
 6         for si in s:
 7             if i == len(g):
 8                 break
 9             if si >= g[i]:
10                 n += 1
11                 i += 1
12         return n
13         
原文地址:https://www.cnblogs.com/fcyworld/p/6533386.html