Skip to content

Commit 583232b

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 2435
1 parent c340390 commit 583232b

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
- [2215 Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays/description/)
303303
- [2353 Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/description/)
304304
- [2390 Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/description/)
305+
- [2435 Paths in Matrix Whose Sum Is Divisible by K](https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/description/)
305306
- [2466 Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/description/)
306307
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
307308
- [2785 Sort Vowels in a String](https://leetcode.com/problems/sort-vowels-in-a-string/description/)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def numberOfPathsTD(self, grid: List[List[int]], k: int) -> int:
8+
"""
9+
You are given a 0-indexed m x n integer matrix grid and an integer k.
10+
You are currently at position (0, 0) and you want to reach position
11+
(m - 1, n - 1) moving only down or right.
12+
13+
Return the number of paths where the sum of the elements on the path is
14+
divisible by k. Since the answer may be very large, return it modulo 109 + 7.
15+
"""
16+
# Top-Down Dynamic Programming (Memoization)
17+
ROWS = len(grid)
18+
COLS = len(grid[0])
19+
MOD = 10**9 + 7
20+
dp = [[[-1] * k for _ in range(COLS)] for _ in range(ROWS)]
21+
22+
def dfs(row: int, col: int, remain: int):
23+
if row == ROWS - 1 and col == COLS - 1:
24+
remain = (grid[row][col] + remain) % k
25+
return 0 if remain else 1
26+
if row == ROWS or col == COLS:
27+
return 0
28+
if dp[row][col][remain] > -1:
29+
return dp[row][col][remain]
30+
dp[row][col][remain] = (
31+
dfs(row + 1, col, (grid[row][col] + remain) % k) % MOD
32+
+ dfs(row, col + 1, (grid[row][col] + remain) % k) % MOD
33+
) % MOD
34+
return dp[row][col][remain]
35+
36+
return dfs(0, 0, 0)
37+
38+
def numberOfPathsBU(self, grid: List[List[int]], k: int) -> int:
39+
"""
40+
You are given a 0-indexed m x n integer matrix grid and an integer k.
41+
You are currently at position (0, 0) and you want to reach position
42+
(m - 1, n - 1) moving only down or right.
43+
44+
Return the number of paths where the sum of the elements on the path is
45+
divisible by k. Since the answer may be very large, return it modulo 109 + 7.
46+
"""
47+
# Bottom-Up Dynamic Programming (Tabulation)
48+
ROWS = len(grid)
49+
COLS = len(grid[0])
50+
MOD = 10**9 + 7
51+
dp = [[[0] * k for _ in range(COLS + 1)] for _ in range(ROWS + 1)]
52+
target_remain = (k - (grid[ROWS - 1][COLS - 1] % k)) % k
53+
dp[ROWS - 1][COLS - 1][target_remain] = 1
54+
55+
for row in reversed(range(ROWS)):
56+
for col in reversed(range(COLS)):
57+
if row == ROWS - 1 and col == COLS - 1:
58+
continue
59+
for remain in range(k):
60+
new_remain = (grid[row][col] + remain) % k
61+
dp[row][col][remain] = (
62+
dp[row + 1][col][new_remain] % MOD
63+
+ dp[row][col + 1][new_remain] % MOD
64+
) % MOD
65+
66+
return dp[0][0][0]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._2435_paths_in_matrix_whose_sum_is_divisible_by_k import (
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["grid", "k", "expected"],
12+
argvalues=[
13+
([[5, 2, 4], [3, 0, 5], [0, 7, 2]], 3, 2),
14+
([[0, 0]], 5, 1),
15+
([[7, 3, 4, 9], [2, 3, 6, 2], [2, 3, 7, 0]], 1, 10),
16+
],
17+
)
18+
def test_funcTD(grid: List[List[int]], k: int, expected: int):
19+
"""Tests the solution of a LeetCode problem."""
20+
paths = Solution().numberOfPathsTD(grid, k)
21+
assert paths == expected
22+
23+
24+
@pytest.mark.parametrize(
25+
argnames=["grid", "k", "expected"],
26+
argvalues=[
27+
([[5, 2, 4], [3, 0, 5], [0, 7, 2]], 3, 2),
28+
([[0, 0]], 5, 1),
29+
([[7, 3, 4, 9], [2, 3, 6, 2], [2, 3, 7, 0]], 1, 10),
30+
],
31+
)
32+
def test_funcBU(grid: List[List[int]], k: int, expected: int):
33+
"""Tests the solution of a LeetCode problem."""
34+
paths = Solution().numberOfPathsBU(grid, k)
35+
assert paths == expected

0 commit comments

Comments
 (0)