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. ActiveMQ队列、主题模式区别

    1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...

  2. 在Codeblocks下配置GoogleTest单元测试工具

    开发工具 我和我的组员的都是使用的是大一老师推荐的codeblocks,所以,就愉快的决定了工具统一为codeblocks,语言C++. 测试单元 老师推荐的是JUnit和VSTS工具,但同学们从大一 ...

  3. vue的登陆验证及返回登录前页面实现

    一.路由配置部分如下所示, 导出路由示例 let router = new VueRouter({ routes: [ // 登陆 { name: 'login', path: '/login', c ...

  4. 了解vue APi

    阳光那么好,何必自寻烦恼,过好每一个当下,一万个美丽的未来抵不过一个温暖的现在. 一.Vue.nextTick(): 该api 是在Dom节点更新结束之后执行的一个延时回调.在修改数据之后,立即使用这 ...

  5. Maven工程无异常 启动没有出现Starting ProtocolHandler的原因

    这个情况可能的原因 一般来说有3种1.数据库没连接上2.注册中心没连接上3.逆向工程生成的mapper 有问题解决:哪个Maven工程出问题,就那个工程的src/main/resource目录下面添加 ...

  6. 学习HTML5 canvas遇到的问题

    学习HTML5 canvas遇到的问题 1. 非零环绕原则(nonzZero rule) 非零环绕原则是canvas在进行填充的时候是否要进行填充的判断依据. 在判断填充的区域拉一条线出来,拉到图形的 ...

  7. Python文本数据互相转换(pandas and win32com)

    (工作之后,就让自己的身心都去休息吧) 今天介绍一下文本数据的提取和转换,这里主要实例的转换为excel文件(.xlsx)转换world文件(.doc/docx),同时需要使用win32api,同py ...

  8. PHP的Memcached简单实现

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.也可动态缓存一些实 ...

  9. markdown反射型xss漏洞复现

    markdown xss漏洞复现 转载至橘子师傅:https://blog.orange.tw/2019/03/a-wormable-xss-on-hackmd.html 漏洞成因 最初是看到Hack ...

  10. mysql怎么修改密码

    第一种方式: 最简单的方法就是借助第三方工具Navicat for MySQL或Navicat Premium来修改,方法如下: 1.登录mysql到指定库,如:登录到student库. 2.然后点击 ...