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。

相关推荐
漫随流水3 小时前
leetcode算法(二叉树的层序遍历Ⅱ)
数据结构·算法·leetcode·二叉树
源代码•宸3 小时前
Leetcode—166. 加一【简单】new(big.Int)法
经验分享·算法·leetcode·职场和发展·golang·new.bigint
老鼠只爱大米3 小时前
LeetCode算法题详解 128:最长连续序列
算法·leetcode·面试题·并查集·哈希集合·最长连续序列
Jeremy爱编码4 小时前
电话号码的字母组合
java·算法·leetcode
YuTaoShao4 小时前
【LeetCode 每日一题】1339. 分裂二叉树的最大乘积
算法·leetcode·职场和发展
leoufung4 小时前
LeetCode 172. Factorial Trailing Zeroes 题解
算法·leetcode·职场和发展
梭七y4 小时前
【力扣hot100题】(131)排序链表
算法·leetcode·链表
想进个大厂4 小时前
代码随想录day6哈希表
算法·leetcode·散列表
圣保罗的大教堂4 小时前
leetcode 1339. 分裂二叉树的最大乘积 中等
leetcode
im_AMBER4 小时前
Leetcode 97 移除链表元素
c++·笔记·学习·算法·leetcode·链表