174. Dungeon Game Leetcode Python
来源:互联网 发布:定额编号软件 编辑:程序博客网 时间:2024/06/01 08:39
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
.
- 这道题是二维动态规划问题,关键在于构造转移方程。 首先要求每一步移动过程中knight的hp要大于0,其次要求最小。
- 所以我们可以构造一个MxN 的矩阵DP,用来记录移动过程。其转移方程为 dp[row][col]=max(1,min(dp[row+1][col],dp[row][col+1])-dungeon[row][col]) 空间复杂度和时间复杂度都为 O(mn).这题和前面的求path的两题很类似。
- This is a 2D dynamic planning problem, we need to make HP greater than 0 in every step. And we greedy search from the bottom.
- The time and space complexity in this problem are both O(mn)
- The code is as blow
class Solution: # @param dungeon, a list of lists of integers # @return a integer def calculateMinimumHP(self, dungeon): m=len(dungeon) n=len(dungeon[0]) dp=[[0 for index in range(n)] for index in range(m)] dp[m-1][n-1]=max(1,1-dungeon[m-1][n-1]) for row in reversed(range(m-1)): dp[row][n-1]=max(1,dp[row+1][n-1]-dungeon[row][n-1]) for col in reversed(range(n-1)): dp[m-1][col]=max(1,dp[m-1][col+1]-dungeon[m-1][col]) for row in reversed(range(m-1)): for col in reversed(range(n-1)): dp[row][col]=max(1,min(dp[row+1][col],dp[row][col+1])-dungeon[row][col]) return dp[0][0]
- 174. Dungeon Game Leetcode Python
- [Leetcode]174. Dungeon Game @python
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- leetCode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- [LeetCode]problem 174. Dungeon Game
- LeetCode | 174. Dungeon Game (Hard)
- JSP的application对象,请求转发和重定向的区别
- 优秀软件Agisoft PhotoScan Professional
- 現成成为一位seo专业谜团怎么现实
- Java排序算法之归并排序
- Android之Adapter的用法
- 174. Dungeon Game Leetcode Python
- 3674: 可持久化并查集加强版 可持久化线段树
- Unable to determine simulator device(无法确定设备模拟器)
- canada goose chilliwack parka-678035
- 互联网企业与传统企业思维的本质区别
- Java排序算法:希尔排序(Shell排序)
- MyEclipse优化启动 去除不需要加载的模块 关掉没用的启动项 修改myeclipse.ini
- 让人想死的中文编码有关问题
- jackwills outlet 1jRF428jzt