From 0c9a53591555fed9a3b4dd9b457e273c8838c9a8 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Thu, 23 Feb 2023 16:38:35 +0330 Subject: 502 --- 502/main.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 502/main.py (limited to '502') diff --git a/502/main.py b/502/main.py new file mode 100755 index 0000000..08dcbaa --- /dev/null +++ b/502/main.py @@ -0,0 +1,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])) -- cgit v1.2.3