Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22182f3

Browse files
Merge pull request #43 from codewithhridoy/javascript
hard: 502. IPO
2 parents 64701e1 + a288270 commit 22182f3

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number} k - The maximum number of projects to invest in.
3+
* @param {number} initialCapital - The initial capital.
4+
* @param {number[]} profits - An array containing the profits from each project.
5+
* @param {number[]} capitalRequirements - An array containing the capital requirements for each project.
6+
* @return {number} - The maximized capital after investing in up to 'k' projects.
7+
*/
8+
9+
function findMaximizedCapital(k, initialCapital, profits, capitalRequirements) {
10+
if (initialCapital >= Math.max(...capitalRequirements)) {
11+
profits.sort((a, b) => b - a);
12+
return profits.slice(0, k).reduce((totalCapital, profit) => totalCapital + profit, initialCapital);
13+
}
14+
15+
let currentCapital = initialCapital;
16+
17+
for (let i = 0; i < k; i++) {
18+
let maxProfit = -Infinity;
19+
let selectedProjectIndex = -1;
20+
21+
for (let j = 0; j < profits.length; j++) {
22+
if (currentCapital < capitalRequirements[j]) continue;
23+
24+
if (profits[j] > maxProfit) {
25+
selectedProjectIndex = j;
26+
maxProfit = profits[j];
27+
}
28+
}
29+
30+
if (selectedProjectIndex === -1) {
31+
break;
32+
}
33+
34+
capitalRequirements[selectedProjectIndex] = Infinity;
35+
currentCapital += maxProfit;
36+
}
37+
38+
return currentCapital;
39+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
![image.png](https://assets.leetcode.com/users/images/e283c535-2d2c-40ca-a5a7-bb349f8146f1_1718424122.7133617.png)
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /