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.

思路:此题还是有一些难度的,刚開始想的双指针。前后扫描,可是每次求最小的高度的时候都须要遍历一次,效率上不是非常高。为o(n2)。

代码例如以下:

public class Solution {
public int largestRectangleArea(int[] height) { int max = 0;//最大值
int i = 0;//左指针
int j = height.length - 1;//右指针
boolean isMinChange = true; //双指针扫描
while(i <= j){
int minHeight = Integer.MAX_VALUE;//范围内最小值
if(isMinChange){//最小值是否改变
isMinChange = false;
//又一次得到最小值
for(int k = i ; k <= j;k++){
if(height[k] < minHeight){
minHeight = height[k];
}
}
}
//面积
int area = (j - i + 1)*minHeight;
if(max < area){
max = area;
}
if(i == j){//假设相等,则结束
break;
}
if(height[i] < height[j]){//左指针添加,直到比当前大
int k = i;
while(k <= j && height[k] <= height[i]){
if(height[k] == minHeight){//推断最小值是否改变
isMinChange = true;
}
k++;
}
i = k;
}else{//右指针减小,直到比当前大
int k = j;
while( k >= i && height[k] <= height[j]){
if(height[k] == minHeight){//推断最小值是否改变
isMinChange = true;
}
k--;
}
j = k;
}
}
return max;
}
}

解法上过不了OJ,所以仅仅能在网上參看资料。最后找到资料例如以下。是用栈解决的。

public class Solution {
public int largestRectangleArea(int[] height) {
if (height == null || height.length == 0) return 0; Stack<Integer> stHeight = new Stack<Integer>();
Stack<Integer> stIndex = new Stack<Integer>();
int max = 0; for(int i = 0; i < height.length; i++){
if(stHeight.isEmpty() || height[i] > stHeight.peek()){
stHeight.push(height[i]);
stIndex.push(i);
}else if(height[i] < stHeight.peek()){
int lastIndex = 0;
while(!stHeight.isEmpty() && height[i] < stHeight.peek()){
lastIndex = stIndex.pop();
int area = stHeight.pop()*(i - lastIndex);
if(max < area){
max = area;
}
}
stHeight.push(height[i]);
stIndex.push(lastIndex);
}
}
while(!stHeight.isEmpty()){
int area = stHeight.pop()*(height.length - stIndex.pop());
max = max < area ? area:max;
} return max;
}
}

leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  3. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  6. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

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

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

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  9. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

随机推荐

  1. pat 甲级 L3-002. 堆栈

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...

  2. LOJ#2086. 「NOI2016」区间

    $n \leq 500000$个区间,从中挑出一些,使得至少有一个点被$m$个选中区间包含,且选中区间长度的极差最小. 区间题死脑筋晚期:把区间按左端点排序,然后右端点用个优先队列来弹,然后需要维护下 ...

  3. 页面get post等查看

    原文发布时间为:2010-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://www.fiddler2.com/Fiddler2/firstrun.asp

  4. post sharp 与log4net 结合使用,含执行源码 转拷

    环境: VS 2012 PostSharp-4.1.28 (下载地址)https://visualstudiogallery.msdn.microsoft.com/a058d5d3-e654-43f8 ...

  5. [LeetCode] Single Number II 位运算

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  6. [Oracle] Redo&Undo梳理

    Oracle Redo&undo Oracle中的redo和undo是关键技术的核心, 诸如实例恢复, 介质恢复, DataGuard, 闪回机制等都是给予redo和undo的, 所以很有必要 ...

  7. MySql视图笔记(转载)

    1.       视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过这个窗口,可以看到系统专门提供的数据(也可以查看到数据表的全部数据),使 ...

  8. sublime text3 无法安装插件

    下载 Package Control.sublime-package 点击这里下载: 打开sublime3 -> 首选项 -> 浏览插件 (程序自动打开插件目录) 删除 Package C ...

  9. LeetCode OJ--Linked List Cycle **

    https://oj.leetcode.com/problems/linked-list-cycle/ 判断一个链表是否为循环链表(这个链表可能是 1 2 3 4 然后4指向2) 巧妙的方法:设置两个 ...

  10. webapi 初识 net

    1.新建一个webapi 项目. 2.新建筛选器文件,用户在接口执行前后进行特性操作. public class MyActionWebApiAttribute : ActionFilterAttri ...