forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0174.py
21 lines (19 loc) · 770 Bytes
/
0174.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def calculateMinimumHP(self, dungeon):
"""
:type dungeon: List[List[int]]
:rtype: int
"""
row, col = len(dungeon), len(dungeon[0])
mem = [[0]*col for _ in range(row)]
for i in range(row-1,-1,-1):
for j in range(col-1,-1,-1):
if i == row-1 and j == col-1:
mem[i][j] = max(0, -dungeon[i][j])
elif i == row-1:
mem[i][j] = max(0, mem[i][j+1] - dungeon[i][j])
elif j == col-1:
mem[i][j] = max(0, mem[i+1][j] - dungeon[i][j])
else:
mem[i][j] = max(0, min(mem[i+1][j], mem[i][j+1]) - dungeon[i][j])
return mem[0][0] + 1