求职网站,北京网站优化公司哪家好,wordpress所有外链本地化,排名网Leetcode 3195. Find the Minimum Area to Cover All Ones I 1. 解题思路2. 代码实现 题目链接#xff1a;3195. Find the Minimum Area to Cover All Ones I
1. 解题思路
这一题还是挺简单的#xff0c;只要找到所有1所在的元素的上下左右4个边界#xff0c;作为目标矩形…Leetcode 3195. Find the Minimum Area to Cover All Ones I 1. 解题思路2. 代码实现 题目链接3195. Find the Minimum Area to Cover All Ones I
1. 解题思路
这一题还是挺简单的只要找到所有1所在的元素的上下左右4个边界作为目标矩形的四个边即可。
2. 代码实现
给出python代码实现如下
class Solution:def minimumArea(self, grid: List[List[int]]) - int:n, m len(grid), len(grid[0])lb, rb m, -1ub, db n, -1for 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-lb1) * (db-ub1)提交代码评测得到耗时2844ms占用内存46.2MB。