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 ...
随机推荐
- list,tuple,set,dict汇总
有序/无序 追加/删除元素 元素可/不可重复 元素类型 创建方式 List 有序 可追加删除追加:list.append(item),list.insert(index,item)删除:list. ...
- 震惊!几道Python 理论面试题,Python面试题No18
本面试题题库,由公号:非本科程序员 整理发布 第1题: 简述解释型和编译型编程语言? 解释型语言编写的程序不需要编译,在执行的时候,专门有一个解释器能够将VB语言翻译成机器语言,每个语句都是执行的时候 ...
- BFS:CF356C-Compartments
C. Compartments time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- loj2074 「JSOI2016」灯塔
loj 题面错的--去bzoj上看吧qwq 观察到 \(\sqrt{|i-j|}\) 的取值只有 \(\sqrt{n}\) 级别个,然后就很显然了,rmq. #include <iostream ...
- IOS开发学习笔记010-面向对象的三大特性
面向对象的三大特性 1.封装 2.继承 3.多态 一.封装 将类内部的属性保护起来,在外部不能直接访问,那么如果需要访问怎么办呢? OC提供了set方法来对成员变量进行访问 set方法 1.作用:提供 ...
- MySQL5.7(三)数据表操作
概念在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代表记录中的一个域.1.创 ...
- python拼接
拼接: name=zhuhuan age=23 salary=333 info=''' ----- info of %s----- age:%s name:%s salary:%s %(name,ag ...
- 解决This application failed to start because it could not find or load the Qt platform plugin "windows
解决方案:所在环境python根目录下qt.conf,重新设置path即可,此类问题通常在目录转移之后出现.
- spaCy 并行分词
spaCy 并行分词 在使用spacy的时候,感觉比nltk慢了许多,一直在寻找并行化的方案,好在找到了,下面给出spaCy并行化的分词方法使用示例: import spacy nlp = spacy ...
- LeetCode with Python -> Linked List
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...