-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path502.py
More file actions
executable file
·33 lines (26 loc) · 1018 Bytes
/
502.py
File metadata and controls
executable file
·33 lines (26 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import heapq
class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
# sort by capital
cp = [(-p, c) for p,c in zip(profits, capital)]
cp.sort(key = lambda x:x[1])
# reachable capital heap by priority
i = 0
reachable = []
for i in range(len(cp)):
if cp[i][1]<=w:
reachable.append(cp[i])
i+=1
else: break
#heapify this by min heap;
heapq.heapify(reachable)
# we can still invest and we have k index rest
while reachable and k > 0:
p, _ = heapq.heappop(reachable)
w += -p #pulls out max profit from the reachable capital
k -= 1
#now add more items
while i < len(cp) and cp[i][1] <= w:
heapq.heappush(reachable, cp[i])
i += 1
return w