084 Largest Rectangle in Histogram 柱状图中最大的矩形
给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 。
您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积。
详见:https://leetcode.com/problems/largest-rectangle-in-histogram/description/
Java实现:
方法一:遍历数组,每找到一个局部峰值,就向前遍历所有的值,算出共同的矩形面积,每次对比保留最大值。
class Solution {
public int largestRectangleArea(int[] heights) {
int res=0;
int n=heights.length;
for(int i=0;i<n;++i){
if(i+1<n&&heights[i]<=heights[i+1]){
continue;
}
int minH=heights[i];
for(int j=i;j>=0;--j){
minH=Math.min(minH,heights[j]);
int area=minH*(i-j+1);
res=Math.max(res,area);
}
}
return res;
}
}
方法二:
class Solution {
public int largestRectangleArea(int[] heights) {
int n=heights.length;
if (heights == null || n == 0){
return 0;
}
int res=0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < n; ++i) {
//如果高度递增,那么一次入栈。
if (stack.isEmpty() || heights[stack.peek()] <= heights[i]) {
stack.push(i);
}
//如果当前柱比栈顶的低,那么把栈顶的拿出来,计算所有已经出栈的最大面积。
else {
int start = stack.pop();
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
res = Math.max(res, heights[start] * width);
--i;
}
}
//循环过后栈中是递增的条目,计算在栈中递增条目的最大面积。
while (!stack.isEmpty()) {
int start = stack.pop();
int width = stack.isEmpty() ? n : n - stack.peek() - 1;
res = Math.max(res, heights[start] * width);
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4322653.html
084 Largest Rectangle in Histogram 柱状图中最大的矩形的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- LeetCode第[84]题(Java):Largest Rectangle in Histogram(最大的矩形柱状图)
题目:最大的矩形柱状图 难度:hard 题目内容: Given n non-negative integers representing the histogram's bar height wher ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
随机推荐
- Jquery Plugin模版
1. [代码][JavaScript]代码 01// remember to change every instance of "pluginName" to the name o ...
- the art of seo(chapter seven)
Content Marketing ***Leveraging Major Social Media Platforms***LinkedIn, Facebook,Google+, Pinterest ...
- linux系统配置之网络配置(centos)
CentOS---网络配置详解 一.配置文件详解在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf 配置域名服务 ...
- CodeForces960F:Pathwalks (主席树+DP)
You are given a directed graph with n nodes and m edges, with all edges having a certain weight. The ...
- Bootstrap 轻量级后台管理系统模板--ACE使用介绍
在上一篇基于Bootstrap介绍了一个免费的后台管理模板Charisma UI. 参见链接: 基于Jquery.Bootstrap的后台管理免费UI框架推荐--Charisma UI 今天继续分享一 ...
- 谈谈Spring Ioc的理解
原文:http://blog.csdn.net/qq_22654611/article/details/52606960 学了几天Ioc了,但是对它的理解还是模模糊糊,看了这篇博客感觉对Ioc有了更深 ...
- opencv MatExpr MatOp
opencv提供了很多Mat的操作,其中涉及到两个重要的类:MatOp和MatExpr C++: MatExpr abs(const Mat& m) C++: void absdiff(Inp ...
- Flutter实战视频-移动电商-24.Provide状态管理基础
24.Provide状态管理基础 Flutter | 状态管理特别篇 —— Provide:https://juejin.im/post/5c6d4b52f265da2dc675b407?tdsour ...
- DOM学习笔记(二)对象方法与属性
所有 HTML 元素被定义为对象,而编程接口(对象的访问)则是对象方法和对象属性. 事实上,常用的只用window对象及其子对象document对象,以及事件Event对象. Window 对象 Wi ...
- 使用fastadmin的页面跳转模板
1.效果图 2.修改tp默认跳转模板文件( /thinkphp/tpl/dispatch_jump.tpl ),将文件中的内容全部替换成下面的内容然后保存即可,注意替换语言包和图片路径 {__NOLA ...