jayati

502. IPO

Feb 22nd, 2023
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
  4.             int n = profits.size();
  5.         vector<pair<int,int>> projects;
  6.        
  7.         // Create a vector of pairs of profits and capital for each project
  8.         for(int i = 0; i < n; i++){
  9.             projects.push_back({capital[i], profits[i]});
  10.         }
  11.        
  12.         // Sort the projects based on their capital requirements
  13.         sort(projects.begin(), projects.end());
  14.        
  15.         priority_queue<int> pq;
  16.         int idx = 0;
  17.        
  18.         // Iterate k times to choose k distinct projects with the highest profits
  19.         for(int i = 0; i < k; i++){
  20.             while(idx < n && projects[idx].first <= w){
  21.                 pq.push(projects[idx].second);
  22.                 idx++;
  23.             }
  24.             if(!pq.empty()){
  25.                 w += pq.top();
  26.                 pq.pop();
  27.             }
  28.             else{
  29.                 break;
  30.             }
  31.         }
  32.        
  33.         return w;
  34.     }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment