Typescript/Priority-Queue (Heap) Clean Intuitive Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nFor each iteration, we basically need to get a list of project that can be done with th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor each iteration, we basically need to get a list of project that can be done with the current capital at hand.\n\nThen we need to get the most profitable out of that list and add to our current capital at hand.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNote: The capital at hand can never decrease, so you don\'t have to iterate over a project more than once.\n\nFor each step <b>(k - 0)</b>\n    - Loop through the list of doable projects(project\'s capital < capital at hand), and add their profits to a max priority queue.\n    - If queue is empty, return capital at hand\n    - Otherwise, add maximum profit in queue to capital at hand.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(nlogn)$$\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```\nfunction findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number {\n    // consider capital at hand first at every iteration\n    // get a list of projects you can do with that capital,\n    // pick most profitable project to do from that list.\n\n    // Note: profit is not (profit[i]-capital[i]), it is just (profit[i])\n\n    let queue = new MaxPriorityQueue();\n    \n    let combinedData: number[][] = capital.map((item, idx) => [item, profits[idx]]);\n    combinedData.sort((a,b) => a[0]-b[0]);\n\n    let currentCapital: number = w;\n    let currentIdx: number = 0;\n\n    while(k > 0) {\n\n        while(currentIdx < combinedData.length && combinedData[currentIdx][0] <= currentCapital){\n            const [capital, profit] = combinedData[currentIdx];\n\n            queue.enqueue(profit);\n\n            currentIdx += 1;\n        }\n\n        if(queue.size() === 0){\n            return currentCapital;\n        }\n\n        const {element} = queue.dequeue();\n        currentCapital += element;\n\n        k-=1;\n    }\n\n    return currentCapital;\n};\n```
['Sorting', 'Heap (Priority Queue)', 'TypeScript']