-
Notifications
You must be signed in to change notification settings - Fork 112
/
0174-DungeonGame.cs
34 lines (28 loc) · 1.22 KB
/
0174-DungeonGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 25 MB
// Link: https://leetcode.com/submissions/detail/356829519/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0174_DungeonGame
{
public int CalculateMinimumHP(int[][] dungeon)
{
var rows = dungeon.Length;
var cols = dungeon[0].Length;
for (int i = rows - 1; i >= 0; i--)
for (int j = cols - 1; j >= 0; j--)
{
if (i == rows - 1 && j == cols - 1) continue;
int rightValue = int.MinValue, downValue = int.MinValue;
var current = dungeon[i][j];
if (i < cols - 1) rightValue = current > 0 ? current + dungeon[i + 1][j] : Math.Min(current, current + dungeon[i + 1][j]);
if (j < rows - 1) downValue = current > 0 ? current + dungeon[i][j + 1] : Math.Min(current, current + dungeon[i][j + 1]);
dungeon[i][j] = Math.Max(rightValue, downValue);
}
return dungeon[0][0] <= 0 ? 1 - dungeon[0][0] : 1;
}
}
}