Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Maximize Capital

Problem Statement

Given a set of investment projects with their respective profits, we need to find the most profitable projects. We are given an initial capital and are allowed to invest only in a fixed number of projects. Our goal is to choose projects that give us the maximum profit. Write a function that returns the maximum total capital after selecting the most profitable projects.

We can start an investment project only when we have the required capital

.....

.....

.....

Like the course? Get enrolled and start learning!
Mohammed Dh Abbas

Mohammed Dh Abbas

· 2 years ago

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