[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的時候有人說可以用在這題,看了一下還真是,以每行為x軸,每列往上累計的連續的1當成高度,就可以完全使用一樣的方法了。

 1 int largestArea(vector<int>height){
 2     stack<int> s;
 3     int maxArea = 0;
 4     int i = 0;
 5     height.push_back(0);
 6     int len = height.size();
 7 
 8     while (i < len){
 9         if (s.empty() || height[s.top()] < height[i]){
10             s.push(i++);
11         }else{
12             int t = s.top();
13             s.pop();
14             maxArea = max(maxArea, height[t] * (s.empty()? i : (i-s.top()-1)));
15         }
16     }
17     return maxArea;
18 }
19 
20 int maximalRectangle(vector<vector<char>> &matrix){
21     vector<int> height;
22     int maxRect=0;
23     for (int row=0; row<matrix.size(); row++){
24         for (int col=0; col<matrix[row].size(); col++){
25             if (matrix[row][col] == '0'){
26                 height.push_back(0);
27             }
28             else{
29                 int c=0;
30                 for (int i=row; i>-1; i--){
31                     if (matrix[i][col] != '0'){
32                         c++;
33                     }else {
34                         break;
35                     }
36                 }
37                 height.push_back(c);
38             }
39         }
40         
41         for (int i=0;i<height.size(); i++){
42             cout << height[i] << " ";
43         }
44         cout << endl;
45         
46         maxRect = max(maxRect, largestArea(height));
47         height.clear();
48     }
49     return maxRect;
50 }
51 
52 
53 int maximalRectangle2(vector<vector<char>> &matrix){
54     int maxRect = 0;
55     if (matrix.size() < 1) return 0;
56     vector<int>height(matrix[0].size(), 0);
57     for (int i=0; i<matrix.size(); i++){
58         for (int j=0; j<matrix[i].size(); j++){
59             if (matrix[i][j] == '1'){
60                 height[j] += 1;
61             }else{
62                 height[j] = 0;
63             }
64         }
65         maxRect = max(maxRect, largestArea(height));
66     }
67     return maxRect;
68 }

第一個maximalRectangle函數我用了很蠢的遍歷方法來獲得每行的height,這樣活生生的把原本O(n^2)搞成了O(n^3)。。。 最后看了別人博客,說height是可以通過前一行的值算出了的(有點類似DP的思想...如果這也算的話),豁然開朗,才寫出了

maximalRectangle2這個真正的O(n^2)方法。

轉載于:https://www.cnblogs.com/agentgamer/p/3695355.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377088.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377088.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377088.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

什么是alpha測試_什么是ALPHA?

什么是alpha測試Α (ALPHA) Alpha is the first and foremost letter of the Greek alphabet. In the classification of Greek numerals or numbers, it constitutes a value of 1. Alpha是希臘字母的第一個也是最重要的字母 。 在希臘數字或希臘數字的分類中&#xff0c;它的…

《leetcode : 647. 回文子串 思考分析雙指針解法》

647. 回文子串 如何確定是回文串&#xff1a; 找中心然后往兩邊擴散&#xff0c;判斷是否對稱即可。 在遍歷中心點的時候&#xff0c;注意中心點可以是一個元素也可以是兩個元素。 class Solution { public:int cal_two_extend(const string& s,int i,int j,int n){int re…

天草初級班(3)

算術運算指令算術運算指令是反映CPU計算能力的一組指令&#xff0c;也是編程時經常使用的一組指令。它包括&#xff1a;加、減、乘、除及其相關的輔助指令。 該組指令的操作數可以是8位、16位和32位(80386)。當存儲單元是該類指令的操作數時&#xff0c;該操作數的尋址方式可以…

4.3.3版本之引擎bug

bug描述&#xff1a;   IOS設備上&#xff0c;當使用WWW www WWW.LoadFromCacheOrDownload(url, verNum); 下載資源時&#xff0c;第一次下載某個資源&#xff0c;www.assetBundle必定為空。 解決辦法&#xff1a;   引擎版本降到4.3.2或者升到4.3.4或更高。 這個bug絕對是…

sml完整形式_411的完整形式是什么?

sml完整形式411&#xff1a;信息 (411: Information) 411 is an abbreviation of “Information". 411是“信息”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenger, a…

php 檢測用戶是否關閉瀏覽器

1、例子1 echo str_repeat(" ",3000);ignore_user_abort(true); mylog(online);while (true) {/** 1、程序正常結束 connection_status 0* 2、點擊瀏覽器“停止”按鈕 connection_status 1* 3、超時 connection_status 2*/echo "test<br>\n&qu…

explain用法

explain用法 EXPLAIN SELECT …… 變體&#xff1a; 1. EXPLAIN EXTENDED SELECT …… 將執行計劃“反編譯”成SELECT語句&#xff0c;運行SHOW WARNINGS 可得到被MySQL優化器優化后的查詢語句 2. EXPLAIN PARTITIONS SELECT …… 用于分區表的EXPLAIN 執行計劃包含的信息 id…

《位運算技巧以及Leetcode的一些位運算題目》

目錄技巧練習位運算[461. 漢明距離](https://leetcode-cn.com/problems/hamming-distance/)[190. 顛倒二進制位](https://leetcode-cn.com/problems/reverse-bits/)[136. 只出現一次的數字](https://leetcode-cn.com/problems/single-number/)[260. 只出現一次的數字 III](http…

linux讀取配置文件(C語言版)

一個通用的linux系統中C語言版讀取配置文件的函數。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <errno.h>#define KEYVALLEN 100/* 刪除左邊的空格 */ char * l_trim(char * szOutput, con…

java 范圍搜尋要怎么弄_搜索范圍

java 范圍搜尋要怎么弄Problem statement: 問題陳述&#xff1a; Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 給定一個以升序排列的整數nums數組&#xff0c;請找到給定目標值的開始和結束…

boa + ajax + cgi ajax請求cgi

最近公司要做一個通訊管理機,然后需要和另外一個同事一起做,我們需要用到boaAjaxCGI,以前沒試過與CGI交互,一開始發現問題挺大的,用ajax請求cgi,總是不返回數據,又或者請求回來的是cgi的源碼,后來發現,通過本地IIS或者直接打開html頁面請求的,返回來的都是cgi的源碼或者返回失敗…

《DBNotes:single_table訪問方法、MRR多范圍讀取優化、索引合并》

目錄single_table訪問方法constrefref_or_nullrangeindexallMRR多范圍讀取優化索引合并intersectionunionsort-unionsingle_table訪問方法 const 在主鍵列或者unique二級索引與一個常數進行等值比較時才有效。 如果主鍵或者unique二級索引的索引列由多個列構成&#xff0c;則…

怎樣通過命令管理Windows7桌面防火墻

&#xff08;1&#xff09;啟用桌面防火墻netsh advfirewall set allprofiles state on&#xff08;2&#xff09;設置默認輸入和輸出策略netsh advfirewall set allprofiles firewallpolicy allowinbound,allowoutbound以上是設置為允許&#xff0c;如果設置為拒絕使用blockin…

ruby推送示例_Ruby for循環示例

ruby推送示例for循環 (The for loop) In programming, for loop is a kind of iteration statement which allows the block to be iterated repeatedly as long as the specified condition is not met or a specific number of times that the programmer knows beforehand. …

《DBNotes: Buffer Pool對于緩沖頁的鏈表式管理》

目錄Buffer Pool回顧Buffer Pool內部組成freelistflushlistLRU鏈表管理以及改進Buffer Pool回顧 我們知道針對數據庫的增刪改刪操作都是在Buffer Pool中完成的&#xff0c;一條sql的執行步驟可以認為是這樣的&#xff1a; 1、innodb存儲引擎首先在緩沖池中查詢有沒有對應的數據…

一個延時調用問題

如果用下面第1行的寫法&#xff0c;調用 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector(removeFromSuperview) object:nil]; 可以生效 如果用下面第3行的寫法&#xff0c;調用 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:…

onclicklistener 方法使用匯總

相信很多像我一樣的新手學習ANDROID開發會遇到這個問題&#xff0c;通過這幾天的歸類和總結&#xff0c;將我的理解寫在下面&#xff0c;歡迎大家一起前來討論&#xff1a; 以按鈕BUTTON的監聽事件為例&#xff0c;以下的監聽實現都是等價的&#xff1a; 1.使用接口繼承按鈕監聽…

《源碼分析轉載收藏向—數據庫內核月報》

月報原地址&#xff1a; 數據庫內核月報 現在記錄一下&#xff0c;我可能需要參考的幾篇文章吧&#xff0c;不然以后還得找&#xff1a; MySQL 代碼閱讀 MYSQL開源軟件源碼閱讀小技巧 MySQL 源碼分析 聚合函數&#xff08;Aggregate Function&#xff09;的實現過程 MySQL …

vim中的jk為什么是上下_JK的完整形式是什么?

vim中的jk為什么是上下JK&#xff1a;開玩笑 (JK: Just Kidding) JK is an abbreviation of "Just Kidding". JK是“ Just Kidding”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceb…

百度歸來的學長做報告

今天下午下課到現在才總算閑下來&#xff0c;本來計劃這個時間應該是讀英語&#xff0c;做英語模擬題的時間&#xff0c;但是&#xff0c;我不得不寫點什么來記錄下剛才的事——在百度實習并且簽下工作的學長做報告。 原本認為每個人的成功&#xff08;請允許我目前的眼光簽個好…