We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
DP isn't needed at all here. Wasted a lot of time performing a DP approach with a table before realising it's pointless.
Why?
As we only ever take the first possible cut and thus only ever have one choice of cut we will never re-use our solutions (which is the whole point of DP) thus making a DP approach pointless.
Let k be the location of our cut such that we have two arrays, a[i]..a[k] and a[k+1]..a[j].
Let opt(i,j) denote our solution for an array running from i to j
Our solution is given as opt(i,j) = max(opt(i,k),opt(k+1,j))
It should be clear now that because k takes on only one value for a particular i and j, and i <= k < j, that we never get a situation where we hit opt(i,j) more than once and we get a simple binary tree struture for our recursion.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nikita and the Game
You are viewing a single comment's thread. Return to all comments →
Hint:
DP isn't needed at all here. Wasted a lot of time performing a DP approach with a table before realising it's pointless.
Why?
As we only ever take the first possible cut and thus only ever have one choice of cut we will never re-use our solutions (which is the whole point of DP) thus making a DP approach pointless.
Let k be the location of our cut such that we have two arrays, a[i]..a[k] and a[k+1]..a[j].
Let opt(i,j) denote our solution for an array running from i to j
Our solution is given as opt(i,j) = max(opt(i,k),opt(k+1,j))
It should be clear now that because k takes on only one value for a particular i and j, and i <= k < j, that we never get a situation where we hit opt(i,j) more than once and we get a simple binary tree struture for our recursion.