mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0123.买卖股票的最佳时机III.md
This commit is contained in:
@ -442,6 +442,24 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> 版本二(绕)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn max_profit(prices: Vec<i32>) -> i32 {
|
||||||
|
let mut dp = vec![0, -prices[0], 0, -prices[0], 0];
|
||||||
|
|
||||||
|
for p in prices {
|
||||||
|
dp[1] = dp[1].max(-p);
|
||||||
|
dp[2] = dp[2].max(dp[1] + p);
|
||||||
|
dp[3] = dp[3].max(dp[2] - p);
|
||||||
|
dp[4] = dp[4].max(dp[3] + p);
|
||||||
|
}
|
||||||
|
dp[4]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user