beginner-friendly solution ✅|| simple Priority Queue technique 📘
Intuition\n Describe your first thoughts on how to solve this problem. \nTo maximize profit at any given capital w, we need to select a task that offers the hig
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo maximize profit at any given capital `w`, we need to select a task that offers the highest profit and has a capital requirement less than or equal to `w`. We can use a **max heap (priority queue)** to keep track of the profits of tasks whose capital requirements are less than or equal to `w`. By repeatedly selecting the task with the maximum profit, we can efficiently choose tasks until we reach the limit of `k` tasks.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Create a List of Tasks:** Form a 2D list `tasks` where each entry consists of the `capital` and `profit` of a respective task.\n2. **Sort by Capital:** Sort the `tasks` list based on the capital required for each task.\n3. **Use a Max Heap:** Initialize a max heap (priority queue) to store the profits of tasks whose capital requirements are less than or equal to the current `w`.\n4. **Iterate Through Tasks:**\n    - For each task, if its capital requirement is less than or equal to `w`, add its profit to the max heap.\n    - Poll the maximum profit from the heap and add it to `w`.\n5. **Repeat Selection:** Continue this process until you have selected `k` tasks.\n6. **Return Final Capital:** After selecting `k` tasks, return the final value of `w`.\n\n# Complexity\n- Time complexity:$$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution \n{\n    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) \n    {\n        int n = profits.length;\n        int[][] tasks = new int[n][2];\n\n        for(int i = 0; i < n; i ++)\n        {\n            tasks[i][0] = capital[i];\n            tasks[i][1] = profits[i];\n        }\n\n        Arrays.sort(tasks, Comparator.comparingDouble(o -> o[0]));\n\n        Queue<Integer> pq = new PriorityQueue(Comparator.reverseOrder());\n\n        int ptr = 0;\n        while(k > 0)\n        {\n            while((ptr < n) && (tasks[ptr][0] <= w))\n            {\n                pq.offer(tasks[ptr][1]);\n                ptr ++;\n            }\n            if(pq.size() <= 0)\n            {\n                return w;\n            }\n            w = w + pq.poll();\n            k --;\n        }\n\n        return w;\n    }\n}\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/e6643423-3c88-4d88-93d1-efc0d302e122_1718456689.68904.jpeg)\n
['Sorting', 'Heap (Priority Queue)', 'Java']