|
| 1 | +# [502. IPO](https://leetcode.com/problems/ipo/description) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +title: "Maximized Capital Calculation" |
| 6 | +summary: "A guide to solving the problem of finding maximized capital after investing in up to 'k' projects." |
| 7 | +date: "2024年06月15日" |
| 8 | +modifiedDate: "2024年06月15日" |
| 9 | +tags: ["algorithm", "capital", "investing"] |
| 10 | +slug: "maximized-capital-calculation" |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +# Intuition |
| 17 | + |
| 18 | +To solve the problem of finding the maximized capital after investing in up to 'k' projects, we need to consider both the capital requirements and the potential profits from each project. The goal is to strategically choose projects that yield the highest profits while respecting the capital constraints. |
| 19 | + |
| 20 | +# Approach |
| 21 | + |
| 22 | +1. **Initial Check**: If the initial capital is sufficient to fund any project, sort the profits in descending order and sum the top 'k' profits. |
| 23 | +2. **Iterative Selection**: If the initial capital is not sufficient: |
| 24 | + - Initialize the current capital with the initial capital. |
| 25 | + - For up to 'k' projects, iteratively select the project with the highest profit that can be funded with the current capital. |
| 26 | + - Update the capital and mark the selected project as funded to avoid re-selection. |
| 27 | + |
| 28 | +This approach ensures that we always choose the most profitable project available within the current capital constraints. |
| 29 | + |
| 30 | +# Complexity |
| 31 | + |
| 32 | +- **Time complexity**: $$O(k \cdot n)$$ |
| 33 | + |
| 34 | + - Where 'k' is the number of projects we can invest in, and 'n' is the number of projects. In the worst case, we may need to iterate through all projects 'k' times. |
| 35 | + |
| 36 | +- **Space complexity**: $$O(1)$$ |
| 37 | + - The space used is constant, as we are only storing a few variables for the current capital and indexes. |
| 38 | + |
| 39 | +# Code |
| 40 | + |
| 41 | +```javascript |
| 42 | +/** |
| 43 | + * @param {number} k - The maximum number of projects to invest in. |
| 44 | + * @param {number} initialCapital - The initial capital. |
| 45 | + * @param {number[]} profits - An array containing the profits from each project. |
| 46 | + * @param {number[]} capitalRequirements - An array containing the capital requirements for each project. |
| 47 | + * @return {number} - The maximized capital after investing in up to 'k' projects. |
| 48 | + */ |
| 49 | +function findMaximizedCapital(k, initialCapital, profits, capitalRequirements) { |
| 50 | + if (initialCapital >= Math.max(...capitalRequirements)) { |
| 51 | + profits.sort((a, b) => b - a); |
| 52 | + return profits |
| 53 | + .slice(0, k) |
| 54 | + .reduce((totalCapital, profit) => totalCapital + profit, initialCapital); |
| 55 | + } |
| 56 | + |
| 57 | + let currentCapital = initialCapital; |
| 58 | + |
| 59 | + for (let i = 0; i < k; i++) { |
| 60 | + let maxProfit = -Infinity; |
| 61 | + let selectedProjectIndex = -1; |
| 62 | + |
| 63 | + for (let j = 0; j < profits.length; j++) { |
| 64 | + if (currentCapital < capitalRequirements[j]) continue; |
| 65 | + |
| 66 | + if (profits[j] > maxProfit) { |
| 67 | + selectedProjectIndex = j; |
| 68 | + maxProfit = profits[j]; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (selectedProjectIndex === -1) { |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + capitalRequirements[selectedProjectIndex] = Infinity; |
| 77 | + currentCapital += maxProfit; |
| 78 | + } |
| 79 | + |
| 80 | + return currentCapital; |
| 81 | +} |
| 82 | +``` |
0 commit comments