Find the Minimum Area to Cover All Ones II - Problem

You're given a 2D binary grid containing 0s and 1s. Your mission is to find exactly 3 non-overlapping rectangles that together cover all the 1s in the grid with the minimum total area.

Think of this as placing three rectangular stickers on a grid to cover all the marked spots (1s) while using as little sticker material as possible. The rectangles:

  • Must have non-zero areas (can't be empty)
  • Cannot overlap each other
  • Are allowed to touch at edges
  • Must have horizontal and vertical sides (axis-aligned)

Goal: Return the minimum possible sum of areas of these three rectangles.

Input & Output

example_1.py — Basic Grid
$ Input: grid = [[1,0,1],[1,1,1]]
Output: 5
💡 Note: We can place 3 rectangles: (0,0)-(0,0) covering grid[0][0]=1 with area 1, (0,2)-(0,2) covering grid[0][2]=1 with area 1, and (1,0)-(1,2) covering the bottom row with area 3. Total area = 1+1+3 = 5.
example_2.py — Sparse Grid
$ Input: grid = [[1,0,1,0],[0,1,0,1]]
Output: 5
💡 Note: Optimal placement uses 3 rectangles: (0,0)-(0,0) for grid[0][0], (0,2)-(1,3) covering the right portion, and (1,1)-(1,1) for grid[1][1]. This gives areas 1+4+1 = 6. Better solution: use vertical strips.
example_3.py — Clustered Pattern
$ Input: grid = [[1,1],[1,1]]
Output: 4
💡 Note: All 1s form a 2×2 cluster. We need exactly 3 rectangles with non-zero areas. Minimum is to use three 1×1 rectangles covering 3 cells (area=3) plus one 1×1 rectangle covering the remaining cell (area=1), but we need exactly 3. Optimal: split into regions giving total area 4.

Constraints

  • 1 ≤ grid.length ≤ 30
  • 1 ≤ grid[0].length ≤ 30
  • grid[i][j] is either 0 or 1
  • At least 1 cell contains 1
  • The grid will always have a valid solution

Visualization

Tap to expand
Minimum Area to Cover All Ones II INPUT 2D Binary Grid (2x3) 1 0 1 1 1 1 [0,0] [0,1] [0,2] grid = [ [1, 0, 1], [1, 1, 1] ] Total 1s: 5 | Grid: 2x3 Need: 3 rectangles Goal: Min total area ALGORITHM STEPS 1 Partition Grid Split into 3 regions (horizontal/vertical cuts) 2 Find Min Bounding Box For each region's 1s 3 Try All Partitions 6 ways to split grid 4 Take Minimum Sum of 3 rect areas Optimal Partition: R1 R2 R3 R1:2 + R2:1 + R3:2 = 5 FINAL RESULT Optimal Coverage 1 1 1 1 1 0 Area Calculation Rect 1: 1x2 = 2 Rect 2: 1x1 = 1 Rect 3: 1x2 = 2 OUTPUT 5 [OK] All 1s covered Key Insight: The grid can be partitioned into 3 non-overlapping regions in 6 different ways: 3 horizontal splits (H-H-H, H-VV, VV-H) and 3 vertical splits (V-V-V, V-HH, HH-V). For each partition, find the minimum bounding rectangle for 1s in each region, then sum areas. TutorialsPoint - Find the Minimum Area to Cover All Ones II | Optimal Solution
Asked in
Google 24 Amazon 18 Meta 15 Microsoft 12
24.3K Views
Medium Frequency
~25 min Avg. Time
856 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