直方图最大矩形覆盖
来源:互联网 发布:制作相册影集的软件 编辑:程序博客网 时间:2024/05/17 07:41
给出的n个非负整数表示每个直方图的高度,每个直方图的宽均为1,在直方图中找到最大的矩形面积。
以上直方图宽为1,高度为[2,1,5,6,2,3]。
最大矩形面积如图阴影部分所示,含有10单位
样例
给出 height = [2,1,5,6,2,3],返回10
struct Value{ Value(int a, int b) { x = a; y = b; } int x; int y;};class Solution {public: /** * @param height: A list of integer * @return: The area of largest rectangle in the histogram */ int largestRectangleArea(vector<int> &height) { // write your code here int n = height.size(); if (n < 1) { return 0; } stack<Value> buf; int result = 0; height.push_back(0); for (int i = 0; i <= n; i++) { if (height[i] == 0) { while (!buf.empty()) { Value top = buf.top(); int temp = (i - top.x) * top.y; if (temp > result) { result = temp; } buf.pop(); } } else if (buf.empty()) { buf.push(Value(i, height[i])); } else { Value top = buf.top(); if (height[i] > top.y) { buf.push(Value(i, height[i])); } else if (height[i] < top.y) { int x = top.x; while (!buf.empty() && height[i] < top.y) { x = top.x; int temp = (i - top.x) * top.y; if (temp > result) { result = temp; } buf.pop(); if (!buf.empty()) { top = buf.top(); } } if (buf.empty() || height[i] > top.y) { buf.push(Value(x, height[i])); } } } } return result; }};
0 0
- 直方图最大矩形覆盖
- LintCode 直方图最大矩形覆盖
- lintcode:直方图最大矩形覆盖
- LintCode-直方图最大矩形覆盖
- 直方图最大矩形覆盖-LintCode
- lintcode(122)直方图最大矩形覆盖
- 【LeetCode】直方图最大矩形覆盖(栈)
- lintcode 直方图最大矩形覆盖与最大子矩阵
- 直方图最大面积矩形
- 直方图内最大矩形
- 直方图内最大矩形
- 直方图内最大矩形
- 直方图最大矩形
- 直方图最大矩形面积
- 直方图内最大矩形
- 直方图包含最大矩形
- 直方图内最大矩形
- 直方图内最大矩形
- sqlmap用户手册
- 机器学习笔记_ SVM
- URL请求中NSString带中文字符的转码操作
- C语言数据库
- Centos7下Hadoop-2.5.1和Hbase1.0.1的伪分布式安装 转(非原创哈)自己收藏看的
- 直方图最大矩形覆盖
- mysql连接远程数据库
- 浮点数计算注意精度问题
- Windows 安装MySQL-5.7.10-winx64
- SOCKET学习笔记1——TCP通讯
- MongoDB 官方文档
- Spring架包变更org.springframework.dao.DataAccessResourceFailureException
- AWS ubuntu14.04实例上进行condor8.4.3 集群内配置
- POJ 2349 Arctic Network