Problem

Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.

You are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.

Initially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.

The answer is guaranteed to fit in a 32-bit signed integer.

Examples

Example 1:

Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
Explanation: Since your initial capital is 0, you can only start the project indexed 0.
After finishing it you will obtain profit 1 and your capital becomes 1.
With capital 1, you can either start the project indexed 1 or the project indexed 2.
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.

Example 2:

Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6

Solution

Method 1 - Using Max heap

  • We have a limited capital, and we need to pick up the projects such that we have max profit. That profit feeds into capital.
  • That means we need to sort the capital array, and we can either do it by normal sorting or use min heap.
  • Then we can use max heap to keep track of all available projects that have capital requirements less than or equal to capital w, sorted by their profits in descending order.
  • Then iteratively select k projects to invest in, by removing projects from the minHeap and adding them to the maxHeap as long as their capital requirements are less than or equal to W.
  • Once k projects have been selected, terminate and return the total capital available, which is equal to the initial capital plus the sum of profits of the selected projects

Here is the video explanation:

Code

Java
Using Project class

This code is cleaner and easier to read.

class Solution {
	static class Project {
		int capital;
		int profit;
		public Project(int capital, int profit) {
			this.capital = capital;
			this.profit = profit;
		}
	}
    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        PriorityQueue<Project> minHeap = new PriorityQueue<>((a, b) -> a.capital - b.capital);
        PriorityQueue<Project> maxHeap = new PriorityQueue<>((a, b) -> b.profit - a.profit);
        
        for (int i = 0; i < profits.length; i++) {
            minHeap.add(new Project(capital[i], profits[i]));
        }

        int W = w;
        while (k-- > 0) {
            while (!minHeap.isEmpty() && minHeap.peek().capital <= W) {
                maxHeap.add(minHeap.poll());
            }
            
            if (maxHeap.isEmpty()) {
	            break;
	        }
            
            W += maxHeap.poll().profit;
        }
        
        return W;
    }
}
Without Project class

We can only use capital or profit index.

class Solution {
    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> capital[a] - capital[b]);
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> profits[b] - profits[a]);
        
        for (int i = 0; i < profits.length; i++) {
            minHeap.add(i);
        }

        int W = w;
        while (k-- > 0) {
            while (!minHeap.isEmpty() && capital[minHeap.peek()] <= W) {
                maxHeap.add(minHeap.poll());
            }
            
            if (maxHeap.isEmpty()) {
	            break;
	        }
            
            W += profits[maxHeap.poll()];
        }
        
        return W;
    }
}

Complexity

  • ⏰ Time complexity: O(n log n) - where n is number of projects, and k is number of projects we choose. O(n) to make minHeap, but then we poll out the elements from elements from minHeap and in worst case we can poll out elements which takes O(n log n), and then choosing k projects from maxHeap - which require k log n time, as we removing 1 element from heap takes log n time.
  • 🧺 Space complexity: O(n)

Dry Run

k = 3 (number of projects we can choose)
w = 40 (initial capital)
profits = [25, 30, 10, 50, 5]
capital = [10, 20, 30, 40, 50]
Initialization

minHeap is populated with the project projects:

minHeap: [Project(10, 25), Project(20, 30), Project(30, 10), Project(40, 50), Project(50, 5)]
Choosing k projects
Iteration 1

k = 3, W = 40

Choose all the projects where capital required is below or equal to capital we have

minHeap: [Project(50, 5)]
maxHeap: [Project(40, 50), Project(20, 30), Project(10, 25), Project(30, 10)]

We poll the most profitable project from maxHeap and add its profit to W.

W += 50 = 90
maxHeap after polling: [Project(20, 30), Project(10, 25), Project(30, 10)]
Iteration 2

k = 2, W = 90

Choose all the projects where capital required is below or equal to capital we have:

minHeap[]
maxHeap: [Project(20, 30), Project(10, 25), Project(30, 10), Project(50, 5)]

We poll the most profitable project from maxHeap and add its profit to W.

W += 30 = 120
maxHeap after polling: [Project(10, 25), Project(30, 10), Project(50, 5)]
Iteration 3

k = 1, W = 120

Choose all the projects where capital required is below or equal to capital we have:

minHeap[]
maxHeap: [Project(10, 25), Project(30, 10), Project(50, 5)]

We poll the most profitable project from maxHeap and add its profit to W.

W += 25 = 130
maxHeap after polling: [Project(30, 10), Project(50, 5)]
Completion

As we have chosen all k projects, we are done.