OpenJudge 1768: Kadane's Algorithm on 2D Arrays
OpenJudge 1768: Kadane's Algorithm on 2-D Arrays Simple Version Given a 2-D matrix of integers, find the sub-matrix of this whose sum is maximized, and find that sum. The size of the matrix is no larger than 100 by 100. Analysis O(N^4) - Works, but naive That's true, an naive O(N^4) version would work. We simply need to calculate (two dimensional!) prefix sums first. A quick review to what 2-D prefix sums are: The 2-D prefix sum of two coordinates, X and Y, is the sum of all elements that have X-coordinate less than or equal to X, and Y coordinate less than or equal to Y. It can be calculated through tabulation by removing overlapping areas. Let P be the prefix sum array and V be the array of values, so the formula is as follows: P[x, y] = V[x, y] - P[x-1, y] - P[x, y-1] + P[x-1, y-1]. We need to add the P[x-1, y-1] at the end since subtracting P[x-1, y] and P[x, y-1]...