Watch 10 video solutions for Dungeon Game, a hard level problem involving Array, Dynamic Programming, Matrix. This walkthrough by Techdose has 45,495 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.lengthn == dungeon[i].length1 <= m, n <= 200-1000 <= dungeon[i][j] <= 1000Problem Overview: You are given a 2D dungeon grid where each cell either damages or heals the knight. The knight starts at the top-left and must reach the bottom-right to rescue the princess, moving only right or down. The challenge is computing the minimum initial health required so the knight never drops to 0 or below at any point along the path.
Approach 1: Dynamic Programming from Bottom-Right (O(m*n) time, O(m*n) space)
The key insight is to work backward from the destination instead of forward from the start. At each cell, you compute the minimum health required to enter that cell so the knight can safely reach the princess. Create a DP table where dp[i][j] stores the minimum health needed before stepping into cell (i, j). The transition chooses the better of the two possible next moves: right or down. You calculate minHealthNeeded = min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j], then clamp it to at least 1 because health must always remain positive. Iterating from the bottom-right to the top-left ensures all dependencies are already computed. This approach directly models the survival constraint and works well for grid problems involving path decisions in a matrix with cumulative effects.
Approach 2: Space Optimized Dynamic Programming (O(m*n) time, O(n) space)
The 2D DP table can be reduced to a single row because each state depends only on the current row and the row below it. Use a 1D array where dp[j] represents the minimum health required to enter the current cell in that column. Iterate from the bottom row upward and from right to left within each row. Update each entry using the same recurrence: choose the smaller required health from the right or downward move, subtract the current dungeon value, and ensure the result is at least 1. This reduces memory usage from O(m*n) to O(n) while preserving the same transition logic. Space optimization like this is common in dynamic programming problems where each state only depends on a limited set of neighbors.
The difficulty comes from recognizing that forward simulation fails because you don't know how much health you must reserve for future damage. Reversing the perspective solves that uncertainty.
Recommended for interviews: The bottom-up dynamic programming solution is what most interviewers expect. It clearly shows understanding of grid-based array transitions and optimal substructure. The space-optimized version is a strong follow-up that demonstrates deeper mastery of DP state reduction.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (2D DP Table) | O(m*n) | O(m*n) | Best for understanding the full state transition clearly during interviews |
| Space Optimized Dynamic Programming | O(m*n) | O(n) | When memory usage matters or when demonstrating DP state compression |