788. 旋转数字

This commit is contained in:
li_chx 2026-05-02 17:45:17 +08:00
parent fa937a142f
commit a9da2f1227
Signed by: li_chx
GPG Key ID: C7CF27EFA1E58BAC
1 changed files with 22 additions and 30 deletions

View File

@ -1,41 +1,33 @@
use crate::arr::make_matrix;
mod arr; mod arr;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn max_path_score(grid: Vec<Vec<i32>>, k: i32) -> i32 { pub fn rotated_digits(n: i32) -> i32 {
let k = (k as usize + 1).min(grid.len() + grid[0].len() + 1); let mut ans = 0;
let mut dp = vec![vec![vec![-1;k]; grid[0].len()]; grid.len()]; let check = |mut x: i32| -> bool {
for i in 0..k { let mut is_diff = false;
dp[0][0][i] = 0; while x != 0 {
} let y = x % 10;
for i in 0..grid.len() { match y {
for j in 0..grid[0].len() { 2 => is_diff = true,
let cost = if grid[i][j] > 0 { 1 } else { 0 }; 3 => return false,
let score = grid[i][j]; 4 => return false,
for p in cost..k { 5 => is_diff = true,
let mut ans = -1; 6 => is_diff = true,
if j > 0 && dp[i][j - 1][p - cost] != -1 { 7 => return false,
ans = dp[i][j - 1][p - cost]; 9 => is_diff = true,
} _ => {}
if i > 0 && dp[i - 1][j][p - cost] != -1 {
ans = ans.max(dp[i - 1][j][p - cost]);
}
dp[i][j][p] = dp[i][j][p].max(if ans == -1 { -1 } else { ans + score });
} }
x /= 10;
} }
is_diff
};
for i in 1..=n {
ans += if check(i) { 1 } else { 0 }
} }
*dp[grid.len() - 1][grid[0].len() - 1] ans
.iter()
.max()
.unwrap_or(&0)
} }
} }
fn main() { fn main() {
println!( println!("{:?}", Solution::rotated_digits(10));
"{:?}",
Solution::max_path_score(make_matrix("[[0, 1],[2, 0]]"), 1)
);
} }