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

embedded/2024/10/24 6:27:54/

文章目录

    • 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;}
}

总结

暂无


http://www.ppmy.cn/embedded/42822.html

相关文章

C++ 数据结构算法 学习笔记(32) -五大排序算法

C 数据结构算法 学习笔记(32) -五大排序算法 选择算法 如下若有多个女生的身高需要做排序: 常规思维: 第一步先找出所有候选美女中身高最高的&#xff0c;与最后一个数交换 第二步再找出除最后一位美女外其它美女中的最高者&#xff0c;与倒数第二个美女交换位置 再找出除最…

NB65 第k轻的牛牛

描述 在农场里&#xff0c;农民们有一群牛&#xff0c;每头牛的体重不同。农民们将所有牛的体重记录在一个数组中。现在农民们想要知道&#xff0c;如果将这些牛的体重从小到大排序&#xff0c;那么第k小的体重是多少。请你编写一个程序&#xff0c;找出数组中第k小的元素。 你…

自定义RedisTemplate序列化器

大纲 RedisSerializerFastJsonRedisSerializer自定义二进制序列化器总结代码 在《RedisTemplate保存二进制数据的方法》一文中&#xff0c;我们将Java对象通过《使用java.io库序列化Java对象》中介绍的方法转换为二进制数组&#xff0c;然后保存到Redis中。实际可以通过定制Red…

MyBatis常见报错:org.apache.ibatis.binding.BindingException

哈喽&#xff0c;大家好&#xff0c;我是木头左&#xff01; 异常现象描述 当开发者在使用MyBatis进行数据库操作时&#xff0c;可能会遇到org.apache.ibatis.binding.BindingException: Parameter appId not found这样的错误提示。这个错误通常会让程序无法正常运行&#xff…

leetcode每日一题第八十九天

class Solution { public:int subarraySum(vector<int>& nums, int k) {unordered_map<int,int> mp;mp[0] 1;int count 0,pre 0;for(auto x:nums){pre x;if(mp.find(pre-k) ! mp.end()){count mp[pre-k];}mp[pre];}return count;} };

Vue小程序项目知识积累(三)

1.CSS中的var( ) var() 函数用于插入自定义属性&#xff08;也称为CSS变量&#xff09;的值。 var(--main-bg-color,20rpx) 设置一个CSS变量的值&#xff0c;但是如果 --main-bg-color 变量不存在&#xff0c;它将默认返回 20rpx。 CSS变量必须在一个有效的CSS规则&#xf…

Autodesk 3DS Max v2025 解锁版安装教程 (3D 建模软件)

前言 Autodesk 3ds Max 是一款功能强大的 3D 建模和动画解决方案&#xff0c;游戏开发人员、视觉效果艺术家和平面设计师使用它来创建庞大的世界、令人惊叹的场景和引人入胜的虚拟现实 (VR) 体验。 Autodesk 3DS MAX是业界使用最广泛的3D建模和动画软件程序之一&#xff0c;它…

python-pytorch 下批量seq2seq+Bahdanau Attention实现问答1.0.000

python-pytorch 下批量seq2seq+Bahdanau Attention实现简单问答1.0.000 前言原理看图数据准备分词、index2word、word2index、vocab_size输入模型的数据构造注意力模型decoder的编写关于损失函数和优化器在预测时完整代码参考前言 前面实现了 luong的dot 、general、concat注意…