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 return its area.

解题思路:

求01矩阵中,全是1的子矩阵的最大面积。

把矩阵按照每一行看做是直方图,可以转化为上一题,JAVA实现如下:

	static public int maximalRectangle(char[][] matrix) {
		if(matrix.length==0||matrix[0].length==0)
			return 0;
		int result=0;
		int[] dp=new int[matrix[0].length];
		for(int i=0;i<matrix.length;i++){
			for(int j=0;j<matrix[0].length;j++)
				if(matrix[i][j]==‘1‘)
					dp[j]++;
				else dp[j]=0;
			result=Math.max(result, largestRectangleArea(dp));
		}
		return result;
	}
	 public static int largestRectangleArea(int[] height) {
	        Stack<Integer> stk = new Stack<Integer>();
	        int ret = 0;
	        for (int i = 0; i <= height.length; i++) {
	            int h=0;
	            if(i<height.length)
	                h=height[i];
	            if (stk.isEmpty() || h >= height[stk.peek()])
	                stk.push(i);
	            else {
	                int top = stk.pop();
	                ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
	                i--;
	            }
	        }
	        return ret;
	}
时间: 2024-10-06 16:48:37

Java for LeetCode 085 Maximal Rectangle的相关文章

【leetcode】Maximal Rectangle

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. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

[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 return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

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.首先进行观察,发现如果直接遍历然后求出每一

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Histogram第四种思路,或者翻译版Largest Rectangle in [email protected],JAVA实现如下: public int largestRectangleArea(int[] height) { Stack<Integer> stk = new Stack<In

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

链接: https://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矩形面积,比如: [ ['1','1','1','0'], ['1','1','1','1']

[LeetCode 85] Maximal Rectangle (华为2015机试)

题目链接:maximal-rectangle import java.util.Arrays; /** * Given a 2D binary matrix filled with 0's and 1's, * find the largest rectangle containing all ones and return its area. * */ public class MaximalRectangle { // 65 / 65 test cases passed. // Status

LeetCode 85. Maximal Rectangle

1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char>>& matrix) { 4 /** largest rectangle based solution **/ 5 if(matrix.size()<=0 || matrix[0].size()<=0) 6 return 0; 7 int m=matrix.size(); 8 int n=matrix[0].size()+

【leetcode】Maximal Rectangle (hard)★

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 找到01矩形中最大的全1子矩阵. 我自己的思路: 我先用一个跟输入相同大小的矩阵numInCol 存储从当前位置开始向下有多少连续的1. 如 1 0 1 0 1 1 1 1 1 其numInCol 是 1 0 3 0 2 2 1 1 1 然后用一个变量tmpans

LeetCode – Refresh – Maximal Rectangle

This is the extension of Largest Rectangle in Histogram. We can just project 2D matrix to 1D array and compute it line by line. 1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char> > &matrix) { 4 if (matrix.size() == 0)