LeetCode85 Maximal Rectangle java题解

 public static int maximalRectangle(char[][] matrix) {

		 int rowNum=matrix.length;
		 if(rowNum==0)
			 return 0;
		 int columnNum=matrix[0].length;

		 int[][] height=new int[rowNum][columnNum+1];
		 int maxarea=0;

		 for(int i=0;i<rowNum;i++)
		 {
			 for(int j=0;j<columnNum;j++)
			 {
				 int k=i;
				 height[i][j]=0;
				 while(k>=0&&j<columnNum)
				 {
					 if(matrix[k][j]=='1')
						 height[i][j]++;
					 else
						break;
					 k--;
				 }

			 }
			 height[i][columnNum]=-1;
		 }

		 Stack<Integer> stack=new Stack<>();
		 for(int i=0;i<rowNum;i++)
		 {
			 for(int j=0;j<=columnNum;j++)
			 {
				 int a=height[i][j];
				 int b=stack.isEmpty()?-1:stack.peek();
				 if(stack.isEmpty()||height[i][j]>=height[i][stack.peek()])
					 stack.push(j);
				 else
				 {

						int tempPop=stack.pop();
						maxarea=Math.max(maxarea, height[i][tempPop]*(stack.isEmpty()?

j:j-1-stack.peek()));
						j--;
				 }
			 }
			 stack.clear();
		 }

		 return maxarea;

	    }

题目:

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

也就是说,给一个仅仅有0和1的2维矩阵,求当中1可以组成的最大四方形面积

解题:

这题能够看作是前面一题(LeetCode84)的拓展,前面一题中输入的是一个数组,数组每个元素的值看作的矩形的高度,在这题中先对矩阵做一个处理。对矩阵的元素计算其高度,处理完之后得到一个每个原矩阵元素的高度矩阵,把这个矩阵当作输入就和前面一题是类似了。

代码:

时间: 2024-10-27 13:34:46

LeetCode85 Maximal Rectangle java题解的相关文章

leetcode 85 Maximal Rectangle ----- java

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 6. 求矩阵中由1组成的最大子矩阵面积. 1.首先进行观察,发现如果直接遍历然后求出每一

leetcode85 - Maximal Rectangle - hard

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

Maximal Rectangle leetcode java

题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解: 这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决.解决方法是: 按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0.然后对每一行计算 Largetst Rectangle in H

【leetcode刷题笔记】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. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

LeetCode: Maximal Rectangle 解题报告

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: 1 public class Solution { 2 publ

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的个数以及每一列连续的1的个数. public int maximalRectangle(char[][] matrix) { int lenX = matrix.length; if (lenX == 0) { r

LeetCode: Maximal Rectangle

LeetCode: 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. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

Leetcode:Maximal Rectangle 最大全1子矩阵

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. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状