leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle
https://leetcode.com/problems/largest-rectangle-in-histogram/
https://leetcode.com/problems/maximal-rectangle/
class Solution {
public:
int largestRectangleArea2(vector<int> height) {
if(height.size()==) return ;
int ret = numeric_limits<int>::min();
for(int i=;i<height.size();++i) {
int area = height[i];
int left = i-, right = i+;
for(;left>= && height[i]<=height[left];--left) area += height[i];
for(;right<height.size() && height[i]<=height[right];++right) area += height[i];
ret = max(ret, area);
}
return ret;
}
int largestRectangleArea(vector<int>& height) {
//return largestRectangleArea2(height);
if(height.size()==) return ;
height.push_back(-);
stack<int> st;
int ret = numeric_limits<int>::min(), hId, width, area, start;
for(int i=;i<height.size();++i) {
if(st.empty()) {
st.push(i);
start = st.top();
}
if(height[st.top()] < height[i]) st.push(i);
while(!st.empty() && height[st.top()] > height[i]) {
hId = st.top(); st.pop();
width = st.empty()? i-start: i-st.top()-;
ret = max(ret, width * height[hId]);
}
st.push(i);
}
return ret;
}
};
class Solution {
public:
void clear(vector<int> &vec) {
for(int i=;i<vec.size();++i) vec[i] = ;
}
void clearStack(stack<int> &st) {
while(!st.empty()) st.pop();
}
int largestRectangleArea(vector<int>& height) {
if(height.size()==) return ;
height.push_back(-);
stack<int> st;
int ret = numeric_limits<int>::min(), hId, width, area, start;
for(int i=;i<height.size();++i) {
if(st.empty()) {
st.push(i);
start = st.top();
}
if(height[st.top()] < height[i]) st.push(i);
while(!st.empty() && height[st.top()] > height[i]) {
hId = st.top(); st.pop();
width = st.empty()? i-start: i-st.top()-;
ret = max(ret, width * height[hId]);
}
st.push(i);
}
return ret;
}
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.size() == ) return ;
vector<int> height(matrix[].size());
int ret = numeric_limits<int>::min(), start;
for(int i=;i<matrix.size();++i) {
for(int j=;j<matrix[].size();++j) {
if(matrix[i][j]=='') height[j]++;
else height[j] = ;
}
ret = max(ret, largestRectangleArea(height));
}
return ret;
}
};
leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle的更多相关文章
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- [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] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
随机推荐
- Firefly 配置说明!
原地址:http://www.9miao.com/question-15-43023.html 下图一一个典型的config.json的配置:<ignore_js_op> "db ...
- Android支付接入(五):机锋网
原地址:http://blog.csdn.net/simdanfeg/article/details/9012083 前边已经陆续跟大家走了一遍运营商和支付宝付费接入,今天跟大家一起看看机锋网的支付接 ...
- Android学习 RadioButton
http://blog.csdn.net/cuiran/article/details/45065477 https://www.baidu.com/s?wd=BadgeView%20RadioBut ...
- java 布尔值一种赋值方法
在研读jmeter的代码时发现一个很常见的布尔值赋值方式,记录下来备忘: public static void main(String[] args) { // TODO Auto-generated ...
- codeforces #313 div1 B
模拟判定就可以了 判定字符串是否相等用hash来判断 QAQ 值得一提的是一开始我交的时候T了 结果我将递归的顺序调整了一下就A了 (并不知道为什么 #include<cstdio> #i ...
- MapReduce编程系列 — 2:计算平均分
1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...
- python numpy笔记:给matlab使用者
利用Numpy,python可以进行有效的科学计算.本文给过去常用matlab,现在正学习Numpy的人. 在进行矩阵运算等操作时,使用array还是matrix?? 简短的回答,更多的时候使用arr ...
- [Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=’
SELECT * FROM table_a a where a.id NOT IN (SELECT b.id FROM table_b b); 先将两个数据表的编码统一,如果table_a的编码为 ...
- 关于haproxy hdr_reg(host) 的一些解释
I've recently taken over an environment using HAProxy, and I'm attempting to learn the config and wh ...
- hdu 4502 吉哥系列故事——临时工计划(dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4502 思路:一个简单的dp ,比赛时没做出来呢..... d[i]代表 到第i天时的最大值 #includ ...