[LeetCode] Maximal Rectangle】的更多相关文章

作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 该题目是  leetcode Largest Rectangle in Histogram 的二维版本,首先进行预处理,把一个n×m的矩阵化为n个直方图,然后在每个直方图中计算使用单调栈的方法计算面积最大的矩形,然后求得最大的矩形面积即可. Ps:这题直接在网页里面敲完居然1A,不错. 代码如下:…
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Show TagsHave you met this question in a real interview? Yes  NoDiscussSOLUTION 1: public class Solution { public i…
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用Largest Rectangle in Hist…
原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 解题思路:找出矩阵中最大的矩形,矩形中只包含1.这道题需要利用上一道题(Largest Rectangle in Histogram)的结论.比…
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 在做Largest Rectangle in Histogram的时候有人说可以用在这题,看了一下还真是,以每行为x轴,每列往上累计的连续的1当成高度,就可以完全使用一样的方法了. int largestArea(vector<int>height){ stac…
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang…
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [解题思路] 1.brute force 枚举所有sub-matrix(O(N^2), N = m*n) ,检查每个子矩阵是不是都是1,如果是更新最大面积,检查子矩阵是否都是1需要 花费O(N). 故总的时间为O(N^3) N = m*n 可以过小数据,大数据直接…
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram,如果暴力求解,可以枚举每个点为最小值,向两边扩展,复杂度 O(n^2).我们可以维护一个栈,从而将复杂度降低到 O(n),这个栈的思维非常巧妙,参考了 discuss,我是完全想不出来(或者说忘记了).具体代码可以猛戳 这里. 2016-08-07 补:stack 数组维护的是一个单调递增的数组,…
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 接口: int maximalRectangle(char[][] matrix) 2 思路 这是一道非常综合的题目,要求在0-1矩阵中找出面积最大的全1矩阵.刚看到这道题会比较无从下手,b…
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given heigh…