✅ Java | Easy | Priority Queue | Greedy | With Explanation
The basic intuition is if you have capital then you can make profits by investing on projects and keep on adding the profits to company\'s capital and this proc
**The basic intuition is if you have capital then you can make profits by investing on projects and keep on adding the profits to company\'s capital and this process will go on till the company\'s capital can be used in further project. So I used a greedy approach that if I will sort the List of Pairs according to the capital in increasing order and if any of project have same capital then i will sort them according to profit in decreasing order. Then I have atmost k works so I will run a while loop till k becomes 0. Firstly, I will add all the projects having capital less than or equal to the initial capital (w) to the PriorityQueue. In priority Queue I have greedily tried to sort it by decreasing order of profit and if any project have same profit then i will sort the capital in increasing order so that I can use less capital to get more profit. Then I kept on adding the profit to the capital value of company accordingly and for every iteration I am using the same capital value of company calculated in previous iteration to reinvest on some other work which can yield some more profits and kept on repeating till any project\'s capital exceeds the company\'s capital value. At last we return the maximum capital value.**\n```\nclass Pair{\n    int capital;\n    int profits;\n    Pair(int capital,int profits){\n        this.capital=capital;\n        this.profits=profits;\n    }\n}\nclass comp implements Comparator<Pair>{\n    public int compare(Pair p,Pair q){\n        if(p.capital==q.capital){\n            return q.profits-p.profits;\n        }\n        return p.capital-q.capital;\n    }\n}\nclass compforpq implements Comparator<Pair>{\n    public int compare(Pair p,Pair q){\n        if(p.profits==q.profits){\n            return p.capital-q.capital;\n        }\n        return q.profits-p.profits;\n    }\n}\nclass Solution {\n    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n        List<Pair> li=new ArrayList<>();\n        for(int i=0;i<capital.length;i++){\n            li.add(new Pair(capital[i],profits[i]));\n        }\n        Collections.sort(li,new comp());\n        int inicap=w;\n        int i=0;\n        PriorityQueue<Pair> pq=new PriorityQueue<>(new compforpq());\n        while(k-->0){\n            while(i<li.size() && li.get(i).capital<=inicap){\n                Pair curr=li.get(i);\n                pq.add(new Pair(curr.capital,curr.profits));\n                i++;\n            }\n            if(pq.isEmpty())\n                break;\n            Pair optimal=pq.poll();\n            inicap+=optimal.profits;\n        }\n        return inicap;\n    }\n}\n```\n![image](https://assets.leetcode.com/users/images/8b64310d-1aff-494f-b808-03da62fb3c80_1677128781.9839036.png)\n![image](https://assets.leetcode.com/users/images/bf8feeb1-b1ac-4a08-af2d-6a7163c374f1_1677129830.0696497.jpeg)\n\n
['Greedy', 'Sorting', 'Heap (Priority Queue)', 'Java']