Skip to content

Latest commit

 

History

History

174

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

The demons had captured the princess 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 was initially positioned in the top-left room and must fight his way through 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 (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).

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

Return the knight's minimum initial health so that he can rescue the princess.

Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.

 

Example 1:

Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
Output: 7
Explanation: The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.

Example 2:

Input: dungeon = [[0]]
Output: 1

 

Constraints:

  • m == dungeon.length
  • n == dungeon[i].length
  • 1 <= m, n <= 200
  • -1000 <= dungeon[i][j] <= 1000

Companies:
Uber, Google, Adobe, Amazon

Related Topics:
Array, Dynamic Programming, Matrix

Similar Questions:

Solution 1.

Let dp[i][j] be the minimum health required at dungeon[i][j]. dp[i][j] should be at least 1.

We can compute it from dp[M - 1][N - 1] to dp[0][0] and dp[0][0] is the answer.

Let need be minimum health required in the previous step. need = min(dp[i + 1][j], dp[i][j + 1]).

If dungeon[i][j] < 0, dp[i][j] = need - dungeon[i][j] > 1.

If dungeon[i][j] >= 0, dp[i][j] = max(1, need - dungeon[i][j]).

So combining these two cases, dp[i][j] = max(1, need - dungeon[i][j]) for i in [0, M-1) and j in [0, N-1).

For the corner case i = M - 1 or j = N - 1, either dp[i + 1][j] or dp[i][j + 1] is nonexistent and we can treat it as Infinity.

If both of them are Infinity, i.e. i = M - 1, j = N - 1, dp[i][j] = max(1, 1 - dungeon[i][j]). So we can regard prev as 1.

So in sum:

dp[i][j] = max(1, (need === Infinity ? 1 : need) - dungeon[i][j])
           where need = min(dp[i + 1][j], dp[i][j + 1])
// OJ: https://leetcode.com/problems/dungeon-game
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(1)
class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& A) {
        int M = A.size(), N = A[0].size();
        for (int i = M - 1; i >= 0; --i) {
            for (int j = N - 1; j >= 0; --j) {
                int need = min(i + 1 < M ? A[i + 1][j] : INT_MAX, j + 1 < N ? A[i][j + 1] : INT_MAX);
                if (need == INT_MAX) need = 1;
                A[i][j] = max(1, need - A[i][j]);
            }
        }
        return A[0][0];
    }
};

Or, in case it's not allowed to change the input array.

// OJ: https://leetcode.com/problems/dungeon-game
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(N)
class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& A) {
        int M = A.size(), N = A[0].size();
        vector<int> dp(N + 1, INT_MAX);
        dp[N - 1] = 1;
        for (int i = M - 1; i >= 0; --i) {
            for (int j = N - 1; j >= 0; --j) {
                dp[j] = max(min(dp[j + 1], dp[j]) - A[i][j], 1);
            }
        }
        return dp[0];
    }
};