Created
November 26, 2025 02:47
-
-
Save igavrysh/895e54d5e465a1625281ae9e5d09b21d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def numberOfPaths(self, grid: List[List[int]], k: int) -> int: | |
| mod = 1000000007 | |
| rows = len(grid) | |
| cols = len(grid[0]) | |
| dp = [[[0]*(2*k) for j in range(cols)] for j in range(rows)] | |
| for i in range(rows): | |
| for j in range(cols): | |
| val = grid[i][j] | |
| if i==0 and j==0: | |
| dp[i][j][val%k] = val | |
| dp[i][j][k+val%k] = 1 | |
| for kk in range(k): | |
| if i-1>=0: | |
| t_rem = (kk + val)%k | |
| dp[i][j][t_rem] = dp[i][j][t_rem] + dp[i-1][j][k+kk] | |
| dp[i][j][k+t_rem] = (dp[i][j][k+t_rem] + dp[i-1][j][k+kk]) % mod | |
| if j-1>=0: | |
| t_rem = (kk + val)%k | |
| dp[i][j][t_rem] += dp[i][j-1][kk] + val | |
| dp[i][j][k+t_rem] = (dp[i][j][k+t_rem] + dp[i][j-1][k+kk]) % mod | |
| ''' | |
| print("===================") | |
| for i in range(rows): | |
| for j in range(cols): | |
| print("(", end="") | |
| for kk in range(k): | |
| print(f"{dp[i][j][kk]},", end="") | |
| print(")", end="") | |
| print("") | |
| print("===================") | |
| for i in range(rows): | |
| for j in range(cols): | |
| print("(", end="") | |
| for kk in range(k): | |
| print(f"{dp[i][j][k+kk]},", end="") | |
| print(")", end="") | |
| print("") | |
| print("===================") | |
| ''' | |
| return dp[rows-1][cols-1][k] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment