101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Histogram Approach - Complex approach
  2. Dynamic Programming Approach - Complex approach

Approach 1: Histogram Approach

The key insight for solving this problem efficiently is to transform it into a series of "largest rectangle in histogram" problems. For each row in the matrix, we can calculate the height of the histogram at each position (how many consecutive 1's are above the current position, including the current position).

Once we have the heights for a row, we can use a stack-based algorithm to find the largest rectangle in that histogram. We repeat this process for each row and keep track of the maximum area found.

Algorithm:

  1. For each row in the matrix:
  2. Calculate the height of the histogram at each position (number of consecutive 1's above, including current position).
  3. If the current cell is '0', the height is reset to 0.
  4. If the current cell is '1', the height is incremented by 1 from the height in the previous row.
  5. Use a stack-based algorithm to find the largest rectangle in the histogram for the current row.
  6. Keep track of the maximum area found across all rows.

Time Complexity:

O(m × n)

Where m is the number of rows and n is the number of columns in the matrix. We process each cell once to build the histograms, and the stack-based algorithm for finding the largest rectangle in a histogram is O(n) for each row.

Space Complexity:

O(n)

We need O(n) space to store the heights for the current row and O(n) space for the stack used in the histogram algorithm.

Approach 2: Dynamic Programming Approach

We can also solve this problem using dynamic programming by maintaining three arrays for each position: height (same as in the histogram approach), left (the leftmost position where the rectangle can extend), and right (the rightmost position where the rectangle can extend).

For each position, we calculate these values and then compute the area of the largest rectangle that can be formed with the current position as the bottom-most row.

Algorithm:

  1. Initialize three arrays: height, left, and right, each of size n (number of columns).
  2. For each row in the matrix:
  3. Update the height array: If the current cell is '1', increment the height; otherwise, reset it to 0.
  4. Update the left array: Find the leftmost position where the rectangle can extend horizontally.
  5. Update the right array: Find the rightmost position where the rectangle can extend horizontally.
  6. Calculate the area for each position: (right[j] - left[j]) * height[j].
  7. Keep track of the maximum area found.

Time Complexity:

O(m × n)

Where m is the number of rows and n is the number of columns in the matrix. We process each cell once and perform constant-time operations for each cell.

Space Complexity:

O(n)

We need three arrays of size n to store the height, left, and right values for the current row.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function maximalRectangle(matrix):
if matrix is empty:
return 0
m = number of rows in matrix
n = number of columns in matrix
heights = array of size n, initialized with 0s
maxArea = 0
for i from 0 to m-1:
// Update heights for current row
for j from 0 to n-1:
if matrix[i][j] == '1':
heights[j] += 1
else:
heights[j] = 0
// Find largest rectangle in current histogram
maxArea = max(maxArea, largestRectangleInHistogram(heights))
return maxArea
function largestRectangleInHistogram(heights):
stack = empty stack
maxArea = 0
i = 0
while i < length of heights:
// If stack is empty or current height is >= height at stack top, push to stack
if stack is empty or heights[i] >= heights[stack.top()]:
stack.push(i)
i++
else:
// Pop the stack and calculate area with the popped height as the limiting height
top = stack.pop()
// Calculate width based on stack
width = i if stack is empty else i - stack.top() - 1
// Update max area
maxArea = max(maxArea, heights[top] * width)
// Process remaining elements in stack
while stack is not empty:
top = stack.pop()
width = i if stack is empty else i - stack.top() - 1
maxArea = max(maxArea, heights[top] * width)
return maxArea
ProblemSolutionCode
101 Logo
onenoughtone