leetcode 301: Smallest Rectangle Enclosing Black Pixels

Smallest Rectangle Enclosing Black Pixels Description:An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizon 阅读详情

Smallest Rectangle Enclosing Black Pixels

Total Accepted: 575 Total Submissions: 1481 Difficulty: Medium

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]
and x = 0, y = 2,

Return 6.

[CODE]

public class Solution {
    int l,r,u,d;
    
    public int minArea(char[][] image, int x, int y) {
        //input check
        u=x;
        d=x;
        l=y;
        r=y;
        int m = image.length-1;
        int n = image[0].length-1;
        
        boolean[][] visited = new boolean[m+1][n+1];
        
        search(image, visited, x, y, m, n);
        
        return (r-l+1) * (d-u+1);
    }
    
    private void search(char[][] image, boolean[][] visited, int x, int y, int m, int n) {
        if(x<0 || x>m || y<0 || y>n || image[x][y]!='1' || visited[x][y]) return;
        visited[x][y] = true;
        
        if(x<u) u=x;
        else if(x>d)d=x;
        if(y<l) l=y;
        else if(y>r) r=y;
        
        search(image, visited, x-1, y, m, n);
        search(image, visited, x, y-1, m, n);
        search(image, visited, x+1, y, m, n);
        search(image, visited, x, y+1, m, n);
    }
}


相关推荐

iTunes登录协议全解析:从抓包到签名验证(含HTTPDebugger避坑指南)

本文深入解析了iTunes登录协议的核心流程,包括sign-sap-setup-cert和sign-sap-setup-buffer等关键请求,并提供了使用HTTPDebugger等工具进行抓包分析时绕过反调试检测的实战技巧与避坑指南。文章详细拆解了PLIST编解码、请求签名验证机制及密码加密逻辑,为网络安全研究和协议逆向提供了完整的技术路径。

weixin_29224309的博客 347

LeetCode 302. Smallest Rectangle Enclosing Black Pixels(最小面积矩形)

原题网址:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are con

James Pan 1786

Vanna 2.0:基于LLM的智能文本到SQL框架,实现企业级数据查询与权限控制

文本到SQL技术是自然语言处理与数据库交互的重要桥梁,它允许用户使用日常语言直接查询数据库,无需编写复杂的SQL语句。其核心原理是利用大语言模型理解用户意图,并结合检索增强生成技术,将自然语言问题转换为准确的结构化查询语言。这项技术的价值在于大幅降低了数据访问门槛,提升了业务人员的数据自助分析能力,并能在金融分析、销售报表、运营监控等场景中实现实时数据洞察。Vanna 2.0作为一个开源框架,通过引入用户感知的智能体架构和行级安全策略,解决了企业级应用中的多租户数据隔离与权限控制等关键问题,使得自然语言数据

weixin_42629522的博客 556

[leetcode] 302. Smallest Rectangle Enclosing Black Pixels 解题报告

题目链接: https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are co

小榕流光的专栏 1232

FFmpeg视频缩放终极指南:从基础命令到保持宽高比的5种实战技巧

本文是FFmpeg视频缩放的终极指南,详细讲解了从基础命令到保持宽高比的5种实战技巧。针对不同平台(如抖音、B站、Instagram)的分辨率要求,提供了具体的参数模板和批量处理脚本,帮助内容创作者高效、精准地调整视频大小,避免画面变形或黑边问题,实现多平台适配。

juice的博客 470

FOC电机控制中电流采样延迟补偿的原理和方法

摘要: FOC电机控制中,电流采样、计算与PWM更新存在固有延迟(约1.5个PWM周期),导致相位滞后,限制电流环带宽。主流补偿方法包括: 一阶前向预测法:基于历史电流值预测未来电流,抵消延迟,工程应用广泛; 相位超前补偿:通过s/z域校正器设计实现等效预测; 状态观测器法(如龙伯格观测器):通过电机模型闭环估计状态,兼具噪声抑制和参数辨识能力,但复杂度高。实际需注意参数敏感性、离散化精度及延迟时间校准,避免过补偿风险。一阶预测法因平衡效果与复杂度成为工业首选。

jifengzhiling的博客 2261

[leetcode-302]Smallest Rectangle Enclosing Black Pixels

[leetcode-302]Smallest Rectangle Enclosing Black Pixels

100120101的博客 1686

Plecs电力电子仿真专业教程-第一季 补充课程 Plecs的安装教程与软件下载

PLECS4.1.64免安装版本–点我下载 Plecs.Standalone-3.7.2版本--关注加私信给我可以提供下载链接 补充课程 Plecs的安装教程与软件下载 详细介绍Plecs软件的安装过程

1万+

[LeetCode302]Smallest Rectangle Enclosing Black Pixels

Hard ..An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally

MoM 879

AutoSAR配置与实践(基础篇)2.5 RTE对数据一致性的管理

SWC内部不同的Runnable访问共同全局变量,Runnable类似C文件中的函数,这些函数如果被放在不同Task上运行, 可能出现出现同一时刻多个函数共同运行的情况。可以通过为受影响的任务分配相同的优先级,为受影响的任务分配相同的内部操作系统资源,或者将OS的任务配置为非抢占性来实现。数据一致性:当多个操作同时读写同一个数据,由于任务的抢占,出现了数据被篡改的情况,造成非预期的数据结果。SWC内部变量保护,限定局变量的作用域在变量的作用域在SWC的不同runnable间。

nihaoljq2010的博客 2633

最新Python爬虫实战(入门爬虫篇)——案例16:2345天气网城市天气预报数据采集(详细爬虫思路截图+抓包动图演示+完整爬虫代码+详细注释)

本次案例通过 Python 爬虫技术,成功实现了对 2345 天气网城市 15 天天气预报数据的批量爬取与 Excel 保存。代码采用模块化设计,包含网页请求、HTML 解析、数据清洗、Excel 持久化等完整流程;通过配置模拟浏览器的请求头,有效规避基础反爬策略,保证请求的稳定性。整个爬虫逻辑遵循 “网页请求 → HTML 解析 → 提取天气信息 → 保存到 Excel” 的标准流程,与静态网页爬取范式高度一致。

努力让自己发光,对的人才能迎着光而来 10万+

leetcode 187: Repeated DNA Sequences

Total Accepted: 1161 Total Submissions: 6887 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes usef

西施豆腐渣 6035

白话机器学习算法理论+实战番外篇之Xgboost

1. 写在前面 如果想从事数据挖掘或者机器学习的工作,掌握常用的机器学习算法是非常有必要的,在这简单的先捋一捋, 常见的机器学习算法: 监督学习算法:逻辑回归,线性回归,决策树,朴素贝叶斯,K近邻,支持向量机,集成算法Adaboost等 无监督算法:聚类,降维,关联规则, PageRank等 我前面已经尝试用最白话的语言完成了一个白话机器学习算法理论+实战系列,这个系列已经基本包含了上面这些算...

Miracle8070 1万+

[Leetcode] 302. Smallest Rectangle Enclosing Black Pixels (暴力/ 二分法)

302.Smallest Rectangle Enclosing Black Pixels (题目链接) Hard An image is represented by a binary matrix with0as a white pixel and1as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizont...

热爱编程,热爱技术 354

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

UVa 12307 Smallest Enclosing Rectangle(旋转卡壳+最小覆盖矩形)

题意:给出点集,求覆盖所有点的最小面积矩形和最小周长矩形。 参考了很多Killerfear前辈的代码:传送门。 枚举每条边为矩形底边,找出最高点和最左最右点。 判断最高点是三角形面积,最左最右点是点积的正负。 最左最右点各占据凸包一半的点。 可以推出这三个点的性质都是单峰函数。 #include #include #include #include #i

Just One Last Coding~♬ 1133

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

uva 319 - Pendulum(几何)

题目链接:uva 319 - Pendulum 注意高度不能高过水平线,一种周期是绕某点一直转圈,一种周期是返回起点。 #include #include #include #include #include using namespace std; const double pi = 4 * atan(1); const double eps = 1e-8; i

不慌不忙、不急不躁 885

LeetCode 题解(284) : Smallest Rectangle Enclosing Black Pixels

题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontal

2500

LintCode - Smallest Rectangle Enclosing Black Pixels

解法一 4 binary search class Solution { public: int minArea(vector&lt;vector&lt;char&gt;&gt; &amp;image, int x, int y) { int n = image.size(), m =image[0].size(); int left=0, right=...

real_lisa的博客 256

Smallest rectangle Enclose Black Pixels

1 public class Solution { 2 private char[][] image; 3 public int minArea(char[][] image, int x, int y) { 4 if (image.length == 0 || image[0].length == 0) { 5 ret...

weixin_34123613的博客 103

[LeetCode]Smallest Rectangle Enclosing Black Pixels

用的dfs,但是感觉这个方法应该不是最优的,否则这个题目不应该是hard public class Solution { int up = Integer.MAX_VALUE; int low = Integer.MIN_VALUE; int left = Integer.MAX_VALUE; int right = Integer.MIN_...

233

[LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

  An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally an...

weixin_33877885的博客 269

Lintcode600 · Smallest Rectangle Enclosing Black Pixels go

/** 600 · Smallest Rectangle Enclosing Black Pixels Algorithms Hard Accepted Rate 38% DescriptionSolutionNotesDiscussLeaderboard Description An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are con

ScutKevinYzy的博客 275

[Leetcode] 302. Smallest Rectangle Enclosing Black Pixels 解题报告

题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizon

魔豆(Magicbean)的博客 2353

Zxing生成二维码并去掉白边(matrix.enclosingRectangle)

//生成二维码方法 suspend fun createQRCode(dataStr: String,size: Int):Bitmap? = withContext(Dispatchers.IO) { val hashTable = Hashtable<EncodeHintType,Any>() hashTable[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L hashTab.

Zmj_Dns的博客 952

302. Smallest Rectangle Enclosing Black Pixels

题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally ...

weixin_33785108的博客 236
上一篇: leetcode 300: Longest Increasing Subsequence
下一篇: leetcode 303: Range Sum Query - Immutable
xudli
博客等级 码龄18年 204粉丝 275原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值