·
·
文章目录
  1. 题目介绍
  2. 复杂度
  3. 解题思路

Assign Cookies

题目介绍

LeetCode 455. Assign Cookies

复杂度

时间复杂度: O(nlogn), 空间复杂度: O(1)

解题思路

贪心算法的思想,分配的最优策略是每次分配只关注未分配饼干的最小胃口的小朋友。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
func findContentChildren(_ g: [Int], _ s: [Int]) -> Int {
let gSort = g.sorted()
let sSort = s.sorted()
var i = 0
var j = 0
while i < gSort.count && j < sSort.count {
if gSort[i] <= sSort[j] {
i += 1
}
j += 1
}
return i
}
}
**版权声明**

Ivan’s Blog by Ivan Ye is licensed under a Creative Commons BY-NC-ND 4.0 International License.
叶帆创作并维护的叶帆的博客博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证

本文首发于Ivan’s Blog | 叶帆的博客博客( http://yeziahehe.com ),版权所有,侵权必究。

本文链接:http://yeziahehe.com/2020/02/28/AssignCookies/

支持一下
扫一扫,支持yeziahehe
  • 微信扫一扫
  • 支付宝扫一扫