Update 0121.买卖股票的最佳时机.md

This commit is contained in:
fwqaaq
2023-06-03 14:05:04 +08:00
committed by GitHub
parent 2d66c713f0
commit b716a8ab44

View File

@ -527,6 +527,21 @@ impl Solution {
} }
``` ```
> 动态规划
```rust
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut dp = vec![-prices[0], 0];
for p in prices {
dp[0] = dp[0].max(-p);
dp[1] = dp[1].max(dp[0] + p);
}
dp[1]
}
}
```
<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">