Skip to content

Commit 48ef49f

Browse files
authored
Create number-of-ways-to-stay-in-the-same-place-after-some-steps.py
1 parent 2759acc commit 48ef49f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n^2), n is the number of steps
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def numWays(self, steps, arrLen):
6+
"""
7+
:type steps: int
8+
:type arrLen: int
9+
:rtype: int
10+
"""
11+
MOD = int(1e9+7)
12+
l = min(1+steps//2, arrLen)
13+
dp = [0]*(l+2)
14+
dp[1] = 1
15+
while steps > 0:
16+
steps -= 1
17+
new_dp = [0]*(l+2)
18+
for i in xrange(1, l+1):
19+
new_dp[i] = (dp[i] + dp[i-1] + dp[i+1]) % MOD
20+
dp = new_dp
21+
return dp[1]

0 commit comments

Comments
 (0)