Maximum Sum Rectangle in Image
Problem Statement
In image processing, finding regions of interest often involves locating rectangular areas with maximum pixel intensity sum.
Given a 2D matrix representing pixel intensities (can be negative for edge-detected images), find the maximum sum of any rectangular sub-region.
This is useful for:
- Detecting bright regions in astronomical images
- Finding high-contrast areas after edge detection
- Locating regions of interest in thermal imaging
Constraints
- 1ā¤rows,colsā¤100
- ā1000ā¤matrix[i][j]ā¤1000
Approach
Use Kadane's algorithm extended to 2D by fixing left and right columns, then computing max sum subarray for each row range.
Example:
matrix = [[1, -2, 3], [-4, 5, -6], [7, -8, 9]]
9
The maximum sum rectangle is just the single cell with value 9.
1. Background Knowledge
The Maximum Sum Rectangle problem extends the classic 1D Maximum Subarray Sum problem to 2D matrices with possibly negative values. In 1D, Kadane's algorithm finds the contiguous subarray with maximum sum in O(n) time by tracking the maximum suffix sum while scanning.
In 2D, we seek a sub-rectangle (i1āā¤iā¤i2ā,j1āā¤jā¤j2ā) maximizing āi=i1āi2āā\sum_{j=j_1}^{j_2}matrix[i][j]. Negative values require checking the empty rectangle (sum 0), but constraints imply non-empty solutions are feasible. This is NP-hard in general geometric settings, but the fixed-size grid enables dynamic programming.
Prerequisites:
- Prefix sums for O(1) range queries: prefix[i+1][j+1]=matrix[i][j]+prefix[i][j+1]+prefix[i+1][j]āprefix[i][j]
- Understanding Kadane's algorithm as a 1D DP: maxeāndinghāere[i]=max(matrix[i],maxeāndinghāere[iā1]+matrix[i])
2. Algorithm Approach
Standard O(N³) Solution: Fix left/right column pairs (l,r), create a 1D "height" array where temp[i]= sum of row i from columns l to r, then apply Kadane's on temp to find best row range. Repeat for all O(N2) column pairs.
Alternatives:
- "Sliced" subwindow search for sublinear approximations on natural images
- Branch-and-bound or geometric methods for sparse/special cases
For constraints Nā¤100, O(N³) = 10ā¶ operations is optimal.
3. Step-by-Step Strategy
- Precompute prefix sums (optional, but using temp sums directly is equivalent):
for l = 0 to cols-1
initialize temp[rows] = {0}
for r = l to cols-1
for i = 0 to rows-1
temp[i] += matrix[i][r]
max_rectangle = max(max_rectangle, kadane(temp))
- Kadane's 1D on temp[] (handles negatives correctly):
int kadane(vector<int>& arr) {
int max_so_far = INT_MIN, max_ending_here = 0;
for (int x : arr) {
max_ending_here = max(x, max_ending_here + x);
max_so_far = max(max_so_far, max_ending_here);
}
return max_so_far;
}
Initialize max_so_far = 0 if empty rectangle allowed.
-
Track global maximum across all (l,r) pairs.
-
Edge cases: All-negative matrix (return max element), 1x1 matrix, empty sub-rectangle.
4. Common Pitfalls
- Forgetting negatives in Kadane: Must reset max_ending_here = 0 (or max element) when prefix sum drops below 0.
- Off-by-one indexing: Column loops l=0 to cols-1, r=l to cols-1; row sums correct.
- Empty rectangle: If all negative, max sum is largest single element (not 0 unless specified).
- Integer overflow: With 100Ć100Ć1000=107, use long long.
- 1D Kadane on non-contiguous: temp[i] must represent contiguous row sums.
- Symmetry optimization: No need (N=100), but row-major vs column-major irrelevant.
5. Time & Space Complexity
Time: O(rowsĆcols2)=O(N3). Outer loops: O(cols2), inner temp update + Kadane: O(rows).
Space: O(rows) for temp array (or O(rowsĆcols) for prefix sums). Optimal.
| N | Time (ops) | Acceptable? |
|---|---|---|
| 100 | 10ā¶ | Yes |
| 400 | 6.4Ć10ā· | Tight |
Proof sketch: cols2 pairs Ć rows for sum + rows for Kadane = O(N3). No better exact algorithm known for dense matrices with negatives.