Skip to content

Commit

Permalink
Update p1690
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Feb 2, 2024
1 parent fedf672 commit 8832247
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Call test by running `cargo test p[xxx]_`.
- [2788. Split Strings by Separator](src/leetcode/p2788_split_strings_by_separator)
- [2859. Sum of Values at Indices With K Set Bits](src/leetcode/p2859_sum_of_values_at_indices_with_k_set_bits)

### Medium(151)
### Medium(152)

- [2. Add Two Numbers](src/leetcode/p2_add_two_numbers)
- [3. Longest Substring Without Repeating Characters](src/leetcode/p3_longest_substring_without_repeating_characters)
Expand Down Expand Up @@ -216,6 +216,7 @@ Call test by running `cargo test p[xxx]_`.
- [1641. Count Sorted Vowel Strings](src/leetcode/p1641_count_sorted_vowel_strings)
- [1653. Minimum Deletions to Make String Balanced](src/leetcode/p1653_minimum_deletions_to_make_string_balanced)
- [1686. Stone Game VI](src/leetcode/p1686_stone_game_vi)
- [1690. Stone Game VII](src/leetcode/p1690_stone_game_vii)
- [1792. Maximum Average Pass Ratio](src/leetcode/p1792_maximum_average_pass_ratio)
- [1797. Design Authentication Manager](src/leetcode/p1797_design_authentication_manager)
- [1798. Maximum Number of Consecutive Values You Can Make](src/leetcode/p1798_maximum_number_of_consecutive_values_you_can_make)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub mod p1641_count_sorted_vowel_strings;
pub mod p1653_minimum_deletions_to_make_string_balanced;
pub mod p165_compare_version_numbers;
pub mod p1686_stone_game_vi;
pub mod p1690_stone_game_vii;
pub mod p169_majority_element;
pub mod p16_3sum_closest;
pub mod p173_binary_search_tree_iterator;
Expand Down
49 changes: 49 additions & 0 deletions src/leetcode/p1690_stone_game_vii/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
pub struct Solution;

impl Solution {
pub fn stone_game_vii(mut stones: Vec<i32>) -> i32 {
fn dfs(cache: &mut [Vec<i32>], pre_sum: &[i32], left: usize, right: usize) -> i32 {
if left == right {
return 0;
}
if cache[left][right] != -1 {
return cache[left][right];
}
let mut res = pre_sum[right] - pre_sum[left] - dfs(cache, pre_sum, left + 1, right);
res = res.max(
pre_sum[right - 1]
- (left != 0).then(|| pre_sum[left - 1]).unwrap_or(0)
- dfs(cache, pre_sum, left, right - 1),
);
cache[left][right] = res;
res
}
for i in 1..stones.len() {
stones[i] += stones[i - 1];
}
dfs(
&mut vec![vec![-1; stones.len()]; stones.len()],
&stones,
0,
stones.len() - 1,
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test1() {
assert_eq!(Solution::stone_game_vii([5, 3, 1, 4, 2].to_vec()), 6);
}

#[test]
fn test2() {
assert_eq!(
Solution::stone_game_vii([7, 90, 5, 1, 100, 10, 10, 2].to_vec()),
122
);
}
}

0 comments on commit 8832247

Please sign in to comment.