Simple python solution

  • 0
    class Solution(object):
    def calculateMinimumHP(self, d):
        :type dungeon: List[List[int]]
        :rtype: int
        m, n = len(d), len(d[0])
        dp = []
        dp.append(1 if d[m-1][n-1]>=0 else -d[m-1][n-1]+1)
        for i in range (n-2,-1, -1):
            dp.append( 1 if d[m-1][i]>=dp[-1] else dp[-1]-d[m-1][i])
        dp = dp[::-1]
        for i in range (m-2,-1,-1):
            dp[n-1] = 1 if d[i][n-1] >=dp[n-1] else dp[n-1]-d[i][n-1]
            for j in range (n-2, -1, -1):
                MIN = min(dp[j],dp[j+1])
                dp[j] = 1 if d[i][j] >=MIN else MIN-d[i][j]
        return dp[0]

Log in to reply

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