Path with K Coins
Posted: 28 Nov, 2020
Difficulty: Hard
The Ultimate Ninja Ankush is playing a game where he has a grid of ‘N’ * ‘M’ size and each block of the grid has some coins. The Ultimate Ninja ankush is carrying a knapsack that can fit only ‘K’ coins, and since he is very greedy, he wants to know the number of paths where he can collect exactly ‘K’ coins. He is initially at the top-left and wants to reach bottom-right
More formally, return the number of paths whose total sum of coins is equal to ‘K’ from 0,0 to ‘N - 1’, ‘M - 1’.
Note:
Since the answer can be very large print the answer modulo 10 ^ 9 + 7.
For example
Given:
‘N’ = 3, ‘M’ = 3
‘Grid[][]’ = [[5, 2, 5],
[3, 3, 1],
[3, 5, 1]]
‘K’ = 14.
The answer will be 1, since There is only 1 path that is from (0,0) to (0,1) to (0,2) to (1,2) to (2,2) this is because 5 + 2 + 5 + 1 + 1 = 14
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘M,’ where ‘M’ is the number of rows in ‘SEATS’ and ‘N’ where ‘N’ is the number of columns in ‘GRID’.
The next ‘M’ lines of each test case contains ‘N’ space-separated integers, which denotes the number of coins at each point.
The next line of each test case contains a single integer ‘K’.
Output Format :
For each test case, You are supposed to return an integer that denotes the total number of paths with the sum of coins equal to ‘K’.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= ‘T’ <= 10
1 <= ‘N’ <= 20
1 <= ‘M’ <= 20
0 <= ‘K’ <= 1000
Time Limit: 1sec.
Approach 1
The idea is to recursively travel all paths and calculate which path’s total sum of coins is equal to ‘K’.
The steps are as follows:
- We will use a helper function ‘pathWithKCoinsHelper’ which takes ‘grid’, ‘row’, ‘col’ , ‘myCoins’, and ‘K’ as input parameters and returns the number of paths that exist such that the sum of coins equal ‘K’, Initially ‘row’ and ‘col’, and ‘myCoins’ are 0.
- The base condition would be to check if we are at the last cell and if ‘myCoins’ is equal to ‘K’. If they are, return 1, indicating that we found a valid path.
- If we are out of bounds or ‘myCoins’ is greater than ‘K’, return 0, indicating that we did not find a valid path.
- Recur for ‘row + 1’, and ‘myCoins + grid[row][col]’ and store it in a variable ‘moveForward’ which denotes the number of valid paths if we move forward.
- Similarly, recur for ‘col + 1’ and ‘myCoins + grid[row][col]’ and store it in a variable ‘moveDown’, which denotes the number of valid paths if we move down.
- Return the final answer as the sum of ‘moveDown’ and ‘moveForward’, which denotes the total number of valid paths if we moved in the downward direction or in the forward direction.
Approach 2
The idea is to recursively travel all paths and calculate which path’s total sum of coins is equal to ‘K’.
We can see that there will be recurring subproblems. Therefore we can hash the recursion states ‘row’, ‘col’, ‘myCoins’ in a 3-D array ‘dp’ where ‘dp[row][col][myCoins]’ denotes the number of valid paths at the ‘row’, ‘col’ with ‘myCoins’ in the knapsack.
The steps are as follows:
- We will use a helper function ‘pathWithKCoinsHelper’ which takes ‘grid’, ‘row’, ‘col’ , ‘myCoins’, and ‘K’ as input parameters and returns the number of paths that exist such that the sum of coins equal ‘K’, Initially ‘row’ and ‘col’, and ‘myCoins’ are 0.
- The base condition would be to check if we are at the last cell and if ‘myCoins’ is equal to ‘K’. If they are, return 1, indicating that we found a valid path.
- If we are out of bounds or ‘myCoins’ is greater than ‘K’, return 0, indicating that we did not find a valid path.
- If we have already visited the block in the grid with a given ‘row’, ‘col’ and with ‘myCoins’ amount of coins, then return ‘dp[row][col][myCoins]’.
- Recur for ‘row + 1’, and ‘myCoins + grid[row][col]’ and store it in a variable ‘moveForward’ which denotes the number of valid paths if we move forward.
- Similarly, recur for ‘col + 1’ and ‘myCoins + grid[row][col]’ and store it in a variable ‘moveDown’, which denotes the number of valid paths if we move down.
- Hash the current state by assigning the answer for the current state to ‘dp[row][col][myCoins].
- Return the final answer as the sum of ‘moveDown’ and ‘moveForward’.
SIMILAR PROBLEMS
Circle Intersection
Posted: 22 Apr, 2022
Difficulty: Easy
Max Non Adjacent sum
Posted: 10 May, 2022
Difficulty: Easy
Mario And His Princess
Posted: 12 May, 2022
Difficulty: Moderate
Pair Product Div by K
Posted: 15 May, 2022
Difficulty: Moderate
No Repeated Digits
Posted: 17 Jun, 2022
Difficulty: Easy