[LeetCode] 84. Largest Rectangle in Histogram

LeetCode刷题:84. Largest Rectangle in Histogram LeetCode刷题:84. Largest Rectangle in Histogram 原题链接:https://leetcode.com/problems/largest-rectangle-in-histogram/ Givennnon-negative integers representing the histogram's bar height where the width... 阅读详情

Largest Rectangle in Histogram

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
在这里插入图片描述
Example:

Input: [2,1,5,6,2,3]
Output: 10

解法1

在每个局部峰值的时候往前遍历所有的可能的矩阵大小,找到最大的。
局部峰值的意思就是当前值比下一个值大,比如例子中的2,6,3。

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int len = heights.size();
        if (len == 0)
            return 0;
        int max_value = 0;
        for(int i=0;i<len;i++){
            if(i==len-1 || heights[i]>heights[i+1]){
                int min_value = INT_MAX;
                for(int j=i;j>=0;j--){
                    min_value = min(min_value, heights[j]);
                    max_value = max(max_value, min_value*(i-j+1));
                }
            }
        }
        return max_value;
    }
};

解法2:单调栈

维护一个单调递增的栈,保存单调递增的高度的下标,便于计算间隔。
在heights的末尾加上一个0,便于处理最后一个高度值。

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int len = heights.size();
        if (len == 0)
            return 0;
        int max_value = 0;
        stack<int> index;
        heights.push_back(0);
        for(int i=0;i<=len;i++){
            if(index.empty() || heights[i] >= heights[index.top()])
                index.push(i);
            else{
                while(!index.empty() && heights[index.top()] > heights[i]){
                    int cur = index.top(); 
                    index.pop();
                    max_value = max(max_value, heights[cur]*(index.empty() ? i : i-index.top()-1));
                }
                index.push(i);
            }
            
            
        }
        return max_value;
    }
};

参考

https://www.cnblogs.com/grandyang/p/4322653.html

学习笔记( 11 ) - 解决:finalshell连接虚拟机(ubuntu)超时,主机ping不通虚拟机,主机没有VMnet8网卡, Connection refused: connect 解决:finalshell连接虚拟机(ubuntu)超时,主机ping不通虚拟机,主机没有VMnet8网卡, Connection refused: connect 阅读详情

相关推荐

Balser相机的硬件触发实战:从连线到信号处理

本文详细介绍了Balser相机硬件触发的实战应用,从硬件接线、信号定义到软件配置(Pylon Viewer及API)提供了完整指南。重点阐述了如何利用外部物理电信号实现微秒级精准触发,解决工业场景下的同步与延迟问题,并分享了信号调理、抗干扰等高级实战经验。

y9z0a1b的博客 337

【LeetCode】解题84:Largest Rectangle in Histogram

LeetCode解题 84:Largest Rectangle in Histogram Problem 84: Largest Rectangle in Histogram [Hard]解题思路1. 分治法2. 栈Solution (Java)修改过程 Problem 84: Largest Rectangle in Histogram [Hard] Given n non-negative i...

Fayedily的博客 342

DexProtector内存校验绕过与DEX提取实战

DEX文件是Android应用的核心执行单元,其动态加载与运行时保护机制直接关系到App安全强度。DexProtector作为主流商业加固方案,摒弃传统文件级防护,转而依托ART虚拟机下的匿名内存映射段(anonymous mmap)构建多维校验体系——涵盖字节码完整性校验、内存页属性检测及运行时环境感知。其技术价值在于将防护锚点从静态APK迁移至动态内存态,大幅提升逆向门槛。典型应用场景包括金融类App的防篡改加固、游戏客户端的反外挂保护,以及政企应用的合规性安全增强。本文聚焦DexProtector v

weixin_30315435的博客 714

[LeetCode 84, 85]Largest Rectangle in Histogram / Maximal Rectangle

LeetCode第84,85号题,解题报告。利用栈的O(n)算法

zlasd的博客 423

实战:利用 Agent Harness 自动化 PPT 制作

本文将带你从零开始,用轻量级Agent编排框架,结合主流大模型(GPT-4o、通义千问、文心一言等),搭建一套全自动化的PPT生成工作流:从用户输入自然语言需求,到需求解析、大纲生成、内容填充、数据匹配、排版美化、导出成品,全程不需要手动操作,最快10分钟就能产出符合要求的PPT。我们不会讲晦涩的Agent理论,所有步骤都有可直接运行的代码示例,哪怕你只有基础的Python语法基础,也能跟着一步步做出能用的自动PPT工具。

AI Agent Harness Engineering 569

PCL 快速均匀下采样

快速均匀下采样 是一种点云降采样技术,通过设置采样步长来实现等距降采样,从而减少点的数量。用户可以通过指定步长,选择每隔一定数量的点采样一个点,这样可以在保持点云几何特性的同时,显著减少点的数量。

纵马踏花向自由 870

LeetCode84——Largest Rectangle in Histogram

在柱状图中找到面积最大的矩形。

上善若水 2079

【机器人技术基础】MATLAB Robotics Toolbox PUMA560 运动学正逆解

机器人技术基础实验2报告 1. 实验目的和要求 2. 实验手段 2.1 实验原理 2.2 函数详解 3. 实验主要内容 4. 实验结果和分析 4.1 正运动学求解 例1 例2 4.2 逆运动学求解 例1 例2 1. 实验目的和要求 掌握常见形式的机器人的D-H坐标系建立方法、运动学建模; 正逆运动学分析与求解; 使用Robotics Toolbox对直角坐标机器人、圆柱坐标机器人、球坐标机器人、PUMA560其中之一建立运动学模型,正逆求解和仿真。 2. 实验手段 2.1

【人工智障 | 计算鸡视觉 | 机器孩】 1万+

LeetCode Top 100 Liked Questions 84. Largest Rectangle in Histogram (Java版; Hard)

welcome to my blog LeetCode Top 100 Liked Questions 84. Largest Rectangle in Histogram (Java版; Hard) 题目描述 Given n non-negative integers representing the histogram's bar height where the width of each ...

littlehaes的博客 162

Android实现自动输入文本效果 (附完整源码)

Android实现自动输入文本效果 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 414

Megatron-LM从头训练大语言模型:并行策略图示【Tensor并行、Pipeline并行、Data并行、Sequence并行、virtual_pipeline并行、Context并行】

节点包含的层每GPU的参数内存每GPU的激活内存节点0Stage 0层1-1212层参数/2 (TP切分)8k序列激活 (CP切分)节点1Stage 1层13-2412层参数/2 (TP切分)8k序列激活 (CP切分)节点2Stage 2层25-3612层参数/2 (TP切分)8k序列激活 (CP切分)节点3Stage 3层37-4812层参数/2 (TP切分)8k序列激活 (CP切分)负载均衡:每个节点处理相同数量的层(12层)内存优化。

u013250861的博客 285

Leetcode 84. Largest Rectangle in Histogram

题目描述:找出数组中最大的矩形面积。 题目链接:Leetcode 84. Largest Rectangle in Histogram 题目思路:这个题目就是85. Maximal Rectangle的基础,求出最大面积高度,只要找到每个柱子左边第一个比他小,和右边第一个比它小的值,然后计算出短板*宽度就是面积了,如果用传统的方法就是循环对每个柱子做一次,这样的话如果所有柱子高度相同,时间...

Za七杂⑧ 390

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[leetcode] 84. Largest Rectangle in Histogram

Description Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where wid...

Learning from the mistakes 195

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【LeetCode】 84. Largest Rectangle in Histogram

84. Largest Rectangle in Histogram

夏至亦韵 329

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

leetcode题解:第84题Largest Rectangle in Histogram

题目 Largest Rectangle in Histogram Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above i...

chenf1999的博客 695

LeetCode 84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of ea

ActiveCoder的博客 845

Leetcode-84. Largest Rectangle in Histogram

公众号不定期发布一篇关于Leetcode解题技巧的文章,尝试从多角度、不同方法对题目进行解析。欢迎大家关注! 题目描述(困难难度) 给一个柱状图,输出一个矩形区域的最大面积。 解法一 暴力破解 以题目给出的例子为例,柱形图高度有 1, 2, 3, 5, 6,我们只需要找出每一个高度对应的最大的面积,选出最大的即可。如果求高度为 3 的面积最大的,我们只需要遍历每一个高度,然后看连续的大于等于 ...

猫敷雪 623

leetcode 84 Largest Rectangle in Histogram

leetcode 84 Largest Rectangle in Histogram

快递小可的博客 734

LeetCode84 Largest Rectangle in Histogram

LeetCode84 Largest Rectangle in Histogram问题链接 LeetCode84问题描述 Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle

yanqinghe123的专栏 334

leetcode 84.Largest Rectangle in Histogram, 85.Maximal Rectangle

leetcode 84.Largest Rectangle in Histogram(Hard) leetcode 85.Maximal Rectangle(Hard)

liquewei_26的博客 376

LeetCode 84. Largest Rectangle in Histogram 题解

本题是单调栈的经典应用,也是面试中的高频题目。单调递增栈:栈中元素的高度递增找到每个柱子的左右边界计算以每个柱子为高的矩形面积通过本题可以深入理解单调栈在解决"下一个更小/更大元素"类问题中的应用。

cannonjinx的博客 2180

LeetCode(84) Largest Rectangle in Histogram

题目Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each

指尖飞舞 2717

LeetCode //C - 84. Largest Rectangle in Histogram

【代码】LeetCode //C - 84. Largest Rectangle in Histogram。

Made in Code 1077

LeetCode84 Largest Rectangle in Histogram java题解

题目: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram wher

u012249528的专栏 1413

【Leetcode】84. Largest Rectangle in Histogram

试想有这样的一棵树,其树根为全数组的最小值,其左孩子是其左边的所有数字的最小值,其右孩子是其右边子树的最小值,并且左右子树满足相同的性质。这里的单调严格不严格是不打紧的,都能保证答案正确,主要原因是,如果某个高度连续出现了若干次,当最后一次它出栈的时候,以它为高的最大矩形面积就被确定了,会更新res。3、它的每个节点的左子树的节点个数,是其左边连续的一段大于它的数的个数。它的每个节点的右子树的节点个数,是其右边连续的一段大于它的数的个数。的柱子,按照相同的手法求一遍栈内元素为高度的最大矩形面积即可。

数学、算法爱好者的博客 341
上一篇: [leetcode] 76. Minimum Window Substring
下一篇: [LeetCode] 93. Restore IP Addresses
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值