題目:原題鏈接(簡單)
解法 | 時間複雜度 | 空間複雜度 | 執行用時 |
---|---|---|---|
Ans 1 (Python) | O(mlogm+nlogn) | O(m+n) | 212ms (53.19%) |
LeetCode的Python執行用時隨緣,只要時間複雜度沒有明顯差異,執行用時一般都在同一個量級,僅作參考意義。
解法一:
【思路】
如果滿足的儘量多的話,那應該優先滿足胃口小的孩子。
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
idx1 = 0
idx2 = 0
while idx1 < len(g) and idx2 < len(s):
if g[idx1] <= s[idx2]:
idx1 += 1
idx2 += 1
else:
idx2 += 1
return idx1