mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0122.买卖股票的最佳时机II(动态规划).md
This commit is contained in:
@ -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]
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user