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,给定的 ...
随机推荐
- MySQL分区表(转)
查看分区情况 SELECT * FROM information_schema.PARTITIONS WHERE table_name='table_name': PARTITION_NAME:分区的 ...
- spoj 227
留着 #include <cstdio> #include <cstring> #include <cstdlib> #define lson l, m, rt & ...
- Scala的Pattern Matching Anonymous Functions
参考自http://stackoverflow.com/questions/19478244/how-does-a-case-anonymous-function-really-work-in-sca ...
- MySQL提示:The server quit without updating PID file问题的解决办法
错误如下: [root@snsgou mysql]# service mysql restartMySQL server PID file could not be found![失败]Startin ...
- Android:反编译查看源码
下载>>>>>>>>>>>>>>> 使用图形化反编译工具:Androidfby 打开Androidfby中的A ...
- SRM 586 DIV1 L1
可以化简为求n条线段的最大覆盖问题,需要注意的是对于实数而言. #include <iostream> #include <vector> #include <strin ...
- POJ2240——Arbitrage(Floyd算法变形)
Arbitrage DescriptionArbitrage is the use of discrepancies in currency exchange rates to transform o ...
- 退出myeclipse 8.5配置中心
用myeclipse 8.5没多久,进入软件中心下载插件,找不到退出按钮,唉,木想到就是一图标.
- HTTPConnection与HTTPClient的区别
HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等.HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便.比如重访问的自定 ...
- BZOJ2870: 最长道路tree
题解: 子树分治的做法可以戳这里:http://blog.csdn.net/iamzky/article/details/41120733 可是码量... 这里介绍另一种好写又快的方法. 我们还是一颗 ...