Skip to content

Commit ab456db

Browse files
author
root
committed
Added dungeon game leetcode
1 parent c96e7dd commit ab456db

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
3+
n = len(dungeon)
4+
m = len(dungeon[0])
5+
dp = [[0 for i in range(m)] for j in range(n)]
6+
dp[n-1][m-1] = 1 if dungeon[n-1][m-1]>0 else abs(dungeon[n-1][m-1])+1
7+
8+
9+
for i in range(n-1,-1,-1):
10+
for j in range(m-1,-1,-1):
11+
if i==n-1 and j==m-1:
12+
continue
13+
else:
14+
if i==n-1:
15+
dp[i][j] = max(dp[i][j+1]-dungeon[i][j],1)
16+
17+
elif j==m-1:
18+
dp[i][j] = max(dp[i+1][j]-dungeon[i][j],1)
19+
20+
else:
21+
dp[i][j] = max(min(dp[i+1][j],dp[i][j+1])-dungeon[i][j],1)
22+
# print(dp)
23+
24+
return dp[0][0]

0 commit comments

Comments
 (0)