Watch 10 video solutions for Dungeon Game, a hard level problem involving Array, Dynamic Programming, Matrix. This walkthrough by Techdose has 49,643 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 grid representing a dungeon. Each cell contains a positive value (health gain) or negative value (damage). Starting at the top-left, you move only right or down to reach the bottom-right where the princess is located. The challenge is computing the minimum initial health required so the knight never drops to 0 or below at any step.
Approach 1: Dynamic Programming (Bottom-Up) (Time: O(m*n), Space: O(m*n))
The key insight: the minimum health required at a cell depends on the minimum health required for the next step (right or down). Instead of thinking forward from the start, compute the requirement backward from the princess. Create a DP table where dp[i][j] represents the minimum health needed when entering cell (i, j). Start from the bottom-right cell and compute the minimum health needed to survive that cell. For every other cell, choose the cheaper path between moving right or down, then subtract the dungeon value. Ensure the result is at least 1 since health cannot drop to zero. This backward dynamic programming formulation turns a tricky survival constraint into a simple state transition. The approach uses classic dynamic programming over a matrix, iterating through all cells exactly once.
Approach 2: Space Optimized Dynamic Programming (Time: O(m*n), Space: O(n))
The full DP table is not strictly required because each state depends only on the current row and the row below. Replace the 2D table with a 1D array representing the minimum health needed for each column in the next step. Iterate from the bottom row upward and update the array from right to left. At each cell, compute the required health using the minimum between the right move and the downward move already stored in the array. Subtract the dungeon value and clamp the result to at least 1. This keeps the same transition logic but reduces memory from O(m*n) to O(n). Space optimization like this is common in array-based DP problems where only adjacent states are needed.
Recommended for interviews: The bottom-up dynamic programming solution is what interviewers typically expect. It shows you recognize the backward dependency and can define the correct DP state. Mentioning the space-optimized version demonstrates deeper understanding of memory optimization and state compression, which is often discussed as a follow-up.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (2D Table) | O(m*n) | O(m*n) | Standard interview solution. Clear state definition and easiest to reason about. |
| Space Optimized Dynamic Programming | O(m*n) | O(n) | When memory usage matters or when discussing DP state compression. |