Leetcode: maximal rectangle

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

Difficulty: 90 This is a very comprehensive question. We need to find the largest full 1 matrix in the 0-1 matrix. I can't start with this question. brute force is just a look at each matrix. There are a total of M (m + 1)/2 * n (n + 1) /2 sub-matrix (similar to a string sub-string, the number of sub-strings of a string is n (n + 1)/2, but here is a two-dimensional situation, so it is a multiplication of two ), the complexity is quite high. It is definitely not the answer that the interviewer wants.

After looking at the analysis on the Internet, we found that this question can be done on the basis of largest rectangle in histogram. Suppose we cut down the matrix along a certain row, then take the cut row as the bottom, and regard the matrix from the bottom to a histogram (histogram ). The height of each item in the histogram is the number of items starting from the bottom row to 1. Based on largest rectangle in histogram, we can find the current row as a maximum matrix at the bottom edge of the matrix. Next, if largest rectangle in histogram is performed for each row and the largest matrix is selected, it is the child matrix with the largest area in the entire matrix.

The basic idea of the algorithm has come out, and the rest is the problem of saving time and space.
How do we calculate the height of the histogram when a row is bottom? If re-calculation is performed, the calculated quantity is the current number of rows multiplied by the number of columns. However, here we will find some traces of dynamic planning. If we know the height of the histogram of the previous row, we only need to check whether the column element corresponding to the newly added row (bottom) is 0, if yes, the height is 0; otherwise, the height of the previous histogram is increased by 1. With historical information, we can update the height online. We know that the algorithm complexity of largest rectangle in histogram is O (n ). Therefore, the complexity of solving the matrix with a row as the bottom edge is O (n + n) = O (n ). Perform the following operation on each row. The total time complexity of the algorithm is O (M * n ).

In space, we only need to save the height O (n) of the previous histogram, plus the space O (n) used in largest rectangle in histogram ), so the total space complexity is O (n ).

Some small details on programming: if the values of the adjacent elements on the left and right of a row are equal, traverse to the right element (the element on the left is on the top of the stack), pop operations are required, rather than push

 1 public class Solution { 2     public int maximalRectangle(char[][] matrix) { 3         if (matrix==null || matrix.length==0 || matrix[0].length==0) return 0; 4         int[] height = new int[matrix[0].length]; 5         int maxArea = 0; 6          7         for (int row=0; row<matrix.length; row++) { 8             LinkedList<Integer> stack = new LinkedList<Integer>(); 9             for (int i=0; i<matrix[0].length; i++) {10                 if (matrix[row][i] != ‘0‘) height[i] = height[i] + 1;11                 else height[i] = 0;12             }13             for (int col=0; col<matrix[0].length; col++) {14                 if (stack.size()==0 || height[col]>height[stack.peek()]) {15                     stack.push(col);16                 }17                 else {18                     int temp = stack.pop();19                     int width = (stack.size()==0)? col : col-stack.peek()-1;20                     maxArea = Math.max(maxArea, width*height[temp]);21                     col--;22                 }23             }24             while (stack.size() != 0) {25                 int temp = stack.pop();26                 int width = (stack.size()==0)? height.length : height.length-stack.peek()-1;27                 maxArea = Math.max(maxArea, width*height[temp]);28             }29         }30         31         return maxArea;32     }33 }

 

Leetcode: maximal rectangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.