Skip to content

Latest commit

 

History

History
140 lines (72 loc) · 4.05 KB

0174._dungeon_game.md

File metadata and controls

140 lines (72 loc) · 4.05 KB

174. Dungeon Game

难度: Hard

刷题内容

原题连接

内容描述

The demons had captured the princess (P) 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 (K) was initially positioned in the top-left room and must fight his way through the 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, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).

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

 

Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.

For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.

-2 (K)	-3	3
-5	-10	1
10	30	-5 (P)
 

Note:

The knight's health has no upper bound.
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.

解题方案

思路 1 - 时间复杂度: O(row * col)- 空间复杂度: O(row * col)******

dp[i][j] 代表从dungeon[i][j]出发救出公主所需要的最少血量

如果我们知道了 dp[i+1][j], dp[i][j+1],那么dp[i][j]就知道了

  • 如果在cell dungeon[i][j]中能够加的血量足够进入下一个格子(即从dungeon[i][j]出来后还剩下的血量达到了从dp[i+1][j], dp[i][j+1]出发的最小血量),那么我们在进入dungeon[i][j]前只要血量够活就行,即1滴血
  • 如果在cell dungeon[i][j]中能够加的血量不能进入下一个格子(即从dungeon[i][j]出来后还剩下的血量没有达到从dp[i+1][j], dp[i][j+1]出发的最小血量),假设差k滴血,那么我们在进入dungeon[i][j]前就需要拥有差的那些血量,即k滴血

beats 42.18%

class Solution:
    def calculateMinimumHP(self, dungeon):
        """
        :type dungeon: List[List[int]]
        :rtype: int
        """
        if not dungeon:
            return 1
        
        row = len(dungeon)
        col = len(dungeon[0]) if row else 0
        
        dp = [[sys.maxsize] * (col+1) for i in range(row+1)]
        dp[-1][-2], dp[-2][-1] = 1, 1
        
        for i in range(row-1, -1, -1):
            for j in range(col-1, -1, -1):
                tmp = dungeon[i][j] - min(dp[i+1][j], dp[i][j+1]) # 加的血够不够继续走
                if tmp >= 0: # 够, 只要1滴血够活就行
                    dp[i][j] = 1
                else:       # 不够,初始就要这么多血
                    dp[i][j] = -tmp
                
        return dp[0][0]

思路 2 - 时间复杂度: O(row * col)- 空间复杂度: O(col)******

观察上一种思路的dp公式可发现,对于dp表中的任意一格,只依赖它右边和下面的格子,所以可以压缩整个dp表到一维从而达成线性空间复杂度。

beats 42.18%

class Solution:
    def calculateMinimumHP(self, dungeon):
        """
        :type dungeon: List[List[int]]
        :rtype: int
        """
        if not dungeon:
            return 1
        
        row = len(dungeon)
        col = len(dungeon[0]) if row else 0
        
        dp = [sys.maxsize] * (col-1) + [1, sys.maxsize]        
        for i in range(row-1, -1, -1):
            for j in range(col-1, -1, -1):
                tmp = dungeon[i][j] - min(dp[j], dp[j + 1]) 
                dp[j] = 1 if tmp >= 0 else -tmp
                
        return dp[0]