174. Dungeon Game
来源:互联网 发布:华为手机usb共享网络 编辑:程序博客网 时间:2024/05/24 05:01
这道题是说,从左上角到右下角,每经过一个格子会增加血量或者减少血量,求到达右下角坐标还活着的话最少需要多少血量。
动态规划。从(m, n)反推回(0, 0)
dp[i][j]表示从(i, j)这个坐标到右下角的坐标最少需要多少血量。从右下角到左上角反着推。首先根据右下角可以推出最右边一列和最下面一行。然后可以得到中间的部分。
class Solution {public: int calculateMinimumHP(vector<vector<int>>& dungeon) { int m = dungeon.size(); int n = dungeon[0].size(); int dp[m][n]; dp[m - 1][n - 1] = max(1, -dungeon[m-1][n-1] + 1); for(int i = m - 2; i >= 0; --i){ dp[i][n-1] = max(1, dp[i+1][n-1] - dungeon[i][n-1]); } for(int j = n - 2; j >= 0; --j){ dp[m-1][j] = max(1, dp[m-1][j+1] - dungeon[m-1][j]); } for(int i = m - 2; i >= 0; --i){ for(int j = n - 2; j >= 0; --j){ dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]); } } return dp[0][0]; }};
阅读全文
0 0
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- 174. Dungeon Game
- leetCode 174. Dungeon Game
- 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- (算法分析Week9)Wildcard Matching[Hard]
- linux centos7 rhel7 虚拟机中怎么挂在卸载 光盘镜像 U盘
- 动态节点排序,jQ静态方法$.inArray, 剪切append,箭头函数 jq与dom对象互转
- 观察者模式
- 图论之拓扑排序基础
- 174. Dungeon Game
- RecyclerView设置背景图片长宽一样(以GridLayoutManager为例)
- hystrix构建高可用服务架构
- GCD的基础概念
- unity中通过射线用Button控制ui的出现
- css的初始化
- Ubuntu16.04下编译打包运行MapReduce Hadoop3.0.0-alpha4
- hibernate 查询语句中提示: “xxx”列无效
- 适配器与外观模式