public class Solution
{
public int LargestRectangleArea(int[] hist)
{
// The main function to find the
// maximum rectangular area under
// given histogram with n bars
int n = hist.Length;
// Create an empty stack. The stack
// holds indexes of hist[] array
// The bars stored in stack are always
// in increasing order of their heights.
Stack<int> s = new Stack<int>(); int max_area = ; // Initialize max area
int tp; // To store top of stack
int area_with_top; // To store area with top
// bar as the smallest bar // Run through all bars of
// given histogram
int i = ;
while (i < n)
{
// If this bar is higher than the
// bar on top stack, push it to stack
if (s.Count == || hist[s.Peek()] <= hist[i])
{
s.Push(i++);
} // If this bar is lower than top of stack,
// then calculate area of rectangle with
// stack top as the smallest (or minimum
// height) bar. 'i' is 'right index' for
// the top and element before top in stack
// is 'left index'
else
{
tp = s.Peek(); // store the top index
s.Pop(); // pop the top // Calculate the area with hist[tp]
// stack as smallest bar
area_with_top = hist[tp] *
(s.Count == ? i : i - s.Peek() - ); // update max area, if needed
if (max_area < area_with_top)
{
max_area = area_with_top;
}
}
} // Now pop the remaining bars from
// stack and calculate area with every
// popped bar as the smallest bar
while (s.Count > )
{
tp = s.Peek();
s.Pop();
area_with_top = hist[tp] *
(s.Count == ? i : i - s.Peek() - ); if (max_area < area_with_top)
{
max_area = area_with_top;
}
} return max_area;
}
}

参考:https://www.geeksforgeeks.org/largest-rectangle-under-histogram/

补充一个python的实现:

 class Solution:
def largestRectangleArea(self, heights: 'List[int]') -> int:
heights.append(0)#默认最后补充一个0,方便统一处理
n = len(heights)
s = []
max_area = 0#最大面积
tp = 0#栈顶索引
area_with_top = 0#临时面积
i = 0
while i < n:
if len(s) == 0 or heights[s[-1]] <= heights[i]:
s.append(i)#栈内记录的是高度递增的索引
i += 1
else:#遇到了高度比当前栈顶元素低的元素时,
tp = s.pop(-1)#清算栈内的元素的高度
if len(s) == 0:
area_with_top = heights[tp] * i#栈内没有元素,则宽度是i
else:#高度是栈顶元素,宽度是i - 1 - 前一个栈顶元素的索引
area_with_top = heights[tp] * (i - s[-1] - 1)
max_area = max(max_area,area_with_top)#更新最大值
# while len(s) > 0:#处理栈内剩余元素,处理流程和遇到一个
# tp = s.pop(-1)
# if len(s) == 0:
# area_with_top = heights[tp] * i
# else:
# area_with_top = heights[tp] * (i - s[-1] - 1)
# max_area = max(max_area,area_with_top)
return max_area

采用了一个小技巧,在heights最后补一个0,则21行到27行的代码就可以省略了。

最新文章

  1. 在VM虚拟机上安装Microsoft Dynamics CRM 2016 步骤图解及安装注意事项
  2. OrchardNoCMS实体关系映射扩展
  3. Python SMTP邮件模块
  4. 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现
  5. linux下安装swftools工具
  6. QT:浮动的饼状统计图(自绘不规则窗口)
  7. synchronized关键字以及实例锁 类锁
  8. 书写规范的javaScript
  9. linux主机名为bogon的原因及修改方法
  10. 笔记:XML-解析文档-流机制解析器(SAX、StAX)
  11. Codeforces 193 D. Two Segments
  12. Python迭代器、生成器
  13. [Python] 00 - Books
  14. 找不到命令 ifconfig
  15. 洛谷 P3386 【模板】二分图匹配
  16. GOOD BLOG URL
  17. centos6.5 手动安装gcc
  18. express form/ajax 后端获取前端数据
  19. 死磕salt系列-salt入门
  20. tyvj P1050 最长公共子序列

热门文章

  1. c++ 生成dll文件并调用-转
  2. Tcl脚本整理照片
  3. Linux下查看CPU型号,内存大小,硬盘空间的命令
  4. 周强201771010141《面向对象程序设计(java)》第六周学习总结
  5. C# 流水号生成器开发
  6. webView 获取内容高度不准确的原因是因为你设置了某个属性
  7. vue-router(配置子路由--单页面多路由区域操作)
  8. mount: mounting proc on /proc failed: Device or resource busy
  9. Centos系统下 Gitolite安装与相关配置(git权限控制软件)
  10. 开源中国/码云 README.md上传图片的爬坑记录