Suppose we have a value h and a list of numbers called blacklist. We are currently at height h, and are playing a game to move a small ball down to height 0. Now, in even rounds (starting from 0) we can move the ball 1, 2, or 4 stairs down. And in odd rounds, we can move the ball 1, 3, or 4 stairs down. Some levels are blacklisted. So if the ball reach there, it will die immediately. We have to find the number of ways the ball can move down at height 0. If the answer is too large, then mod the result by 10^9 + 7.
So, if the input is like h = 5 blacklist = [2, 1], then the output will be 2, because on round 0, move one step first (5 to 4), then in the next round from 4 to 0. Another possible way could be at round 0, move two steps (5 to 3), then in the next round 3 to 0.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(h, blacklist): blacklist = set(blacklist) if 0 in blacklist or h in blacklist: return 0 dp = [[0, 0] for i in range(h + 1)] dp[0] = [1, 1] m = 10 ** 9 + 7 for i in range(1, h + 1): for x in [1, 2, 3, 4]: if i - x >= 0 and i - x not in blacklist: if x != 3: dp[i][0] += dp[i - x][1] if x != 2: dp[i][1] += dp[i - x][0] dp[i][0] %= m dp[i][1] %= m return dp[h][0] h = 5 blacklist = [2, 1] print(solve(h, blacklist))
5, [2, 1]
2