20240316 Finished

This commit is contained in:
jackfiled 2024-03-16 11:37:58 +08:00
parent d6d6d5cea5
commit 3bdc0b9647
2 changed files with 72 additions and 1 deletions

View File

@ -71,4 +71,5 @@ mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array;
mod p2386_find_the_k_sum_of_an_array;
mod p299_bulls_and_cows;
mod p2129_capitalize_the_title;
mod p1261_find_elements_in_a_contaminated_binary_tree;
mod p1261_find_elements_in_a_contaminated_binary_tree;
mod p2684_maximum_number_of_moves_in_a_grid;

View File

@ -0,0 +1,70 @@
/**
* [2684] Maximum Number of Moves in a Grid
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn max_moves(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
for j in (0..n - 1).rev() {
for i in 0..m {
// [row - 1, col + 1]
if i != 0 {
if grid[i - 1][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i - 1][j + 1] + 1);
}
}
// [row, col + 1]
if grid[i][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i][j + 1] + 1);
}
// [row + 1, col + 1]
if i != m - 1 {
if grid[i + 1][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i + 1][j + 1] + 1);
}
}
}
}
let mut result = 0;
for i in 0..m {
result = result.max(dp[i][0]);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2684() {
assert_eq!(
3,
Solution::max_moves(vec![
vec![2, 4, 3, 5],
vec![5, 4, 9, 3],
vec![3, 4, 2, 11],
vec![10, 9, 13, 15]
])
);
assert_eq!(
0,
Solution::max_moves(vec![vec![3, 2, 4], vec![2, 1, 9], vec![1, 1, 7]])
);
}
}