1. 程式人生 > >85:Maximal Rectangle【陣列】【雜湊】【棧】【DP】

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

/*題意:給出包含0和1的矩陣,從圖中找出最大包含1的矩形的面積(全為1)*/

/**
 *思路: 可轉化成求“最大矩形面積”
 *       將每一列的連續的1想象成柱子,掃描每一列,記錄以當前元素
 *     為柱子底端時柱子的高度。
 *       掃描每一行,求以當前行,為所有柱子底端時,求最大矩形面積
 */


class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int n = matrix.size();
        if(n == 0) return 0;
        int m = matrix[0].size();
        vector<vector<int>> height(n, vector<int>(m));
        //記錄1的高度
        for(int i = 0; i < m; i ++) {
            int h = 0;
            for(int j = 0; j < n; j ++) {
                if(matrix[j][i] == '1') h++;
                else h = 0; //不連續,高度置為0
                height[j][i] = h;
            }
        }

        //同求“最大矩形面積”
        int maxx = 0;
        for(int i = 0; i < n; i ++) {
            stack<int> s;
            s.push(-1);
            for(int j = 0; j < m; j++) {
                int pre = s.top();
                if(pre < 0 || height[i][pre] < height[i][j])
                    s.push(j);
                else {
                    s.pop();
                    maxx = max(maxx, height[i][pre]*(j-s.top()-1));
                    j--;
                }
            }
            while(s.top() != -1) {
                int pre = s.top();
                s.pop();
                maxx = max(maxx, height[i][pre]*(m-s.top()-1));
            }
        }
        return maxx;
    }
};


相關推薦

85:Maximal Rectangle陣列DP

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

C++模板字串

介紹:關於字串hash,一句話概括,就是把字串有效的轉化為一個整數 hash[i]=(hash[i-1]*p+idx(s[i]))%mod for example:取p=13, mod=101,求abc對應的整數 hash[0]=1; 表示a對映1。 hash[1]=(hash[0]

分散式一致性 Distributed Hash Table 學習筆記

目錄 一致性雜湊介紹 一致性雜湊原理 一致性雜湊應用 1、一致性雜湊介紹        一致性雜湊,英文:distributed hashtable, 簡稱DHT, 是麻省理工提出的一種演算法,該演算法可以有效解決分散式儲存結

一致性演算法

在瞭解一致性雜湊演算法之前,最好先了解一下快取中的一個應用場景,瞭解了這個應用場景之後,再來理解一致性雜湊演算法,就容易多了,也更能體現出一致性雜湊演算法的優點,那麼,我們先來描述一下這個經典的分散式快取的應用場景。 場景描述 假設,我們有三臺快取伺服器,用於快取圖片,我們為這三臺快取伺服器編號為0

P3370 模板字串(Hash詳解)

題目連結 題意: 給定N個字串(第i個字串長度為Mi,字串內包含數字、大小寫字母,大小寫敏感),請求出N個字串中共有多少個不同的字串。 單hash——模數19260817(80分) #include<iostream> #include<al

Supermarket POJ - 1456並查集+表思想+貪心

題目連結   原來,並查集還有這樣的作用——題記。   我想用個雜湊表的思維來解這道題,但是,顯然O(N^2)的雜湊表去查詢並插入顯然是不行的,那麼既然掛在圖論專題,我就得用相應的方式解答咯(要是不掛在圖論專題,我可能會自閉了),我們對於每個物品按照價值降序排列,

影象檢索--感知演算法

瞭解決上面方法對儲存空間和檢索時間的不切實際的要求,近年來近似最近鄰搜尋(approximate nearest neighbor search)技術發展迅猛,因為其對空間和時間的需求大幅降低,而且能夠得到不錯的檢索結果,因此成為了一種實用的替代方案。在這其中,雜湊(hashing)作為一種代表性方

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 only 1’s and return its area. Example: Inpu

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"

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

codewars打怪日記 Greed is Good JavaScript中陣列用法和 表的使用

codewars是一個線上程式設計網站,其獎勵機制像打怪升級。你不能檢視高於你級別的問題的答案。除非自己通過提交測試。通過提交之後可以看到各種解法排行榜 。通過對比自己解法和排行榜對比,可以找到差距,提高能力。      描述 : greed dice 是一個骰子游戲,使用