Skip to content

Commit 9dbabc4

Browse files
committed
Add solution and test-cases for problem 2435
1 parent f03226b commit 9dbabc4

File tree

6 files changed

+74
-25
lines changed

6 files changed

+74
-25
lines changed
18.5 KB
Loading
3 KB
Loading
10.3 KB
Loading

leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2435.Paths in Matrix Whose Sum Is Divisible by K][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**.
5+
6+
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** `10^9 + 7`.
77

8-
**Example 1:**
8+
**Example 1:**
9+
10+
![1](./1.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3
14+
Output: 2
15+
Explanation: There are two paths where the sum of the elements on the path is divisible by k.
16+
The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.
17+
The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
22+
![2](./2.png)
1923

20-
### 思路1
21-
> ...
22-
Paths in Matrix Whose Sum Is Divisible by K
23-
```go
24+
```
25+
Input: grid = [[0,0]], k = 5
26+
Output: 1
27+
Explanation: The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.
2428
```
2529

30+
**Example 3:**
31+
32+
![3](./3.png)
33+
34+
```
35+
Input: grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1
36+
Output: 10
37+
Explanation: Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.
38+
```
2639

2740
## 结语
2841

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod2435 = 1000000007
4+
5+
func Solution(grid [][]int, k int) int {
6+
m, n := len(grid), len(grid[0])
7+
dp := make([][][]int, m)
8+
for i := 0; i < m; i++ {
9+
dp[i] = make([][]int, n)
10+
for j := 0; j < n; j++ {
11+
dp[i][j] = make([]int, k)
12+
}
13+
}
14+
sum := grid[0][0] % k
15+
dp[0][0][sum] = 1
16+
17+
for idx := 1; idx < n; idx++ {
18+
sum = (sum + grid[0][idx]) % k
19+
dp[0][idx][sum] = 1
20+
}
21+
22+
sum = grid[0][0] % k
23+
24+
// int first column
25+
for idx := 1; idx < m; idx++ {
26+
sum = (sum + grid[idx][0]) % k
27+
dp[idx][0][sum] = 1
28+
}
29+
30+
for i := 1; i < m; i++ {
31+
for j := 1; j < n; j++ {
32+
for idx := 0; idx < k; idx++ {
33+
sum = (idx + grid[i][j]) % k
34+
dp[i][j][sum] = (dp[i][j][sum] + dp[i-1][j][idx]) % mod2435
35+
dp[i][j][sum] = (dp[i][j][sum] + dp[i][j-1][idx]) % mod2435
36+
}
37+
}
38+
}
39+
return dp[m-1][n-1][0]
540
}

leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{5, 2, 4}, {3, 0, 5}, {0, 7, 2}}, 3, 2},
18+
{"TestCase2", [][]int{{0, 0}}, 5, 1},
19+
{"TestCase3", [][]int{{7, 3, 4, 9}, {2, 3, 6, 2}, {2, 3, 7, 0}}, 1, 10},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)