20240813 Finished
This commit is contained in:
parent
5c1d526ff9
commit
df0f915696
|
@ -203,3 +203,4 @@ mod p64_minimum_path_sum;
|
||||||
mod p63_unique_paths_ii;
|
mod p63_unique_paths_ii;
|
||||||
mod p97_interleaving_string;
|
mod p97_interleaving_string;
|
||||||
mod p72_edit_distance;
|
mod p72_edit_distance;
|
||||||
|
mod p123_best_time_to_buy_and_sell_stock_iii;
|
37
src/problem/p123_best_time_to_buy_and_sell_stock_iii.rs
Normal file
37
src/problem/p123_best_time_to_buy_and_sell_stock_iii.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/**
|
||||||
|
* [123] Best Time to Buy and Sell Stock III
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn max_profit(prices: Vec<i32>) -> i32 {
|
||||||
|
let n = prices.len();
|
||||||
|
let (mut buy1, mut sell1) = (-prices[0], 0);
|
||||||
|
let (mut buy2, mut sell2) = (-prices[0], 0);
|
||||||
|
|
||||||
|
for i in 1..n {
|
||||||
|
buy1 = buy1.max(-prices[i]);
|
||||||
|
sell1 = sell1.max(buy1 + prices[i]);
|
||||||
|
buy2 = buy2.max(sell1 - prices[i]);
|
||||||
|
sell2 = sell2.max(buy2 + prices[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
sell2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_123() {
|
||||||
|
assert_eq!(4, Solution::max_profit(vec![1, 2, 3, 4, 5]));
|
||||||
|
assert_eq!(6, Solution::max_profit(vec![3, 3, 5, 0, 0, 3, 1, 4]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user