✅easiest-soln | Goldman Sachs🔥| well-explained👁️
If you found my answer helpful, please consider giving it an upvote\uD83D\uDE0A\nReviseWithArsh #6Companies30Days Challenge 2023\nChallenge Company 2 : Goldman 
## If you found my answer helpful, please consider giving it an upvote\uD83D\uDE0A\n**ReviseWithArsh #6Companies30Days Challenge 2023\nChallenge Company 2 : Goldman Sachs\nQ9. IPO**\n\n### Approach\n\n![9.jpg](https://assets.leetcode.com/users/images/fd117b53-eb1f-4648-8c3a-e523a971f0bc_1673445011.7832413.jpeg)\n\n\n### Complexity\n- **Time complexity of O(nlogn)** where n is the number of projects. Because of the sorting and priority queue operations\n- **Space complexity of O(n)** is due to the usage of priority queue and project array\n\n# Code\n```\nclass Solution {\npublic:\n    int findMaximizedCapital(int investmentOpportunities, int initialCapital, vector<int>& profits, vector<int>& capitalRequirements) {\n        // Create an array of pairs, where each pair contains the capital required and its corresponding profit for a project\n        vector <pair <int,int>> projects;\n        for(int i = 0; i<profits.size(); i++) projects.push_back({capitalRequirements[i],profits[i]});\n        // Sort the array by capital required\n        sort(projects.begin(),projects.end());\n        \n        // Use a priority queue to store the profits of projects with capital requirements less than or equal to the current capital\n        priority_queue <int> pq;\n        int currentCapital = initialCapital, i=0;\n        // Continue until no more investments are available or all projects have been considered\n        while(i<projects.size() && investmentOpportunities) {\n            // If the current capital is greater than or equal to the capital requirement of a project\n            if(currentCapital >= projects[i].first) {\n                // add the project to priority queue\n                pq.push(projects[i++].second); \n            } else {\n                // if there is a project in priority queue with lower capital requirement than current capital, \n                // we extract the project with highest profit\n                if(!pq.empty()){\n                    currentCapital += pq.top();\n                    pq.pop();\n                    investmentOpportunities--;\n                }\n                else return currentCapital; // no investment opportunities remaining\n            }\n        }\n        // Add remaining projects to the current capital\n        while(!pq.empty() && investmentOpportunities > 0) {\n            currentCapital += pq.top();\n            pq.pop();\n            investmentOpportunities--;\n        }\n        // Return the final capital\n        return currentCapital;\n    }\n};\n\n```\n- This code finds the maximum final capital that a company can obtain by investing in k projects.\n- It takes an initial capital, and for each project, an array of profits and an array of capital requirements.\n- It first creates a pair of capital requirements and profits for each project, sorts it by capital requirements, and then uses a priority queue to keep track of the maximum profits for projects that have capital requirements less than or equal to the current capital.\n- For each new capital requirement that is less than or equal to the current capital, it is added to the priority queue. When the current capital is less than the capital requirement of a project, it pops the project with the highest profit from the priority queue, adds its profit to the current capital, and decrements the number of investments available.
['C++']