- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
IPO in C++
Suppose one company A wants to start its IPO soon. In order to sell a good price of its shares to B, A would like to work on some projects to increase its capital before the IPO. A has limited resources, it can only finish at most k distinct projects before the IPO. Can you help A, by designing the best way to maximize its total capital after finishing at most k distinct projects?
Suppose we have several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. At first, we have W capital. When we finish a project, we will obtain its pure profit and the profit will be added to our total capital.
To sum up, pick a list of at most k distinct projects from given projects list to maximize our final capital, and output the final maximized capital.
So if the input is like − k = 2, W = 0, profit list is like [1,2,4], capital is [0,1,1], then the output will be 5. This is because, as we have capital 0 at first, so we can start project at index 0, so we can get profit 1, so capital will be 1. With capital 1, we can start project at index 1 or 2, we will select project at index 2 to get more profit, so the final answer will be 0 + 1 + 4 = 5.
To solve this, we will follow these steps −
- Create priority queue pqCapital and pqMain
- n := size of Profits
- for initialize i := 0, when i < n, update (increase i by 1), do −
- insert { Profits[i], Capital[i] } into pqCapital
- for initialize i := 0, when i <k, update (increase i by 1), do −
- while (pqCapital is not empty and second value of top element of pqCapital <= W), do −
- insert top element of pqCapital into pqMain
- delete element from pqCapital
- if pqMain is empty, then −
- Come out from the loop
- W := W + first value of the top element of pqMain
- delete element from pqMain
- while (pqCapital is not empty and second value of top element of pqCapital <= W), do −
- return W
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; struct Comparator{ bool operator() (pair <int, int> a, pair <int, int> b){ return !(a.second < b.second); } }; class Solution { public: int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) { priority_queue < pair <int, int>, vector < pair <int, int> >, Comparator> pqCapital; priority_queue < pair <int ,int> > pqMain; int n = Profits.size(); for(int i = 0; i < n; i++){ pqCapital.push({Profits[i], Capital[i]}); } for(int i = 0; i < k; i++){ while(!pqCapital.empty() && pqCapital.top().second <= W){ pqMain.push(pqCapital.top()); pqCapital.pop(); } if(pqMain.empty()) break; W += pqMain.top().first; pqMain.pop(); } return W; } }; main(){ Solution ob; vector<int> v = {1,2,4}, v1 = {0,1,1}; cout << (ob.findMaximizedCapital(2,0, v, v1)); }
Input
2 0 [1,2,4] [0,1,1]
Output
5
To Continue Learning Please Login
Login with Google