- [Leetcode 3195. Find the Minimum Area to Cover All Ones I](#Leetcode 3195. Find the Minimum Area to Cover All Ones I)
- [1. 解题思路](#1. 解题思路)
- [2. 代码实现](#2. 代码实现)
1. 解题思路
这一题还是挺简单的,只要找到所有1所在的元素的上下左右4个边界,作为目标矩形的四个边即可。
2. 代码实现
给出python代码实现如下:
python
class Solution:
def minimumArea(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
lb, rb = m, -1
ub, db = n, -1
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
lb = min(lb, j)
rb = max(rb, j)
ub = min(ub, i)
db = max(db, i)
return (rb-lb+1) * (db-ub+1)
提交代码评测得到:耗时2844ms,占用内存46.2MB。