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。

相关推荐
想吃火锅10053 天前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展
凌波粒3 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
退休倒计时3 天前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript
小欣加油3 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒3 天前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
凌波粒3 天前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode
吃着火锅x唱着歌3 天前
LeetCode 2530.执行K次操作后的最大分数
数据结构·算法·leetcode
sheeta19983 天前
LeetCode 每日一题笔记 日期:2026.06.16 题目:3612. 字符串特殊符号处理
笔记·算法·leetcode
CoderYanger3 天前
A.每日一题:2095. 删除链表的中间节点
java·数据结构·程序人生·leetcode·链表·面试·职场和发展
青山木3 天前
Hot 100 --- 矩阵置零
线性代数·算法·leetcode·矩阵·哈希算法