【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
找到01矩形中最大的全1子矩阵。
我自己的思路:
我先用一个跟输入相同大小的矩阵numInCol 存储从当前位置开始向下有多少连续的1。
如
其numInCol 是
然后用一个变量tmpans来记录以某个位置开始,其右下矩形的最大全1矩阵的大小。
方法是如果当前位置是1,则用1 * numInCol[当前位置] 即只有这一列的大小
如果其后面紧接着的位置也是1,则用 2 * (这两列中连续1高度小的值) 即这两列的矩形大小
如果其后面紧接着的位置也是1,则用 3 * (这三列中连续1高度小的值) 即这三列的矩形大小
........................
依次类推
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.empty())
return ; int row = matrix.size();
int col = matrix[].size();
int ans = ; vector<vector<int> > numInCol(row, vector<int>(col, ));
int tmpans = ; //对每一列
for(int j = ; j < col; j++)
{
numInCol[row - ][j] = (matrix[row - ][j] == '') ? : ;
for(int i = row - ; i >= ; i--)
{
if(matrix[i][j] == '')
{
numInCol[i][j] = numInCol[i + ][j] + ;
}
}
} //对整个矩阵
for(int i = row - ; i >= ; i--)
{
tmpans = numInCol[i][col - ];
ans = max(ans, tmpans);
for(int j = col - ; j >= ; j--)
{
int jj = j;
int len = ;
int minlen = numInCol[i][j];
while(jj < col && matrix[i][jj] == '')
{
minlen = min(minlen, numInCol[i][jj]);
tmpans = max(tmpans, len * minlen);
ans = max(ans, tmpans);
jj++;
len++; }
}
} return ans;
}
}; int main()
{
Solution s;
vector<vector<char> > matrix(, vector<char>(, ''));
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = ''; int ans = s.maximalRectangle(matrix); return ;
}
网上的答案,整体的思路差不多,也是通过一列列的数据来判断,但是并没有像我一样,把所有的值都存下来,而是只存了一行的列信息,每到新的一行再更新,这样空间少了很多。其次,没有像我这样每次都循环后面的列是否为1,而是利用栈,如果高度是递增的就先不计算最大矩形而是把信息压栈,等到遇到高度减少的时候再依次计算这一段的最大矩形。
class Solution {
public:
int maximalRectangle(vector<vector<char>> &matrix) {
if (matrix.empty()) return ;
int rows = matrix.size();
int cols = matrix[].size();
int maxArea = ;
vector<int> heights(cols + , );
vector<int> stack;
for (int i = ; i < rows; i++) {
stack.clear();
for (int j = ; j < cols + ; j++) {
if (j < cols) {
if (matrix[i][j] == '') {
heights[j]++;
} else {
heights[j] = ;
}
}
if (stack.empty() || heights[j] >= heights[stack.back()]) {
stack.push_back(j);
continue;
}
while (!stack.empty() && heights[j] < heights[stack.back()]) {
int top = stack.back();
stack.pop_back();
int begin = stack.empty() ? : stack.back() + ;
int area = heights[top] * (j - begin);
if (area > maxArea) maxArea = area;
}
stack.push_back(j);
}
}
return maxArea;
}
};
【leetcode】Maximal Rectangle (hard)★的更多相关文章
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【LeetCode】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...
- 【LeetCode】836. Rectangle Overlap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...
- 【LeetCode】223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 【Leetcode】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【leetcode】1019. Next Greater Node In Linked List
题目如下: We are given a linked list with head as the first node. Let's number the nodes in the list: n ...
随机推荐
- [译]JavaScript:将字符串两边的双引号转换成单引号
原文:http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html 代码的不一致性总是让人发狂,如果每位开发者都能 ...
- R-处理数据对象的实用函数
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- MySQL Cluster 配置文件(config.ini)详解
MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...
- bootstrap实现弹出窗口
bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...
- linux 驱动程序中断号的申请
1.linux下查看硬中断与软中断 硬中断:/proc/interrupts 软中断:/proc/softirqs 往往一些硬件中断号都是跟CPU所分配的硬件中断号相匹配的. 即同类型的cpu可能对同 ...
- Tomcat无法启动问题
检查环境变量设置 再打开一个cmd, 1.输入if not exist "%JRE_HOME%\bin\java.exe" echo no jre java 回车 结果:no ...
- Python学习笔记一
原来Python的文件配置好环境变量直接双击就可以运行,当然也可以控制台+编辑器 first try: import turtle window=turtle.Screen() babbage=tur ...
- ubuntu14.04安装django
1) sudo apt-get install python-pip#安装pip 2) pip install Django==1.8.1
- 《Head First Servlet JSP》web服务器,容器,servlet的职责
(一)web服务器,容器,servlet的职责 (二)J2EE服务器与web容器