[JavaScript] 16 lines heap solution with explanation
Runtime: 552 ms, faster than 61.11% of JavaScript online submissions for IPO.\nMemory Usage: 87.7 MB, less than 27.78% of JavaScript online submissions for IPO.
Runtime: 552 ms, faster than 61.11% of JavaScript online submissions for IPO.\nMemory Usage: 87.7 MB, less than 27.78% of JavaScript online submissions for IPO.\n\nidea: two heaps, respectively max(priority: profits) and min(priority: capital)\nstep1. put every group in maxheap\nstep2. dequeue item from maxheap, if its capital is more than our capital, then put it in minheap\nstep3. check whether there are element  in minheap that its capital is smaller than our capital or not, if yes put it in maxheap again\n```\nvar findMaximizedCapital = function(k, res, profits, capital) {     // w => res\n    let maxheap = new MaxPriorityQueue({priority: v => v[0]});\n    let minheap = new MinPriorityQueue({priority: v => v[1]});\n    for(let i = 0; i < profits.length; i++){                  //step 1\n        maxheap.enqueue([profits[i],capital[i]]);\n    }\n    while(k&&maxheap.size()){\n        let [value,limit] = maxheap.dequeue().element;        //step 2\n        if(limit<=res) k--, res+=value;    \n        else minheap.enqueue([value,limit]);\n        while(minheap.size()&&minheap.front().priority<=res){ //step 3\n        let [value,limit] = minheap.dequeue().element;    \n        maxheap.enqueue([value,limit]);    \n    }}\n    return res;\n};\n```
['JavaScript']