Difficulty: Hard
You are given a 2D binary array grid
. You need to find three non-overlapping rectangles (axis-aligned, non-zero area) such that all the 1
s in grid
lie inside these rectangles.
Return the minimum possible sum of the areas of these rectangles.
Note: Rectangles are allowed to touch (share borders or corners), but they must not overlap in area.
Input:
grid = [[1,0,1],
[1,1,1]]
Output:
5
Explanation:
1
s at (0,0)
and (1,0)
are covered by a rectangle of area 2
.1
s at (0,2)
and (1,2)
are covered by a rectangle of area 2
.1
at (1,1)
is covered by a rectangle of area 1
.2 + 2 + 1 = 5
.Input:
grid = [[1,0,1,0],
[0,1,0,1]]
Output:
5
Explanation:
1
s at (0,0)
and (0,2)
are covered by a rectangle of area 3
.1
at (1,1)
is covered by a rectangle of area 1
.1
at (1,3)
is covered by a rectangle of area 1
.3 + 1 + 1 = 5
.1 <= grid.length, grid[i].length <= 30
grid[i][j]
is either 0
or 1
.1
s in grid
.https://drawtocode.vercel.app/problems/find-the-minimum-area-to-cover-all-ones-ii
Loading component...
Loading component...