Suppose we are at position 0 of n length list, and on each step, we can move right one place or left one place (not exceeding bounds), or stay at the same position. Now if we can take exactly k steps, then we have to find how many unique walks we can take and reach back to index 0. If the answer is very large return it mod 10^9 + 7.
So, if the input is like n = 7 k = 4, then the output will be 9, as the actions are-
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, length, n): MOD = 10 ** 9 + 7 N = length def dp(i, jumps): if jumps == 0: return +(i == 0) count = dp(i, jumps - 1) if i >= 0: count += dp(i + 1, jumps - 1) if i <= N - 1: count += dp(i - 1, jumps - 1) return count return dp(0, n) % MOD ob = Solution() n = 7 k = 4 print(ob.solve(n, k))