import java.util.Stack;

/**
*
* Source : https://oj.leetcode.com/problems/largest-rectangle-in-histogram/
*
*
* 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.
*
* 6
* +---+
* 5 | |
* +---+ |
* | | |
* | | |
* | | | 3
* | | | +---+
* 2 | | | 2 | |
* +---+ | | +---+ |
* | | 1 | | | | |
* | +---+ | | | |
* | | | | | | |
* +---+---+---+---+---+---+
*
* Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
*
*
* 6
* +---+
* 5 | |
* +-------|
* |-------|
* |-------|
* |-------| 3
* |-------| +---+
* 2 |-------| 2 | |
* +---+ |-------|---+ |
* | | 1 |-------| | |
* | +---|-------| | |
* | | |-------| | |
* +---+---+---+---+---+---+
*
*
* 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.
*/
public class LargestRectangle { /**
* 找到直方图中面积最大的矩形的面积
*
* 从左向右遍历数组
* 如果当前元素大于栈顶元素,说明当前是一个递增序列,将当前元素压入栈
* 如果当前元素小于栈顶元素,则pop出栈顶元素,计算当前栈顶元素和到当前位置的面积,和最大面积比较,更新最大面积,直到栈为空
*
*
* 边界条件
* 为了计算第一个元素,在栈底压入0
*
* @param arr
* @return
*/
public int findLargest (int[] arr) {
int result = 0;
Stack<Integer> stack = new Stack<Integer>();
stack.push(0);
for (int i = 0; i < arr.length; i++) {
if (stack.empty() || arr[i] >= arr[stack.peek()]) {
stack.push(i);
} else {
int cur = stack.pop();
result = Math.max(result, arr[cur] * (i - cur));
i --;
}
}
return result;
} /**
* 相比于上面的方法,这里会将每一个递增序列前面所有的元素计算一遍,而不是仅仅计算当前递增序列
*
* @param arr
* @return
*/
public int findLargest1 (int[] arr) {
int res = 0;
for (int i = 0; i < arr.length; ++i) {
if (i + 1 < arr.length && arr[i] <= arr[i + 1]) {
continue;
}
int minH = arr[i];
for (int j = i; j >= 0; --j) {
minH = Math.min(minH, arr[j]);
int area = minH * (i - j + 1);
res = Math.max(res, area);
}
}
return res;
} public static void main(String[] args) {
LargestRectangle largestRectangle = new LargestRectangle();
int[] arr = new int[]{2,1,5,6,2,3};
int[] arr1 = new int[]{2,1,5,6,5,2,3};
// System.out.println(largestRectangle.findLargest(arr));
// System.out.println(largestRectangle.findLargest(arr1));
System.out.println(largestRectangle.findLargest1(arr1));
}
}

leetcode — largest-rectangle-in-histogram的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

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

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

  5. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  6. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  7. [LeetCode] Largest Rectangle in Histogram

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

  8. leetcode -- Largest Rectangle in Histogram TODO O(N)

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

  9. [LeetCode] Largest Rectangle in Histogram 解题思路

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

  10. LeetCode——Largest Rectangle in Histogram

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

随机推荐

  1. 洛谷.5300.[GXOI/GZOI2019]与或和(单调栈)

    LOJ BZOJ 洛谷 想了一个奇葩的单调栈,算的时候要在中间取\(\min\),感觉不靠谱不写了=-= 调了十分钟发现输出没取模=v= BZOJ好逗逼啊 题面连pdf都不挂了 哈哈哈哈 枚举每一位. ...

  2. 黑洞版视频裂变程序【接口版】全新上线,全新UI,支持分享数据统计

    黑洞版视频裂变程序[接口版]全新上线,全新UI,支持分享数据统计!   后台效果   程序统一售价:1899/套(包安装,包更新) 注:本程序不属于之前视频程序的更新版,展现形式和广告位设置均不同,是 ...

  3. Dockerfile中COPY命令的简单性

    dockerfile中的COPY命令是不会拷贝目录结构的,它只会单纯把包含的所有文件拷贝到另一个目录中去. 相关链接:https://www.cnblogs.com/sparkdev/p/957324 ...

  4. thrift小试--C++

    [转自]http://blog.csdn.net/poechant/article/details/6618284# Thrift可以实现C++.Java.Python等多种语言的自动生成,此处以C+ ...

  5. BZOJ3497 : Pa2009 Circular Game

    令先手为$A$,后手为$B$,将相邻同色棋子合并成块,首先特判一些情况: 如果所有格子都是满的,那么显然$A$必败. 否则如果所有块都只有一个棋子,那么显然平局. 枚举$A$的第一步操作,如果可以使得 ...

  6. [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费

    There are N piles of stones arranged in a row.  The i-th pile has stones[i] stones. A move consists ...

  7. mysql数据库备份_可执行文件

    这段时间接手运维的工作,刚开始就尝到了数据丢失的痛!老板抱怨,同事抱怨!都说先删库再跑路,我还不想跑! 下面是我的备份记录:(分4步) 1.编写备份执行文件sqlAutoBak.sh #!/bin/s ...

  8. Retrofit 实现获取往里圆角图片,且传值到另一个页面

    记得加网络权限 java包: // compile 'jp.wasabeef:glide-transformations:3.0.1' implementation 'com.squareup.ret ...

  9. Hadoop源码分析(1):HDFS读写过程解析

    一.文件的打开 1.1.客户端 HDFS打开一个文件,需要在客户端调用DistributedFileSystem.open(Path f, int bufferSize),其实现为: public F ...

  10. [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...