Last active
November 25, 2019 18:29
-
-
Save liketheflower/52038fbe5d808fc51a85c738337a7c4e to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
DP top down | |
dp(pos, step) = sum(dp(pos+1, step-1), dp(pos-1, step-1), dp(pos, step-1)) | |
""" | |
from functools import lru_cache | |
class Solution: | |
def numWays(self, steps: int, arrLen: int) -> int: | |
MOD = 10**9 + 7 | |
@lru_cache(None) | |
def dp(pos, steps): | |
if pos < 0 or pos >= arrLen: return 0 | |
if pos > steps: return 0 | |
if steps == pos: return 1 | |
return (dp(pos+1, steps-1) + dp(pos-1, steps-1) + dp(pos, steps-1)) % MOD | |
return dp(0, steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment