20210203 Finished
This commit is contained in:
parent
e27a6218c0
commit
495f019092
|
@ -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;
|
42
src/problem/p1690_stone_game_vii.rs
Normal file
42
src/problem/p1690_stone_game_vii.rs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user