mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0455.分发饼干Python3代码
This commit is contained in:
@ -156,6 +156,7 @@ class Solution {
|
|||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
# 思路1:优先考虑胃饼干
|
||||||
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
||||||
g.sort()
|
g.sort()
|
||||||
s.sort()
|
s.sort()
|
||||||
@ -165,6 +166,20 @@ class Solution:
|
|||||||
res += 1
|
res += 1
|
||||||
return res
|
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:
|
Go:
|
||||||
```golang
|
```golang
|
||||||
//排序后,局部最优
|
//排序后,局部最优
|
||||||
|
Reference in New Issue
Block a user