diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9d9414f..21f6de6 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -288,3 +288,5 @@ mod p3211_generate_binary_strings_without_adjacent_zeros; mod p3216_lexicographically_smallest_string_after_a_swap; mod p3165_maximum_sum_of_subsequence_with_non_adjacent_elements; + +mod p3259_maximum_energy_boost_from_two_drinks; diff --git a/src/problem/p3259_maximum_energy_boost_from_two_drinks.rs b/src/problem/p3259_maximum_energy_boost_from_two_drinks.rs new file mode 100644 index 0000000..8151ce7 --- /dev/null +++ b/src/problem/p3259_maximum_energy_boost_from_two_drinks.rs @@ -0,0 +1,42 @@ +/** + * [3259] Maximum Energy Boost From Two Drinks + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn max_energy_boost(energy_drink_a: Vec, energy_drink_b: Vec) -> i64 { + let n = energy_drink_a.len(); + let mut dp_drink_a = vec![0; n]; + let mut dp_drink_b = vec![0; n]; + + for i in 0..n { + if i >= 2 { + dp_drink_a[i] = dp_drink_a[i - 1].max(dp_drink_b[i - 2]) + energy_drink_a[i] as i64; + dp_drink_b[i] = dp_drink_b[i - 1].max(dp_drink_a[i - 2]) + energy_drink_b[i] as i64; + } else if i >= 1 { + dp_drink_a[i] = dp_drink_a[i - 1] + energy_drink_a[i] as i64; + dp_drink_b[i] = dp_drink_b[i - 1] + energy_drink_b[i] as i64; + } else { + dp_drink_a[i] = energy_drink_a[i] as i64; + dp_drink_b[i] = energy_drink_b[i] as i64; + } + } + + dp_drink_a[n - 1].max(dp_drink_b[n - 1]) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3259() { + assert_eq!(5, Solution::max_energy_boost(vec![1, 3, 1], vec![3, 1, 1])); + assert_eq!(7, Solution::max_energy_boost(vec![4, 1, 1], vec![1, 1, 3])); + } +}