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 ***@的更多相关文章

  1. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  2. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  3. [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 ...

  4. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  5. 【leetcode】Maximal Rectangle (hard)★

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [LeetCode OJ] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  8. LeetCode OJ 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  9. 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 ...

  10. leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

随机推荐

  1. Matlab数据转化至python端,并写入数据库

    因工作原因,一些获取的行业数据以已知的结构体存储在.mat文件中, 现需要将其存储在数据库中并且能够灵活调用至python dataframe里进行操作 原数据的一个例子如下 目标如上: 然后是转化代 ...

  2. Flask学习笔记:数据库迁移操作flask-script+alembic/flask-migrate

    数据库迁移是将代码中模型类(即表)的修改同步到数据库中, flask-sqlalchemy的模型类一旦使用create_all()映射到数据库中后,对这个模型类的修改(例如添加了一个新的字段)就不会再 ...

  3. 自动化运维之使用Python3收发电子邮件~~~附源码

    一.背景介绍   1.1  一些专业名称的解释 MUA——Mail User Agent,邮件用户代理.是用户与电子邮件系统的交互接口,一般来说它就是我们PC机上的一个程序,提供一个好的用户界面,它提 ...

  4. [BZOJ2331]地板(插头DP)

    Description lxhgww的小名叫"小L",这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板 ...

  5. linux下编译openjdk8

    一.准备工作                                                           1.0 工作环境 Ubuntu 12.04,32位机 1.1.安装JD ...

  6. 推荐Android几个优质的完整项目学习

    ==>来自于微信公众号==鸿洋.大家可以关注一波大神之作. 后台经常有人问我能不能推荐几个完整项目用于学习.借着周末的机会,给大家推荐几个,项目我基本都在本地运行过,并且会在文章末尾提供每个项目 ...

  7. Android stadio litepal

    今天看到技术交流群里有人招聘Android,要求会litepal. 我立马百度了下.嗯,我的学习技术的精神,是值得称赞的. litepal就是操作数据库的一个框架.git地址: https://git ...

  8. js 监听后退事件及跳转页面

    //直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window. ...

  9. NOI p 2017 TG游记

    嗨小朋友们大家好 还记得我是谁吗 对了我就是为iot配音的演员 弹鸡鸡 今天呐我特别的要向长沙市的oier们 洛谷的oier们 还有cnblogs的oier们问声好 为什么呢 因为我们在2017年11 ...

  10. Asp.net自定义控件开发任我行(6)-嵌入资源下

    摘要 上一章,我们讲了嵌入.css文件,这一章,我们来讲一下嵌入.js文件,也顺带一个嵌入Image文件 内容 我们前面的几章,一运行,下拉框就显示出来了,但是DropDwonList的下拉框是被隐藏 ...