LeetCode-Maximal Rectangle[code]

code:

 1 #include <iostream>
 2 #include <vector>
 3 #include <stack>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 class Solution {
 8 public:
 9     int largestRectangleArea(vector<int> &height) {
10         height.push_back(0);
11         int i = 0;
12         int result = 0;
13         stack<int> s;
14         while (i < height.size())
15         {
16             if (s.empty() || height[i]>height[s.top()])
17                 s.push(i++);
18             else
19             {
20                 int tmp = s.top();
21                 s.pop();
22                 result = max(result, height[tmp] * (s.empty() ? i : i - s.top() - 1));
23             }
24         }
25         return result;
26     }
27
28     int maximalRectangle(vector<vector<char> > &matrix) {
29         if (matrix.size() == 0 || matrix[0].size() == 0) return 0;
30         int H = matrix.size(), W = matrix[0].size();
31         int ret = 0;
32
33         int **tmpHeight = new int*[H];
34         for (int i = 0; i < H; i++)
35         {
36             tmpHeight[i] = new int[W];
37         }
38
39         for (int i = 0; i < H; i++)
40         {
41             for (int j = 0; j < W; j++)
42             {
43                 if (matrix[i][j] == ‘0‘)
44                 {
45                     tmpHeight[i][j] = 0;
46                 }
47                 else
48                 {
49
50                     tmpHeight[i][j] = (i == 0 ? 1 : tmpHeight[i - 1][j] + 1);
51                 }
52             }
53         }
54         for (int i = 0; i < H; i++)
55         {
56             vector<int> vtmp(tmpHeight[i], tmpHeight[i] + W);
57             ret = max(ret, largestRectangleArea(vtmp));
58         }
59         return ret;
60     }
61 };
62
63 int main()
64 {
65     vector<vector<char>> v;
66
67     char a1[] = { ‘0‘, ‘1‘, ‘0‘, ‘1‘, ‘1‘};
68     char a2[] = { ‘0‘, ‘1‘, ‘1‘, ‘1‘, ‘0‘ };//要用‘1‘和‘0‘来赋值!因为是char数组!不能用1,0 !
69     char a3[] = { ‘1‘, ‘1‘, ‘1‘, ‘1‘,‘1‘ };
70     v.push_back(vector<char>(a1, a1 + 5));
71     v.push_back(vector<char>(a2, a2 + 5));
72     v.push_back(vector<char>(a3, a3 + 5));
73     Solution s;
74     cout << s.maximalRectangle(v) << endl;
75     return 0;
76     }
时间: 2024-08-05 02:19:49

LeetCode-Maximal Rectangle[code]的相关文章

LeetCode: Maximal Rectangle

LeetCode: 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. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] 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的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

[LeetCode] Maximal Rectangle(good)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang

LeetCode: Maximal Rectangle 解题报告

Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Show TagsHave you met this question in a real interview? Yes  NoDiscussSOLUTION 1: 1 public class Solution { 2 publ

Leetcode:Maximal Rectangle 最大全1子矩阵

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. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状

[leetcode]Maximal Rectangle @ Python [图解] [很难]

原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: 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 histogra

(每日算法)Leetcode --- Maximal Rectangle(最大子矩阵)

求在0-1矩阵中找出面积最大的全1矩阵 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 首先,想使用遍历两次的暴力方法解决是不靠谱的,先打消这个念头. 这道题的解法灵感来自于 Largest Rectangle in Histogram 这道题,假设我们把矩阵沿着某一行切下来,然后把切的行作为底面,将自底面往上

[LeetCode] 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. 此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用Largest Rectangle in Hist

【leetcode刷题笔记】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矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

Maximal Rectangle leetcode java

题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解: 这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决.解决方法是: 按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0.然后对每一行计算 Largetst Rectangle in H