Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode IPO problem solution

YASH PAL, 31 July 2024

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.

Leetcode IPO problem solution

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;
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes