Python solution using heap (266 ms)


  • 0
    S
    from collections import deque
    from heapq import heappop, heappush
    
    class Solution(object):
    def findMaximizedCapital(self, k, W, Profits, Capital):
        h = []
        ret = W
        pc = deque(sorted(zip(Profits, Capital), key=lambda x: x[1]))
        for _ in range(k):
            while pc:
                _,c = pc[0]  # peak 
                if c > ret:
                    break
                p,c = pc.popleft()
                heappush(h, (-p, p))
            if h:
                _,p = heappop(h)
                ret += p 
        return ret
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.