当前位置: 首页 > news >正文

【算法刷题day60】Leetcode:84. 柱状图中最大的矩形

文章目录

    • Leetcode 84. 柱状图中最大的矩形
      • 解题思路
      • 代码
      • 总结

草稿图网站
java的Deque

Leetcode 84. 柱状图中最大的矩形

题目:84. 柱状图中最大的矩形
解析:代码随想录解析

解题思路

反方向接雨水。见上一篇文章

代码

class Solution {public int largestRectangleArea(int[] heights) {int[] newHeights = new int[heights.length+2];System.arraycopy(heights, 0, newHeights, 1, heights.length);int res = 0;Stack<Integer> stack = new Stack<>();stack.push(0);for (int i = 1; i < newHeights.length; i++) {if (newHeights[i] > newHeights[stack.peek()]) stack.push(i);else if (newHeights[i] == newHeights[stack.peek()]) {stack.pop();stack.push(i);} else {while (newHeights[i] < newHeights[stack.peek()]) {int mid = stack.peek();stack.pop();int left = stack.peek();int h = newHeights[mid];int w = i - left - 1;res = Math.max(res, w * h);}stack.push(i);}}return res;}
}//双指针
class Solution {public int largestRectangleArea(int[] heights) {int length = heights.length;int []leftHeight = new int[length];int []rightHeight = new int[length];leftHeight[0] = -1;for (int i = 1; i < length; i++) {int t = i-1;while (t >= 0 && heights[t] >= heights[i])t = leftHeight[t];leftHeight[i] = t;}rightHeight[length-1] = length;for (int i = length-2; i >= 0; i--) {int t = i+1;while (t < length && heights[t] >= heights[i])t = rightHeight[t];rightHeight[i] = t;}int res = 0;for (int i = 0; i < length; i++) {int area = (rightHeight[i] - leftHeight[i] - 1) * heights[i];res = Math.max(res, area);}return res;}
}

总结

暂无

相关文章:

  • 大规模语言模型的书籍分享
  • 听说部门来了个00后测试开发,一顿操作给我整麻了
  • 自己动手写docker——Namespace
  • 【chagpt】广泛使用API之前:考虑成本和数据隐私
  • 01-05.Vue自定义过滤器
  • 在树莓派3B+中下载opencv(遇到的各种问题及解决)
  • 宿舍管理系统代码详解(操作界面)
  • 人人皆是黑客?EvilProxy推出一键反向代理服务
  • vue深度选择器(:deep​)
  • LeetCode 343. 整数拆分 (dp动态规划)
  • Xshell远程连接服务器需要哪些依赖包、
  • Python条件分支与循环
  • 【全开源】点餐小程序系统源码(ThinkPHP+FastAdmin+UniApp)
  • 快速搭建 WordPress 外贸电商网站指南
  • Linux——多线程(一)
  • 深入了解以太坊
  • 网络传输文件的问题
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 2019.2.20 c++ 知识梳理
  • Angular 响应式表单 基础例子
  • go语言学习初探(一)
  • jdbc就是这么简单
  • mysql外键的使用
  • PhantomJS 安装
  • Yii源码解读-服务定位器(Service Locator)
  • 分布式事物理论与实践
  • 关于Flux,Vuex,Redux的思考
  • 码农张的Bug人生 - 见面之礼
  • 使用Gradle第一次构建Java程序
  • 一份游戏开发学习路线
  • 一起参Ember.js讨论、问答社区。
  • 原生 js 实现移动端 Touch 滑动反弹
  • 栈实现走出迷宫(C++)
  • 主流的CSS水平和垂直居中技术大全
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​一些不规范的GTID使用场景
  • (C11) 泛型表达式
  • (SERIES12)DM性能优化
  • (规划)24届春招和25届暑假实习路线准备规划
  • (九)信息融合方式简介
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (三)elasticsearch 源码之启动流程分析
  • (十三)Maven插件解析运行机制
  • (新)网络工程师考点串讲与真题详解
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • ..回顾17,展望18
  • .naturalWidth 和naturalHeight属性,
  • .NET CLR Hosting 简介
  • .net framework profiles /.net framework 配置
  • .NET企业级应用架构设计系列之开场白
  • @modelattribute注解用postman测试怎么传参_接口测试之问题挖掘
  • @Transactional注解下,循环取序列的值,但得到的值都相同的问题
  • @Transient注解