给出 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 柱状图中最大的矩形的更多相关文章

  1. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  2. 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 ...

  3. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  4. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  5. LeetCode第[84]题(Java):Largest Rectangle in Histogram(最大的矩形柱状图)

    题目:最大的矩形柱状图 难度:hard 题目内容: Given n non-negative integers representing the histogram's bar height wher ...

  6. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram

  7. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

  8. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  9. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

随机推荐

  1. Appium基础——需要知道的

      Appium使用平台厂商提供的自动化框架: 1.ios 苹果的UIAutomation 2.android google的UIAutomator Appium底层使用厂商提供的自动化框架,就不需要 ...

  2. CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳)

    本文主要是对主流居中方法进行了归纳,有些地方甚至就是把别人的代码直接复制过来的,没有什么自己的东西,除了大漠以及张鑫旭的方法外,还有来自司徒正美.怿飞博客的几个方法 以下方法,由于测试环境的原因,IE ...

  3. 随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍

    系列文章目录 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(1)-总体说明 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(2)-abp说明 随应潮流-基于ABP+A ...

  4. 第九章-IO编程

    IO是输出输入的意思 在计算机中常用到的数据交换的地方是磁盘, 网络等 输入流是从外面(磁盘, 网络)流进内存 输出流是从内存流到外面(磁盘, 网络) 同步IO是指等待IO完成再继续执行 异步IO是在 ...

  5. C语言之fileno()函数--获取已经打开的文件的文件描述符(小技巧)

    open函数相关的:  /* open 是系统调用 返回的是文件句柄*/ #include <sys/stat.h> #include <fcntl.h> int open(c ...

  6. 洛谷 1541 乌龟棋——dp

    题目:https://www.luogu.org/problemnew/show/P1541 以用了几张牌为阶段.注意知道了用了4种牌各几张后,当前位置就是确定的,所以不用记录什么的. #includ ...

  7. Python的中文处理

    一.使用中文字符 在python源码中如果使用了中文字符,运行时会有错误,解决的办法是在源码的开头部分加入字符编码的声明,下面是一个例子: #!/usr/bin/env python # -*- co ...

  8. C# web 总结

    (1)Cshtml 中 “@” 符号转义 在 cshtml 中需要使用 “@” 符号,如 “@幸福摩天轮版权所有”.那么我们需要使用转义,使用 “@@” 就好!“© ”和 “@” 好像呀. <t ...

  9. POJ3735【矩阵快速幂】

    逛了一圈...觉得这篇讲的比较清楚:传送门~ 简要概括: 1.线性代数的知识,单位矩阵的利用:(如果不知道单位矩阵的,先去补习一下线代,做几题行列式就会了): 2.然后构造好矩阵以后,直接做M次乘积运 ...

  10. MyBatis中的RowBounds

    myBatis中实现分页的方式是采用RowBounds这个类,用法如下,xml语句不变 传入两个参数,strat起始行, limit是当前页显示多少条数据,原理是RowBounds在处理分页时,只是简 ...