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. redis批量删除脚本

    服务器上安装了redis客户端,通过客户端利用脚本对数据批量删除,脚本内容如下: #!/bin/bash name="$1" echo $name ./redis-cli -h r ...

  2. mvc3 学习链接收集

    原文发布时间为:2011-04-17 -- 来源于本人的百度文章 [由搬家工具导入] The mvc3 study links collection http://dotnetslackers.com ...

  3. Qualcomm MPM introduction

    在 Qualcomm chip 裡有一個 hardware block 名為 MPM. 在 RPM CPU halts 後,MPM 會保持在 on 的狀態用來處理 全系統 sleep process ...

  4. Spring Boot学习——数据库操作及事务管理

    本文讲解使用Spring-Data-Jpa操作数据库. JPA定义了一系列对象持久化的标准. 一.在项目中使用Spring-Data-Jpa 1. 配置文件application.properties ...

  5. Integer.ParseInt()异常

    这里传参数:bookPage.nextPage,action接收到的是string型. 程序需要将string转成int来使用. 用上try { pageNUmber = Integer.parseI ...

  6. Android与H5互调

    前言 微信,微博,微商,QQ空间,大量的软件使用内嵌了H5,这个时候就需要了解Android如何更H5交互的了:有些外包公司,为了节约成本,采用Android内嵌H5模式开发,便于在IOS上直接复用页 ...

  7. Oracle 表分区partition(http://love-flying-snow.iteye.com/blog/573303)

    http://www.jb51.net/article/44959.htm Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划 ...

  8. TopCoder SRM 722 Div1 Problem 600 DominoTiling(简单插头DP)

    题意  给定一个$12*12$的矩阵,每个元素是'.'或'X'.现在要求$1*2$的骨牌铺满整个矩阵, 'X'处不能放置骨牌.求方案数. 这道题其实和 Uva11270 是差不多的,就是加了一些条件. ...

  9. java 两个int类型的数据相除并输出百分号保留两位有效数字

    java代码: public void IntA(int a , int b){ //首先判断分母不能为0 if(b!=0){ folat num = (float) a*100/b; Decimal ...

  10. 转:浅析Collections.sort

    浅析Collections.sort 问题引入   在之前的一次Java上机实习中,老师布置了一道很简单的题: 从控制台输入10个整数,对它们进行升序排序并输出.   考虑到只有10个数,需要比较的次 ...