Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
A more simple approach using a Max Heap

Mohammed Dh Abbas

May 23, 2024

from heapq import * class Solution: def findMaximumCapital(self, capital, profits, numberOfProjects, initialCapital): max_heap = [(-profits[i], capital[i]) for i in range(len(profits))] heapify(max_heap) total_capital = initialCapital for _ in range(numberOfProjects): temp = [] while max_heap: prof, cap = heappop(max_heap) if -prof + total_capital >= cap and cap <= total_capital: total_capital += -prof break else: temp.append((prof, cap)) for tup in temp: heappush(max_heap, tup) return total_capital

0

0

Comments
Comments

On this page