Skip to content
Problem 174 · LeetCode 174(opens in a new tab)

Dungeon Game

HardDynamic programming

A knight starts in the top-left room of an m × n grid called dungeon and must reach a princess in the bottom-right room. Each step goes one room right or one room down. Entering a room changes the knight's health by the number in it: a negative number is a monster, a positive one is a potion, and 0 does nothing. The first and last rooms count too.

If the knight's health ever falls to 0 or lower, he dies on the spot. Return the smallest positive starting health that lets him reach the princess along some path.

Example 1

Input:  dungeon = [[-3, 5, -4], [-6, -8, 2], [4, -2, -1]]
Output: 4
Why:    right, right, down, down: health goes 4 → 1 → 6 → 2 → 4 → 3
2 more examples and the constraints

Example 2

Input:  dungeon = [[5]]
Output: 1
Why:    the knight must start alive, with at least 1 health

Example 3

Input:  dungeon = [[0, -3, 2, -4]]
Output: 6
Why:    6 → 6 → 3 → 5 → 1; starting with 5 would end at 0

Constraints

  • 1 ≤ m, n ≤ 200
  • −1,000 ≤ dungeon[r][c] ≤ 1,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Recursion with memoO(m·n)
  2. DP table, from the end backO(m·n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 200 × 200 dungeonVerdict
Recursion with memoO(m·n)
Passes; deep recursion
DP table, from the end backO(m·n)
Best; two plain loops

m = rows, n = columns. Trying every path would be about C(m + n, m) paths.

Edge cases to test

  • One room. [[5]] needs 1, [[-1000]] needs 1,001: you must end alive, with at least 1.
  • Only potions. The answer is still 1, never 0 or less.
  • Biggest sum is not safest. [[0, -5, 10], [-1, -1, -1]]: the path through 10 has the largest total but dips to −5 first. Going down first needs only 4.
  • A monster in the last room. [[2, 3], [1, -9]]: the princess's room counts, and the knight must survive it.

Common mistakes

  • Filling the table forward from the start, which cannot tell a path with a high total from one that stays alive.
  • Leaving out max(1, …), so a big potion makes the need 0 or negative.
  • Forgetting that the first and last rooms change health too.

How to talk through it

  1. 1Clarify. Do the first and last rooms count? Must health stay at least 1 at every moment?
  2. 2Say why forward fails. Two numbers matter going forward, the current health and the lowest point, and they trade off.
  3. 3Flip the direction. From the end back, one number per room is enough: the least health needed to enter it.
  4. 4Code the table. Pad with infinity, seed 1 next to the princess, and take max(1, min(down, right) − value).
  5. 5Test out loud. Run a single potion room, a single monster room, and the case where the largest sum is not the safe path.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?