国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[leetcode]174. Dungeon Game

siberiawolf / 1555人閱讀

摘要:為了保證騎士可以最終到達,我們可以從終點逆向走到起點。為正數(shù)表示是藥水,騎士可以相應(yīng)降低初始血量。為負數(shù)表明要增加血量來保證存活。用二維空間來表示當前位置所需的最小血量,不斷向左上方走直到起點。用來表示當前層與下一層。

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms
laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0"s) or contain magic orbs that increase the knight"s health (positive integers).

In order to reach the princess as quickly as possible, the knight
decides to move only rightward or downward in each step.

本題為最小路徑。在搜索的時候,保持最小值至少為1。
為了保證騎士可以最終到達,我們可以從終點逆向走到起點。每到一個地方有兩種選擇,向上或者向左。
選擇需要初始血量最小的走法。matrix為正數(shù)表示是藥水,騎士可以相應(yīng)降低初始血量。為負數(shù)表明要增加血量來保證存活。
用二維空間來表示當前位置所需的最小血量,不斷向左上方走直到起點。因為一個點只有兩種走法,血量只與右邊和下邊有關(guān)。
我們可以減少空間到O(2n)。用來表示當前層與下一層。但是我們是逆向走,在dp[j+1]是已經(jīng)是當前層,而dp[j]還停留在下一層未更新。所以我們只需一個O(n)空間就行了。

time: O(n^2), space: O(n)

public class Solution {
    public int calculateMinimumHP(int[][] matrix) {
        if(matrix == null || matrix.length == 0) return 0;
        int m = matrix.length, n = matrix[0].length;
        int[] dp = new int[n+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[n-1] = 1;
        for(int i=m-1;i>=0;i--) {
            for(int j=n-1;j>=0;j--){
                dp[j] = Math.max(1 , Math.min(dp[j],dp[j+1]) - matrix[i][j]);
            }   // dp[j] means go down,  dp[j+1] means go right
        }
        return dp[0];
    }
}

time: O(n^2), space: O(n^2)

public class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length;
        int n = dungeon[0].length;
     
        //init dp table
        int[][] h = new int[m][n];
     
        h[m - 1][n - 1] = Math.max(1 - dungeon[m - 1][n - 1], 1);
     
        //init last col
        for (int i = m - 2; i >= 0; i--) {
                h[i][n - 1] = Math.max(h[i + 1][n - 1] - dungeon[i][n - 1], 1);
        }
     
        //init last row
        for (int j = n - 2; j >= 0; j--) {
                h[m - 1][j] = Math.max(h[m - 1][j + 1] - dungeon[m - 1][j], 1);
        }
     
        //calculate dp table
        for (int i = m - 2; i >= 0; i--) {
                for (int j = n - 2; j >= 0; j--) {
                        int down = Math.max(h[i + 1][j] - dungeon[i][j], 1);
                        int right = Math.max(h[i][j + 1] - dungeon[i][j], 1);
                        h[i][j] = Math.min(right, down);
                }
        }
     
        return h[0][0];
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66287.html

相關(guān)文章

  • 174. Dungeon Game

    摘要:題目解答這一題最重要的是把所剩血量初始化血量走這一步消耗的血量這句話讀懂。那么我們假設(shè)是走完后所剩余的血量肯定是大于等于的。如果想存活下來,最少需要上一步血量,即上一步血量然后分類討論上一步血量的可能性,注意邊界情況的初始化即可。 題目: The demons had captured the princess (P) and imprisoned her in the bottom-...

    wanglu1209 評論0 收藏0
  • [Leetcode] Dungeon Game 地牢游戲

    摘要:動態(tài)規(guī)劃復(fù)雜度時間空間遞歸棧思路騎士向右或者向下走,如果血量小于就死掉了,這會使得計算變得很復(fù)雜。假設(shè)表示點和的血量,表示走到和要扣除的血量。最右下角那個節(jié)點沒有待逆推的節(jié)點,所以我們假設(shè)其逆推節(jié)點的血量為。 Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-r...

    taoszu 評論0 收藏0
  • [Leetcode] Nim Game 尼姆游戲

    摘要:腦筋急轉(zhuǎn)彎復(fù)雜度時間空間思路這題往小說可以追溯到小學(xué)奧數(shù)或者腦筋急轉(zhuǎn)彎的書中,往大說可以深究到博弈論。代碼如果一開始就是的倍數(shù),你就輸了,因為對方可以用同樣的策略 Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each ...

    cartoon 評論0 收藏0
  • LeetCode[45] Jump Game II

    摘要:復(fù)雜度思路每次設(shè)置一個窗口,觀察在這一步下能到達的最遠距離,不斷的移動這個窗口。計數(shù),需要移動幾次,才能覆蓋到末尾的值。 LeetCode[45] Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Eac...

    keelii 評論0 收藏0
  • Leetcode PHP題解--D37 682. Baseball Game

    摘要:題目鏈接題目分析給定一個字符串數(shù)組,每一個字符串有以下形式數(shù)字。直接計算得分。。代表上一輪分數(shù)無效。思路這題沒什么好說的了。用區(qū)分各種情況,進行相應(yīng)處理即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 682. Baseball Game 題目鏈接 682. Baseball Game 題目分析 給定一個字符串數(shù)組,每一個字符串有以下形式: 數(shù)字。直接計算得分。 +。代表本輪...

    wzyplus 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<