Priority Queue || Optimized || Beats 100% || Explained line by line || C++, Python, Java
Intuition\n Describe your first thoughts on how to solve this problem. \n * The intuition behind this code is to maximize the available capital after selecting 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n * The intuition behind this code is to maximize the available capital after selecting up to k projects, by strategically choosing the projects with the highest profit that can be started within the current capital constraints. It does this by sorting projects by their capital requirements to quickly find the most affordable ones, then using a max-heap to efficiently select the highest-profit projects that are currently affordable, updating the available capital with each project\'s profit. This greedy approach ensures that at each step, the most beneficial project within financial reach is selected to optimize the total capital.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n * **Sort Projects by Capital:** Begin by sorting the projects based on the capital required to ensure you look at the cheapest projects first.\n\n * **Use Max-Heap for Profits:** Employ a max-heap (inverted to a min-heap using negative values) to always have quick access to the project with the highest available profit.\n\n * **Process Projects Within Capital:** As long as there are projects you can afford, add their profits (negatively) to the heap.\n\n * **Select Top Profit Projects:** For up to k iterations, choose the most profitable project you can afford by popping from the heap, increasing your capital.\n\n * **Resulting Capital:** After potentially choosing k projects, the resulting capital is the maximum capital achieved.\n   * Create pairs of profits[i] and capital[i].`\n   * Sort pairs by required capital.\n   * MaxHeap: While iterating:\n   * Push profits of affordable projects into a max heap.\n   * Pop and add top profit to capital.\n   * Repeat k times.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(n)\n\n```python []\nclass Solution:\n    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:\n        n=len(profits)\n        temp=[(capital[i], profits[i]) for i in range(n)]\n        temp.sort()\n        maxHeap=[]\n        j=0\n        while k>0:\n            while j<n and w>=temp[j][0]:\n                heapq.heappush(maxHeap,-temp[j][1])\n                j+=1\n            if not maxHeap:\n                break\n            w-=heapq.heappop(maxHeap)\n            k-=1\n        return w\n```\n```java []\nclass Solution {\n    class Pair {\n        int first;\n        int second;\n        \n        Pair(int first, int second) {\n            this.first = first;\n            this.second = second;\n        }\n    }\n    \n    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n        int j = 0;\n        int n = profits.length;\n        List<Pair> temp = new ArrayList<>();\n        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());\n\n        for (int i = 0; i < n; ++i) {\n            temp.add(new Pair(capital[i], profits[i]));\n        }\n\n        Collections.sort(temp, Comparator.comparingInt(p -> p.first));\n        \n        while (k-- > 0) {\n            while (j < n && w >= temp.get(j).first) {\n                maxHeap.offer(temp.get(j++).second);\n            }\n\n            if (maxHeap.isEmpty()) break;\n            w += maxHeap.poll();\n        }\n\n        return w;\n    }\n}\n```\n```C++ []\n#define pi pair<int, int>\nclass Solution {\npublic:\n    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {\n        ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n        int j = 0, n = profits.size();\n        vector<pi> temp;\n        priority_queue<int> maxHeap;\n\n        for(int i = 0; i < n; ++i) {\n            temp.push_back({capital[i], profits[i]});\n        }\n\n        sort(begin(temp), end(temp));\n        while(k--) {\n            while(j < n && w >= temp[j].first) {\n                maxHeap.push(temp[j++].second);\n            }\n\n            if(maxHeap.empty()) break;\n            w += maxHeap.top();\n            maxHeap.pop();\n        }\n\n        return w;\n    }\n};\n```\n
['C++', 'Java', 'Python3']