KOTLIN and JAVA solution using 2 PriorityQueues
\u2618\uFE0F\u2618\uFE0F\u2618\uFE0F If this solution was helpful, please consider upvoting! \u2705\n\n# Intuition\nTo maximize capital, we should always select
\u2618\uFE0F\u2618\uFE0F\u2618\uFE0F If this solution was helpful, please consider upvoting! \u2705\n\n# Intuition\nTo maximize capital, we should always select the most profitable project that we can afford with the current capital. \n\nThis approach combines sorting by capital (to ensure we only pick feasible projects) and prioritizing profits (to maximize returns).\n\nTo efficiently manage this process:\n\n- Use a min-heap to sort projects by their capital requirement.\n- Use a max-heap to keep track of the most profitable projects that can be undertaken given the current capital.\n\n\n# Approach\n- Pair the projects as (capital[i], profits[i]) and sort them by the capital requirement.\n- Use a min-heap to hold the sorted projects.\n- Use a max-heap to hold the profits of projects that are feasible with the current capital.\n- Iterate up to k times:\n    - Push all feasible projects (those with capital requirement \u2264 current capital) into the max-heap.\n    - If the max-heap is not empty, pick the most profitable project, add its profit to the current capital, and remove it from the heap.\n    - If the max-heap is empty, break early (no more feasible projects).\n- Return the final capital.\n\n\n# Code\n```Kotlin []\nclass Solution {\n    fun findMaximizedCapital(k: Int, w: Int, profits: IntArray, capital: IntArray): Int {\n        val n = profits.size\n\n        // Pairing and sorting by capital requirement\n        val minCapital = PriorityQueue(compareBy<Pair<Int, Int>> { it.first })\n        for (i in profits.indices) {\n            minCapital.offer(Pair(capital[i], profits[i]))\n        }\n\n        val maxProfit = PriorityQueue<Int>(compareByDescending { it })\n        var currentCapital = w\n\n        // Selecting up to k projects\n        repeat(k) {\n            // Push all feasible projects into max-heap\n            while (minCapital.isNotEmpty() && minCapital.peek().first <= currentCapital) {\n                maxProfit.offer(minCapital.poll().second)\n            }\n\n            // If no feasible projects, break\n            if (maxProfit.isEmpty()) return currentCapital\n\n            // Select the most profitable project\n            currentCapital += maxProfit.poll()\n        }\n\n        return currentCapital\n    }\n}\n```\n\n\n```Java []\nclass Solution {\n    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n        int n = profits.length;\n\n        // Pairing and sorting by capital requirement\n        PriorityQueue<int[]> minCapital = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));\n        for (int i = 0; i < n; i++) {\n            minCapital.offer(new int[]{capital[i], profits[i]});\n        }\n\n        // Max-heap for profits\n        PriorityQueue<Integer> maxProfit = new PriorityQueue<>(Comparator.reverseOrder());\n\n        // Selecting up to k projects\n        while (k-- > 0) {\n            // Push all feasible projects into max-heap\n            while (!minCapital.isEmpty() && minCapital.peek()[0] <= w) {\n                maxProfit.offer(minCapital.poll()[1]);\n            }\n\n            // If no feasible projects, break\n            if (maxProfit.isEmpty()) break;\n\n            // Select the most profitable project\n            w += maxProfit.poll();\n        }\n\n        return w;\n    }\n}\n```
['Array', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Java', 'Kotlin']