788. 旋转数字
This commit is contained in:
parent
fa937a142f
commit
a9da2f1227
52
src/main.rs
52
src/main.rs
|
|
@ -1,41 +1,33 @@
|
|||
use crate::arr::make_matrix;
|
||||
|
||||
mod arr;
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn max_path_score(grid: Vec<Vec<i32>>, k: i32) -> i32 {
|
||||
let k = (k as usize + 1).min(grid.len() + grid[0].len() + 1);
|
||||
let mut dp = vec![vec![vec![-1;k]; grid[0].len()]; grid.len()];
|
||||
for i in 0..k {
|
||||
dp[0][0][i] = 0;
|
||||
}
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[0].len() {
|
||||
let cost = if grid[i][j] > 0 { 1 } else { 0 };
|
||||
let score = grid[i][j];
|
||||
for p in cost..k {
|
||||
let mut ans = -1;
|
||||
if j > 0 && dp[i][j - 1][p - cost] != -1 {
|
||||
ans = dp[i][j - 1][p - cost];
|
||||
}
|
||||
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 });
|
||||
pub fn rotated_digits(n: i32) -> i32 {
|
||||
let mut ans = 0;
|
||||
let check = |mut x: i32| -> bool {
|
||||
let mut is_diff = false;
|
||||
while x != 0 {
|
||||
let y = x % 10;
|
||||
match y {
|
||||
2 => is_diff = true,
|
||||
3 => return false,
|
||||
4 => return false,
|
||||
5 => is_diff = true,
|
||||
6 => is_diff = true,
|
||||
7 => return false,
|
||||
9 => is_diff = true,
|
||||
_ => {}
|
||||
}
|
||||
x /= 10;
|
||||
}
|
||||
is_diff
|
||||
};
|
||||
for i in 1..=n {
|
||||
ans += if check(i) { 1 } else { 0 }
|
||||
}
|
||||
*dp[grid.len() - 1][grid[0].len() - 1]
|
||||
.iter()
|
||||
.max()
|
||||
.unwrap_or(&0)
|
||||
ans
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::max_path_score(make_matrix("[[0, 1],[2, 0]]"), 1)
|
||||
);
|
||||
println!("{:?}", Solution::rotated_digits(10));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue