This solution beats 100% cpp coder.


  • 0
    G
    class Solution {
    public:
        int calculateMinimumHP(vector<vector<int>>& dungeon) {
            int prve=0;
            int m=dungeon.size();
            int n=dungeon[0].size();
            vector<int> x(n,0);
            for(int i=n-1;i>=0;i--){
                if(prve+dungeon[m-1][i]<0)
                    x[i]=prve+dungeon[m-1][i];
                    else x[i]=0;
                prve=x[i];
            }
            for(int j=m-2;j>=0;j--)
             for(int i=n-1;i>=0;i--){
                 if(i==n-1){
                     x[i]=min(0,x[i]+dungeon[j][i]);
                     prve=x[i];
                 }
                 else{
                     x[i]=min(0,max(prve,x[i])+dungeon[j][i]);
                     prve=x[i];
                 }
                 
             }
             return -x[0]+1;
        }
    };
    

    not very hard once you get the mian idea o(n) space


Log in to reply
 

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