博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Largest Rectangle in Histogram
阅读量:4494 次
发布时间:2019-06-08

本文共 2746 字,大约阅读时间需要 9 分钟。

题目:

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,

Given height = [2,1,5,6,2,3],
return 10.

思路:

1)对每个直方维护它能拓展到左右两边最远的边界。

package area;public class LargestRectangleInHistogram {    public int largestRectangleArea(int[] height) {        int n = height.length;        int max = 0;        int[] left = new int[n];        int[] right = new int[n];        for (int pos = 0; pos < n; ++pos) {            left[pos] = right[pos] = pos;            int i = pos - 1;            for (; i >= 0; --i) {                if (height[i] < height[pos]) {                    left[pos] = i + 1;                    break;                }            }            if (i == -1) left[pos] = 0;                        for (i = pos + 1; i < n; ++i) {                if (height[i] < height[pos]) {                    right[pos] = i - 1;                    break;                }            }            if (i == n) right[pos] = n - 1;            int area = height[pos] * (right[pos] - left[pos] + 1);            if (area > max)                max = area;        }        return max;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        int[] height = { 1,1,1,1 };        LargestRectangleInHistogram l = new LargestRectangleInHistogram();        System.out.println(l.largestRectangleArea(height));    }}

 

2)维护一个栈,对比当前元素,如果大于当前元素,则pop,否则push当前元素的index

package area;import java.util.Stack;public class LargestRectangleInHistogram {    public int largestRectangleArea(int[] height) {        Stack
stack = new Stack
(); int max = 0; int n = height.length; int[] newHeight = new int[n + 1]; for (int i = 0; i < n; ++i) newHeight[i] = height[i]; newHeight[n] = 0; int i = 0; while (i < n + 1) { if (stack.isEmpty() || newHeight[stack.peek()] <= newHeight[i]) { stack.push(i++); } else { int index = stack.pop(); int area = (stack.isEmpty() ? i : (i - stack.peek() - 1)) * newHeight[index]; max = (max < area ? area : max); } } return max; } public static void main(String[] args) { // TODO Auto-generated method stub int[] height = { 1,1 }; LargestRectangleInHistogram l = new LargestRectangleInHistogram(); System.out.println(l.largestRectangleArea(height)); }}

 

转载于:https://www.cnblogs.com/null00/p/5096633.html

你可能感兴趣的文章
Codeforces Round #328 (Div. 2)D. Super M 虚树直径
查看>>
Java判断是否为移动端
查看>>
chromedriver下载链接以及对应版本
查看>>
[SimplePlayer] 6. 音频同步
查看>>
把一个SVN项目的目录结构 导入到另外一个空白的SVN项目里
查看>>
Android之Adapter用法总结-(转)
查看>>
总结列表显示ListView知识点
查看>>
android 教程实例系列
查看>>
lucene笔记
查看>>
tomcat无法正常shutdown
查看>>
zookeeper + dubbo 搭建
查看>>
根据前序遍历和中序遍历求出二叉树并打印
查看>>
UOJ356 [JOI2017春季合宿] Port Facility 【启发式合并】【堆】【并查集】
查看>>
Delphi的命令行编译命令
查看>>
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》--微软中国.NET Micro Framework项目组工程师所作之序...
查看>>
php服务端搜索,功能改进
查看>>
unity, 在surface shader中访问顶点色
查看>>
Spring声明式事务配置
查看>>