L32: Dynamic Programming (L4) : Some Problems and Discussions
L32: Dynamic Programming (L4) : Some Problems and Discussions
(L4)
Some Problems and Discussions
DP : problem
• Egg Dropping Puzzle | DP-11
https://www.geeksforgeeks.org/egg-dropping-puzzle-dp-11/
• Suppose that we wish to know which stories in a 36-story
building are safe to drop eggs from, and which will cause
the eggs to break on landing. The problem is not actually
to find the critical floor, but merely to decide floors
from which eggs should be dropped so that the total
number of trials are minimized.
2
• …..An egg that survives a fall can be used again.
…..A broken egg must be discarded.
…..If an egg breaks when dropped, then it would break if
dropped from a higher floor.
…..If an egg survives a fall then it would survive a
shorter fall.
…..It is not ruled out that the first-floor windows break
eggs, nor is it ruled out that the 36th-floor do not cause
an egg to break.
3
Problem Understanding & Recursive Structure
• When we drop an egg from a floor x, there can be two
cases (1) The egg breaks (2) The egg doesn’t break.
4
Systematically DP solution
1. Plan a brute-force/recursive solution
2. Make a recursive definition of the problem
5
Recursive Defn: Egg Drop
• EggDrop (n,k) = min [ for all x: 1…k
{1+max(eggDrop(n - 1, x - 1),eggDrop(n, k - x))} ]
• Base Cases:
If k == 1 or k == 0 then return k
If n == 1 return k
6
Systematically DP solution
1. Plan ----
2. Make a recursive ------
3. Find whether it has overlapping sub-problems. If yes, it can be
solved using DP (“DP” means bottom-up DP )
4. Memoized solution generation
5. From memoized: generation of DP Solution (in bottom up fashion)
6. Time Complexity: O (n*k*k) Space: O (n*k)
7
DP solution of Egg Drop: Further Optimization
1. Out of the DP-tabulation, which data is being used in immediate next
calculations.
2. For DP[i][j], DP[i-1][j-1] & DP[i][1...j-1] are being used actually
3. Hence we can have tabulation as 1-d array instead of 2-d array
4. Through that, Space Complexity: O (n)
5. Optimization of time: Let dp[i][j] store the max number of floors
that can be processed using i moves and j eggs. By doing so, we will
only be required to find the max reachable floor in the previous
move, and will avoid checking each k floor for all the states; Time
Complexity: O (n*k)
6. For more clarity, read from geeksforgeeks
8
9
Codechef: World War III
• https://www.codechef.com/problems/WOWR4
• As the zombie apocalypse is at its peak and there is rising tensions
among nations for getting a hold of the zombie cure. The cure is
available as N batches and each batch contains Ai vials (may not
be sorted) of the cure. Two superpowers are battling for the same.
As a UN peace advocate, Prof Paradox, refuses to sort this quarrel
out, turning it into the next World War. You, on the other hand, are
determined to avoid another possible doomsday scenario.
• You have to split the N batches into 2 equal shares, such that each
party gets an equal number of vials of cures without splitting any
batch. You may even choose to discard some batches to ensure
equal shares.
• Find the maximum number of vials that each nation can get and
prevent this war.
10
Codechef: World War III
• Sample Input: 4
1236
• Sample Output: 6
• EXPLANATION 1: We can divide the batches into [1,2,3]and [6],
which have the same number of vials, 6 each.
• Input: 6
123456
• Output: 10
11
World War III: Systematically DP solution
1. Plan a brute-force/recursive solution
– Brute force recursive strategy can solve this problem easily.
Each batch has 3 possibilities:
• It can go to superpower A
• It can go to superpower B
• It is just discarded
– We have to recur for all three scenarios and get the maximum
possible solution
12
World War III: Recursive Defn
• Let A,B be the sum of cures for superpower A and B, arr be the
array containing batches of vials of length n and i be the index of
the current element we are dealing with.
• maxSum(A,B, i):- {initial call as maxSum(0,0, 0)}
val1 = maxSum(A+arr[i], B, i + 1) //arr[i] with A
val2 = maxSum(A , B+arr[i], i + 1)
val3 = maxSum(A, B, i + 1) //arr[i] discarded
return max (val1,val2, val3)
• Base Case:
if (i == n)
if (A == B) return A
else return 0
Complexity: O(3N) 13
World War III: Systematically DP solution
1. Plan a brute-force/recursive solution
2. Make a recursive definition of the problem
3. Find whether it has overlapping sub-problems. If yes, it can be
solved using DP (“DP” means bottom-up DP )
4. Memoized solution generation
5. From memoized: generation of DP Solution (in bottom up fashion)
14
World War III: Systematically DP solution
1. Plan ----
2. Make a recursive ------
15
Improvement in DP Solutions is also possible
• Coin-Change using Min Coins : DP solution made
• Bottom-Up solution used a 2-d array: dp[n+1][sum+1]
• Time : O(n*sum); Space: O(n*sum)
• Crux of DP logic:
if (sum-coins[i]) is greater than 0, then
dp[i][sum] = min(1+dp[i][sum-coins[i]], dp[i+1][sum])
else dp[i][sum] = dp[i+1][sum]
• For calculating current dp[i][sum]: we only need previous row dp[i-
1][sum] or current row dp[i][sum-coins[i]].
• There is no need to store all previous rows, just one prev row is used.
• So Space can be reduced to O (sum).
16
Practice Problems
• Geeks:
https://www.geeksforgeeks.org/top-50-dynamic-programming-
coding-problems-for-interviews/
• P1: Green island Bomber:
https://www.codechef.com/problems/GENTHRU
• P2: Polygon & Cannon
https://codeforces.com/problemset/problem/1360/E
• P3: Snake & Ladders: Quickest Up
https://www.hackerrank.com/challenges/the-quickest-way-
up/problem
• Optional Practice Problems
https://www.codechef.com/problems/RAMSICK
https://www.hackerrank.com/challenges/jeanies-route/problem
https://www.codechef.com/problems/AUSCRIC (scc) 17