Skip to content

Commit e63c7fe

Browse files
committed
+ problem 2435
1 parent 307ec36 commit e63c7fe

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 2435. Paths in Matrix Whose Sum Is Divisible by K
2+
You are given a **0-indexed** `m x n` integer matrix `grid` and an integer `k`. You are currently at position `(0, 0)` and you want to reach position `(m - 1, n - 1)` moving only **down** or **right**.
3+
4+
Return *the number of paths where the sum of the elements on the path is divisible by* `k`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png)
8+
<pre>
9+
<strong>Input:</strong> grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3
10+
<strong>Output:</strong> 2
11+
<strong>Explanation:</strong> There are two paths where the sum of the elements on the path is divisible by k.
12+
The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.
13+
The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.
14+
</pre>
15+
16+
#### Example 2:
17+
![](https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png)
18+
<pre>
19+
<strong>Input:</strong> grid = [[0,0]], k = 5
20+
<strong>Output:</strong> 1
21+
<strong>Explanation:</strong> The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.
22+
</pre>
23+
24+
#### Example 3:
25+
![](https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png)
26+
<pre>
27+
<strong>Input:</strong> grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1
28+
<strong>Output:</strong> 10
29+
<strong>Explanation:</strong> Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.
30+
</pre>
31+
32+
#### Constraints:
33+
* `m == grid.length`
34+
* `n == grid[i].length`
35+
* <code>1 <= m, n <= 5 * 10<sup>4</sup></code>
36+
* <code>1 <= m * n <= 5 * 10<sup>4</sup></code>
37+
* `0 <= grid[i][j] <= 100`
38+
* `1 <= k <= 50`
39+
40+
## Solutions (Rust)
41+
42+
### 1. Solution
43+
```Rust
44+
use std::collections::VecDeque;
45+
46+
impl Solution {
47+
pub fn number_of_paths(grid: Vec<Vec<i32>>, k: i32) -> i32 {
48+
let k = k as usize;
49+
let m = grid.len();
50+
let n = grid[0].len();
51+
let mut prev_row_mod = vec![VecDeque::from(vec![0; k]); n];
52+
prev_row_mod[0][0] = 1;
53+
54+
for r in 0..m {
55+
let mut row_mod = prev_row_mod.clone();
56+
row_mod[0].rotate_right(grid[r][0] as usize % k);
57+
58+
for c in 1..n {
59+
for i in 0..k {
60+
row_mod[c][i] = (row_mod[c][i] + row_mod[c - 1][i]) % 1_000_000_007;
61+
}
62+
row_mod[c].rotate_right(grid[r][c] as usize % k);
63+
}
64+
65+
prev_row_mod = row_mod;
66+
}
67+
68+
prev_row_mod[n - 1][0]
69+
}
70+
}
71+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 2435. 矩阵中和能被 K 整除的路径
2+
给你一个下标从 **0** 开始的 `m x n` 整数矩阵 `grid` 和一个整数 `k` 。你从起点 `(0, 0)` 出发,每一步只能往 **** 或者往 **** ,你想要到达终点 `(m - 1, n - 1)`
3+
4+
请你返回路径和能被 `k` 整除的路径数目,由于答案可能很大,返回答案对 <code>10<sup>9</sup> + 7</code> **取余** 的结果。
5+
6+
#### 示例 1:
7+
![](https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png)
8+
<pre>
9+
<strong>输入:</strong> grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3
10+
<strong>输出:</strong> 2
11+
<strong>解释:</strong> 有两条路径满足路径上元素的和能被 k 整除。
12+
第一条路径为上图中用红色标注的路径,和为 5 + 2 + 4 + 5 + 2 = 18 ,能被 3 整除。
13+
第二条路径为上图中用蓝色标注的路径,和为 5 + 3 + 0 + 5 + 2 = 15 ,能被 3 整除。
14+
</pre>
15+
16+
#### 示例 2:
17+
![](https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png)
18+
<pre>
19+
<strong>输入:</strong> grid = [[0,0]], k = 5
20+
<strong>输出:</strong> 1
21+
<strong>解释:</strong> 红色标注的路径和为 0 + 0 = 0 ,能被 5 整除。
22+
</pre>
23+
24+
#### 示例 3:
25+
![](https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png)
26+
<pre>
27+
<strong>输入:</strong> grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1
28+
<strong>输出:</strong> 10
29+
<strong>解释:</strong> 每个数字都能被 1 整除,所以每一条路径的和都能被 k 整除。
30+
</pre>
31+
32+
#### 提示:
33+
* `m == grid.length`
34+
* `n == grid[i].length`
35+
* <code>1 <= m, n <= 5 * 10<sup>4</sup></code>
36+
* <code>1 <= m * n <= 5 * 10<sup>4</sup></code>
37+
* `0 <= grid[i][j] <= 100`
38+
* `1 <= k <= 50`
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
use std::collections::VecDeque;
45+
46+
impl Solution {
47+
pub fn number_of_paths(grid: Vec<Vec<i32>>, k: i32) -> i32 {
48+
let k = k as usize;
49+
let m = grid.len();
50+
let n = grid[0].len();
51+
let mut prev_row_mod = vec![VecDeque::from(vec![0; k]); n];
52+
prev_row_mod[0][0] = 1;
53+
54+
for r in 0..m {
55+
let mut row_mod = prev_row_mod.clone();
56+
row_mod[0].rotate_right(grid[r][0] as usize % k);
57+
58+
for c in 1..n {
59+
for i in 0..k {
60+
row_mod[c][i] = (row_mod[c][i] + row_mod[c - 1][i]) % 1_000_000_007;
61+
}
62+
row_mod[c].rotate_right(grid[r][c] as usize % k);
63+
}
64+
65+
prev_row_mod = row_mod;
66+
}
67+
68+
prev_row_mod[n - 1][0]
69+
}
70+
}
71+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn number_of_paths(grid: Vec<Vec<i32>>, k: i32) -> i32 {
5+
let k = k as usize;
6+
let m = grid.len();
7+
let n = grid[0].len();
8+
let mut prev_row_mod = vec![VecDeque::from(vec![0; k]); n];
9+
prev_row_mod[0][0] = 1;
10+
11+
for r in 0..m {
12+
let mut row_mod = prev_row_mod.clone();
13+
row_mod[0].rotate_right(grid[r][0] as usize % k);
14+
15+
for c in 1..n {
16+
for i in 0..k {
17+
row_mod[c][i] = (row_mod[c][i] + row_mod[c - 1][i]) % 1_000_000_007;
18+
}
19+
row_mod[c].rotate_right(grid[r][c] as usize % k);
20+
}
21+
22+
prev_row_mod = row_mod;
23+
}
24+
25+
prev_row_mod[n - 1][0]
26+
}
27+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@
14511451
[2432][2432l]|[The Employee That Worked on the Longest Task][2432] |![rs]
14521452
[2433][2433l]|[Find The Original Array of Prefix Xor][2433] |![rs]
14531453
[2434][2434l]|[Using a Robot to Print the Lexicographically Smallest String][2434] |![rs]
1454+
[2435][2435l]|[Paths in Matrix Whose Sum Is Divisible by K][2435] |![rs]
14541455
[2437][2437l]|[Number of Valid Clock Times][2437] |![py]
14551456
[2438][2438l]|[Range Product Queries of Powers][2438] |![rs]
14561457
[2439][2439l]|[Minimize Maximum of Array][2439] |![rs]
@@ -2981,6 +2982,7 @@
29812982
[2432]:Problemset/2432-The%20Employee%20That%20Worked%20on%20the%20Longest%20Task/README.md#2432-the-employee-that-worked-on-the-longest-task
29822983
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README.md#2433-find-the-original-array-of-prefix-xor
29832984
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README.md#2434-using-a-robot-to-print-the-lexicographically-smallest-string
2985+
[2435]:Problemset/2435-Paths%20in%20Matrix%20Whose%20Sum%20Is%20Divisible%20by%20K/README.md#2435-paths-in-matrix-whose-sum-is-divisible-by-k
29842986
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README.md#2437-number-of-valid-clock-times
29852987
[2438]:Problemset/2438-Range%20Product%20Queries%20of%20Powers/README.md#2438-range-product-queries-of-powers
29862988
[2439]:Problemset/2439-Minimize%20Maximum%20of%20Array/README.md#2439-minimize-maximum-of-array
@@ -4510,6 +4512,7 @@
45104512
[2432l]:https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/
45114513
[2433l]:https://leetcode.com/problems/find-the-original-array-of-prefix-xor/
45124514
[2434l]:https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
4515+
[2435l]:https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/
45134516
[2437l]:https://leetcode.com/problems/number-of-valid-clock-times/
45144517
[2438l]:https://leetcode.com/problems/range-product-queries-of-powers/
45154518
[2439l]:https://leetcode.com/problems/minimize-maximum-of-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@
14511451
[2432][2432l]|[处理用时最长的那个任务的员工][2432] |![rs]
14521452
[2433][2433l]|[找出前缀异或的原始数组][2433] |![rs]
14531453
[2434][2434l]|[使用机器人打印字典序最小的字符串][2434] |![rs]
1454+
[2435][2435l]|[矩阵中和能被 K 整除的路径][2435] |![rs]
14541455
[2437][2437l]|[有效时间的数目][2437] |![py]
14551456
[2438][2438l]|[二的幂数组中查询范围内的乘积][2438] |![rs]
14561457
[2439][2439l]|[最小化数组中的最大值][2439] |![rs]
@@ -2981,6 +2982,7 @@
29812982
[2432]:Problemset/2432-The%20Employee%20That%20Worked%20on%20the%20Longest%20Task/README_CN.md#2432-处理用时最长的那个任务的员工
29822983
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README_CN.md#2433-找出前缀异或的原始数组
29832984
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README_CN.md#2434-使用机器人打印字典序最小的字符串
2985+
[2435]:Problemset/2435-Paths%20in%20Matrix%20Whose%20Sum%20Is%20Divisible%20by%20K/README_CN.md#2435-矩阵中和能被-k-整除的路径
29842986
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README_CN.md#2437-有效时间的数目
29852987
[2438]:Problemset/2438-Range%20Product%20Queries%20of%20Powers/README_CN.md#2438-二的幂数组中查询范围内的乘积
29862988
[2439]:Problemset/2439-Minimize%20Maximum%20of%20Array/README_CN.md#2439-最小化数组中的最大值
@@ -4510,6 +4512,7 @@
45104512
[2432l]:https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/
45114513
[2433l]:https://leetcode.cn/problems/find-the-original-array-of-prefix-xor/
45124514
[2434l]:https://leetcode.cn/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
4515+
[2435l]:https://leetcode.cn/problems/paths-in-matrix-whose-sum-is-divisible-by-k/
45134516
[2437l]:https://leetcode.cn/problems/number-of-valid-clock-times/
45144517
[2438l]:https://leetcode.cn/problems/range-product-queries-of-powers/
45154518
[2439l]:https://leetcode.cn/problems/minimize-maximum-of-array/

0 commit comments

Comments
 (0)