mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0121.买卖股票的最佳时机.md
This commit is contained in:
@ -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">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user