# Leetcode IPO problem solution

In this Leetcode IPO problem solution 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.

## Problem solution in Python.

```def findMaximizedCapital(self, k, W, Profits, Capital):
main = []
for i in range(len(Profits)):
main.append([-1*Profits[i],Capital[i]])
main.sort(key=lambda x:(x[1]))
heap = []
index = 0
for i in range(k):
while index<len(main):
pro, cap = main[index]
if cap>W:
break
heapq.heappush(heap,pro)
index+=1
if heap:
W += heappop(heap)*-1
else:
break
return W```

## Problem solution in Java.

```class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
boolean[] projectTaken = new boolean[profits.length];
int nextProject = 0;
while(k>0){
int profitSoFar = 0;
for(int i=0; i<capital.length; i++){
if(capital[i] <= w && !projectTaken[i]){
if(profitSoFar < profits[i]){
profitSoFar = profits[i];
nextProject = i;
}
}
}

projectTaken[nextProject] = true;
w += profitSoFar;
k--;
}

return w;
}
}
```

## Problem solution in C++.

```class Solution {
#define p pair<int,int>
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<int> maxHeapProfit; // max heap on profit
priority_queue< p, vector<p>, greater<p> > minHeapOnCapital; // min heap on capital
for( int i=0; i < Profits.size(); i++ ) {
minHeapOnCapital.push( make_pair( Capital[i], Profits[i] ) );
}

int profit = W;
while( k ) {
while( !minHeapOnCapital.empty() && profit >= minHeapOnCapital.top().first ) {
maxHeapProfit.push( minHeapOnCapital.top().second );
minHeapOnCapital.pop();
}
if( maxHeapProfit.empty() ) break;

k--;
profit += maxHeapProfit.top();
maxHeapProfit.pop();
}
return profit;
}
};```