Find the Minimum Area to Cover All Ones II

You are given a 2D binary array grid. You need to find 3 non-overlapping rectangles having non-zero areas with horizontal and vertical sides such that all the 1's in grid lie inside these rectangles.

Return the minimum possible sum of the area of these rectangles.

Note that the rectangles are allowed to touch.

Example 1
1 0 1
1 1 1
Inputgrid = [[1,0,1],[1,1,1]]
Output5
The 1's can be covered by rectangles of areas 2, 2, and 1, for a minimum total area of 5.
Example 2
1 0 1 0
0 1 0 1
Inputgrid = [[1,0,1,0],[0,1,0,1]]
Output5
The 1's can be covered by rectangles of areas 3, 1, and 1, for a minimum total area of 5.

Constraints

  • 1 <= grid.length, grid[i].length <= 30
  • grid[i][j] is either 0 or 1.
  • The input is generated such that there are at least three 1's in grid.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate