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) return 0;
 5         int n = matrix.size(), m = matrix[0].size(), result = 0;
 6         vector<int> height(m, 0), left(m), right(m);
 7         for (int i = 0; i < n; i++) {
 8             for (int j = 0; j < m; j++) {
 9                 if (matrix[i][j] == ‘1‘) height[j]++;
10                 else height[j] = 0;
11             }
12             for (int j = 0; j < m; j++) {
13                 left[j] = j;
14                 while (left[j] > 0 && height[j] <= height[left[j]-1]) left[j] = left[left[j]-1];
15             }
16             for (int j = m-1; j >= 0; j--) {
17                 right[j] = j;
18                 while (right[j] < m-1 && height[j] <= height[right[j]+1]) right[j] = right[right[j]+1];
19             }
20             for (int j = 0; j < m; j++) {
21                 result = max(result, height[j]*(right[j] - left[j] + 1));
22             }
23         }
24         return result;
25     }
26 };
时间: 2024-12-15 00:24:56

LeetCode – Refresh – 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 [含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】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

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=

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