-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path502.py
58 lines (48 loc) · 2 KB
/
502.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
__________________________________________________________________________________________________
sample 208 ms submission
from heapq import nlargest, heappop, heappush
class Solution:
def findMaximizedCapital(self, k: int, W: int, Profits: List[int], Capital: List[int]) -> int:
if W >= max(Capital):
return W + sum(nlargest(k, Profits))
n = len(Profits)
projects = [(Capital[i], Profits[i]) for i in range(n)]
projects.sort(key=lambda x: -x[0])
available = []
while k > 0:
while projects and projects[-1][0] <= W:
heappush(available, -projects.pop()[1])
if available:
W -= heappop(available)
else:
break
k -= 1
return W
__________________________________________________________________________________________________
sample 232 ms submission
from heapq import nlargest
class Solution:
def findMaximizedCapital(self, k: int, W: int, Profits: List[int], Capital: List[int]) -> int:
# to speed up: if all projects are available
if W >= max(Capital):
return W + sum(nlargest(k, Profits))
n = len(Profits)
for i in range(min(n, k)):
idx = -1
# if there are available projects,
# pick the most profitable one
for j in range(n):
if W >= Capital[j]:
if idx == -1:
idx = j
elif Profits[idx] < Profits[j]:
idx = j
# not enough capital to start any project
if idx == -1:
break
# add the profit from chosen project
# and remove the project from further consideration
W += Profits[idx]
Capital[idx] = float('inf')
return W
__________________________________________________________________________________________________