DP 2
DP 2
DP 2
Pre-requisites
What is Dynamic Programming
What are overlapping subproblems
What is an optimal substructure
Greedy vs D
Where to use dynamic programming
What are the various approaches of DP
What is memoization
Steps of solving a DP proble
Problems of Memoization
Today’s Checklist
What is the Limitation of Top-DownProgramming
What is Tabulation
How is tabulation different from memoization
Fibonacci with tabulatio
Problems of tabulation
Since the process starts with the big picture, it can be difficult to make changes later on in the process
Breaking down the bigger problem into smaller subproblems can take a lot of time, especially if the design
is complex
It requires massive amounts of expensive recursion.
Answer: 3
EXPLANATION:
In the bottom-up dynamic programming approach, we’ll reorganize the order in which we solve the
subproblems.
So, we start with the smallest problem and reach till the largest problem which is the given problem.
Let’s checkout the code using a table to store the previously computed results.
Code:
https://pastebin.com/KTqZP2iN
OUTPUT:
Now, if we see, the above approach will only allow us to compute the solution to each problem only once,
and we’ll only need to save two intermediate results at a time
For example, when we’re trying to find the answer for n = 2, we only need to have the solutions to n=1 and
n=0 available. Similarly, for n = 3, we only need to have the solutions to n = 2 and n = 1
To implement this space optimization, we make use of only two variables, and these variables store the
required 2 values at each point of iteration until we reach n
This will allow us to use less memory space in our code
This is called space-state reduction.
CODE:
https://pastebin.com/jJRPUtrT
Q1. Given an integer array of coins[ ] of size N representing different types of currency and an integer sum,
The task is to find the number of ways to make a sum by using different combinations from coins[]. Assume
Output1: 4
Explanation: there are four solutions: {1, 1, 1, 1}, {1, 1, 2}, {2, 2}, {1, 3}.
Output: 5
Explanation
The Idea to Solve this Problem is by using the Bottom Up(Tabulation). By using the linear array for space
optimization
Update the level wise number of ways of coin till the ith coin
Code:
https://pastebin.com/jJRPUtrT
Output:
We are given N items where each item has some weight and profit associated with it. We are also given a bag
with capacity W, [i.e., the bag can hold at most W weight in it]. The target is to put the items into the bag such
that the sum of profits associated with them is the maximum possible.
Input1:
Output1: 3
Explanation: There are two items which have weight less than or equal to 4. If we select the item with weight 4,
the possible profit is 1. And if we select the item with weight 1, the possible profit is 3. So the maximum possible
profit is 3. Note that we cannot put both the items with weight 4 and 1 together as the capacity of the bag is 4.
Output2: 0
Explanation
A simple solution is to consider all subsets of items and calculate the total weight and profit of all subsets.
From all such subsets, pick the subset with maximum profit
To consider all subsets of items, there can be two cases for every item.
The maximum value obtained from ‘N’ items is the max of the following two values
Maximum value obtained by N-1 items and W weight (excluding nth item)
Value of nth item plus maximum value obtained by N-1 items and (W – weight of the Nth item) [including
Nth item]
If the weight of the ‘Nth‘ item is greater than ‘W’, then the Nth item cannot be included and Case 1 is the only
possibility
Since subproblems are evaluated again, this problem has Overlapping Sub-problems property and optimal
substructure property
Re-computation of the same subproblems can be avoided by constructing a temporary array K[][] in a
bottom-up manner.
In a DP[][] table let’s consider all the possible weights from ‘1’ to ‘W’ as the columns and the element that
The state DP[i][j] will denote the maximum value of ‘j-weight’ considering all values from ‘1 to ith‘. So if we
consider ‘wi‘ (weight in ‘ith‘ row) we can fill it in all columns which have ‘weight values > wi‘. Now two
Formally if we do not fill the ‘ith‘ weight in the ‘jth‘ column then the DP[i][j] state will be the same as DP[i-1]
[j]
So we take the maximum of these two possibilities to fill the current state.
Code:
https://pastebin.com/amGZwQVE
Output:
Q3. There are N stones, numbered 1,2,…,N. The height of ith stone is hi.
There is a frog who is initially on Stone 1. He will repeat an action some number of times to reach Stone N. The
action is that if the frog is currently on Stone i, it jumps to one of the following: Stone i+1,i+2,…,i+K. Here, a cost
Find the minimum possible total cost incurred before the frog reaches Stone N.
Input1:
n = 5
k = 3
10 30 40 50 20
Output1:
30
Input2:
3 1
10 20 10
Output2:
20
Explanation
Let's define dp[i] as the minimum cost to reach Stone i from Stone 1. Our goal is to find dp[N], which
Code:
https://pastebin.com/QGT6r83E
Output:
Upcoming lectures:
Interview Problems on DP-1