My short easy clean solution in java!!!!


  • 0
    D

    public class Solution {

    public int calculateMinimumHP(int[][] dungeon) {
        int m=dungeon.length;
        int n=dungeon[0].length;
        
        for(int i=m-1;i>=0;i--){
            for(int j=n-1;j>=0;j--){
                if(i==m-1&&j==n-1) {
                    dungeon[i][j]=1-dungeon[i][j];
                    if(dungeon[i][j]<1) dungeon[i][j]=1;
                }
                else if(i==m-1&&j!=n-1) {
                    dungeon[i][j]=dungeon[i][j+1]-dungeon[i][j];
                    if(dungeon[i][j]<1) dungeon[i][j]=1;
                }
                else if(i!=m-1&&j==n-1){
                    dungeon[i][j]=dungeon[i+1][j]-dungeon[i][j];
                    if(dungeon[i][j]<1) dungeon[i][j]=1;
                }
                else{
                    dungeon[i][j]=Math.min(dungeon[i][j+1],dungeon[i+1][j])-dungeon[i][j];
                    if(dungeon[i][j]<1) dungeon[i][j]=1;
                }
            }
        }
        return dungeon[0][0];
    }
    

    }


Log in to reply
 

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