From 495f019092f8ff42ae480ead821560af39c06107 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 3 Feb 2024 10:44:02 +0800 Subject: [PATCH] 20210203 Finished --- src/problem/mod.rs | 3 ++- src/problem/p1690_stone_game_vii.rs | 42 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/problem/p1690_stone_game_vii.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 7351a4d..3a4f70b 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -35,4 +35,5 @@ mod p514_freedom_trail; mod p2808_minimum_seconds_to_equalize_a_circular_array; mod p2670_find_the_distinct_difference_array; mod lcp24_nums_game; -mod p1686_stone_game_vi; \ No newline at end of file +mod p1686_stone_game_vi; +mod p1690_stone_game_vii; \ No newline at end of file diff --git a/src/problem/p1690_stone_game_vii.rs b/src/problem/p1690_stone_game_vii.rs new file mode 100644 index 0000000..24dc181 --- /dev/null +++ b/src/problem/p1690_stone_game_vii.rs @@ -0,0 +1,42 @@ +/** + * [1690] Stone Game VII + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn stone_game_vii(stones: Vec) -> i32 { + let n = stones.len(); + let mut sum = vec![0; n + 1]; + + for (index, value) in stones.iter().enumerate() { + sum[index + 1] = sum[index] + *value; + } + + let mut dp = vec![vec![0;n];n]; + + for i in (0..=n-2).rev() { + for j in i+1..n { + dp[i][j] = (sum[j + 1] - sum[i + 1] - dp[i + 1][j]).max( + sum[j] - sum[i] - dp[i][j - 1] + ); + } + } + + dp[0][n - 1] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1690() { + assert_eq!(Solution::stone_game_vii(vec![5,3,1,4,2]), 6); + } +}