题目描述:
自己的提交:
class Solution: def numWays(self, steps: int, arrLen: int) -> int: l = min(steps,arrLen) dp = [0] * l dp[0] = 1 MOD = 10 ** 9 + 7 for step in range(steps): dp_ = dp[:] for i in range(len(dp)): if i == 0: dp_[i] = (dp[i] + dp[i+1]) % MOD elif i == len(dp) - 1: dp_[i] = (dp[i] + dp[i-1]) % MOD else: dp_[i] = (dp[i-1] + dp[i] + dp[i+1]) % MOD dp = dp_ return dp[0]
另:
class Solution: def numWays(self, steps: int, arrLen: int) -> int: l = min(steps,arrLen) dp = [0] * l dp[0] = 1 MOD = 10 ** 9 + 7 for step in range(steps): dp_ = [0] * l for i in range(len(dp)): for j in [-1,0,1]: if 0 <= i+j < l: dp_[i] += dp[i+j] dp_[i] %= MOD dp = dp_ return dp[0]
原文地址:https://www.cnblogs.com/oldby/p/11929036.html
时间: 2024-11-25 05:43:07