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相加
    //然后转化为求每一行的最大直方图面积的求解
    //最后求出最大全为1的矩形面积
    public int maximalRectangle(char[][] matrix) {
    	//边界条件
    	if(matrix.length == 0 || matrix[0].length == 0){
    		return 0;
    	}

    	/**
    	 * 按列将每列的1逐行相加
    	 */
        for(int j = 0; j < matrix[0].length; j++){
            for(int i = 1; i < matrix.length; i++){
                if(matrix[i][j] != '0'){//不是0才相加。是0无论
                	matrix[i][j] = (char) (matrix[i-1][j] + 1);
                }
            }
        }
        int maxArea = 0;//最大矩形面积
        for(int i= 0; i < matrix.length; i++){
            maxArea = max(matrix[i],maxArea);//循环求最大
        }
        return maxArea;
    }

    /**
     * 依据每行。求最大直方图的面积
     * @param height char数组
     * @param maxArea 当前最大面积
     * @return
     */
    private int max(char[] height,int maxArea){
        if(height.length == 0){//为空直接返回
            return maxArea;
        }
        /**
         * 两个栈,分别存在高度和索引
         */
        Stack<Character> stHeight = new Stack<Character>();
        Stack<Integer> stIndex = new Stack<Integer>();
        /**
         * 遍历
         */
        for(int i = 0 ; i < height.length; i++){
        	//栈为空。或者高度比栈顶高度大,入栈
            if(stHeight.isEmpty() || height[i] > stHeight.peek()){
                stHeight.push(height[i]);
                stIndex.push(i);
            }else if(height[i] < stHeight.peek()){//高度比栈顶高度小
                int lastIndex = 0;//最后的索引值
                while(!(stHeight.isEmpty()) && height[i] < stHeight.peek()){
                    lastIndex = stIndex.pop();
                    int area = (stHeight.pop() - '0')*(i - lastIndex);//计算面积
                    maxArea = maxArea < area ? area:maxArea;
                }
                stHeight.push(height[i]);//当前值入栈
                stIndex.push(lastIndex);//最小索引入栈
            }
        }
        //假设栈不为空。继续计算
        while(!(stHeight.isEmpty())){
            int area = (stHeight.pop() - '0')*(height.length - stIndex.pop());
            maxArea = maxArea < area ? area:maxArea;
        }
        return maxArea;
    }
}

详细代码和思路例如以下:

时间: 2024-08-06 12:06:28

leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法的相关文章

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:螺旋数组,需

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

[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

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

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 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetCode 75.Sort Colors (颜色排序) 解题思路和方法

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl