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,给定的 ...
随机推荐
- How to Cope with Deadlocks
http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html How to Cope with Deadlocks This section ...
- Redis-PHP-Hash 表相关API
Hashes 相关 ================================ hDel - 删除一个哈希 key hExists - 检查哈希 key是否存在 hGet - 获得某哈希 key ...
- Servlet课程0425(四) Servlet实现简单用户登录验证
Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...
- eclipse中的输入提示怎么设置
对于大多数的开发人员来说,打代码是一件非常繁琐的事情,eclipse中为我们提供了自动提示的功能,但是默认的提示只有当我们输入小数点后才能出现提示框,那么我们如何设置eclipse,能够让它为我们提示 ...
- ArcGIS学习记录—ArcGIS ArcMap编辑状态中线打断的问题
摘要:在处理数据时,我们经常会遇到线打断的问题,比如需要指定在线上某处打断线,或者新建网络数据集时需要在线的交点处打段线等等.现将桌面版中我所遇到的线打断的工具总结如下: 在ArcGIS矢量处理数据时 ...
- PHP设计模式浅析
工厂模式 提到的最多, 用途也最广. 简单说就是: 定义一个用户创建对象的接口. 把创建对象的过程封装起来. 工厂类是指包含一个专门用来创建其他对象的方法的类,工厂类在多态性编程实践中是至关重要的,它 ...
- Spring Framework 4.1.1
http://repo.spring.io/libs-release-local/org/springframework/spring/4.1.1.RELEASE/
- clone函数
http://blog.csdn.net/caianye/article/details/5947282 http://wenku.baidu.com/link?url=qnq7laYDYm1V8tl ...
- 函数fsp_seg_inode_page_get_nth_inode
#define FSEG_ARR_OFFSET (FSEG_PAGE_DATA + FLST_NODE_SIZE) #define FSEG_PAGE_DATA FIL_PAGE_DATA #defi ...
- poj2352
纪念树状数组初步(……): 这题已经给了y升序,y相同时x升序,(yeah) 所以容易想到O(n^2)的模拟算法: 其实分析一下就是对于当前xi,统计之前有多少个小于等于xi(因为已经保证了没有相同坐 ...