Skip to content

Commit db04cef

Browse files
authored
Create 174-dungeon-game.js
1 parent febb57f commit db04cef

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

174-dungeon-game.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[][]} dungeon
3+
* @return {number}
4+
*/
5+
const calculateMinimumHP = function(dungeon) {
6+
if(dungeon.length === 0) return 1
7+
const rows = dungeon.length
8+
const cols = dungeon[0].length
9+
10+
for(let i = rows - 1 ; i >= 0; i--) {
11+
for(let j = cols - 1; j >= 0; j--) {
12+
if(i==rows-1 && j==cols-1) dungeon[i][j]=Math.max(1, 1-dungeon[i][j]);
13+
else if(i==rows-1) dungeon[i][j]=Math.max(1, dungeon[i][j+1]-dungeon[i][j]);
14+
else if(j==cols-1) dungeon[i][j]=Math.max(1, dungeon[i+1][j]-dungeon[i][j]);
15+
else dungeon[i][j]=Math.max(1, Math.min(dungeon[i+1][j], dungeon[i][j+1])-dungeon[i][j]);
16+
}
17+
}
18+
return dungeon[0][0]
19+
};

0 commit comments

Comments
 (0)