diff options
author | Jack Sangdahl <[email protected]> | 2025-08-21 21:28:19 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-08-21 21:28:19 -0600 |
commit | 35df6818e96a1bd2db2a7aca00224ff39e1b8f13 (patch) | |
tree | dc0ad4bebb16120625cfb5840d15c23367063743 /find_min_area_conver_ones1.py | |
parent | 4d218d1ca8008396494b307b54560167f18ae0f6 (diff) | |
download | leetcode-35df6818e96a1bd2db2a7aca00224ff39e1b8f13.tar.gz leetcode-35df6818e96a1bd2db2a7aca00224ff39e1b8f13.zip |
3195. find the minimum area to cover all ones 1
Diffstat (limited to 'find_min_area_conver_ones1.py')
-rw-r--r-- | find_min_area_conver_ones1.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/find_min_area_conver_ones1.py b/find_min_area_conver_ones1.py new file mode 100644 index 0000000..9374874 --- /dev/null +++ b/find_min_area_conver_ones1.py @@ -0,0 +1,32 @@ +# 3195. Find the Minimum Area to Cover All Ones I + +""" +you are given a 2d binary array 'grid'. find a rectangle with horizontal and +vertical lines with the smallest area, such that all the 1's in 'grid' lie +inside this rectangle. return the minimum possible area of the rectangle. +""" + + +class Solution: + def minimumArea(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + m, n = len(grid), len(grid[0]) + min_row, max_row = m, -1 + min_col, max_col = n, -1 + for i in range(m): + for j in range(n): + if grid[i][j] == 1: + min_row = min(min_row, i) + max_row = max(max_row, i) + min_col = min(min_col, j) + max_col = max(max_col, j) + return (max_row - min_row + 1) * (max_col - min_col + 1) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumArea(grid=[[0, 1, 0], [1, 0, 1]])) + print(obj.minimumArea(grid=[[1, 0], [0, 0]])) |