[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: Accepted
//	Runtime: 292 ms
//	Submitted: 0 minutes ago
	//时间复杂度O(n ^ 2), 空间复杂度 O(n)
    public int maximalRectangle(char[][] matrix) {
    	if(matrix.length == 0) {
        	return 0;
        }
    	int m = matrix.length;
    	int n = matrix[0].length;
        int[] H = new int[n];
        int[] L = new int[n];
        int[] R = new int[n];
        Arrays.fill(H, 0);
        Arrays.fill(L, 0);
        Arrays.fill(R, n);

        int ret = 0;
        for(int i = 0; i < m; ++i) {
        	int left = 0;
        	int right = n;

        	for (int j = 0; j < n; j++) {
				if (matrix[i][j] == '1') {
					++H[j];
					L[j] = Math.max(L[j], left);
				} else {
					left = j + 1;
					H[j] = 0;
					L[j] = 0;
					R[j] = n;
				}
			}

        	for (int j = n - 1; j >= 0; j --) {
				if (matrix[i][j] == '1') {
					R[j] = Math.min(R[j], right);
					ret = Math.max(ret, H[j] * (R[j] - L[j]));
				} else {
					right = j;
				}
			}
        }
        return ret;

    }
}    

华为2015机试题大概描述:

给定一个矩阵,里面只包含‘0’和‘1’,求出给最大的正方形的边长,该正方形里面全是‘1’

    public int maximalRectangle1(char[][] matrix) {
        if(matrix.length == 0) {
        	return 0;
        }
        int m = matrix.length;
    	int n = matrix[0].length;
        int[] H = new int[n];
        int[] L = new int[n];
        int[] R = new int[n];
        Arrays.fill(H, 0);
        Arrays.fill(L, 0);
        Arrays.fill(R, n);

        int maxLen = 0;
        for(int i = 0; i < m; ++i) {
        	int left = 0;
        	int right = n;

        	for (int j = 0; j < n; j++) {
				if (matrix[i][j] == '1') {
					++H[j];
					L[j] = Math.max(L[j], left);
				} else {
					left = j + 1;
					H[j] = 0;
					L[j] = 0;
					R[j] = n;
				}
			}

        	for (int j = n - 1; j >= 0; j --) {
				if (matrix[i][j] == '1') {
					R[j] = Math.min(R[j], right);
					maxLen = Math.max(maxLen, Math.min(H[j], R[j] - L[j]));
				} else {
					right = j;
				}
			}
        }
        return maxLen;
    }
时间: 2024-08-24 12:32:28

[LeetCode 85] Maximal Rectangle (华为2015机试)的相关文章

华为 2015 机试 输出:数字后面的连续出现的(2个或多个)相同字符(数字或者字符),删去一个,非数字后面的不要删除,例如,对应输出为:33aabb55pin。

1 package 华为机试; 2 //C++ 输入:由数字和字母组成的字符串,例如:333aaabb55ppin 3 //输出:数字后面的连续出现的(2个或多个)相同字符(数字或者字符),删去一个,非数字后面的不要删除,例如,对应输出为:33aabb55pin. 4 5 //这句话的核心就是在字符串删除一些字符,感觉处理很复杂,删除哪些字符呢?我们观察发现, 本字符串中删除了一个3,一个a,一个p,满足的规则是啥呢? 333中删除最后一个3,3aa删除了一个a,5pp中删除一个p, 6 //规

[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 [含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

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 85.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的矩阵,求所有为1组成的最大矩阵的面积. 此题能够巧妙转化为求最大直方图面积的问题. public class Solution { //其思想是将每一列的1逐行相加,遇0为0.遇1相加 //然后转化为求每一行的最大直方图面积的求解

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

19.2.23 [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

85. Maximal Rectangle Hard 161653FavoriteShare 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"],

2014年七月华为校招机试题目--最难的一道, 呵呵!

今天百无聊赖之时, 漫心看到14年的华为校招机试题目, 一共三道, 前两道皆是平平, 第三道却柳暗花明, 让人眼前一亮. 咋一看, 饶有趣味, 看似平淡无奇, 然而却玄机颇深(对我这种弱渣而言).(不过对于ACMer, 好像应该用基础算法, 就能解决!) (然而我也只会基础的算法!!忏愧的紧!!!).如果有幸被大神看到, 能指点我一两招, 不胜感激!  下面是题目和我的详细题解思路(可供巨巨一笑!嘿嘿!). 2014年七月华为校招机试题目: 第三题: 输入一个正整数X,在下面的等式左边的数字之间