aboutsummaryrefslogtreecommitdiffstats
path: root/502/main.py
blob: 08dcbaa697166f8f4507794e1a41b13235cbdd39 (plain) (blame)
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
#!/usr/bin/env python
from typing import List
import heapq


class Solution:
    def findMaximizedCapital(
        self, k: int, w: int, profits: List[int], capital: List[int]
    ) -> int:
        n = len(profits)
        projects = list(zip(capital, profits))
        print(projects)
        projects.sort()
        print(projects)

        q = []
        ptr = 0

        for _ in range(k):
            while ptr < n and projects[ptr][0] <= w:
                heapq.heappush(q, -projects[ptr][1])
                ptr += 1
            if not q:
                break
            w += -heapq.heappop(q)

        return w


if __name__ == "__main__":
    solution = Solution()
    print(solution.findMaximizedCapital(2, 0, [1, 2, 3], [0, 1, 1]))