The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).

To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

Return the knight's minimum initial health so that he can rescue the princess.

Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.

&nbsp;
Example 1:

Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
Output: 7
Explanation: The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-&gt; RIGHT -&gt; DOWN -&gt; DOWN.


Example 2:

Input: dungeon = [[0]]
Output: 1


&nbsp;
Constraints:


	m == dungeon.length
	n == dungeon[i].length
	1 &lt;= m, n &lt;= 200
	-1000 &lt;= dungeon[i][j] &lt;= 1000


class Solution:


	def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:

		dp = defaultdict(lambda: inf)
		dp[(len(dungeon), len(dungeon[0]) - 1)] = 1

		for i in range(len(dungeon) - 1, -1, -1):

			for j in range(len(dungeon[0]) - 1, -1, -1):

				dp[(i, j)] = min(dp[(i + 1, j)], dp[(i, j + 1)]) - dungeon[i][j]

				if dp[(i, j)] <= 0:
					dp[(i, j)] = 1

		return dp[(0, 0)]
class Solution {
    Integer[][] min;
    public int calculateMinimumHP(int[][] dungeon) {
        min = new Integer[dungeon.length][dungeon[0].length];
        int answer = min(0, 0, dungeon);
        return Math.max(answer, 1);
    }
    public int min(int i, int j, int[][] dungeon){
        if(i > dungeon.length - 1 || j > dungeon[0].length - 1) return 400000;
        if(i == dungeon.length - 1 && j == dungeon[0].length - 1) return - dungeon[i][j] + 1; 
        if(min[i][j] == null){
            int down = min(i + 1, j, dungeon);
            int right = min(i, j + 1, dungeon);
            min[i][j] = Math.min(Math.max(right, 1), Math.max(down, 1)) - dungeon[i][j];
        }
        return min[i][j];
    }
}
class Solution {
public:
    int solve(int i, int j, int m , int n, vector<vector<int>> &grid)
    {
        // if we come out of the grid simply return a large value
        if(i >= m || j >= n)
            return INT_MAX;
        
        // calucate health by the 2 possible ways
        int down = solve(i + 1, j, m, n, grid);
        int right = solve(i, j + 1, m, n, grid);
        
		// take the min both both
        int health = min(down, right);
        
        // we reach the destination when both the sides return INT_MAX
        if(health == INT_MAX)
        {
            health = 1; // both are +ve large integers so min health required = 1
        }
        
        int ans = 0;
        if(health - grid[i][j] > 0)
        {
            ans = health - grid[i][j];
        }
        else
        {
            ans = 1;
        }
             
        return ans;
    }
    
    int calculateMinimumHP(vector<vector<int>>& dungeon) 
    {
        int m = dungeon.size();
        int n = dungeon[0].size();
        
        return solve(0, 0, m, n, dungeon);
    }
};
/**
 * The dynamic programming solution.
 * 
 * Time Complexity:  O(m*n)
 * Space Complexity: O(1)
 * 
 * @param {number[][]} dungeon
 * @return {number}
 */
var calculateMinimumHP = function(dungeon) {
	const m = dungeon.length
	const n = dungeon[0].length

	const ii = m - 1
	const jj = n - 1

	for (let i = ii; i >= 0; i--) {
		for (let j = jj; j >= 0; j--) {
			if (i < ii || j < jj) {
				const hc = dungeon[i][j]

				const hp1 = (i < ii) ? Math.min(hc, hc + dungeon[i + 1][j]) : -Infinity
				const hp2 = (j < jj) ? Math.min(hc, hc + dungeon[i][j + 1]) : -Infinity

				dungeon[i][j] = Math.max(hp1, hp2)
			}
		}
	}

	return Math.max(1 - dungeon[0][0], 1)
}
Dungeon Game