174. Dungeon Game
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum initial health a knight needs to start with to travel from the top-left to the bottom-right of a dungeon. The knight's health changes based on the values in the dungeon rooms and must always remain above zero. Moving is restricted to rightward and downward steps.
Solution
Dungeon Game in Python
class Solution:
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
m, n = len(dungeon), len(dungeon[0])
# dp[j] stores the minimum health needed to enter cell (i, j).
# We use a 1D array of size n+1. dp[n] acts as a sentinel for the right boundary.
dp = [float('inf')] * (n + 1)
# To start from the cell to the right or below the princess's room,
# you need 1 health. We set dp[n-1] = 1. This is a trick to handle base case.
# For the last cell (m-1, n-1), needed will be min(dp[n-1], dp[n]) = min(1, inf) = 1.
dp[n - 1] = 1
# Iterate from bottom-right to top-left.
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
# Health needed after leaving (i,j) is the minimum required for the next step (right or down).
# dp[j] holds value from row i+1, dp[j+1] holds value for current row i.
needed = min(dp[j], dp[j + 1])
# Calculate health needed before entering (i,j).
# It must be at least 1, and enough to have 'needed' health after the room.
dp[j] = max(1, needed - dungeon[i][j])
return dp[0]
Dungeon Game in Java
import java.util.Arrays;
class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length;
int n = dungeon[0].length;
// dp[j] stores the minimum health needed to enter cell (i, j).
// We use a 1D array of size n+1. dp[n] acts as a sentinel for the right boundary.
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
// To start from the cell to the right or below the princess's room,
// you need 1 health. We set dp[n-1] = 1. This is a trick to handle base case.
// For the last cell (m-1, n-1), needed will be min(dp[n-1], dp[n]) = min(1, MAX_VALUE) = 1.
dp[n - 1] = 1;
// Iterate from bottom-right to top-left.
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
// Health needed after leaving (i,j) is the minimum required for the next step (right or down).
// dp[j] holds value from row i+1, dp[j+1] holds value for current row i.
int needed = Math.min(dp[j], dp[j + 1]);
// Calculate health needed before entering (i,j).
// It must be at least 1, and enough to have 'needed' health after the room.
dp[j] = Math.max(1, needed - dungeon[i][j]);
}
}
return dp[0];
}
}
Dungeon Game in C++
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon[0].size();
// dp[j] stores the minimum health needed to enter cell (i, j).
// We use a 1D array of size n+1. dp[n] acts as a sentinel for the right boundary.
vector<int> dp(n + 1, INT_MAX);
// To start from the cell to the right or below the princess's room,
// you need 1 health. We set dp[n-1] = 1, which will be used to calculate
// dp[n-1] for the last row. This is a trick to handle the base case cleanly.
// For the last cell (m-1, n-1), needed will be min(dp[n-1], dp[n]) = min(1, INT_MAX) = 1.
dp[n-1] = 1;
// Iterate from bottom-right to top-left.
for (int i = m - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
// Health needed after leaving (i,j) is the minimum required for the next step (right or down).
// dp[j] holds value from row i+1, dp[j+1] holds value for current row i.
int needed = min(dp[j], dp[j + 1]);
// Calculate health needed before entering (i,j).
// It must be at least 1, and enough to have 'needed' health after the room.
dp[j] = max(1, needed - dungeon[i][j]);
}
}
return dp[0];
}
};
Dungeon Game in TypeScript
function calculateMinimumHP(dungeon: number[][]): number {
const m = dungeon.length;
const n = dungeon[0].length;
// dp[j] stores the minimum health needed to enter cell (i, j).
// We use a 1D array of size n+1. dp[n] acts as a sentinel for the right boundary.
const dp = new Array(n + 1).fill(Infinity);
// To start from the cell to the right or below the princess's room,
// you need 1 health. We set dp[n-1] = 1. This is a trick to handle base case.
// For the last cell (m-1, n-1), needed will be min(dp[n-1], dp[n]) = min(1, Infinity) = 1.
dp[n - 1] = 1;
// Iterate from bottom-right to top-left.
for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
// Health needed after leaving (i,j) is the minimum required for the next step (right or down).
// dp[j] holds value from row i+1, dp[j+1] holds value for current row i.
const needed = Math.min(dp[j], dp[j + 1]);
// Calculate health needed before entering (i,j).
// It must be at least 1, and enough to have 'needed' health after the room.
dp[j] = Math.max(1, needed - dungeon[i][j]);
}
}
return dp[0];
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.