[LeetCode]1514. Path with Maximum Probability (Medium) Python3-2023.07.01
Posted on 2023-07-01 15:30 Uriel 閱讀(41) 評論(0) 編輯 收藏 引用 所屬分類: 搜索 、閑來無事重切Leet Code給出n堆不同數(shù)量的cookie,問分給k個人,問拿最多的那個人至少拿多少個cookie,DFS
1 #1514
2 #Runtime: 66 ms (Beats 76.87%)
3 #Memory: 16.2 MB (Beats 98.88%)
4
5 class Solution:
6 def distributeCookies(self, cookies: List[int], k: int) -> int:
7 cnt = [0] * k
8 ans = float('inf')
9 def DFS(c):
10 nonlocal ans
11 if c == len(cookies):
12 ans = min(ans, max(cnt))
13 return
14 for i in range(k):
15 cnt[i] += cookies[c]
16 DFS(c + 1)
17 cnt[i] -= cookies[c]
18 if not cnt[i]:
19 break
20
21 DFS(0)
22 return ans
2 #Runtime: 66 ms (Beats 76.87%)
3 #Memory: 16.2 MB (Beats 98.88%)
4
5 class Solution:
6 def distributeCookies(self, cookies: List[int], k: int) -> int:
7 cnt = [0] * k
8 ans = float('inf')
9 def DFS(c):
10 nonlocal ans
11 if c == len(cookies):
12 ans = min(ans, max(cnt))
13 return
14 for i in range(k):
15 cnt[i] += cookies[c]
16 DFS(c + 1)
17 cnt[i] -= cookies[c]
18 if not cnt[i]:
19 break
20
21 DFS(0)
22 return ans