|
| 1 | +# 502. IPO |
| 2 | +[Leetcode](https://leetcode.com/problems/ipo/) |
| 3 | + |
| 4 | +## Problem |
| 5 | + |
| 6 | +Suppose LeetCode will start its **IPO** soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the **IPO**. Since it has limited resources, it can only finish at most `k` distinct projects before the **IPO**. Help LeetCode design the best way to maximize its total capital after finishing at most `k` distinct projects. |
| 7 | + |
| 8 | +You are given `n` projects where the `ith` project has a pure profit `profits[i]` and a minimum capital of `capital[i]` is needed to start it. |
| 9 | + |
| 10 | +Initially, you have `w` capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital. |
| 11 | + |
| 12 | +Pick a list of **at most** `k` distinct projects from given projects to **maximize your final capital**, and return *the final maximized capital*. |
| 13 | + |
| 14 | +The answer is guaranteed to fit in a 32-bit signed integer. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1] |
| 20 | +Output: 4 |
| 21 | +Explanation: Since your initial capital is 0, you can only start the project indexed 0. |
| 22 | +After finishing it you will obtain profit 1 and your capital becomes 1. |
| 23 | +With capital 1, you can either start the project indexed 1 or the project indexed 2. |
| 24 | +Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital. |
| 25 | +Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4. |
| 26 | + |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2] |
| 33 | +Output: 6 |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +**Constraints:** |
| 38 | + |
| 39 | +- `1 <= k <= 105` |
| 40 | +- `0 <= w <= 109` |
| 41 | +- `n == profits.length` |
| 42 | +- `n == capital.length` |
| 43 | +- `1 <= n <= 105` |
| 44 | +- `0 <= profits[i] <= 104` |
| 45 | +- `0 <= capital[i] <= 109` |
| 46 | + |
| 47 | +## Approach |
| 48 | + |
| 49 | +To solve this problem, we can use two priority queues (heaps): |
| 50 | + |
| 51 | +1. A **min heap** (`minCapitalHeap`) to keep track of projects sorted by their minimum capital requirements. |
| 52 | +2. A **max heap** (`maxProfitHeap`) to store projects sorted by their profits in descending order. |
| 53 | + |
| 54 | +The algorithm proceeds as follows: |
| 55 | + |
| 56 | +1. Initialize `currentCapital`with the initial capital **`w`**. |
| 57 | +2. Add all projects to `minCapitalHeap`. |
| 58 | +3. For each of the **`k`** iterations: |
| 59 | + - While `minCapitalHeap`is not empty and the project’s minimum capital requirement is less than or equal to `currentCapital`, move the project from `minCapitalHeap` to `maxProfitHeap`. |
| 60 | + - If `maxProfitHeap`is empty, break the loop. |
| 61 | + - Otherwise, select the project with the highest profit from `maxProfitHeap`, add its profit to `currentCapital`, and remove it from `maxProfitHeap`. |
| 62 | + |
| 63 | +## Solution |
| 64 | + |
| 65 | +```java |
| 66 | +class Solution { |
| 67 | + public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) { |
| 68 | + // Min heap to track projects by their minimum capital requirements |
| 69 | + PriorityQueue<Integer> minCapitalHeap= new PriorityQueue<>((i, j) -> capital[i] - capital[j]); |
| 70 | + |
| 71 | + // Max heap to store projects sorted by their profits in descending order |
| 72 | + PriorityQueue<Integer> maxProfitHeap= new PriorityQueue<>((i, j) -> profits[j] - profits[i]); |
| 73 | + |
| 74 | + // Initialize current capital with the initial amount |
| 75 | + int currentCapital= w; |
| 76 | + |
| 77 | + // Add all projects to the minCapitalHeap |
| 78 | + for (int i = 0; i < capital.length; i++) |
| 79 | + minCapitalHeap.offer(i); |
| 80 | + |
| 81 | + // Select at most k distinct projects |
| 82 | + for (int i = 0; i < k; i++) { |
| 83 | + // Move projects from minCapitalHeap to maxProfitHeap if their capital requirement is met |
| 84 | + while (!minCapHeap.isEmpty() && capital[minCapitalHeap.peek()] <= currentCapital) |
| 85 | + maxProfitHeap.offer(minCapitalHeap.poll()); |
| 86 | + |
| 87 | + // If no profitable projects left, exit the loop |
| 88 | + if (maxProHeap.isEmpty()) break; |
| 89 | + |
| 90 | + // Select the project with the highest profit, update current capital |
| 91 | + currentCapital+= profits[maxProfitHeap.poll()]; |
| 92 | + } |
| 93 | + |
| 94 | + return currentCapital; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Complexities |
| 100 | + |
| 101 | +- Time complexity: O(n log n) due to the heap operations. |
| 102 | +- Space complexity: O(n) for the heaps. |
0 commit comments