Skip to content

Commit b34f310

Browse files
committed
[LeetCode Sync] Runtime - 3176 ms (20.18%), Memory - 358.9 MB (12.11%)
1 parent 50b9344 commit b34f310

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<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>
2+
3+
<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>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img src="https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png" style="width: 437px; height: 200px;" />
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+
<p><strong class="example">Example 2:</strong></p>
17+
<img src="https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png" style="height: 85px; width: 132px;" />
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+
<p><strong class="example">Example 3:</strong></p>
25+
<img src="https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png" style="width: 257px; height: 200px;" />
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+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>m == grid.length</code></li>
37+
<li><code>n == grid[i].length</code></li>
38+
<li><code>1 &lt;= m, n &lt;= 5 * 10<sup>4</sup></code></li>
39+
<li><code>1 &lt;= m * n &lt;= 5 * 10<sup>4</sup></code></li>
40+
<li><code>0 &lt;= grid[i][j] &lt;= 100</code></li>
41+
<li><code>1 &lt;= k &lt;= 50</code></li>
42+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def numberOfPaths(self, grid: List[List[int]], k: int) -> int:
3+
m, n = len(grid), len(grid[0])
4+
5+
@cache
6+
def dfs(i: int, j: int, sum_val: int) -> int:
7+
if i < 0 or i >= m or j < 0 or j >= n:
8+
return 0
9+
sum_val = (sum_val + grid[i][j]) % k
10+
if i == m - 1 and j == n - 1:
11+
return int(sum_val == 0)
12+
return (dfs(i + 1, j, sum_val) + dfs(i, j + 1, sum_val)) % (10**9 + 7)
13+
14+
15+
result = dfs(0, 0, 0)
16+
dfs.cache_clear()
17+
return result

0 commit comments

Comments
 (0)