Easiest ✅|| Beats 100% 🔥🔥|| Java✅ || 0ms
Intuition\nThe intuition behind this code is to maximize the available capital after selecting up to k projects, by strategically choosing the projects with the
# Intuition\nThe 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* 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\n# Code\n```\nclass Solution {\n    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n       int maxCapital = 0;\n        for(int i = 0 ; i < capital.length ; i++){\n            maxCapital = Math.max(capital[i] , maxCapital);\n        }\n        \n        if(w >= maxCapital){\n            PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {\n                @Override\n                public int compare(Integer a, Integer b) {\n                    return a - b;\n                }\n            });\n            for(int p : profits){\n                maxHeap.add(p);\n                if(maxHeap.size()>k)  maxHeap.poll();\n            }\n            for(int h: maxHeap) w+=h;\n            return w;\n        }\n        \n        \n        int index ; \n        int n = profits.length;\n        for(int i = 0 ; i < Math.min(k , n) ; i++){\n            index = -1;\n            for(int j = 0 ; j<n ; ++j){\n                 if (w >= capital[j] && (index == -1 || profits[index] < profits[j])){\n                     index = j;\n                 }\n            }\n            if(index==-1) break;\n            w+=profits[index];\n            capital[index] = Integer.MAX_VALUE;\n        }\n        return w;\n    }\n}\n```\n\n```\nclass Solution {\npublic:\n    int findMaximizedCapital(int k, int w, vector<int>& profits,\n                             vector<int>& capital) {\n        int n = profits.size();\n        std::vector<std::pair<int, int>> projects;\n\n        // Creating vector of pairs (capital, profits)\n        for (int i = 0; i < n; ++i) {\n            projects.emplace_back(capital[i], profits[i]);\n        }\n\n        // Sorting projects by capital required\n        std::sort(projects.begin(), projects.end());\n\n        // Max-heap to store profits, using greater to create a max-heap\n        std::priority_queue<int> maxHeap;\n        int i = 0;\n\n        // Main loop to select up to k projects\n        for (int j = 0; j < k; ++j) {\n            // Add all profitable projects that we can afford\n            while (i < n && projects[i].first <= w) {\n                maxHeap.push(projects[i].second);\n                i++;\n            }\n\n            // If no projects can be funded, break out of the loop\n            if (maxHeap.empty()) {\n                break;\n            }\n\n            // Otherwise, take the project with the maximum profit\n            w += maxHeap.top();\n            maxHeap.pop();\n        }\n\n        return w;\n    }\n};\n```\n\n**Please upvote if you like the solution**\n![image](https://assets.leetcode.com/users/images/987fd021-2ecc-4626-a275-03f00da5bc84_1718420892.6290808.png)\n
['Array', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Java']