Largest Rectangular Area in a Histogram 最大连续面积
在HankerRank遇到一题计算柱状图连续矩形面积的问题.
举例 hist = [3, 2, 3]. 在这个柱状图里面最大可以容纳一个high = 2 length = 3的连续矩形, 其面积 = 2*3 = 6.

按照提示需要使用stack来是时间复杂度保持在O(n). 搜索提示和代码, 把自己的理解描述如下:
加入数组是升序的 hist = [1, 2, 3]

因为数组是升序的, 所以每一个都会和之后的 high 形成更大的连续面积.
也因为数组是升序的, 所以每一个都会和之前的 high 无法形成更大的连续面积. 3和2无法形成, 因为2和3 已经组合.
所以升序的计算最好是在数组的最后面, i = 3时
i = 3 计算 index = 2的面积. area = 3 * 1; h[index] * (i - (index - 1) - 1)
i = 3 计算 index = 1的面积. area = 2 * 2; h[index] * (i - (index - 1) - 1)
i = 3 计算 index = 0的面积. area = 1 * 3; h[index] * i加入数组是降序的 hist = [3, 2, 1]

和上面分析相反, 但是在i+1均可计算前一个连续矩形面积.
i = 0 不计算
i = 1 计算 index = 0 的面积. area = 3 * 1; h[index] * i
i = 2 计算 index = 1 的面积. area = 2 * 2; h[index] * i
i = 3 计算 index = 2 的面积. area = 1 * 3; h[index] * i
public static long GetLargestArea(int[] h)
{
long maxarea = -1;
long toparea = -1;
int i = 0;
int top;
var stack = new Stack<int>();
while(i < h.Length)
{
if(stack.Count == 0 || h[i] >= h[stack.Peek()])
{
// 把升序的数组索引入栈.
stack.Push(i++);
}
else
{
// 把降序的数组索引出栈.并计算其面积.
top = stack.Pop();
toparea = h[top] * (stack.Count == 0 ? i : (i - stack.Peek() - 1));
if(toparea > maxarea)
maxarea = toparea;
}
}
while(stack.Count != 0)
{
top = stack.Pop();
toparea = h[top] * (stack.Count == 0 ? i : (i - stack.Peek() - 1));
if(toparea > maxarea)
maxarea = toparea;
}
return maxarea;
}
参考:
Largest Rectangular Area in a Histogram
Hackerrank
Largest Rectangular Area in a Histogram 最大连续面积的更多相关文章
- Largest Rectangular Area in a Histogram
题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...
- 【Leetcode_easy】812. Largest Triangle Area
problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...
- Largest Allowed Area【模拟+二分】
Largest Allowed Area 题目链接(点击) 题目描述 A company is looking for land to build its headquarters. It has a ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- [LeetCode] Largest Triangle Area 最大的三角区域
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- LeetCode 812 Largest Triangle Area 解题报告
题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...
- 【LeetCode】812. Largest Triangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...
随机推荐
- 【MySQL】sort by then group by
tb: ### 需求:根据id进行分组,找到分组内hour中最大的一项 错误写法: select id, max(hour) from tb group by id; 正确的写法: ### 需求:根据 ...
- 程序设计与算法(一)C语言程序设计CAP之字符串
C++中的字符串 字符串有三种形式 用双引号括起来的字符串常量,如果"CHINA"."C++ program" 存放于字符串数组中,以'\0'字符(ASCII吗 ...
- 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(四)
这篇文章是对前面三篇的一个总结: 1.从测试结果来看,原生的数据库性能分别是:SQL Server(4587)>Oracle(271)>Mysql(145),测试数据量分别为5W.50W. ...
- pycharm Python Console调试 & django 调试
1.设置断点,然后debug 2.如果在运行中想修改变量,可以直接在pycharm中修改 如将列表的 13改成3,然后选中 Excuting Selection in console ...
- dubbo搭建
1.安装java : yum install java 2.下载Tomcat: wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.1 ...
- Json数据产生树形结构
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- talend工具中往oracle插数据报ORA-01461: can bind a LONG value only for insert into a LONG colum
今天使用talend往oracle插数据报ORA-01461: can bind a LONG value only for insert into a LONG column 数据源是mysql,开 ...
- python headers missing
系统环境 win7 64 bit,cygwin,Gvim8.1 问题 Gvim通过插件管理器Vundle下载好了YouCompleteMe插件的全部文件. 利用cygwin 进入在YouComplet ...
- ubuntu18.04 安装pip3
Ubuntu18.04默认内嵌python2.python3,pip安装时,python2对应安装pip,python3对应安装pip3. sudo apt install python3-pip 检 ...
- C#-----类DateTime的常用方法
1.TryParse(string s, out DateTime result) 将日期和时间的指定字符串表示形式转换为其 System.DateTime 等效项,并返回一个指示转换是否成功的 ...