PLease if anyone could explain me the DP approach of the question Chef and Brackets as I am not able to understand the solution from the editorials.
Please explain so that a beginner like me could understand.
PLease if anyone could explain me the DP approach of the question Chef and Brackets as I am not able to understand the solution from the editorials.
Please explain so that a beginner like me could understand.
In that problems, I solved with back-tracking, with same complexity : O(n * 2^n * k), which is easier to understand and install
Ok I’ll try to explain it in a really simple way.
The first step of solving dynamic programming problems is that you start thinking in a recursive manner. Hows that done you may ask !.
arr[0,N-1] Or arr[i,j] if you like to generalize
S[0,N-1]
Now this newly added 4 will form a bracket with this ‘-4’. So total brackets in this new array (arr[0,N]) is solutions for old one + 1.
S[0,N-1] + 1
-4 {} 4
S[0,N-1] + 1 + S[k+1, N-1]
{-A A}{-4 4} OR
{-A A}{-4 {} 4} . . .
S[0,N-1] + 1 + S[k+1, N-1] + (1+S[k+1,N-1])*S[0,k-1]
S[0,N-1] + (1 + S[k+1,N-1]) * (1 + S[0,K-1])
S[i,j] = S[i,j-1] + (FOR all k such that arr[k] == -arr[j]) (1+S[i,k-1])*(S[k+1,j-1]+1)
Now you have successfully created a recurrence relation. Second step is how to write dynammic programming code for this thing. If you see that to calculate value for a range [i,j] you require values of ranges [i,k-1] and [k+1,j-1] which are definitely smaller ranges when compared to [i,j]. So while writing dp code for this what you have to do is find values of all the small ranges before proceeding to the bigger ranges. Which in lay man terms means->
Here is the link to my solution-Show me the code, talk is cheap
PS: There are some corner case checks in the code. Play around them to understand what they do.
PS2: Code might or might not be perfect, deal with it.
Think this manner: For DP to be applicable we need two things, Optimal substructure and overlapping subproblems. If we talk about optimal substructure, then finding the number of valid brackets in a range say (i,j), we need to find the summation and all permutations(with this i,j) of all valid brackets of the ranges that lie within (i,j). Thus, we can obtain Optimal solution for(i,j) by using optimal solutions to all the subranges. If we talk about overlapping property, lets say, a closing bracket of ith value occurs at some index k<j. We need optimal solution of (k,j) for our calculation. This subproblem may be required for some other (i’,j’) calculation when a[i’]=a[i] and i’<j. Hence, the two conditions satisy and we can now apply DP.
Here is my solution based on the DP explained in editorial. Basically we are pre-computing the number of valid brackets in the range (i+1,j)
. Two possibilities arise here: include the bracket at i’th position or don’t include. Now, for (i,j)
, the number of valid brackets are all that are in the range (i+1,j) + v
where v for the time being is a variable(used when we include the brace at i’th position). When we are not including x, then we are not including the bracket at the i’th position. But if we include, let the bracket at i’th position be the opening brace(no need when a[i]>0 i.e. closing brace) of type x(i.e. -x). Then we need to know where in the range of (i+1,j) we have a closing brace of type x. We include all these combinations i.e. v= m[i+1,k]*m[k+1][j] where k belongs to [i+1,j] and a[i]<0 && a[k] + a[i]=0
.
Thank you so much for such an elaborate explanation…
Thank you so much for the explanation
applause. Such a well thought && entertaining explanation. you should write a book or something.
glad to help
beautifull answer …
S[0,N] + 1 + S[k+1, N-1] Shouldn’t it be S[0,N-1] +1 +S[K+1,N-1]?
This is going to help me in visualising all DP problems in future Thankyou!
@nitinj Can you please elaborate on the corner case checks. I get how the code works, but can’t quite figure out all the if-else checks. Thanks!
hey rick93 check this solution->
http://www.codechef.com/viewplaintext/5615785
I’ve removed extra unnecessary if conditions. The only condition checks for the case where k=i
_/_ nice work bro