Just a refactored version


  • 0
    D

    Thanks for the solution. Here is more a refactored code in Java

    class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
    int m=dungeon.length, n=dungeon[0].length;
    int[][] health = new int[m][n];
    for(int r=m-1; r>=0; r--){
    for(int c=n-1; c>=0; c--){
    if(r==m-1 && c!=n-1) health[r][c] = Math.max(health[r][c+1] - dungeon[r][c], 1);
    else if(r!=m-1 && c==n-1) health[r][c] = Math.max(health[r+1][c] - dungeon[r][c], 1);
    else if(r==m-1 && c==n-1) health[r][c] = Math.max(1 - dungeon[r][c], 1);
    else health[r][c] = Math.min(Math.max(health[r][c+1] - dungeon[r][c], 1), Math.max(health[r+1][c] - dungeon[r][c], 1));
    }
    }
    return health[0][0];
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.