Update 0122.买卖股票的最佳时机II(动态规划).md

This commit is contained in:
fwqaaq
2023-06-03 14:58:09 +08:00
committed by GitHub
parent fd7a98b831
commit 2719dbdd0e

View File

@ -384,9 +384,10 @@ impl Solution {
impl Solution { impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 { pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut dp = vec![-prices[0], 0]; let mut dp = vec![-prices[0], 0];
for i in 1..=prices.len() { for p in prices {
dp[0] = dp[0].max(dp[1] - prices[i - 1]); // 可以看作 low、res
dp[1] = dp[1].max(dp[0] + prices[i - 1]); dp[0] = dp[0].max(dp[1] - p);
dp[1] = dp[1].max(dp[0] + p);
} }
dp[1] dp[1]
} }