- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to count number of ways ball can drop to lowest level by avoiding blacklisted steps in Python
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 −
- blacklist := a new set from the elements of blacklist
- if 0 is in blacklist or h is in blacklist, then
- return 0
- dp := a list of size h, and inside that store pairs [0, 0] at each index
- dp[0] := [1, 1]
- m := 10^9 + 7
- for i in range 1 to h, do
- for each x in [1, 2, 3, 4], do
- if i - x >= 0 and i - x is not in blacklist, then
- if x is not same as 3, then
- dp[i, 0] := dp[i, 0] + dp[i - x, 1]
- if x is not same as 2, then
- dp[i, 1] := dp[i, 1] + dp[i - x, 0]
- if x is not same as 3, then
- dp[i, 0] := dp[i, 0] mod m
- dp[i, 1] := dp[i, 1] mod m
- if i - x >= 0 and i - x is not in blacklist, then
- for each x in [1, 2, 3, 4], do
- return dp[h, 0]
Example
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))
Input
5, [2, 1]
Output
2
- Related Articles
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of ways we can throw n dices in Python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- C++ program to count number of stairways and number of steps in each stairways
- Program to count number of ways we can place nonoverlapping edges to connect all nodes in C++
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to print maximum number of characters by copy pasting in n steps in Python?
- Program to find number of steps to solve 8-puzzle in python
