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。

相关推荐
All for pursuit.2 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
圣保罗的大教堂2 小时前
leetcode 1477. 找两个和为目标值且不重叠的子数组 中等
leetcode
hanlin033 小时前
刷题笔记:力扣第144题-二叉树的前序遍历
笔记·算法·leetcode
圣保罗的大教堂5 小时前
leetcode 3629. 通过质数传送到达终点的最少跳跃次数 中等
leetcode
圣保罗的大教堂5 小时前
leetcode 1914. 循环轮转矩阵 中等
leetcode
wabs6666 小时前
关于二叉树【力扣100.相同的树的思考】
数据结构·c++·算法·leetcode·二叉树·递归法
alphaTao7 小时前
LeetCode 每日一题 2026/9/14-2026/9/20
算法·leetcode
hanlin037 小时前
刷题笔记:力扣第560题-和为k的子数组
笔记·算法·leetcode
Navigator_Z19 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
语戚21 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp