502. IPO
手撸成功,考虑到排序的话,可以多考虑考虑heap。
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
"""
# init - add all project of capital v less than W into heap
capital_heap = []
for i in range(len(Profits)):
heapq.heappush(capital_heap, [Capital[i], Profits[i]])
heap = []
res = 0
while k > 0 and (capital_heap or heap):
while capital_heap and capital_heap[0][0] <= W:
c, p= heapq.heappop(capital_heap)
heapq.heappush(heap, [-p, c])
if not heap:
break
val, cost = heapq.heappop(heap)
val = -val
W = W + val
k -= 1
return W