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.

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.



【题目分析】



【思路】

【java代码】

 1 public class Solution {
 2     public int maximalRectangle(char[][] matrix) {
 3         int row = matrix.length;
 4         if(row == 0) return 0;
 5         int col = matrix[0].length;
 6         if(col == 0) return 0;
 7
 8         int maxArea = 0;
 9
10         for(int i = 0; i < row; i++){
11             int[] height = new int[col];
12             for(int j = 0; j < col; j++){
13                 if(matrix[i][j] == ‘1‘){
14                     for(int k = i; k >=0; k--){
15                         if(matrix[k][j] == ‘1‘){
16                             height[j]++;
17                         }
18                         else break;
19                     }
20                 }
21             }
22             maxArea = Math.max(maxArea, largestRectangleArea(height));
23         }
24
25         return maxArea;
26     }
27
28     public int largestRectangleArea(int[] height) {
29         int len = height.length;
30         Stack<Integer> s = new Stack<Integer>();
31         int maxArea = 0;
32         for(int i = 0; i <= len; i++){
33             int h = (i == len ? 0 : height[i]);
34             if(s.isEmpty() || h >= height[s.peek()]){
35                 s.push(i);
36             }else{
37                 int tp = s.pop();
38                 maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
39                 i--;
40             }
41         }
42         return maxArea;
43     }
44 }
时间: 2024-12-23 23:22:10

85. Maximal Rectangle的相关文章

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"],

84. Largest Rectangle in Histogram *HARD* 柱状图求最大面积 85. Maximal Rectangle *HARD*

1. Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The large

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()+

[leedcode 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. public class Solution { public int maximalRectangle(char[][] matrix) { //利用上题的思路,先求出每一列的连续1个数,保存在DP二维数组中,然后对每一行进行findRectangle if(ma

【一天一道LeetCode】#85. Maximal Rectangle

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. (二)解题 题目大意:给定一个二值矩阵,计算矩阵里面包含1的所有子矩阵的最大面

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

[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