LeetCode OJ-- Maximal Rectangle ***@
https://oj.leetcode.com/problems/maximal-rectangle/
给一个二维矩阵,里面只有0 1,求一个最大的矩阵,里面的所有元素都是1.
首先预处理: 0 1 1 1 0 1 1
做一个二维数组,记录到目前为止本行上连续1的个数,为 0 1 2 3 0 1 2
之后再对每一个位置进行遍历,从行方向向列延伸,记录这个过程中的最大面积。
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.empty() || matrix.size() == || matrix[].size() == )
return ;
int rows = matrix.size();
int cols = matrix[].size();
vector<vector<int> > row_ones;
row_ones.resize(rows);
for(int i = ; i<rows; i++)
row_ones[i].resize(cols);
for(int y = ; y<rows;y++)
{
int count = ;
for(int x = cols - ; x>=; x--)
{
if(matrix[y][x] == '')
count++;
else
count = ;
row_ones[y][x] = count;
}
}
int i_max = ;
for(int y = ; y < rows; y++)
{
for(int x = ; x < cols; x++)
{
if(row_ones[y][x] > )
{
for(int x_length = ; x_length <= row_ones[y][x]; x_length++)
{
int y_length = ;
while(y + y_length < rows && row_ones[y+y_length][x] >= x_length)
y_length++;
int m2 = y_length * x_length;
i_max = max(m2,i_max);
}
}
}
}
return i_max;
}
};
LeetCode OJ-- Maximal Rectangle ***@的更多相关文章
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode OJ] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- LeetCode OJ 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java for LeetCode 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- CDOJ:1636-梦后楼台高锁,酒醒帘幕低垂(Kruskal+暴力)
梦后楼台高锁,酒醒帘幕低垂 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 并查集:HDU5326-Work(并查集比较简单灵活的运用)
Work HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Time Limit: 2000/1000 MS (Java/Others) M ...
- [Poj3133]Manhattan Wiring (插头DP)
Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...
- CSS滚动插件
http://www.dowebok.com/131.html wow.js http://www.jq22.com/jquery-info499 smoove.js http://www.lanr ...
- Python框架之Django学习笔记(十三)
Django站点管理(续1) 上次介绍了Django的站点管理的一些基础知识,这次再来深入了解一下Django的站点管理. Admin是如何工作的: 在幕后,管理工具是如何工作的呢? 其实很简单. 当 ...
- 【Reverse Linked List II】cpp
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- [python][django学习篇[13]增加markdown_1
1 进入虚拟环境,安装markdwon python install markdown 2 修改视图函数detail def detail(request, pk): # get_object_or ...
- 2018CCPC网络赛
A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...
- MQ、JMS以及ActiveMQ的了解和认识
新加入的公司中,架构用到了activeMq,对于以前只了解nginx.tomcat的我有点懵逼,所以在网上找点资料看看,了解下什么是MQ,activemq.具体作用是什么 MQ MQ简介: MQ全称为 ...
- linux常用命令(复制)
显示目录和文件的命令 Ls:用于查看所有文件夹的命令. Dir:用于显示指定文件夹和目录的命令 Tree: 以树状图列出目录内容 Du:显示目录或文件大小 修改目录,文件权限和属主及数组命令 ...