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。

相关推荐
WaitWaitWait012 小时前
LeetCode每日一题4.20
算法·leetcode
蒟蒻小袁2 小时前
力扣面试150题--有效的括号和简化路径
算法·leetcode·面试
阳洞洞5 小时前
leetcode 二分查找应用
算法·leetcode·二分查找
Gerry_Liang7 小时前
LeetCode热题100——283. 移动零
数据结构·算法·leetcode
Espresso Macchiato8 小时前
Leetcode 3523. Make Array Non-decreasing
leetcode··leetcode medium·leetcode 3523·leetcode周赛446
Aqua Cheng.10 小时前
25.4.22华为--算法真题整理(2025年4月22日)
java·算法·leetcode·华为·面试
良木林11 小时前
240423 leetcode exercises
算法·leetcode
满怀101511 小时前
【LeetCode】7.整数反转
leetcode·题解
记得早睡~14 小时前
leetcode98-验证二叉搜索树
数据结构·算法·leetcode
清羽_ls16 小时前
leetcode-位运算
前端·算法·leetcode·位运算