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。

相关推荐
旖旎夜光5 小时前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊5 小时前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt5 小时前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊6 小时前
LeetCode1341~1360题目解析
java·leetcode
海绵宝宝转agent6 小时前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
Pniubi8 小时前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展
j7~1 天前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
Shaoshing1 天前
LeetCode100 42.接雨水
java·算法·leetcode
文丑颜不良啊1 天前
LeetCode1261~1280题目解析
java·leetcode
Shaoshing1 天前
LeetCode100 11. 盛最多水的容器
算法·leetcode