1. 程式人生 > >[LeetCode]*85.Maximal Rectangle

[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.

思路

這裡寫圖片描述

對於上圖的一個01矩陣。我們可以一行一行的分析,假設第三行,我們按列掃描,遇到0時,柱子斷開,重新形成柱子,遇到1時柱子高度加一。這樣的話,我們就可以把問題轉換為[LeetCode]*84.Largest Rectangle in Histogram求解最大矩形面積。

這裡寫圖片描述
程式碼

/*---------------------------------------
*   日期:2015-05-14
*   作者:SJF0115
*   題目: 85.Maximal Rectangle
*   網址:https://leetcode.com/problems/maximal-rectangle/
*   結果:AC
*   來源:LeetCode
*   部落格:
-----------------------------------------*/
#include <iostream> #include <stack> #include <vector> using namespace std; class Solution { public: int maximalRectangle(vector<vector<char>>& matrix) { int row = matrix.size(); if(row == 0){ return 0; }//if int col = matrix[0
].size(); vector<vector<int> > height(row,vector<int>(col,0)); // 計算每一行的高度 int h; for(int j = 0;j < col;++j){ h = 0; for(int i = 0;i < row;++i){ if(matrix[i][j] == '1'){ ++h; }//if
else{ h = 0; }//else height[i][j] = h; }//for }//for /*for(int i = 0;i < row;++i){ for(int j = 0;j < col;++j){ cout<<height[i][j]<<" "; } cout<<endl; }//for*/ // 計算以第i行為底的矩形面積 int maxArea = 0; for(int i = 0;i < row;++i){ maxArea = max(maxArea,MaxRectangle(height[i])); }//for return maxArea; } private: int MaxRectangle(vector<int> height){ int size = height.size(); if(size == 0){ return 0; }//if int maxArea = 0; stack<int> indexStack; int top,width; for(int i = 0;i < size;++i){ if(indexStack.empty() || height[i] >= height[indexStack.top()]){ indexStack.push(i); }//if else{ top = indexStack.top(); indexStack.pop(); width = indexStack.empty() ? i : (i - indexStack.top() - 1); maxArea = max(maxArea,height[top] * width); --i; }//else }//for while(!indexStack.empty()){ top = indexStack.top(); indexStack.pop(); width = indexStack.empty() ? size : (size - indexStack.top() - 1); maxArea = max(maxArea,height[top] * width); }//while return maxArea; } }; int main(){ Solution s; vector<vector<char> > matrix = { {'0','1','0','1','1'}, {'1','1','1','0','0'}, {'1','1','1','1','0'}, {'1','0','1','1','1'}, {'0','1','0','0','0'} }; cout<<s.maximalRectangle(matrix)<<endl; return 0; }

執行時間

這裡寫圖片描述

相關推薦

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: [

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的矩

[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. 思路 對於

演算法學習之動態規劃(leetcode 85. Maximal Rectangle

0x01題目 85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's

Leetcode 85. Maximal Rectangle 最大矩形 解題報告

1 解題思想 這道題我是轉化成上一道題來做的,對於每一行,看成給一個直方圖 2 原題 Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle contai

LeetCode 85. Maximal Rectangle(最大矩形)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones an

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: [

LeetCode 85.Maximal Rectangle (DP-專題)

題目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's a

19.2.23 [LeetCode 85] Maximal Rectangle

n) 當前 ret example -s hid turn 技術 style Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing only 1‘s a

[LeetCode] 85. Maximal Rectangle

ima http margin href cnblogs lan tex ali htm 1. 題目描述 2. 解題報告 此題解法可完全按照 [LeetCode] 84. Largest Rectangle in Histogra

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: Inpu

LeetCode85. 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"

LeetCode 85: Maximal Recetangle

rect etc angle color note ++ nbsp char leetcode Note: The lower one can cross other higher recetangle. Thus height[j] <= height[left/

85:Maximal Rectangle【陣列】【雜湊】【棧】【DP】

/*題意:給出包含0和1的矩陣,從圖中找出最大包含1的矩形的面積(全為1)*/ /** *思路: 可轉化成求“最大矩形面積” * 將每一列的連續的1想象成柱子,掃描每一列,記錄以當前元素 * 為柱子底端時柱子的高度。 * 掃描每一

LeetCode 85Maximal-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, gi

[Leetcode] maximal rectangle 最大矩形

with ++ leetcode 當前 push_back ont 更新 tor tco Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing all

Leetcode:Maximal Rectangle

open top private pro ray 數組 最大 所有 gif   題目大意:給一個由0和1組成r行c列的矩陣M,找出其中面積最大的由1組成的矩形。     這道題目和之前一道提供高度計算最大矩形的題目Largest Rectangle in Histogram

LeetCode】122.Maximal Rectangle

題目描述(Hard) Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. 題目連結 https://le

Maximal Rectangle -- LeetCode

原題連結:  http://oj.leetcode.com/problems/maximal-rectangle/   這是一道很綜合的題目,要求在0-1矩陣中找出面積最大的全1矩陣。剛看到這道題會比較無從下手,brute force就是對於每一個矩陣

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.