20240814 Finished
This commit is contained in:
parent
df0f915696
commit
c7a090afa4
|
@ -203,4 +203,5 @@ mod p64_minimum_path_sum;
|
|||
mod p63_unique_paths_ii;
|
||||
mod p97_interleaving_string;
|
||||
mod p72_edit_distance;
|
||||
mod p123_best_time_to_buy_and_sell_stock_iii;
|
||||
mod p123_best_time_to_buy_and_sell_stock_iii;
|
||||
mod p188_best_time_to_buy_and_sell_stock_iv;
|
42
src/problem/p188_best_time_to_buy_and_sell_stock_iv.rs
Normal file
42
src/problem/p188_best_time_to_buy_and_sell_stock_iv.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* [188] Best Time to Buy and Sell Stock IV
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn max_profit(k: i32, prices: Vec<i32>) -> i32 {
|
||||
let n = prices.len();
|
||||
let k = k as usize;
|
||||
|
||||
let mut dp = vec![(-prices[0], 0); k];
|
||||
|
||||
for i in 1..n {
|
||||
for j in 0..k {
|
||||
dp[j].0 = dp[j].0.max(if j == 0 {
|
||||
-prices[i]
|
||||
} else {
|
||||
dp[j - 1].1 - prices[i]
|
||||
});
|
||||
dp[j].1 = dp[j].1.max(dp[j].0 + prices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
dp[k - 1].1
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_188() {
|
||||
assert_eq!(2, Solution::max_profit(2, vec![2, 4, 1]));
|
||||
assert_eq!(7, Solution::max_profit(2, vec![3, 2, 6, 5, 0, 3]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user