DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode

Posted 鍋氭5澶ф爲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode相关的知识,希望对你有一定的参考价值。

涓€瀹氳璁ょ湡鐪嬪畬杩欑瘒鏂囩珷鉁?/p>

澶ф爲涓嶆暍淇濊瘉鐪嬪畬浣犲氨鍙帉鎻″姩鎬佽鍒掞紝浣嗘槸锛屼綘涓€瀹氬彲浠?AC 鍔ㄦ€佽鍒掍腑鐨勮矾寰勯棶棰橈紒锛侌煒?/p>

鐢变簬绡囧箙闄愬埗涔熶负浜嗕笉璁╁ぇ瀹朵骇鐢熼槄璇荤柌鍔筹紝980. 涓嶅悓璺緞 III 杩欓亾棰樼洰浼氬崟鐙啓涓€绡囦綔涓鸿矾寰勯棶棰樼殑鏀跺熬绡囥€?/p>

鍔ㄦ€佽鍒掍腑鐨勮矾寰勯棶棰橈紝棰樼洰鏉ヨ嚜浜?LeetCode锛屽瓙鏍囬涓?棰樺彿 鍚嶇О 鐨勬牸寮忋€?/p>

62 涓嶅悓璺緞

涓€涓満鍣ㄤ汉浣嶄簬涓€涓?m x n 缃戞牸鐨勫乏涓婅 锛堣捣濮嬬偣鍦ㄤ笅鍥句腑鏍囪涓衡€淪tart鈥?锛夈€?/p>

鏈哄櫒浜烘瘡娆″彧鑳藉悜涓嬫垨鑰呭悜鍙崇Щ鍔ㄤ竴姝ャ€傛満鍣ㄤ汉璇曞浘杈惧埌缃戞牸鐨勫彸涓嬭锛堝湪涓嬪浘涓爣璁颁负鈥淔inish鈥濓級銆?/p>

闂€诲叡鏈夊灏戞潯涓嶅悓鐨勮矾寰勶紵

img

渚嬪锛屼笂鍥炬槸涓€涓? x 3 鐨勭綉鏍笺€傛湁澶氬皯鍙兘鐨勮矾寰勶紵

绀轰緥 1:

杈撳叆: m = 3, n = 2 杈撳嚭: 3 瑙i噴: 浠庡乏涓婅寮€濮嬶紝鎬诲叡鏈?3 鏉¤矾寰勫彲浠ュ埌杈惧彸涓嬭銆?/p>

  1. 鍚戝彸 -> 鍚戝彸 -> 鍚戜笅
  2. 鍚戝彸 -> 鍚戜笅 -> 鍚戝彸
  3. 鍚戜笅 -> 鍚戝彸 -> 鍚戝彸

绀轰緥 2:

杈撳叆: m = 7, n = 3 杈撳嚭: 28

鎻愮ず锛?/p>

  • 1 <= m, n <= 100
  • 棰樼洰鏁版嵁淇濊瘉绛旀灏忎簬绛変簬 2 * 10 ^ 9

鏍规嵁绉诲姩瑙勫垯锛屾垜浠彲浠ョ‘瀹氾紝鍦?[i, j] 浣嶇疆鐨勭偣锛屽彧鑳介€氳繃 [i-1, j] 浠ュ強 [i, j-1] 鍒拌揪

鐢变互涓婂垎鏋愶紝鎴戜滑鍙互鍐欏嚭 solution 浠g爜

Solution

class Solution {
    public int uniquePaths(int m, int n) {
        // 瀹氫箟dp鏁扮粍锛屾暟缁勪腑鐨勫€间唬琛ㄥ埌杈捐浣嶇疆鐨勮矾寰勬暟鐩?/span>
        int[][] dp = new int[m][n];
        // 鍒濆鍖栫涓€琛?amp;绗竴鍒楁暟鍊硷紝鍥犱负鍙兘鍚戜笅鍜屽悜鍙筹紝鎵€浠ュ€奸粯璁や负1
        for(int i = 0; i < m; i++){
            dp[i][0] = 1;
        }
        for(int j = 0; j < n; j++){
            dp[0][j] = 1;
        }
        // 鏍规嵁绉诲姩瑙勫垯锛屾垜浠彲浠ョ‘瀹氾紝鍦?nbsp;[i, j] 浣嶇疆鐨勭偣锛屽彧鑳介€氳繃 [i-1, j] 浠ュ強 [i, j-1] 鍒拌揪
        // 鎵€浠?nbsp;dp[i][j]  = dp[i-1][j] + dp[i][j-1]
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }

        return dp[m-1][n-1];
    }
}

瀵逛簬璇ヨВ鍐虫柟妗堬紝鍏舵椂闂村鏉傚害涓庣┖闂村鏉傚害涓猴細

  • 鏃堕棿澶嶆潅搴︼細O(m * n)
  • 绌洪棿澶嶆潅搴︼細O(m * n)

鎴戜滑鐭ラ亾锛岄€氬父瀵逛簬DP棰樼洰鐨勫垵濮嬭В娉曪紝鏄彲浠?span class="mq-86">瀵瑰叾绌洪棿澶嶆潅搴﹁繘琛屼紭鍖?/strong>鐨勶紝姣斿杩欓亾棰樼洰锛屾垜浠垰寮€濮嬬敤浜嗕簩缁存暟缁勬潵瀛樺偍锛屾墍浠ユ秷鑰楃┖闂翠负 O(m * n) 浣嗘垜浠畬鍏ㄥ彲浠ュ帇缂╀负涓€缁存暟缁勫瓨鍌紝涓嬮潰鎴戜滑灏辨潵灏濊瘯瀹炵幇涓€涓嬨€?/p>

  • 渚濈劧璁板緱锛岃鍒欙細璺緞鏁? 锛坕,j) = 锛坕-1,j) + 锛坕, j-1)

Solution路space-optimization

class Solution {
    public int uniquePaths(int m, int n) {
        int[] result = new int[m];
        // 鍒濆鍖栭琛屼俊鎭?/span>
        for(int i = 0; i < m; i++) result[i] = 1;
        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++){
                // 鏇存柊鏁扮粍锛屽綋鍓嶄綅缃?nbsp;= 鏇存柊鍚庣殑宸︿晶 + 鏇存柊鍓嶇殑涓婁竴浣?/span>
                result[j] += result[j-1];
            }
        }
        return result[m-1];
    }
}

63. 涓嶅悓璺緞 II

涓€涓満鍣ㄤ汉浣嶄簬涓€涓?m x n 缃戞牸鐨勫乏涓婅 锛堣捣濮嬬偣鍦ㄤ笅鍥句腑鏍囪涓衡€淪tart鈥?锛夈€?/p>

鏈哄櫒浜烘瘡娆″彧鑳藉悜涓嬫垨鑰呭悜鍙崇Щ鍔ㄤ竴姝ャ€傛満鍣ㄤ汉璇曞浘杈惧埌缃戞牸鐨勫彸涓嬭锛堝湪涓嬪浘涓爣璁颁负鈥淔inish鈥濓級銆?/p>

鐜板湪鑰冭檻缃戞牸涓湁闅滅鐗?/strong>銆傞偅涔堜粠宸︿笂瑙掑埌鍙充笅瑙掑皢浼氭湁澶氬皯鏉′笉鍚岀殑璺緞锛?/p>

img

缃戞牸涓殑闅滅鐗╁拰绌轰綅缃垎鍒敤 1 鍜?0 鏉ヨ〃绀恒€?/p>

璇存槑锛?/strong> m 鍜?n 鐨勫€煎潎涓嶈秴杩?100銆?/p>

绀轰緥 1:

杈撳叆: [ [0,0,0], [0,1,0], [0,0,0] ] 杈撳嚭: 2 瑙i噴: 3x3 缃戞牸鐨勬涓棿鏈変竴涓殰纰嶇墿銆備粠宸︿笂瑙掑埌鍙充笅瑙掍竴鍏辨湁 2 鏉′笉鍚岀殑璺緞锛?/p>

  1. 鍚戝彸 -> 鍚戝彸 -> 鍚戜笅 -> 鍚戜笅
  2. 鍚戜笅 -> 鍚戜笅 -> 鍚戝彸 -> 鍚戝彸

瀵逛簬 涓嶅悓璺緞 II 璇ラ鎴戜滑鍙互鍙戠幇锛岀浉瀵逛簬绗竴棰橈紝澶氫簡涓€涓檺鍒舵潯浠?鑰冭檻缃戞牸涓湁闅滅鐗?/strong>锛岄偅杩欎釜鏉′欢瀵逛簬鎴戜滑瑙e喅璇ラ鑰岃█鏈変粈涔堝惈涔夊憿锛?/p>

  • 杩欎釜鐐规棤娉曞埌杈?-> 鍒拌揪鐨勮矾寰勪负 0

鎵€浠ユ垜浠杩欎釜鏉′欢杩涜闄愬畾鍚庡氨杞彉涓哄悓绗竴棰樼浉鍚岀殑闂浜嗐€?/p>

Solution

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid == null){
            return 0;
        }

        int nr = obstacleGrid.length, nc = obstacleGrid[0].length;
        int[][] dp = new int[nr][nc];
        // 鍒濆鍖栬竟鐣屾潯浠?/span>
        for(int r = 0; r < nr; r++){
            // 鑻ヨ杈圭晫涓?锛屽嵆闅滅鐗╋紝鍒欎箣鍚庣殑鐐瑰潎涓嶅彲杈撅紝鐩存帴break
            if(obstacleGrid[r][0] == 1break;
            dp[r][0] = 1;
        }
        for(int c = 0; c < nc; c++){
            if(obstacleGrid[0][c] == 1break;
            dp[0][c] = 1;
        }
        // 寰幆濉厖鍐呴儴鏁版嵁
        for(int r = 1; r < nr; r++){
            for(int c = 1; c < nc; c++){
                if(obstacleGrid[r][c] == 1) {
                    // 闅滅鐗╋紝涓嶅彲鍒拌揪锛屾墍浠ョ洿鎺ュ~鍐欎负 0
                    dp[r][c] = 0
                    continue;
                }
                dp[r][c] = dp[r-1][c] + dp[r][c-1];
            }
        }
        return dp[nr-1][nc-1];
    }
}

鉁?Kill 鎺夎棰樹簡

瀵逛簬璇ヨВ鍐虫柟妗堬紝鍏舵椂闂村鏉傚害涓庣┖闂村鏉傚害鍚岀涓€棰樹紭鍖栧墠鐨勮В娉曠浉鍚屻€?/p>

濂斤紝涓嬭竟鎴戜滑鏉ヨ繘琛屼竴涓嬩紭鍖栵紝杩欎釜dp鐨勪紭鍖栧憿锛屾垜浠氨涓嶅啀鍘诲仛灏?O(m*n) 浼樺寲鍒?O(m) 鐨勭┖闂翠紭鍖栦簡銆傛垜浠彲浠ョ洿鎺ュ皢鍏惰繘琛屽師鍦扮殑dp锛屼紭鍖栧埌 O(1锛夌殑鏃堕棿澶嶆潅搴︺€?/p>

娉ㄦ剰浜嗭紒 馃槑

鏍规嵁棰樼洰涓紶閫掔殑鍙傛暟锛屾垜浠彲浠ュ彂鐜帮紝瀹冩湰韬氨鏄竴涓簩缁存暟缁勶紝鎵€浠ユ垜浠鎯冲湪瀹冭嚜韬繘琛岃鏁帮紝浠庤€岀渷鍘荤┖闂翠笂鐨勬秷鑰椼€?/p>

Solution路space-optimization

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid == null || obstacleGrid[0][0] == 1){
            // 鑻ュ乏涓婅涓洪殰纰嶇墿锛岀洿鎺ヨ繑鍥?
            return 0;
        }

        int nr = obstacleGrid.length, nc = obstacleGrid[0].length;
        // 鍒濆鍖栬竟鐣屾潯浠?/span>
        for(int r = 0; r < nr; r++){
            if(obstacleGrid[r][0] == 1){
                for(int i = r; i < nr; i++){
                    // 濡傛灉鍙戠幇涓€涓?锛屽垯灏嗗悗缁叏閮ㄧ疆涓?
                    obstacleGrid[i][0] = 0;
                }
                break;
            }
            obstacleGrid[r][0] = 1;
        }

        for(int c = 1; c < nc; c++){
            // 娉ㄦ剰杩欓噷鐨?c 鍒濆涓?1锛?nbsp;澶у鍙互鑰冭檻涓嬩负浠€涔堬紵
            if(obstacleGrid[0][c] == 1) {
                for(int j = c; j < nc; j++){
                    obstacleGrid[0][j] = 0;
                }
                break;
            }
            obstacleGrid[0][c] = 1;
        }
        // 寰幆濉厖鍐呴儴鏁版嵁
        for(int r = 1; r < nr; r++){
            for(int c = 1; c < nc; c++){
                if(obstacleGrid[r][c] == 1) {
                    obstacleGrid[r][c] = 0
                    continue;
                }
                obstacleGrid[r][c] = obstacleGrid[r-1][c] + obstacleGrid[r][c-1];
            }
        }
        return obstacleGrid[nr-1][nc-1];
    }
}

鍙互鐪嬪埌锛屽湪涓婅竟浠g爜涓紝鍒濆鍖栭鍒楁椂鐨勫垵濮嬫潯浠朵负 int c = 1; c < nc; c++锛屼负浠€涔堟槸浠?寮€濮嬪憿锛?/p>

鍏跺疄鏄洜涓烘垜浠湪鍒濆鍖栬鐨勬椂鍊欙紝灏?obstacleGrid[0][0] 鐨勪綅缃疆涓轰簡 1锛屾墍浠ュ鏋滄垜浠湪鍒濆鍒楁椂渚濇棫浠?寮€濮嬪氨浼氬皢宸︿笂瑙掔殑 1 鐪嬩綔涓洪殰纰嶇墿锛屼粠鑰屾棤娉曞埌杈俱€?/p>

浣嗘槸鍥犱负鍦ㄥ嚱鏁板紑濮嬫椂锛屾垜浠究杩涜浜嗗垽鏂?if(obstacleGrid == null || obstacleGrid[0][0] == 1)锛屾墍浠ュ乏涓婅鐨?1 蹇呯劧鏄垜浠慨鏀瑰悗鐨勶紝鑰岄潪闅滅鐗┿€?/p>

image-20200901224120987

缁忚繃杩欐浼樺寲锛屾渶缁堢┖闂村鏉傚害鍙湁甯搁噺绾у埆锛涘湪鍋氶鏃跺€欏彲浠ョ敤锛屼絾鏄鏋滆鐢ㄥ湪瀹為檯鐜涓笉寤鸿鐩存帴瀵瑰師鏁扮粍杩涜淇敼鍝 馃槈


濡傛灉浣犵湅鍒颁簡杩欙紝鎭枩浣犲張璇诲畬浜嗕竴绡囨枃绔狅紒

鑷虫鏈枃宸茬粡閫艰繎2000瀛椾簡锛屼负浜嗕繚璇佷笉浜х敓闃呰鐤插€︼紝璺緞闂鐨勬渶鍚庝竴涓?boss  980. 涓嶅悓璺緞 III 杩欓亾棰樼洰浼氬崟鐙啓涓€绡囦綔涓篋P璺緞闂鐨勫畬缁撶瘒銆?/p>


鐐逛釜鍦ㄧ湅鍚?馃憞馃憞


以上是关于DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode的主要内容,如果未能解决你的问题,请参考以下文章

WEKA浣跨敤鏁欑▼(缁忓吀鏁欑▼杞浇)

鍔ㄦ€佽鍒掕繘闃剁瘒1---鑳屽寘闂

娴呮瀽涓冪缁忓吀鎺掑簭绠楁硶

缁忓吀鍔熺巼璋变及璁″強Matlab浠跨湡

绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級

缁忓吀鎺掑簭绠楁硶鈥斺€斿爢鎺掑簭