Leetcode 502 Solution

This article provides solution to leetcode question 502 (ipo)

https://leetcode.com/problems/ipo

Solution

class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        assert len(capital) == len(profits)

        d = list(zip(capital, profits))
        d.sort()

        q = []
        j = 0
        for i in range(k):
            while j < len(capital) and d[j][0] <= w:
                heapq.heappush(q, -d[j][1])
                j += 1

            if q:
                w -= heapq.heappop(q)

        return w