码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode greedy]455. Assign Cookies

时间:2017-03-10 23:53:26      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:div   imu   ber   for   cookie   object   maximum   elf   children   

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         

 

[leetcode greedy]455. Assign Cookies

标签:div   imu   ber   for   cookie   object   maximum   elf   children   

原文地址:http://www.cnblogs.com/fcyworld/p/6533386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!