Skip to content

Commit 8d4516e

Browse files
committed
+ problem 502
1 parent b5d3751 commit 8d4516e

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

Problemset/0502-IPO/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 502. IPO
2+
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.
3+
4+
You are given `n` projects where the <code>i<sup>th</sup></code> project has a pure profit `profits[i]` and a minimum capital of `capital[i]` is needed to start it.
5+
6+
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.
7+
8+
Pick a list of **at most** `k` distinct projects from given projects to **maximize your final capital**, and return *the final maximized capital*.
9+
10+
The answer is guaranteed to fit in a 32-bit signed integer.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
15+
<strong>Output:</strong> 4
16+
<strong>Explanation:</strong> Since your initial capital is 0, you can only start the project indexed 0.
17+
After finishing it you will obtain profit 1 and your capital becomes 1.
18+
With capital 1, you can either start the project indexed 1 or the project indexed 2.
19+
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
20+
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
21+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
26+
<strong>Output:</strong> 6
27+
</pre>
28+
29+
#### Constraints:
30+
* <code>1 <= k <= 10<sup>5</sup></code>
31+
* <code>0 <= w <= 10<sup>9</sup></code>
32+
* `n == profits.length`
33+
* `n == capital.length`
34+
* <code>1 <= n <= 10<sup>5</sup></code>
35+
* <code>0 <= profits[i] <= 10<sup>4</sup></code>
36+
* <code>0 <= capital[i] <= 10<sup>9</sup></code>
37+
38+
## Solutions (Rust)
39+
40+
### 1. Solution
41+
```Rust
42+
use std::collections::BinaryHeap;
43+
44+
impl Solution {
45+
pub fn find_maximized_capital(k: i32, w: i32, profits: Vec<i32>, capital: Vec<i32>) -> i32 {
46+
let mut w = w;
47+
let mut projects = capital
48+
.into_iter()
49+
.zip(profits.into_iter())
50+
.collect::<Vec<_>>();
51+
let mut heap = BinaryHeap::new();
52+
let mut i = 0;
53+
54+
projects.sort_unstable();
55+
56+
for _ in 0..k {
57+
while i < projects.len() && projects[i].0 <= w {
58+
heap.push(projects[i].1);
59+
i += 1;
60+
}
61+
62+
w += heap.pop().unwrap_or(0);
63+
}
64+
65+
w
66+
}
67+
}
68+
```

Problemset/0502-IPO/README_CN.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 502. IPO
2+
假设 力扣(LeetCode)即将开始 **IPO** 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 `k` 个不同的项目。帮助 力扣 设计完成最多 `k` 个不同项目后得到最大总资本的方式。
3+
4+
给你 `n` 个项目。对于每个项目 `i` ,它都有一个纯利润 `profits[i]` ,和启动该项目需要的最小资本 `capital[i]`
5+
6+
最初,你的资本为 `w` 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。
7+
8+
总而言之,从给定项目中选择 **最多** `k` 个不同项目的列表,以 **最大化最终资本** ,并输出最终可获得的最多资本。
9+
10+
答案保证在 32 位有符号整数范围内。
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
15+
<strong>输出:</strong> 4
16+
<strong>解释:</strong>
17+
由于你的初始资本为 0,你仅可以从 0 号项目开始。
18+
在完成后,你将获得 1 的利润,你的总资本将变为 1。
19+
此时你可以选择开始 1 号或 2 号项目。
20+
由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。
21+
因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。
22+
</pre>
23+
24+
#### 示例 2:
25+
<pre>
26+
<strong>输入:</strong> k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
27+
<strong>输出:</strong> 6
28+
</pre>
29+
30+
#### 提示:
31+
* <code>1 <= k <= 10<sup>5</sup></code>
32+
* <code>0 <= w <= 10<sup>9</sup></code>
33+
* `n == profits.length`
34+
* `n == capital.length`
35+
* <code>1 <= n <= 10<sup>5</sup></code>
36+
* <code>0 <= profits[i] <= 10<sup>4</sup></code>
37+
* <code>0 <= capital[i] <= 10<sup>9</sup></code>
38+
39+
## 题解 (Rust)
40+
41+
### 1. 题解
42+
```Rust
43+
use std::collections::BinaryHeap;
44+
45+
impl Solution {
46+
pub fn find_maximized_capital(k: i32, w: i32, profits: Vec<i32>, capital: Vec<i32>) -> i32 {
47+
let mut w = w;
48+
let mut projects = capital
49+
.into_iter()
50+
.zip(profits.into_iter())
51+
.collect::<Vec<_>>();
52+
let mut heap = BinaryHeap::new();
53+
let mut i = 0;
54+
55+
projects.sort_unstable();
56+
57+
for _ in 0..k {
58+
while i < projects.len() && projects[i].0 <= w {
59+
heap.push(projects[i].1);
60+
i += 1;
61+
}
62+
63+
w += heap.pop().unwrap_or(0);
64+
}
65+
66+
w
67+
}
68+
}
69+
```

Problemset/0502-IPO/Solution.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::BinaryHeap;
2+
3+
impl Solution {
4+
pub fn find_maximized_capital(k: i32, w: i32, profits: Vec<i32>, capital: Vec<i32>) -> i32 {
5+
let mut w = w;
6+
let mut projects = capital
7+
.into_iter()
8+
.zip(profits.into_iter())
9+
.collect::<Vec<_>>();
10+
let mut heap = BinaryHeap::new();
11+
let mut i = 0;
12+
13+
projects.sort_unstable();
14+
15+
for _ in 0..k {
16+
while i < projects.len() && projects[i].0 <= w {
17+
heap.push(projects[i].1);
18+
i += 1;
19+
}
20+
21+
w += heap.pop().unwrap_or(0);
22+
}
23+
24+
w
25+
}
26+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
[498][498l] |[Diagonal Traverse][498] |![rs]
301301
[500][500l] |[Keyboard Row][500] |![py]
302302
[501][501l] |[Find Mode in Binary Search Tree][501] |![py]
303+
[502][502l] |[IPO][502] |![rs]
303304
[503][503l] |[Next Greater Element II][503] |![rs]
304305
[504][504l] |[Base 7][504] |![py]
305306
[506][506l] |[Relative Ranks][506] |![rs]
@@ -1633,6 +1634,7 @@
16331634
[498]:Problemset/0498-Diagonal%20Traverse/README.md#498-diagonal-traverse
16341635
[500]:Problemset/0500-Keyboard%20Row/README.md#500-keyboard-row
16351636
[501]:Problemset/0501-Find%20Mode%20in%20Binary%20Search%20Tree/README.md#501-find-mode-in-binary-search-tree
1637+
[502]:Problemset/0502-IPO/README.md#502-ipo
16361638
[503]:Problemset/0503-Next%20Greater%20Element%20II/README.md#503-next-greater-element-ii
16371639
[504]:Problemset/0504-Base%207/README.md#504-base-7
16381640
[506]:Problemset/0506-Relative%20Ranks/README.md#506-relative-ranks
@@ -2966,6 +2968,7 @@
29662968
[498l]:https://leetcode.com/problems/diagonal-traverse/
29672969
[500l]:https://leetcode.com/problems/keyboard-row/
29682970
[501l]:https://leetcode.com/problems/find-mode-in-binary-search-tree/
2971+
[502l]:https://leetcode.com/problems/ipo/
29692972
[503l]:https://leetcode.com/problems/next-greater-element-ii/
29702973
[504l]:https://leetcode.com/problems/base-7/
29712974
[506l]:https://leetcode.com/problems/relative-ranks/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
[498][498l] |[对角线遍历][498] |![rs]
301301
[500][500l] |[键盘行][500] |![py]
302302
[501][501l] |[二叉搜索树中的众数][501] |![py]
303+
[502][502l] |[IPO][502] |![rs]
303304
[503][503l] |[下一个更大元素 II][503] |![rs]
304305
[504][504l] |[七进制数][504] |![py]
305306
[506][506l] |[相对名次][506] |![rs]
@@ -1633,6 +1634,7 @@
16331634
[498]:Problemset/0498-Diagonal%20Traverse/README_CN.md#498-对角线遍历
16341635
[500]:Problemset/0500-Keyboard%20Row/README_CN.md#500-键盘行
16351636
[501]:Problemset/0501-Find%20Mode%20in%20Binary%20Search%20Tree/README_CN.md#501-二叉搜索树中的众数
1637+
[502]:Problemset/0502-IPO/README_CN.md#502-ipo
16361638
[503]:Problemset/0503-Next%20Greater%20Element%20II/README_CN.md#503-下一个更大元素-ii
16371639
[504]:Problemset/0504-Base%207/README_CN.md#504-七进制数
16381640
[506]:Problemset/0506-Relative%20Ranks/README_CN.md#506-相对名次
@@ -2966,6 +2968,7 @@
29662968
[498l]:https://leetcode.cn/problems/diagonal-traverse/
29672969
[500l]:https://leetcode.cn/problems/keyboard-row/
29682970
[501l]:https://leetcode.cn/problems/find-mode-in-binary-search-tree/
2971+
[502l]:https://leetcode.cn/problems/ipo/
29692972
[503l]:https://leetcode.cn/problems/next-greater-element-ii/
29702973
[504l]:https://leetcode.cn/problems/base-7/
29712974
[506l]:https://leetcode.cn/problems/relative-ranks/

0 commit comments

Comments
 (0)