Q:
Given a M X N matrix with initial position at top-left cell, find the number of possible unique paths to reach the bottom right cell of the matrix from the initial position
belongs to collection: Top C++ Dynamic programming problems for interviews
Top C++ Dynamic programming problems for interviews
- how we can maximize the total benefit given a capacity of the bag is W and each item is allowed to be used for 0 or 1 time?
- A professional robber is planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping him from robbing each of them is that adjacent houses have security system connected
- Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins does not matter
- You are given an array A of size N. The array contains integers and is of even length. The elements of the array represent N coin of values x1, x2, ... ,xn
- Given a dictionary, you have to split a given string into meaningful words
- Given a sequence A of size N, find the length of the longest increasing subsequence from the given sequence
- Given a set of numbers, check whether it can be partitioned into two subsets or not such that the sum of elements in both subsets is same
- ou are given N eggs, and building with K floors from 1 to K. Each egg is identical, you drop an egg and if an egg breaks, you cannot drop it again
- Weights and values are given for n items along with the maximum capacity allowed W. What is the maximum value we can achieve if we can pick any weights, any number of times for the total allowed capacity of W?
- Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path
- Given a M X N matrix with initial position at top-left cell, find the number of possible unique paths to reach the bottom right cell of the matrix from the initial position
- Given two strings str1 and str2, find length of the longest common sub-sequence between them
- Given two strings string1 and string2 find the minimum cost required to make the given two strings identical. We can delete characters from both the strings
- You are given N eggs, and building with K floors from 1 to K. Each egg is identical, you drop an egg and if an egg breaks, you cannot drop it again
- Given an array of size n and a sum K, determine whether any subset is possible with that sum or not. Print \"yes\" if there is any subset present else print \"no\"
- Given an array of integers where each element represents the max number of steps that can be made forward from that element
- Given a rod of length N inches and an array of prices that contains prices for all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces
- Given a rectangular grid of dimension 2 x N find out the maximum sum such that no two chosen numbers are adjacent, vertically, diagonally or horizontally
- Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1\'s. Output your answer mod 10^9 + 7
- How many non-decreasing numbers can be generated of n digit length?
- Consider a highway of M miles. The task is to place billboards on the highway such that revenue is maximized
- Given two strings string1 and string2 find the minimum cost required to make the given two strings identical
- Given an array of n integers. Find the minimum number of elements from the array to remove or delete so that when the remaining elements are placed in the same sequence order form a sorted sequence
- Given a string find the length of longest palindromic subsequence

C++ programming
Recursion Algorithm:
Of course, we can generate a recursion algorithm. Since the only possible moves are down and right from any point, we can write a recursive relation, f(m,n) = f(m-1,n) + f(m,n-1) Where f(m,n) = number of unique path for grid size m,n f(m-1,n) = number of unique path for grid size [(m-1),n] a down move from the end point of this will result in f(m,n) f(m,n-1) = number of unique path for grid size [m,(n-1)] a right move from the end point of this will result in f(m,n) Hence, f(m,n) is summation of f(m-1,n) and f(m,n-1)So, the algorithm can be:
Function Uniquepaths(m,n): 1) If m==0 & n==0 return 0 2) If m == 0 Then return 1 3) If n==0 Then return 1 4) Return Uniquepaths(m-1,n)+Uniquepaths(m,n-1)But this would generate many overlapping subproblems, which will be computed again and again increasing the time complexity. Hence, we can convert the recursion to dynamic Programming.
Dynamic Programming approach:
1) Create a 2D array DP with size m, n 2) Initialize DP[0][0] =0 which is similar to step 1 in our previous function. 3) for i=1 to n-1 DP[0][i]=1 end for This is similar to step2 in our previous function 4) for i=1 to m-1 DP[i][0]=1 end for This is similar to step3 in our previous function 5) Compute the whole DP table for i=1 to m-1 for j=1 to n-1 DP[i][j]=DP[i-1][j]+DP[i][j-1] end for end for This is similar to step 4 in our previous recursive function 6) Return DP[m-1][n-1] which is the result.C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer