-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0502-ipo.py
More file actions
32 lines (27 loc) · 853 Bytes
/
0502-ipo.py
File metadata and controls
32 lines (27 loc) · 853 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
# time complexity: O(nlogn)
# space complexity: O(n)
import heapq
from typing import List
class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
projects = []
for i in range(len(profits)):
heapq.heappush(projects, (capital[i], profits[i]))
available = []
for _ in range(k):
while projects and projects[0][0] <= w:
heapq.heappush(available, -heapq.heappop(projects)[1])
if len(available) == 0:
break
w -= heapq.heappop(available)
return w
k = 2
w = 0
profits = [1, 2, 3]
capital = [0, 1, 1]
print(Solution().findMaximizedCapital(k, w, profits, capital))
k = 3
w = 0
profits = [1, 2, 3]
capital = [0, 1, 2]
print(Solution().findMaximizedCapital(k, w, profits, capital))