Leetcode 3195. Find the Minimum Area to Cover All Ones I

  • [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。

相关推荐
亓才孓1 小时前
[leetcode]树的操作
算法·leetcode·职场和发展
loser~曹2 小时前
基于快速排序解决 leetcode hot215 查找数组中第k大的数字
数据结构·算法·leetcode
Dream it possible!2 小时前
LeetCode 热题 100_打家劫舍(83_198_中等_C++)(动态规划)
c++·算法·leetcode·动态规划
SylviaW083 小时前
python-leetcode 62.搜索插入位置
数据结构·算法·leetcode
Joe_Wang55 小时前
[图论]拓扑排序
数据结构·c++·算法·leetcode·图论·拓扑排序
梭七y5 小时前
【力扣hot100题】(033)合并K个升序链表
算法·leetcode·链表
月亮被咬碎成星星5 小时前
LeetCode[383]赎金信
算法·leetcode
trust Tomorrow8 小时前
每日一题-力扣-2278. 字母在字符串中的百分比 0331
算法·leetcode
梭七y12 小时前
【力扣hot100题】(022)反转链表
算法·leetcode·链表
LuckyLay19 小时前
LeetCode算法题(Go语言实现)_22
算法·leetcode·golang