码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++算法:柱状图中最大的矩形


    LeetCode84柱状图中最大的矩形

    给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
    求在该柱状图中,能够勾勒出来的矩形的最大面积。
    示例 1:
    输入:heights = [2,1,5,6,2,3]
    输出:10
    解释:最大的矩形为图中红色区域,面积为 10
    示例 2:
    输入: heights = [2,4]
    输出: 4

    分析

    可以用单调栈解决。

    23年3月

    class Solution {
    public:
    int largestRectangleArea(vector& heights) {
    m_c = heights.size();
    vector vMaxLeft;
    {
    vector> vLeft;
    for (int i = 0; i < heights.size(); i++)
    {
    const int& h = heights[i];
    const int iLessNum = std::lower_bound(vLeft.begin(), vLeft.end(), h, [](const std::pair& p, const int i)
    {
    return p.first < i;
    }) - vLeft.begin();
    if (0 == iLessNum)
    {
    vMaxLeft.push_back(-1);
    }
    else
    {
    vMaxLeft.push_back(vLeft[iLessNum - 1].second);
    }
    while (vLeft.size() && (vLeft.back().first >= h))
    {
    vLeft.pop_back();
    }
    vLeft.emplace_back(h, i);
    }
    }

    	 int iMax = INT_MIN;
    	 {
    		 vector> vRight;
    		 for (int i = m_c - 1; i >= 0; i--)
    		 {
    			 const int& h = heights[i];
    			 
    			 const int iLessNum = std::lower_bound(vRight.begin(), vRight.end(), h+1, [](const std::pair& p, const int i)
    			 {
    				 return  p.first < i;
    			 }) - vRight.begin();
    			 if (0 == iLessNum)
    			 {
    				 iMax =max(iMax, h * (m_c -  vMaxLeft[i]-1));
    			 }
    
    			 else
    			 {
    				 const int iRight = vRight[iLessNum - 1].second;
    				 iMax = max(iMax, h * (iRight - vMaxLeft[i]-1));
    			 }
    			 while (vRight.size() && (vRight.back().first >= h))
    			 {
    				 vRight.pop_back();
    			 }
    			 vRight.emplace_back(h, i);
    		 }
    	 }
    
    	 return iMax;
     }
     int m_c;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    };

    23年8月

    class Solution {
    public:
    int largestRectangleArea(vector& heights) {
    m_c = heights.size();
    vector vLeft(m_c, -1), vRight(m_c, m_c);
    stack sta;
    for (int i = 0; i < heights.size(); i++)
    {
    while (sta.size() && (heights[i] <= heights[sta.top()] ))
    {
    vRight[sta.top()] = i;
    sta.pop();
    }
    if (sta.size())
    {
    vLeft[i] = sta.top();
    }
    sta.emplace(i);
    }

    	int iRet = 0;
    	for (int i = 0; i < m_c; i++)
    	{
    		iRet = max(iRet, (vRight[i] - vLeft[i] - 1) * heights[i]);
    	}
    	return iRet;
    }
    int m_c;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    };

    其它

    视频课程

    如果你觉得复杂,想从简单的算法开始,可以学习我的视频课程。
    https://edu.csdn.net/course/detail/38771

    我的其它课程
    https://edu.csdn.net/lecturer/6176

    测试环境

    win7 VS2019 C++17

    相关下载

    doc版文档,排版好
    https://download.csdn.net/download/he_zhidan/88348653

  • 相关阅读:
    深入学习JUC,深入了解Java线程中的锁,及锁的实现原理,底层的知识又增加了!!!
    C++查找实验
    Removing the Bias of Integral Pose Regression 阅读笔记
    C/C++整数的个数 2020年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
    百度发布文心大模型4.0,百度搜索实现重构;AI报告2023
    188. 买卖股票的最佳时机 IV
    量子化学新突破:提前预测分子基态中的化学键断裂时间
    神经网络的图像识别技术,语音识别深度神经网络
    数组清空(bzero与memset)
    selenium.chrome怎么写扩展拦截或转发请求?
  • 原文地址:https://blog.csdn.net/he_zhidan/article/details/133752963
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号