【LeetCode】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 g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], 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.
测试样例
1 | Input: g = [1,2,3], s = [1,1] |
1 | Input: g = [1,2], s = [1,2,3] |
说明
1 | 1 <= g.length <= 3 * 10^4 |
解题
思路
从最小的饥饿值的孩子开始,分配能满足它的最小饼干
补充:
- 先排序
- 贪心算法
- 时间复杂度
O(nlogn)
(排序)
代码
1 | import java.util.Arrays; |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论