Compare commits

..

No commits in common. "08f6ce45811605b44fe468ee39f162aa1234977a" and "1da8b6b735571b212a10996b0f5109d30090f439" have entirely different histories.

1 changed files with 18 additions and 32 deletions

View File

@ -1,44 +1,30 @@
use crate::arr::make_matrix; use crate::arr::make_matrix;
use std::collections::HashSet;
struct Solution; struct Solution;
mod arr; mod arr;
impl Solution { impl Solution {
pub fn max_k_divisible_components( pub fn number_of_paths(grid: Vec<Vec<i32>>, k: i32) -> i32 {
n: i32, let mut arr = vec![vec![vec![0; k as usize]; grid[0].len()]; grid.len()];
edges: Vec<Vec<i32>>, let r#mod = 1e9 as i32 + 7;
values: Vec<i32>, arr[0][0][(grid[0][0] % k) as usize] = 1;
k: i32, for i in 0..grid.len() {
) -> i32 { for j in 0..grid[i].len() {
let mut edge = vec![HashSet::new(); n as usize]; if i < grid.len() - 1 {
edges.iter().for_each(|x| { for l in 0..k {
edge[x[0] as usize].insert(x[1] as usize); arr[i + 1][j][((l + grid[i + 1][j]) % k) as usize] = (arr[i + 1][j][((l + grid[i + 1][j]) % k) as usize] + arr[i][j][l as usize]) % r#mod;
edge[x[1] as usize].insert(x[0] as usize); }
}); }
fn dfs(total_ans:&mut i32, k:i32, root: usize, last: usize, edge: &Vec<HashSet<usize>>,values: &Vec<i32>) -> i64{ if j < grid[0].len() - 1 {
let mut ans = values[root] as i64; for l in 0..k {
for &t in edge[root].iter() { arr[i][j + 1][((l + grid[i][j + 1]) % k) as usize] = (arr[i][j + 1][((l + grid[i][j + 1]) % k) as usize] + arr[i][j][l as usize]) % r#mod;
if t == last { continue; } }
ans += dfs(total_ans,k,t, root, edge,values); }
}
if ans % k as i64 == 0 {
*total_ans += 1;
0
}else {
ans
} }
} }
let mut ans = 0; arr[grid.len() - 1][grid[0].len() - 1][0]
dfs(&mut ans,k,0, usize::MAX, &edge,&values);
ans
} }
} }
fn main() { fn main() {
let result = Solution::max_k_divisible_components( let result = Solution::number_of_paths(make_matrix("[[5,2,4],[3,0,5],[0,7,2]]"), 3);
7,
make_matrix("[[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]"),
vec![3, 0, 6, 1, 5, 2, 1],
3,
);
println!("{:?}", result); println!("{:?}", result);
} }