DP Practice Questions 2
DP Practice Questions 2
Homework # 7
Problem 1
Solution
a. Sub-Problem: dp[i] is the minimum fatigue the person can have if he stops
at the ith stop. b.
Problem 2
Now at this point you have become an algorithm expert and it’s the right time to increase the
difficulty level. You have given an array M = {1 ,2, 3, 8, 6, 7, 9, 10, 2, 1}. You have to calculate the
longest increasing with decreasing subsequence in the array. Let’s understand what a subsequence is
through an example.
1, 3, 6, 10, 1 à Subsequence
1, 2, 3, 5 à Sequence
There is a subtle difference between subsequence and sequence. In Subsequence we can skip some
elements but it should be done in increasing order, like 1 3 2 5 6 is not subsequence. Order of
elements in parent array should remain same in subsequence. Like here 3 comes after 1 so there exist
no subsequence in which 3 comes before 1.
In the problem you have to find such subsequence which has 2 parts, whose first part is increasing in
order and second part is decreasing in order and it is the longest subsequence.
Here, (red is increasing subsequence and green is decreasing subsequence)
i) 1 2 3 8 9 10 2 1
ii) 1 2 3 6 7 9 10 2 1
are one of such subsequences but we can see that second subsequence is longest so we will choose
this one
Solution
We will be having LIS and LDS , Longest increasing Sequence at ending index i and
Longest decreasing Subsequence Starting at index i
Problem 3
You are fascinated with stars and your friend challenges you with the algorithms question. In Problem
you are given a m * m matrix containing only 1 and 0. Your challenge is to find biggest star in matrix
in min amount of time and computations. For example in the following matrix the biggest start is
highlighted, it is M[5][4] with length = 3. The star at M[5][4] entry with length 3 means all of the
following entries have 1.
M[5][3] , M[5][2], M[5][1], //same row, previous columns
M[5][5], M[5][6], M[5][7], //same row, next columns
M[4][4], M[3][4], M[2][4], //same column, previous rows
M[6][4], M[7][4], M[8][4], //same column, next rows
M[4][3], M[3][2], M[2][1], //diagonal
M[6][5], M[7][6], M[8][7], // diagonal
M[6][3], M[7][2], M[8][1] // diagonal
M[4][5], M[3][6], M[2][7], // diagonal
1 1 0 1 1 0 1 1 0 1
1 1 1 0 1 1 1 0 1 0
0 1 0 1 1 1 0 0 0 1
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0
1 0 1 0 1 1 1 0 0 1
0 1 0 0 1 0 1 1 1 1
0 1 1 1 1 0 0 1 0 1
A) DP Recurrence
B) Time Complexity for DP Solution
C) Time Complexity for Naive Solution
Solution
Given a 2D matrix M of binary digits, we have to find the size of the largest Star of 1s
in it. Its DP solution is quite simple
As we simply populate 8 , 2D arrays up [][] down[][] right [][] left [][] and 4 diagonals
in a single pass.
Problem 4
As a CS student, your foremost duty is to find prime numbers, but not just some normal prime
numbers. You are given n (Positive integer) which represents the no of digits in the number. Out of all
possible numbers consisting on n digits, you have to find the numbers that satisfy following three
rules:
1) Sum of every three consecutive digits is a prime number
For example, 3 Consecutive, 283002 , 283002, 283002, 283002
2) Sum of every four consecutive digits is a prime number
Given n the no of digits in the number, you have to find all those numbers which follow these 3 rules.
A) Provide DP recurrence
B) Provide Run Time complexity
C) Provide Pseudo code for its implementation with EXPLANATION
Solution
isValid(num, range, dictionary) {
array = convert_num_to_array(num) // O(n)
sum_array[size(array) + 1]
sum_array[1] = 0
i = range;
printNums(digits) {
dictionary = {}
upper_limit = pow(10, digits)
range = 3
for (i -> 0 to upper_limit) {
if (i / 99999 > 0)
range = 5
else if (i / 9999 > 0)
range = 4
D) Provide DP recurrence
E) Provide Run Time complexity
F) Provide Pseudo code for its implementation with EXPLANATION
G) Solution
https://www.geeksforgeeks.org/coin-change-dp-7/