20210203 Finished

This commit is contained in:
jackfiled 2024-02-03 10:44:02 +08:00
parent e27a6218c0
commit 495f019092
2 changed files with 44 additions and 1 deletions

View File

@ -36,3 +36,4 @@ 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;
mod p1690_stone_game_vii;

View File

@ -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>) -> 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);
}
}