java.lang.Object
g2401_2500.s2435_paths_in_matrix_whose_sum_is_divisible_by_k.Solution

public class Solution extends Object
2435 - Paths in Matrix Whose Sum Is Divisible by K.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code> and an integer <code>k</code>. You are currently at position <code>(0, 0)</code> and you want to reach position <code>(m - 1, n - 1)</code> moving only <strong>down</strong> or <strong>right</strong>.</p> <p>Return <em>the number of paths where the sum of the elements on the path is divisible by</em> <code>k</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png" alt="" /></p> <p><strong>Input:</strong> grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png" alt="" /></p> <p><strong>Input:</strong> grid = [[0,0]], k = 5</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png" alt="" /></p> <p><strong>Input:</strong> grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= m * n <= 5 * 10<sup>4</sup></code></li> <li><code>0 <= grid[i][j] <= 100</code></li> <li><code>1 <= k <= 50</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfPaths

      public int numberOfPaths(int[][] grid, int k)