-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_IPO.py
More file actions
19 lines (14 loc) · 681 Bytes
/
02_IPO.py
File metadata and controls
19 lines (14 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Question link - https://leetcode.com/problems/ipo/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
maxProfit = [] #This is for maxProfit in heap
minCapital = [(c,p) for c,p in zip(capital , profits)]
heapq.heapify(minCapital)
for i in range(k):
while minCapital and minCapital[0][0] <= w:
c,p = heapq.heappop(minCapital)
heapq.heappush(maxProfit , -1 * p)
if not maxProfit:
break
w += -1 * heapq.heappop(maxProfit)
return w