添加0455.分发饼干Python3代码

This commit is contained in:
ironartisan
2021-08-26 20:14:24 +08:00
parent f93ad90c69
commit 1b915d57bf

View File

@ -156,6 +156,7 @@ class Solution {
Python
```python3
class Solution:
# 思路1优先考虑胃饼干
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
@ -165,6 +166,20 @@ class Solution:
res += 1
return res
```
```python
class Solution:
# 思路2优先考虑胃口
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
start, count = len(s) - 1, 0
for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口
if start >= 0 and g[index] <= s[start]:
start -= 1
count += 1
return count
```
Go
```golang
//排序后,局部最优