From 35df6818e96a1bd2db2a7aca00224ff39e1b8f13 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 21 Aug 2025 21:28:19 -0600 Subject: 3195. find the minimum area to cover all ones 1 --- find_min_area_conver_ones1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 find_min_area_conver_ones1.py (limited to 'find_min_area_conver_ones1.py') 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]])) -- cgit v1.2.3