710026. Largest rectangular sub-matrix whose sum is 0
Largest rectangular sub-matrix whose sum is 0
Slug: largest-rectangular-sub-matrix-whose-sum-is-0
Difficulty: Hard
Topic Tags: prefix-sum, Hash, Dynamic Programming, Matrix, Data Structures, Algorithms
Summary
Given a two-dimensional matrix of integers, find the largest rectangular sub-matrix whose sum is 0. The problem can be viewed as finding all possible sub-matrices and then calculating their sums. Then, it's necessary to find the maximum size of those sub-matrices that have a sum equal to 0.
Detailed Explanation
To solve this problem, we will use a prefix sum technique along with dynamic programming. The idea is to first calculate the prefix sum for each row in the matrix. This means that for each cell matrix[i][j], we will store the sum of all elements above it (both on the left and right sides). Then, for each sub-matrix, we can calculate its sum by subtracting the prefix sum of the top-right corner from the prefix sum of the bottom-left corner. This way, we effectively eliminate the need to calculate the sum of each sub-matrix separately.
Here is a step-by-step breakdown of the solution:
- Initialize an array
prefix_sumwith the same number of rows as the input matrix. - Iterate over each row in the input matrix and for each cell, update the corresponding prefix sum accordingly.
- Initialize variables to store the maximum size of the sub-matrix found so far and its top-left corner.
- Iterate over each possible sub-matrix (with varying sizes) starting from the top-left corner of the input matrix.
- For each sub-matrix, calculate its sum using the prefix sum technique described above.
- If the calculated sum is equal to 0 and the size of the sub-matrix is greater than the maximum size found so far, update the maximum size and the top-left corner of the sub-matrix.
Time complexity: O(m * n), where m is the number of rows and n is the number of columns in the input matrix. This is because we are iterating over each cell in the matrix once. Space complexity: O(m), as we need to store the prefix sum array for each row.
Optimized Solutions
Java
public class Solution {
public int largestRectangle(int[][] matrix) {
int rows = matrix.length;
if (rows == 0) return 0;
int cols = matrix[0].length;
if (cols == 0) return 0;
// Calculate prefix sum for each row
int[] prefixSum = new int[cols + 1];
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int j = 0; j < cols; j++) {
sum += matrix[i][j];
prefixSum[j + 1] = sum;
}
}
// Find the maximum size of rectangular sub-matrix
int maxArea = 0, topRow = 0;
for (int i = 0; i < rows; i++) {
int[] rowPrefixSum = new int[cols + 1];
for (int j = i; j < rows; j++) {
// Calculate sum of the current sub-matrix
int subMatrixSum = prefixSum[cols] - (j == i ? 0 : prefixSum[0]);
for (int k = 0; k <= cols; k++) {
rowPrefixSum[k] = (k == 0 ? 0 : rowPrefixSum[k - 1]) + (prefixSum[k] - rowPrefixSum[k - 1]);
}
for (int j = 0; j < cols; j++) {
// Calculate sum of the current sub-matrix
int left = prefixSum[j];
int right = rowPrefixSum[cols] - rowPrefixSum[j];
if (left + right == 0) {
int area = (j + 1) * (rows - i);
if (area > maxArea) {
maxArea = area;
topRow = i;
}
}
}
}
}
return maxArea;
}
}