-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path502. IPO.py
30 lines (28 loc) · 998 Bytes
/
502. IPO.py
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
class Solution(object):
def findMaximizedCapital(self, k, w, profits, capital):
"""
:type k: int
:type w: int
:type profits: List[int]
:type capital: List[int]
:rtype: int
"""
import heapq
maxProfit = []
minCapital = [(c, p) for c, p in zip(capital, profits)]
heapq.heapify(minCapital)
# c is no decendency oder
for i in range(k):
while minCapital and minCapital[0][0] <= w:
# we get all project we can afford currently
c, p = heapq.heappop(minCapital)
# push them in to heap with order
heapq.heappush(maxProfit, -1 * p)
if not maxProfit:
break
# now, pick up largest profit and add to capital
w += -1 * heapq.heappop(maxProfit)
return w
s = Solution()
res = s.findMaximizedCapital(k = 2, w = 0, profits = [1,2,3], capital = [0,1,1])
print(res)