-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh502 Daily.py
22 lines (16 loc) · 871 Bytes
/
h502 Daily.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
usableVals = []
# zip of (profits, capital) sorted by capital
# Profits are negative for later use since heapq is a minheap
capitalAndProfits = sorted([(-1 * p, c) for p, c in zip(profits, capital)],
reverse=True,
key=lambda x : x[1])
for i in range(k) :
while capitalAndProfits and capitalAndProfits[-1][1] <= w :
# Since profit is first, it will prioritize profits
heapq.heappush(usableVals, capitalAndProfits.pop())
if not usableVals :
return w
w -= heapq.heappop(usableVals)[0] # subtract the negative profit
return w