InferenceAI

Enter at least two characters to search.

Start free

Dungeon Starting Health

hardDynamic Programming40 min50 XP

A knight starts at the top-left of dungeon and must reach the bottom-right, moving only right or down. Each cell adds its value to his health (negative values hurt). If his health drops to 0 or below at any point, he dies. Return the minimum starting health that guarantees survival.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[[-2,-3,3],[-5,-10,1],[10,30,-5]]]
TASKdungeon_starting_health()
EXPECTED OUTPUT7

Start with the input values shown in this example.

Example 1 of 3

Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]

Output: 7

Explanation: classic

Input: dungeon = [[0]]

Output: 1

Explanation: single empty cell

Input: dungeon = [[100]]

Output: 1

Explanation: potion

Hint 1

Work backwards from the princess: need[r][c] = max(1, min(need of right, need of below) - dungeon[r][c]).

Topics

DSADynamic ProgrammingPython