The demons had captured the princess (P) 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 (K) was initially positioned in the top-left room and must fight his way through the 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, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0’s) or contain magic orbs that increase the knight’s health (positive integers).
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Write a function to determine the knight’s minimum initial health so that he is able to rescue the princess.
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
-2 (K) -3 3
-5 -10 1
10 30 -5 (P)
Notes:
The knight’s health has no upper bound.
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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
class Solution(object):
def calculateMinimumHP(self, dungeon):
""" :type dungeon: List[List[int]] :rtype: int """
m, n = len(dungeon), len(dungeon[0])
curv = [[0]*(n) for ind in range(m)]
minv = [[0]*(n) for ind in range(m)]
curv[0][0] = dungeon[0][0]
minv[0][0] = dungeon[0][0] if dungeon[0][0]<0 else 0
for ind in range(1,m):
curv[ind][0] = curv[ind-1][0]+dungeon[ind][0]
minv[ind][0] = min( [ minv[ind-1][0], min(curv[ind][0],0) ] )
for ind in range(1,n):
curv[0][ind] = curv[0][ind-1]+dungeon[0][ind]
minv[0][ind] = min( [ minv[0][ind-1], min(curv[0][ind],0) ] )
for ind1 in range(1,m):
for ind2 in range(1,n):
curv[ind1][ind2] = max( curv[ind1-1][ind2]+dungeon[ind1][ind2], curv[ind1][ind2-1]+dungeon[ind1][ind2] )
if minv[ind1-1][ind2]==0 or minv[ind1][ind2-1]==0:
minv[ind1][ind2] = curv[ind1][ind2] if curv[ind1][ind2]<0 else 0
else:
temp = -float('inf') if curv[ind1][ind2]>=0 else curv[ind1][ind2]
minv[ind1][ind2] = max( minv[ind1-1][ind2], minv[ind1][ind2-1], temp)
return -minv[-1][-1]+1 if minv[-1][-1]<0 else 1
It is wrong since I have no idea how to update curv list.
Time complexity : O(m*n)
space complexity: O(m*n)
Get idea from here1, here2, here3
reverse thinking is an efficient way for this problem.
We should maintain 2 2D array of the same size as the dungeon, where each elem represents the minimum health that guarantees the survivals of the knight for the rest of room.
if dungeon[i][j]<0, the knight must have a health greater than minimal value before entering this room in order to compensate for the health lost in this room. The minimum amount of compensate is -dungeon[i][j]
if dungeon[i][j]>0, the knight could enter room with a health as little as minimumvalue-dungeon[i][j], since he could gain dungeon[i][j] health in this room. However, the health point should never below 0. When this happens, we must clip the value to 1 in order to ensure knight is alive before entering room .
class Solution(object):
def calculateMinimumHP(self, dungeon):
""" :type dungeon: List[List[int]] :rtype: int """
m,n = len(dungeon), len(dungeon[0])
d = [[0]*n for ind in range(m) ]
d[m-1][n-1] = self.helper( 1 - dungeon[m-1][n-1] ) # the health point must be >0
for ind in range(m-2,-1,-1):
d[ind][n-1] = self.helper( d[ind+1][n-1] - dungeon[ind][n-1] )
for ind in range(n-2,-1,-1):
d[m-1][ind] = self.helper( d[m-1][ind+1] - dungeon[m-1][ind] )
for ind1 in range(m-2,-1,-1):
for ind2 in range(n-2,-1,-1):
d[ind1][ind2] = self.helper( min(d[ind1+1][ind2], d[ind1][ind2+1]) - dungeon[ind1][ind2] )
return d[0][0]
def helper(self, n):
return n if n>0 else 1
Optimization
time complexity: O(m*n)
space complexity: O(n)
class Solution(object):
def calculateMinimumHP(self, dungeon):
""" :type dungeon: List[List[int]] :rtype: int """
m,n = len(dungeon), len(dungeon[0])
d = [0]*n
d[n-1] = self.helper( 1 - dungeon[m-1][n-1] ) # the health point must be >0
for ind in range(n-2,-1,-1):
d[ind] = self.helper( d[ind+1] - dungeon[m-1][ind] )
for ind1 in range(m-2,-1,-1):
d[n-1] = self.helper( d[n-1] - dungeon[ind1][n-1] )
for ind2 in range(n-2,-1,-1):
d[ind2] = self.helper( min(d[ind2], d[ind2+1]) - dungeon[ind1][ind2] )
return d[0]
def helper(self, n):
return n if n>0 else 1