DDSA
Advertisement

174. Dungeon Game

Time: O(m*n)
Space: O(m*n)

Approach

Reverse DP from bottom-right to top-left. Each cell stores the minimum health required there; clamp to at least 1.

174.cs
C#
// Approach: Reverse DP from bottom-right to top-left. Each cell stores the
// minimum health required there; clamp to at least 1.
// Time: O(m*n) Space: O(m*n)

public class Solution
{
    public int CalculateMinimumHP(int[][] dungeon)
    {
        int m = dungeon.Length;
        int n = dungeon[0].Length;
        int[,] dp = new int[m + 1, n + 1];

        for (int i = 0; i <= m; i++)
        {
            for (int j = 0; j <= n; j++)
                dp[i, j] = int.MaxValue;
        }

        dp[m, n - 1] = 1;
        dp[m - 1, n] = 1;

        for (int i = m - 1; i >= 0; i--)
        {
            for (int j = n - 1; j >= 0; j--)
            {
                dp[i, j] = Math.Min(dp[i + 1, j], dp[i, j + 1]) - dungeon[i][j];
                dp[i, j] = Math.Max(dp[i, j], 1);
            }
        }

        return dp[0, 0];
    }
}
Advertisement
Was this solution helpful?