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 ...
随机推荐
- Jack Straws POJ - 1127 (简单几何计算 + 并查集)
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...
- FZU 2082 过路费(树链剖分)
树链剖分模板题. FZU炸了,等交上去AC了再贴代码.
- LCS及方案数(DP)
Description 对于一个序列
- Java基础知识:Collection接口
*本文是最近学习到的知识的记录以及分享,算不上原创. *参考文献见文末. 这篇文章主要讲的是java的Collection接口派生的两个子接口List和Set. 目录 Collection框架 Lis ...
- opencv使用日记之一:平台搭建Mat类以及图像的读取修改
平台搭建就摸了一整天时间,真的是...不说了,最后我选择的是 opencv3.0(2015/06/04) + win7 + vs2012 注意opencv的版本不同导入的库文件是不一样的,所以请 ...
- 4 Template层-CSRF
1.csrf 全称Cross Site Request Forgery,跨站请求伪造 某些恶意网站上包含链接.表单按钮或者JavaScript,它们会利用登录过的用户在浏览器中的认证信息试图在你的网站 ...
- 某面试公司出的面试题---用JS比较两个版本号高低
一天中午某个公司给我反馈的面试题,说,比较两个文件的版本号,然后我给发过去了,说我的代码不符合他的要求,o(╯□╰)o了var compareVersion = compareVersion||fun ...
- va_list
void Log( const TCHAR *pszFormat, ... ) { TCHAR buf[] ; va_list arglist ; try { _tcscpy_s ( buf, , _ ...
- leetcode 【 Find Peak Element 】python 实现
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 与Python的初次见面
一.Python的介绍 python的创始人为吉多.范罗苏姆.1989年的圣诞期间,吉多.范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 二.Python是 ...