Leetcode 174 Dungeon Game
来源:互联网 发布:云计算架构师招聘 编辑:程序博客网 时间:2024/05/21 18:47
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
.
Notes:
- 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.
简单DP,可以用滚动数组优化
class Solution { public: int calculateMinimumHP(vector<vector<int>>& dungeon) { int m = dungeon.size(); int n=dungeon[0].size(); vector<int> dp(n+1, INT_MAX); dp[n-1] = 1; for(int i = m-1; i>=0; i--) for(int j =n-1; j>=0; j--) dp[j] = max(min(dp[j], dp[j+1])-dungeon[i][j], 1); return dp[0]; } };
- 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(java)
- LeetCode 174 Dungeon Game 题解
- leetcode :Binary Search:Dungeon Game(174)
- Leetcode 174 - Dungeon Game(二分+dp)
- [leetcode] Dungeon Game
- [leetcode] Dungeon Game
- Leetcode Dungeon Game
- LeetCode Dungeon Game
- LeetCode: Dungeon Game
- 3DS MAX 学习笔记 一(阵列工具)
- java第十四天
- 专题八-二叉树
- Android Studio中实现AIDL
- Bootstrap Progress(进度条控件)
- Leetcode 174 Dungeon Game
- 为什么的sdk manager只显示已经安装package
- 【吐血整理】SVN命令行,Subversion的正确使用姿势,让版本控制更简单~
- 创建一维数组,长度为20,元素索引值为索引的二倍,奇数为负偶数为正,然后对数组排序
- java综合技术分享
- Linux学习1
- 彻底了解mapreduce核心Shuffle--解惑各种mapreduce问题
- GCC 设置部分代码的优化级别
- java第十七天