leetcode — largest-rectangle-in-histogram
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的更多相关文章
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 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 ...
- [LeetCode] Largest Rectangle in Histogram 解题思路
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode——Largest Rectangle in Histogram
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
随机推荐
- 我的 FPGA 学习历程(10)—— 实验:数码管驱动
根据黑金 AX301 手册,数码管位选信号命名为 SEL[5:0],其中 SEL[5] 对应最左边的数码管,而SEL[0] 对应最右边数码管:作为约定,在下面的描述中我们对应的称之为数码管 5 和数码 ...
- HDU-AcmKeHaoWanLe训练实录
菜鸡队训练实录. 现场赛记录:[名称:奖项/排名] 2017: ICPC Shenyang:Gold/3 CCPC Hangzhou:Gold/3 ICPC Beijing:Gold/13 CCPC ...
- XIX Open Cup named after E.V. Pankratiev. GP of Poland(AMPPZ-2018)
A. Drone With a Camera 三分套三分. #include<cstdio> #include<cmath> #include<algorithm> ...
- 关于H5的一些杂思细想(一)
作为一名前端程序媛,虽然整天和代码打交道,但是还是有一颗小清新的内心,虽然有时候加起班来不是人,但是空闲的时候还是会整理一下思绪,顺便整理一下自己,两个多月的加班,一直没有更新,今天就把自己最近做的一 ...
- VMware vsphere client报错问题
今天出现一种情况,搞了很久,重装client都不行,原来很简单,防止再犯. 异常信息:"VirtualInfrastructure.Utils.ClientsXml"的类型 ...
- oracle 创建的表为什么在table里没有,但是可以查出来
有两种的可能: 1这个表在其他用户下创建的,当前用户没有权限访问,此表不在属于当前用户 2查询时写的表名,并不是真正意义的表名,可能指向其他用户,或者就不是这个表
- 性能测试学习 第九课--LR12中controller基础知识
1.设计手工场景,理解集合点的策略 2.添加load generator 一.controller基础知识 1.controller的原理 通过场景设计来模拟用户的真实操作并调用vugen中的脚本,然 ...
- 编写程序,输入一个N,返回角谷变换(达到1所需)的次数
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 17:36 * @description ...
- 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法
一.我们先在XML当中自定义一个webview(Second_layout.xml) 代码如下: <?xml version="1.0" encoding="utf ...
- mysql+postgresql备份与恢复
mysql备份一个库, mysqldump -u用户名 -p密码 [选项] [数据库名] > /备份路径/备份文件名 mysqldump -uuser -p123123 auth > / ...