diff --git a/src/main.rs b/src/main.rs index fdbf375..362c93c 100644 --- a/src/main.rs +++ b/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>, 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)); }