Skip to content

Commit 926fa13

Browse files
committed
+ problem 188
1 parent 5439936 commit 926fa13

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 188. Best Time to Buy and Sell Stock IV
2+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `k`.
3+
4+
Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most `k` times and sell at most `k` times.
5+
6+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> k = 2, prices = [2,4,1]
11+
<strong>Output:</strong> 2
12+
<strong>Explanation:</strong> Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> k = 2, prices = [3,2,6,5,0,3]
18+
<strong>Output:</strong> 7
19+
<strong>Explanation:</strong> Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
20+
</pre>
21+
22+
#### Constraints:
23+
* `1 <= k <= 100`
24+
* `1 <= prices.length <= 1000`
25+
* `0 <= prices[i] <= 1000`
26+
27+
## Solutions (Rust)
28+
29+
### 1. Solution
30+
```Rust
31+
impl Solution {
32+
pub fn max_profit(k: i32, prices: Vec<i32>) -> i32 {
33+
let k = k as usize;
34+
let mut dp = vec![[0, i32::MIN]; k + 1];
35+
let mut ret = 0;
36+
37+
for i in 0..prices.len() {
38+
let mut tmp = dp.clone();
39+
40+
for j in 0..=i.min(k) {
41+
if j < k {
42+
tmp[j + 1][1] = tmp[j + 1][1].max(dp[j][0] - prices[i]);
43+
}
44+
tmp[j][0] = tmp[j][0].max(dp[j][1] + prices[i]);
45+
ret = ret.max(tmp[j][0]);
46+
}
47+
48+
dp = tmp;
49+
}
50+
51+
ret
52+
}
53+
}
54+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 188. 买卖股票的最佳时机 IV
2+
给你一个整数数组 `prices` 和一个整数 `k` ,其中 `prices[i]` 是某支给定的股票在第 `i` 天的价格。
3+
4+
设计一个算法来计算你所能获取的最大利润。你最多可以完成 `k` 笔交易。也就是说,你最多可以买 `k` 次,卖 `k` 次。
5+
6+
**注意:**你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> k = 2, prices = [2,4,1]
11+
<strong>输出:</strong> 2
12+
<strong>解释:</strong> 在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> k = 2, prices = [3,2,6,5,0,3]
18+
<strong>输出:</strong> 7
19+
<strong>解释:</strong> 在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
20+
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
21+
</pre>
22+
23+
#### 提示:
24+
* `1 <= k <= 100`
25+
* `1 <= prices.length <= 1000`
26+
* `0 <= prices[i] <= 1000`
27+
28+
## 题解 (Rust)
29+
30+
### 1. 题解
31+
```Rust
32+
impl Solution {
33+
pub fn max_profit(k: i32, prices: Vec<i32>) -> i32 {
34+
let k = k as usize;
35+
let mut dp = vec![[0, i32::MIN]; k + 1];
36+
let mut ret = 0;
37+
38+
for i in 0..prices.len() {
39+
let mut tmp = dp.clone();
40+
41+
for j in 0..=i.min(k) {
42+
if j < k {
43+
tmp[j + 1][1] = tmp[j + 1][1].max(dp[j][0] - prices[i]);
44+
}
45+
tmp[j][0] = tmp[j][0].max(dp[j][1] + prices[i]);
46+
ret = ret.max(tmp[j][0]);
47+
}
48+
49+
dp = tmp;
50+
}
51+
52+
ret
53+
}
54+
}
55+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn max_profit(k: i32, prices: Vec<i32>) -> i32 {
3+
let k = k as usize;
4+
let mut dp = vec![[0, i32::MIN]; k + 1];
5+
let mut ret = 0;
6+
7+
for i in 0..prices.len() {
8+
let mut tmp = dp.clone();
9+
10+
for j in 0..=i.min(k) {
11+
if j < k {
12+
tmp[j + 1][1] = tmp[j + 1][1].max(dp[j][0] - prices[i]);
13+
}
14+
tmp[j][0] = tmp[j][0].max(dp[j][1] + prices[i]);
15+
ret = ret.max(tmp[j][0]);
16+
}
17+
18+
dp = tmp;
19+
}
20+
21+
ret
22+
}
23+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
[173][173l] |[Binary Search Tree Iterator][173] |![py]
153153
[179][179l] |[Largest Number][179] |![rs]
154154
[187][187l] |[Repeated DNA Sequences][187] |![rb]&nbsp;&nbsp;![rs]
155+
[188][188l] |[Best Time to Buy and Sell Stock IV][188] |![rs]
155156
[189][189l] |[Rotate Array][189] |![rs]
156157
[190][190l] |[Reverse Bits][190] |![py]
157158
[191][191l] |[Number of 1 Bits][191] |![rs]
@@ -1739,6 +1740,7 @@
17391740
[173]:Problemset/0173-Binary%20Search%20Tree%20Iterator/README.md#173-binary-search-tree-iterator
17401741
[179]:Problemset/0179-Largest%20Number/README.md#179-largest-number
17411742
[187]:Problemset/0187-Repeated%20DNA%20Sequences/README.md#187-repeated-dna-sequences
1743+
[188]:Problemset/0188-Best%20Time%20to%20Buy%20and%20Sell%20Stock%20IV/README.md#188-best-time-to-buy-and-sell-stock-iv
17421744
[189]:Problemset/0189-Rotate%20Array/README.md#189-rotate-array
17431745
[190]:Problemset/0190-Reverse%20Bits/README.md#190-reverse-bits
17441746
[191]:Problemset/0191-Number%20of%201%20Bits/README.md#191-number-of-1-bits
@@ -3319,6 +3321,7 @@
33193321
[173l]:https://leetcode.com/problems/binary-search-tree-iterator/
33203322
[179l]:https://leetcode.com/problems/largest-number/
33213323
[187l]:https://leetcode.com/problems/repeated-dna-sequences/
3324+
[188l]:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
33223325
[189l]:https://leetcode.com/problems/rotate-array/
33233326
[190l]:https://leetcode.com/problems/reverse-bits/
33243327
[191l]:https://leetcode.com/problems/number-of-1-bits/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
[173][173l] |[二叉搜索树迭代器][173] |![py]
153153
[179][179l] |[最大数][179] |![rs]
154154
[187][187l] |[重复的DNA序列][187] |![rb]&nbsp;&nbsp;![rs]
155+
[188][188l] |[买卖股票的最佳时机 IV][188] |![rs]
155156
[189][189l] |[旋转数组][189] |![rs]
156157
[190][190l] |[颠倒二进制位][190] |![py]
157158
[191][191l] |[位1的个数][191] |![rs]
@@ -1739,6 +1740,7 @@
17391740
[173]:Problemset/0173-Binary%20Search%20Tree%20Iterator/README_CN.md#173-二叉搜索树迭代器
17401741
[179]:Problemset/0179-Largest%20Number/README_CN.md#179-最大数
17411742
[187]:Problemset/0187-Repeated%20DNA%20Sequences/README_CN.md#187-重复的dna序列
1743+
[188]:Problemset/0188-Best%20Time%20to%20Buy%20and%20Sell%20Stock%20IV/README_CN.md#188-买卖股票的最佳时机-iv
17421744
[189]:Problemset/0189-Rotate%20Array/README_CN.md#189-旋转数组
17431745
[190]:Problemset/0190-Reverse%20Bits/README_CN.md#190-颠倒二进制位
17441746
[191]:Problemset/0191-Number%20of%201%20Bits/README_CN.md#191-位1的个数
@@ -3319,6 +3321,7 @@
33193321
[173l]:https://leetcode.cn/problems/binary-search-tree-iterator/
33203322
[179l]:https://leetcode.cn/problems/largest-number/
33213323
[187l]:https://leetcode.cn/problems/repeated-dna-sequences/
3324+
[188l]:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/
33223325
[189l]:https://leetcode.cn/problems/rotate-array/
33233326
[190l]:https://leetcode.cn/problems/reverse-bits/
33243327
[191l]:https://leetcode.cn/problems/number-of-1-bits/

0 commit comments

Comments
 (0)