Find the Minimum Area to Cover All Ones I - Problem

You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1's in grid lie inside this rectangle.

Return the minimum possible area of the rectangle.

Input & Output

Example 1 — Basic Case
$ Input: grid = [[0,1,0],[1,1,0]]
Output: 2
💡 Note: The 1's are at positions (0,1), (1,0), and (1,1). The minimum rectangle covers rows 0-1 and column 1, giving area = 2 * 1 = 2
Example 2 — Single Cell
$ Input: grid = [[1]]
Output: 1
💡 Note: Only one 1 at position (0,0), so minimum rectangle is 1x1 with area = 1
Example 3 — Scattered 1's
$ Input: grid = [[1,0,0],[0,0,1]]
Output: 6
💡 Note: 1's at (0,0) and (1,2). Rectangle must cover rows 0-1 and columns 0-2, giving area = 2 * 3 = 6

Constraints

  • 1 ≤ grid.length, grid[i].length ≤ 100
  • grid[i][j] is 0 or 1

Visualization

Tap to expand
Find Minimum Area to Cover All Ones010110Input GridBounding RectangleHeight = 2Width = 1Area = 2 × 1 = 2Find extreme coordinates of all 1s to determine minimum rectangleOutput: 2
Understanding the Visualization
1
Input Grid
Binary grid with 0's and 1's scattered
2
Find Boundaries
Identify extreme positions of all 1's
3
Calculate Area
Rectangle area = height × width
Key Takeaway
🎯 Key Insight: Only need the topmost, bottommost, leftmost, and rightmost 1's to define the minimum rectangle
Asked in
Google 25 Amazon 20 Microsoft 15
23.1K Views
Medium Frequency
~15 min Avg. Time
890 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