Posted on 2024-01-01 21:13
Uriel 閱讀(35)
評論(0) 編輯 收藏 引用 所屬分類:
貪心 、
閑來無事重切Leet Code
數組g表示每個小盆友需要什么size的餅干,數組s表示每個餅干的size,問這堆餅干s最多能滿足多少位小盆友的要求,貪心,分別對g和s從小到大排序,然后設兩個游標挨個比較
1 #455
2 #Runtime: 230 ms (Beats 12.76%)
3 #Memory: 15 MB (Beats 6.38%)
4
5 class Solution(object):
6 def findContentChildren(self, g, s):
7 """
8 :type g: List[int]
9 :type s: List[int]
10 :rtype: int
11 """
12 g.sort()
13 s.sort()
14 p1, p2 = 0, 0
15 while p1 < len(g) and p2 < len(s):
16 if g[p1] <= s[p2]:
17 p1 += 1
18 p2 += 1
19 else:
20 p2 += 1
21 return p1