1. 程式人生 > >演算法學習之動態規劃(leetcode 85. Maximal Rectangle)

演算法學習之動態規劃(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 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.
0x02解析

使用動態規劃的思想去解決這個問題,自己總結的動態規劃三要素:定義概念邊界初始化一般情況遞推,兩個難點是:角標索引問題

遞推情況剖析
cur_left定義為在一個字元陣列中,當前元素可以延伸到最左邊元素的下標(當前元素為0,則這個值為0)。如在字元陣列"0111001110",對第三個1,其cur_left=1,對最後一個0,其cur_left=0。其示意圖如下圖所示
這裡寫圖片描述
cur_right定義為在一個字元陣列中,當前元素可以延伸到最右邊元素的下標+1(當前元素為0,則這個值為字元陣列的長度)。如在字元陣列"0111001110",對第四個1,其cur_right=8+1,對第一個0,其cur_right=10,其示意圖如下圖所示
這裡寫圖片描述
總結cur_leftcur_right均由當前行的值來確定。如果當前值為'1',則cur_left
cur_right均不變;如果當前值為'0',則cur_left值為當前元素右側,cur_right值為當前元素位置。(左閉右開)
left[i][j]定義為在第i行第j列處,可以延伸到最左邊元素的下標。
right[i][j]定義為在第i行第j列處,可以延伸到最右邊元素的下標+1。
核心思路是從第一行開始一行一行地處理,使[i, j]處最大子矩陣的面積是(right(i, j)-left(i, j))*height(i, j)。其中height統計當前位置及往上'1'的數量;leftright是高度是當前點的height值的左右邊界,即是以當前點為中心,以height為高度向兩邊擴散的左右邊界。

left(i,j) = max(left(i-1,j), cur_left)

right(i,j) = min(right(i-1,j), cur_right)

if matrix[i][j]=='1', height(i,j) = height(i-1,j) + 1;
if matrix[i][j]=='0', height(i,j) = 0;

leftrightheight的值均可以通過前一行和當前行的值來確定,因此,逐行遍歷即可。

舉例說明。字元長方形如下:

0 0 0 1 0 0 0 
0 0 1 1 1 0 0 
0 1 1 1 1 1 0

leftl)、rightr)和heighth)的值如下所示

row 0:
    l: 0 0 0 3 0 0 0
    r: 7 7 7 4 7 7 7
    h: 0 0 0 1 0 0 0
row 1:
    l: 0 0 2 3 2 0 0
    r: 7 7 5 4 5 7 7 
    h: 0 0 1 2 1 0 0 
row 2:
    l: 0 1 2 3 2 1 0
    r: 7 6 5 4 5 6 7
    h: 0 1 2 3 2 1 0
0x03程式碼
public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0] == null) return 0;

        int m = matrix.length, n = matrix[0].length;
        int[] l = new int[n];
        int[] r = new int[n];
        int[] h = new int[n];
        int result = 0;

        for(int i = 0; i < n; i++){
            l[i] = 0;
            r[i] = n;
            h[i] = 0;
        }
        for(int i = 0; i < m; i++){
            int cur_left = 0, cur_right = n;
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == '1') h[j] += 1;
                else                    h[j] = 0;
//              System.out.print(h[j]);
//              System.out.print(" ");
            }
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == '1'){
                    l[j] = Math.max(l[j], cur_left);
                }
                else{
                    l[j] = 0;
                    cur_left = j + 1;
                }
//              System.out.print(l[j]);
//              System.out.print(" ");
            }
            for(int j = n-1; j >= 0; j--){
                if(matrix[i][j] == '1'){
                    r[j] = Math.min(r[j], cur_right);
                }
                else{
                    r[j] = n;
                    cur_right = j;
                }
//              System.out.print(r[j]);
//              System.out.print(" ");
            }
            for(int j = 0; j < n; j++){
                result = Math.max(result, (r[j] - l[j]) * h[j]);
            }
            System.out.println();
        }

        return result;
    }
}

參考 https://discuss.leetcode.com/topic/6650/share-my-dp-solution
參考 http://blog.csdn.net/makuiyu/article/details/44857479

相關推薦

演算法學習動態規劃(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

演算法學習動態規劃--數字三角形最大路徑和

題目: 7 3  8 8  1  0 2  7  4  4 4  5  2  6  5 在上面的數字三角形中尋找一條從頂部到底邊的路徑,使得路徑上所經過的數字之和最大。路徑上的每一步都只能往左下或右下走。只需要求出這個最大和即可,不必給出具體路徑。三角形的行數大於1小於

演算法學習動態規劃--最長公共子序列

題目: 給出兩個字串,求出這樣的一個最長的公共子序列的長度:子序列中的每個字元都能在兩個原串中找到,而且每個字元的先後順序和原串中的先後順序一致。 Sample Input : abcfbc abfc

五大常見演算法策略——動態規劃策略(Dynamic Programming

Dynamic Programming   Dynamic Programming是五大常用演算法策略之一,簡稱DP,譯作中文是“動態規劃”,可就是這個聽起來高大上的翻譯坑苦了無數人,因為看完這個演算法你可能會覺得和動態規劃根本沒太大關係,它對“動態”和“規劃”都沒有太深的體現。   舉個最簡單的例子去先淺顯

[演算法學習筆記]動態規劃鋼條切割問題

問題描述 有一個長度為n的鋼條需要切割成短鋼條出售,長度不同的鋼條售價也不同,如下: 長度i 1 2 3 4 5 6 7 8 9 10 價格p[i] 1 5 8 9 10 1

演算法動態規劃

含義     動態規劃(dynamic programming)是運籌學的一個分支,是求解決策過程(decision process)最優化的數學方法。 分類     動態規劃一般可分為線性動規,

演算法總結動態規劃(DP

適用動態規劃的特點 所解決的問題是最優化問題。 所解決的問題具有“最優子結構”。可以建立一個遞推關係,使得n階段的問題,可以通過幾個k<n階段的低階子問題的最優解來求解。 具有“重疊子結構”的特點。即,求解低階子問題時存在重複計算。 詞典法 大家都知道,遞迴演算法一般都存在大量的重複計算,這會造成不

演算法導論 動態規劃

作者:鄒祁峰 郵箱:[email protected] 日期:2014.03.07 18:00 轉載請註明來自"祁峰"的CSDN部落格 1 問題描述     現有兩條裝配線,Sij表示第i條上完成第j道工序的裝配站。汽車完成組裝需要依次完成1~n工序。請找出完

演算法動態規劃-01揹包問題

文字介紹解決揹包問題 假設山洞裡共有a,b,c,d ,e這5件寶物(不是5種寶物),它們的重量分別是2,2,6,5,4,它們的價值分別是6,3,5,4,6,現在給你個承重為10的揹包, 怎麼裝揹包,可以才能帶走最多的財富。 此時只要理解了狀態轉換方程f[i,j] = Max{ f[i-1,j-Wi]+Pi(

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 最大矩形 解題報告

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

C++ Leetcode初級演算法動態規劃

1.爬樓梯 假設你正在爬樓梯。需要 n 階你才能到達樓頂。 每次你可以爬 1 或 2 個臺階。你有多少種不同的方法可以爬到樓頂呢? 注意:給定 n 是一個正整數。 示例 1: 輸入: 2 輸出: 2 解釋: 有兩種方法可以爬到樓頂。 1.1 階 + 1 階 2.2 階 示

Leetcode詳解演算法 動態規劃(DP

因為最近一段時間接觸了一些Leetcode上的題目,發現許多題目的解題思路相似,從中其實可以瞭解某類演算法的一些應用場景。 這個隨筆系列就是我嘗試的分析總結,希望也能給大家一些啟發。 動態規劃的基本概念 一言以蔽之,動態規劃就是將大問題分成小問題,以迭代的方式求解。 可以使用動態規劃求解的問題