Largest Submatrix With Rearrangements - Problem

Imagine you have a binary matrix (containing only 0s and 1s) and you're tasked with finding the largest rectangular area filled entirely with 1s. The twist? You can rearrange the columns in any order to maximize this area!

Given a binary matrix of size m ร— n, you need to:

  • Rearrange columns optimally
  • Find the largest submatrix where every element is 1
  • Return the area of this largest submatrix

This problem combines matrix manipulation with optimization - you'll need to think about how column reordering can help create larger contiguous blocks of 1s.

Input & Output

example_1.py โ€” Basic Rectangle Formation
$ Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
โ€บ Output: 4
๐Ÿ’ก Note: Rearrange columns to [1,2,0] to get [[1,1,0],[1,1,1],[1,1,1]]. The largest submatrix of 1s has area 4 (2ร—2 rectangle in bottom-right).
example_2.py โ€” Optimal Column Ordering
$ Input: matrix = [[1,0,1,0,1]]
โ€บ Output: 3
๐Ÿ’ก Note: Rearrange columns to group all 1s together: [1,1,1,0,0]. The largest submatrix has area 3 (1ร—3 rectangle).
example_3.py โ€” All Zeros Edge Case
$ Input: matrix = [[0,0],[0,0]]
โ€บ Output: 0
๐Ÿ’ก Note: No matter how we rearrange columns, there are no 1s to form a submatrix. The maximum area is 0.

Visualization

Tap to expand
Building Blocks Rearrangement VisualizationOriginal Towers (Columns)Tower AHeight: 3Tower BHeight: 1Tower CHeight: 3Tower DHeight: 1RearrangeOptimally Arranged TowersTower AHeight: 3Tower CHeight: 3Tower DHeight: 1Tower BHeight: 1Max RectangleArea = 2ร—1 = 2Step-by-Step Process:1. Analyze Each Level: For row i, calculate height of consecutive blocks ending at that row2. Sort by Height: Arrange towers in descending order of their heights for current level3. Calculate Areas: For position j, rectangle width = j+1, height = towers[j].height4. Find Maximum: Track the largest area across all levels and positionsExample Calculation for Bottom Row:Heights: [3, 1, 3, 1] โ†’ After sorting: [3, 3, 1, 1]Position 0: width=1, height=3 โ†’ area = 1ร—3 = 3Position 1: width=2, height=3 โ†’ area = 2ร—3 = 6 โœ“ (maximum)Position 2: width=3, height=1 โ†’ area = 3ร—1 = 3๐ŸŽฏ Key Insight: Greedy sorting finds optimal arrangement without trying all permutations!Time Complexity: O(mn log n) vs O(n!) brute force
Understanding the Visualization
1
Identify Block Towers
Each column represents a tower where 1s are solid blocks and 0s create gaps
2
Calculate Tower Heights
For each level, measure how many consecutive blocks each tower contributes
3
Arrange Optimally
Sort towers by height to create the largest possible rectangular foundation
4
Find Maximum Area
Calculate the area of the largest rectangle that can be built
Key Takeaway
๐ŸŽฏ Key Insight: By treating the problem as a histogram rectangle optimization and using greedy sorting, we can find the optimal column arrangement in O(mn log n) time instead of the exponential time required by brute force approaches.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— mยฒn)

n! permutations, each requiring O(mยฒn) to find largest rectangle

n
2n
โš  Quadratic Growth
Space Complexity
O(mn)

Space for storing matrix copies and rectangle calculation

n
2n
โšก Linearithmic Space

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 โ‰ค m, n โ‰ค 105
  • m ร— n โ‰ค 105
  • matrix[i][j] is either 0 or 1
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
68.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen