diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/1.png b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/1.png new file mode 100644 index 000000000..36f7b5f12 Binary files /dev/null and b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/1.png differ diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/2.png b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/2.png new file mode 100644 index 000000000..8e2c8f0f6 Binary files /dev/null and b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/2.png differ diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/3.png b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/3.png new file mode 100644 index 000000000..62a0d72f6 Binary files /dev/null and b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/3.png differ diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/README.md b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/README.md index df92f7779..277c246d9 100755 --- a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/README.md +++ b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/README.md @@ -1,28 +1,41 @@ # [2435.Paths in Matrix Whose Sum Is Divisible by K][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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**. + +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`. -**Example 1:** +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3 +Output: 2 +Explanation: There are two paths where the sum of the elements on the path is divisible by k. +The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3. +The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Paths in Matrix Whose Sum Is Divisible by K -```go +``` +Input: grid = [[0,0]], k = 5 +Output: 1 +Explanation: The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5. ``` +**Example 3:** + +![3](./3.png) + +``` +Input: grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1 +Output: 10 +Explanation: Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k. +``` ## 结语 diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution.go b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution.go index d115ccf5e..bd844637d 100644 --- a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution.go +++ b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution.go @@ -1,5 +1,40 @@ package Solution -func Solution(x bool) bool { - return x +const mod2435 = 1000000007 + +func Solution(grid [][]int, k int) int { + m, n := len(grid), len(grid[0]) + dp := make([][][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([][]int, n) + for j := 0; j < n; j++ { + dp[i][j] = make([]int, k) + } + } + sum := grid[0][0] % k + dp[0][0][sum] = 1 + + for idx := 1; idx < n; idx++ { + sum = (sum + grid[0][idx]) % k + dp[0][idx][sum] = 1 + } + + sum = grid[0][0] % k + + // int first column + for idx := 1; idx < m; idx++ { + sum = (sum + grid[idx][0]) % k + dp[idx][0][sum] = 1 + } + + for i := 1; i < m; i++ { + for j := 1; j < n; j++ { + for idx := 0; idx < k; idx++ { + sum = (idx + grid[i][j]) % k + dp[i][j][sum] = (dp[i][j][sum] + dp[i-1][j][idx]) % mod2435 + dp[i][j][sum] = (dp[i][j][sum] + dp[i][j-1][idx]) % mod2435 + } + } + } + return dp[m-1][n-1][0] } diff --git a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution_test.go b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution_test.go index 14ff50eb4..e4d88281b 100644 --- a/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution_test.go +++ b/leetcode/2401-2500/2435.Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{5, 2, 4}, {3, 0, 5}, {0, 7, 2}}, 3, 2}, + {"TestCase2", [][]int{{0, 0}}, 5, 1}, + {"TestCase3", [][]int{{7, 3, 4, 9}, {2, 3, 6, 2}, {2, 3, 7, 0}}, 1, 10}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }