Checkpoint Week 1
Checkpoint Week 1
#include <iostream>
f(5)
int f(int n) {
f(4) f(3)
if (n <= 1)
/ \ / \
return n;
f(3) f(2) f(2) f(1)
}
f(2) f(1) f(1) f(0) f(1) f(0)
int main() {
/ \
f(5)
/ \
/ \
/ \
f(2) f(1)
and reused!
/ \
Solution : Store & Reuse Computed
f(1) f(0)
Solve smaller subproblems first and use their results to build up the
solutions.
Key Takeaways
Solution : Import the sys module and use the set recursion limit attribute to
increase the recursion limit to a value that doesn’t give RE (a matter of trial
and error). A general value that works in many problems is 3* 10^5.
Solution : Like the soln above, you could reduce the stack size using the set
recursion limit attribute but then again it would give you a MRD. So the
best thing would be to avoid recursive calls at all. This can be done by
writing the iterative version of the recursive logic you used.
Few resources:
CPH (pg 65 to 69) (If you don’t understand in one read, read it again
Errichto (Lecture 1 and Lecture 2
Luv Competitive Programming DP playlist
If you didn’t understand even after referring to the resources, don’t worry
' j j
it s normal, ust ump to solving problems.
Common Examples of DP :
Knapsack problem
programming based solution. Suppose you are given n items, each with a
It is broadly of 3 types
items in decreasing order of value by weight ratio and the larger ones
first
0-1 Knapsack - Solved using DP (one item can be picked atmost once
Feeling bored from reading lengthy blogs? Don’t worry click here
Practice Problems
Knapsack
Coin Combinations
Subset Sum Problem (Slight variation of knapsack
Let’s say we are tasked to find the length of the longest common
subsequence of strings s and t.
The idea here is to make a 2 dimensional array for dp, where dp[i][j] would
store the length of the longest common subsequence of strings formed
using the first i characters of s and first j characters of t.
Reading resources
GeeksforGeek
takeUforward
Practice Problems
LC
Print LC
Three Strings
Suppose you are given n matrices A1, A2, . . .,An with dimensions p0 x p1,
p1 x p2, . . ., p(n−1) x pn. The goal is to determine the optimal
parenthesization that minimizes the total number of scalar
multiplications.
Knuth's Optimization :
Many problems like Matrix Chain Multiplication follow a quadrangle
inequality, allowing an O(n³) DP solution to be optimized to O(n²).
Practice Problems
Burst Balloon
Slime
Array Descriptio
ROCK - Sweet and Sour Rock
Miscellaneous Problems
Rudolf and k Bridge
How Does the Rook Move
Sending a Sequence Over the Networ
Colored Ball
GCD on a gri
Feed Cats
Rolling Optimization
Reduces the space complexity form O(n) to O(1)
Based on the fact that you do not need to store data once it has be
used to calculate all the states it can. Recall the iterative version of
Fibonacci number problem. We only need the information about two
previous states for the calculation of our current state ( F[n] = F[n-1] +
F[n-2] ). So, the idea is to start calculating F[3] with the help of F[2] and
F[1] and then forget about F[1] as F[4] doesn’t depends on F[1] and so on.
If your logic seems correct, but still Memory Limit Exceeded in showing,
try this approach
problems.
Few resources
CPH (102-106
Nwatx
PROBLEMS
O - Matchin
Team Buildin
Elevator Ride
Carrying Conundru
Hammiltonian Flight
Shuffling Songs
Recommended practice problems for mastering DP :
CSES DP Section : Visit the DP section on the CSES problem set and
with DP.
Jellyfish and ME
Zero Pat
Road Optimizatio
Block Sequenc
Journe
Coloring Tree
Blocking Element
Counting Rhyme