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。

相关推荐
小王努力学编程2 小时前
【算法与数据结构】单调队列
数据结构·c++·学习·算法·leetcode
夏末秋也凉5 小时前
力扣-贪心-53 最大子数组和
数据结构·算法·leetcode
leeyayai_xixihah11 小时前
2.21力扣-回溯组合
算法·leetcode·职场和发展
01_11 小时前
力扣hot100——相交,回文链表
算法·leetcode·链表·双指针
萌の鱼11 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
AllowM11 小时前
【LeetCode Hot100】除自身以外数组的乘积|左右乘积列表,Java实现!图解+代码,小白也能秒懂!
java·算法·leetcode
记得早睡~14 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
qy发大财14 小时前
跳跃游戏(力扣55)
算法·leetcode
郑州吴彦祖77218 小时前
数据结构——二叉树经典习题讲解
java·数据结构·算法·leetcode
qy发大财18 小时前
跳跃游戏II(力扣45)
算法·leetcode