Skip to content

Commit 37d5d8d

Browse files
committedNov 23, 2020
feat: assign-cookies
1 parent 4b0ab2a commit 37d5d8d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
 

‎greedy.assign-cookies.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
455. 分发饼干
7+
https://leetcode-cn.com/problems/assign-cookies/description/
8+
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
9+
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。
10+
如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
11+
"""
12+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
13+
res = 0
14+
g.sort()
15+
s.sort()
16+
17+
idx = len(s) - 1
18+
for i in range(len(g) - 1, -1, -1):
19+
if idx >= 0 and s[idx] >= g[i]:
20+
res += 1
21+
idx -= 1
22+
23+
return res
24+
25+
def findContentChildrenByForce(self, g: List[int], s: List[int]) -> int:
26+
res = 0
27+
g.sort()
28+
s.sort()
29+
m = 0
30+
for i in range(len(s)):
31+
for j in range(m, len(g)):
32+
if s[i] >= g[j]:
33+
res += 1
34+
m = j + 1
35+
break
36+
37+
return res
38+
39+
40+
so = Solution()
41+
print(so.findContentChildren([10,9,8,7],[5,6,7,8]))
42+

0 commit comments

Comments
 (0)
Please sign in to comment.