mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0455.分发饼干Python3代码
This commit is contained in:
@ -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
|
||||
//排序后,局部最优
|
||||
|
Reference in New Issue
Block a user