Sort 125 Discussions, By:
Please Login in order to post a comment
The wording of the problem is not quite precise.
What he means is when partitioning arrays the order of elements stays the same. Man, I was solving a totally different thing...
DP isn't needed at all here. Wasted a lot of time performing a DP approach with a table before realising it's pointless.
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.
I am not sure about the first claim of the editorial. Why can we only consider the first valid split?
HINT for timeouters: Whatch out for the all zeroes case
The best solution (with O(nlogn)) has nothing to do with dynamic programming.
I am getting segmentation fault for test case 7-10.
i) if the array has no positive elements:
for an array of N zeroes, always taking the first valid split yields a score of N-1, which is the best possible
ii) otherwise the array has some positive elements.
claim: any two such arrays differing only by leading/trailing zeroes have the same score. eg score(00110) = score(11) , score(0012003400) = score(120034), etc.
proof: (use induction on length of array) given arrays A and B that differ only by leading/trailing zeroes. any valid splits break them into strictly smaller arrays L(A),R(A) and L(B),R(B). we can show that L(A) and L(B) can now differ only by leading/trailing zeroes (*). so their scores are the same, by induction hypothesis. likewise for R(A), R(B). it follows that score(A) = score(B). QED
from (*) any two splits of an array A that is not all zeroes, differ only in leading/trailing zeroes, and therefore their score is the same, by Claim.
so to sum up, in case (i) the first split is the best, and in case (ii) it doesnt matter which split we choose anyway.
No DP needed for O(nlogn) solution . Simple divide and conquer gives this complexity as well.
There is really an issue here which has already been brought up in the comments in various forms. The word 'partition' is not used in the mathematical sense in the problem statement. I was trying to run over all subsets of the given array giving me the correct sum, which gave me a timeout almost all the time. Finally the comments brought me onto the right track.
Maybe ask for a splitting of the array into two contigous subarrays instead?
I can not see the overlap of subproblems, so this is not necessarily DP. I had best results with straightforward top-down iterative approach.
All test cases pass (Python integer type is autoexpanding), but I woder if there is an approach to eliminate the need for accumulation of huge sums (without hurting complexity...)