|
| 1 | +''' |
| 2 | +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. |
| 3 | + |
| 4 | +Return the minimum possible area of the rectangle. |
| 5 | +''' |
| 6 | + |
| 7 | +class Solution: |
| 8 | + def minimumArea(self, grid: List[List[int]]) -> int: |
| 9 | + |
| 10 | + res = 0 |
| 11 | + |
| 12 | + m = len(grid) |
| 13 | + n = len(grid[0]) |
| 14 | + |
| 15 | + minrow, maxrow = m,-1 |
| 16 | + mincol,maxcol = n,-1 |
| 17 | + |
| 18 | + for i in range(m): |
| 19 | + for j in range(n): |
| 20 | + if grid[i][j] == 1: |
| 21 | + minrow = min(minrow,i) |
| 22 | + maxrow = max(maxrow, i) |
| 23 | + mincol = min(mincol,j) |
| 24 | + maxcol = max(maxcol,j) |
| 25 | + |
| 26 | + return (maxrow-minrow+1)*(maxcol-mincol+1) |
0 commit comments