[leetcode greedy]455. Assign Cookies

Posted wilderness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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         

 

以上是关于[leetcode greedy]455. Assign Cookies的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode] Greedy

[leetcode greedy]134. Gas Station

LeetCode贪心 greedy(共38题)

LeetCode贪心 greedy(共38题)

[JavaScript 刷题] 贪心 - 分配饼干, leetcode 455

LeetCode 455 Assign Cookies