this blog will talk about the info needed to solve DP problems in general.
DP or dynamic programming is a method used to solve bigger problems be dividing them in some way
however while learning DP i recommend being similar with some data structures , sometimes we might just need a set or a map but at expert level i recommend being familiar with segment tree data structure.
this blog will be divided into phases where i explain some basic DP problems or explain some DP method in general.
each phase will contain codes and examples , you can ask more questions in comments.
1.Fibonacci
sorry for this i really know this is what people tend to explain when doing DP but hear me out
the standard Fibonacci function looked something like this
int f(int n){
if(n<1)return 0;
return f(n-1)+f(n-2);
}
thinking about the complexity of this code , it appears that if takes $$$O(f(n))$$$ time complexity.
you should know that $$$f(40)=102334155$$$ so we may not be able to compute $$$f(50)$$$ or anything greater with this function!.
or.. can we do something to make this even faster?
note that computing $$$f(i)$$$ will call $$$f(i-1)$$$ and $$$f(i-2)$$$ but then calling $$$f(i-1)$$$ will also call $$$f(i-2)$$$ , so here is the catch , lets change our function so that whenever we are computing $$$f(n)$$$ for a second time we return the previously computed answer, something like so
int mem[N],vis[N];
int f(int n){
if(n<1)return 0;
if(n==1)return 1;
if(vis[n])return mem[n];
vis[n]=1;
return mem[n]=f(n-1)+f(n-2);
}
what is the complexity now? , turns out it is $$$O(N)$$$ as we may compute each item at most once and the rest will be returned in $$$O(1)$$$.
this is the trick behind (some) DP methods , but knowing this method we will jump into some actual problems that use it.
no comment needed , you may just observe how similar this is to Fibonacci (or you might just jump to solve it directly).
your task is now to try and solve Frog 2.
2. more conditions
previously we only needed to handle one state where as in most cases we may need more , it is really funny how some people solve DP problems with $$$7$$$ states and you see their code like how are you even understanding this!.
lets go even deeper , two states it is , and the problem Vacation might be a best choice for this.
3.Knapsack
this problem was really pain to understand at first , and it is not really present in the contest as long as i can see, we will now talk about Knapsack 1.
in this problem we have to compute $$$dp(W)$$$ as the maximum profit from a capacity of $$$W$$$, now we need to know how to compute it using previous states , when we use an item we add ts value and subtract its weight like so
dp(W)=max(dp(W-w[i])+v[i])
but this way we may use an item more than once (the problem).
and so we may need to add extra state to know which items were used
our state will be $$$dp[W][2^{100}]$$$ (just kidding this is impossible ).
i will leave you to think on how to solve this.
try to solve Knapsack 2 !
4.Tracing
Ok! now we are able to find the minimum,maximum whatsoever but how do we actually know how we reached the answer? like in Knapsack how do we know what items we choose?
till now we used only two arrays mem(the answer),vis(did we calculate it before?)
now we will use another array to know from which state we got our best answer after calculating the answer we use this array to trace it.
we will present this in a simple problem like this problem this problem was solved once and i saw $$$2$$$ or maybe more standard problems like it,
the state was like $$$dp[index in S][index in T]$$$
but knowing the transition might be hard so i will leave you to think!
this way we made sure we tried all cases
we tried matching the current positions and incrementing each index by one,and so (somehow!) we can say we tried every case and got the best answer in the end!.
the code for the answer would be something like this
5.Time complexity
a main issue in DP as we really need to calculate this before going forward with our solution. the time complexity is simply sum of number of operations in all states
for simplicity i usually calculate the complexity of the dp function and multiply this by the number of states (this is not precise in general)
we will go forward with an example Candies
at first glance you might think you knew the solution, but usually you will get a TLE , why?.
lets say your dp function looked like this:
for(int j=0;j<=min(a[i],k);j++){
res+=dp(i+1,k-j);
}
what is the complexity of this?
for each $$$dp[index][number of candies]$$$ we will loop (in worst case) on the number of candies, which means our complexity is $$$nk^2$$$ (an absolute disaster) we will have to get this loop in $$$O(1)$$$ for this to work.
i recommend you try to solve it.
6.DP left right
this kind is as easy as it sounds (maybe the most unexpected answer for a problem)
you might even be able to solve the Deque problem yourself
the idea is to get the answer of an array by getting the answer of some sub arrays .
lets try to solve the problem
the state is as following:
knowing this we can do two transitions:
so the answer would look like:
another good example on this would be Slimes
try to solve it yourself the continue.
Break
here we are actually done with standard DP
from now on we will tend to explain somethings in short and less explained way
till now the learned info is more than enough till expert level or even master level
after this there will a bunch of hard topics that are considered DP approach (each one may need a whole new blog).
you now are able to solve these:
7.DP with a trick
this trick was so amazing when i learned it
in the past we learned that DP goes in a single direction but what if the DP calls itself?
here is an example Sushi
the state of the DP here is
you may try to solve this on your own at first
8.DP bitmask
here a bitmask is chosen to identify if an element is used or not , we may use this in a lot of ways , i remember using a bitmask of $$$3^n$$$ or even $$$4^n$$$ but let's focus now on $$$2^n$$$
let's solve this problem Matching.
what is the time complexity of this code?
9.DP on tree
the most annoying kind especially with re rooting , lets focus on the main idea.
in this kind of dp you must chose a root and calculate the result of each node depending on its children.
like in the Independent Set problem.
the state of the dp is $$$dp[node][color]$$$ so if the current color is black you consider the answer of $$$dp[child][white]$$$ , otherwise you consider both $$$dp[child][white]$$$ and $$$dp[child][black]$$$
note that we must multiply the childrens answer (not add)
10.DP with segment tree
this kind of DP is almost always deduced as we only need to know segment trees so solve this.
some times we only need to find minimum or maximum in range to solve them .
a good example would be Flowers
in this problem we must chose an increasing sub sequence of heights such that the sum of values as maximum
we can solve it for each index from $$$1$$$ to $$$n$$$ get the maximum answer among all heights that are less than $$$h(i)$$$ and add the value of the current flower to it and then update this value in the segment tree.
the solution is left to the reader :).
11. DP with matrices
if you do know matrices you really should not be reading this as all matrices problems are DP in fact, but you might have fun solving Walk
Note that i might make a tutorial about matrices some day! stay tuned :).
12.digit DP
this is one of the most annoying because if you made a mistake debugging is impossible.
problems of digit DP give you two numbers a,b such that their values are really huge (up to $$$10^{100000}$$$) and ask you to count how many numbers between them satisfies something.
in the problem Digit Sum.
this is a not really easy problem , and the state is more annoying
so our solution is:
and i think that's it as much as i would like to solve more , but i think i will leave it to you to view more and more dp problems , thank you for reading :).









