Problem · Array
Maximum Sum Rectangle
Learn this problemProblem statement
You are given a non-empty integer matrix matrix. A rectangle is any non-empty contiguous range of rows combined with any non-empty contiguous range of columns.
Return the maximum possible sum of all values inside one such rectangle.
Function
maximumSumRectangle(matrix: int[][]) → longExamples
Example 1
matrix = [[1,2,-1,-4,-20],[-8,-3,4,2,1],[3,8,10,1,3],[-4,-1,1,7,-6]]return = 29The rectangle spanning rows 1 through 3 and columns 1 through 3 has sum 29, which is optimal.
Example 2
matrix = [[-3,-4],[-1,-2]]return = -1Every rectangle has a negative sum. The best non-empty rectangle contains only -1.
Example 3
matrix = [[2,-1,2,3,-9]]return = 6In the single row, the contiguous range [2,-1,2,3] sums to 6.
Constraints
1 <= matrix.length <= 751 <= matrix[i].length <= 75- Every row has the same length.
-100000 <= matrix[i][j] <= 100000