Detailed Explanation: Java Single Heap Solution. 86% time, 100% memory
The approach in this problem is very similar to the LeetCode 857 problem, but easier.\nGiven:\n w -> initial working capital\n k -> maximum number of projects t
The approach in this problem is very similar to the LeetCode 857 problem, but easier.\nGiven:\n* w -> initial working capital\n* k -> maximum number of projects that may be done to earn profit\n* profits -> Array containing "pure" profits of the projects. (Note: this array contains pure profit and not revenue. So the value in this array can be added to capital as is. i.e., no need to subtract for working capital).\n* capital -> Array containing capital required to do the projects. (With available money \'w\', we can only start those projects whose capital is greater than \'w\', i.e, capital [i] >= w to start the project \'i\'.\n\nAlgorithm:\n1. At any given point, we have available capital \'w\' which means we can do any new project with required capital between [0,w]. \n2. To maximise our total profit, it makes sense to choose the project that gives the maximum profit. (say, max_profit)\n3. Once this profit is obtained, we now have total available capital as w + max_profit. We start again at 1.\nWe do this \'k\' times or till our working capital is no longer enough to do any more projects.\n\nEach of the above steps are done multiple times (k times). So it makes sense to optimise the computations using appropriate data structures.\nDetails of implementation, stepwise:\n* With completion of each project, profit is added to \'w\'. So Value of \'w\' is non decreasing. So the list of eligible projects we can choose will increase with each project completion. We can sort the projects in ascending order of required capital to ease this computation.\n* Data structure best suited to get the maximum/minimum value is Heap. We use max heap here to get project with maximum profit among all eligible projects.\n\nSolution:\n\n\t\n\tpublic int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n        if(capital==null || capital.length==0 || profits==null || profits.length==0 || profits.length!=capital.length)\n            return w;\n        int n = profits.length;\n\t\t\n\t\t// pc[] - profits & capital array\n        int[][] pc = new int[n][2];\n\t\t\n\t\t// max heap. heap property applied on profit\n        PriorityQueue<int[]> pq = new PriorityQueue<>(k,(a,b)->b[0]-a[0]);\n        for(int i=0;i<n;i++){\n            pc[i][0] = profits[i];\n            pc[i][1] = capital[i];\n        }\n        \n\t\t// Sort the projects in asc order of required capital\n        Arrays.sort(pc,(a,b)->a[1]-b[1]);\n        int i=0;\n        while(k-->0){\n            for(;i<n && pc[i][1] <= w;i++)\n                pq.offer(pc[i]);\n            if(pq.isEmpty()) break;\n            int project[] = pq.poll();\n            w += project[0];\n            \n        }\n        return w;\n    }\n
['Heap (Priority Queue)', 'Java']