forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout-of-boundary-paths.py
24 lines (22 loc) · 930 Bytes
/
out-of-boundary-paths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Time: O(N * m * n)
# Space: O(m * n)
class Solution(object):
def findPaths(self, m, n, N, x, y):
"""
:type m: int
:type n: int
:type N: int
:type x: int
:type y: int
:rtype: int
"""
M = 1000000000 + 7
dp = [[[0 for _ in xrange(n)] for _ in xrange(m)] for _ in xrange(2)]
for moves in xrange(N):
for i in xrange(m):
for j in xrange(n):
dp[(moves + 1) % 2][i][j] = (((1 if (i == 0) else dp[moves % 2][i - 1][j]) + \
(1 if (i == m - 1) else dp[moves % 2][i + 1][j])) % M + \
((1 if (j == 0) else dp[moves % 2][i][j - 1]) + \
(1 if (j == n - 1) else dp[moves % 2][i][j + 1])) % M) % M
return dp[N % 2][x][y]