Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2b0120

Browse files
min_dp reverse
1 parent 61c34e0 commit d2b0120

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Source : https://leetcode.com/problems/dungeon-game/
2+
// Author : henrytien
3+
// Date : 2022年02月03日
4+
5+
/*****************************************************************************************************
6+
*
7+
* The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon.
8+
* The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially
9+
* positioned in the top-left room and must fight his way through dungeon to rescue the princess.
10+
*
11+
* The knight has an initial health point represented by a positive integer. If at any point his
12+
* health point drops to 0 or below, he dies immediately.
13+
*
14+
* Some of the rooms are guarded by demons (represented by negative integers), so the knight loses
15+
* health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic
16+
* orbs that increase the knight's health (represented by positive integers).
17+
*
18+
* To reach the princess as quickly as possible, the knight decides to move only rightward or downward
19+
* in each step.
20+
*
21+
* Return the knight's minimum initial health so that he can rescue the princess.
22+
*
23+
* Note that any room can contain threats or power-ups, even the first room the knight enters and the
24+
* bottom-right room where the princess is imprisoned.
25+
*
26+
* Example 1:
27+
*
28+
* Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
29+
* Output: 7
30+
* Explanation: The initial health of the knight must be at least 7 if he follows the optimal path:
31+
* RIGHT-> RIGHT -> DOWN -> DOWN.
32+
*
33+
* Example 2:
34+
*
35+
* Input: dungeon = [[0]]
36+
* Output: 1
37+
*
38+
* Constraints:
39+
*
40+
* m == dungeon.length
41+
* n == dungeon[i].length
42+
* 1 <= m, n <= 200
43+
* -1000 <= dungeon[i][j] <= 1000
44+
******************************************************************************************************/
45+
46+
#include "../inc/ac.h"
47+
class Solution
48+
{
49+
public:
50+
int calculateMinimumHP(vector<vector<int>> &dungeon)
51+
{
52+
int m = dungeon.size();
53+
int n = dungeon[0].size();
54+
55+
vector<int> dp(n + 1, INT_MAX);
56+
dp[n - 1] = 1;
57+
58+
for (int i = m - 1; i >= 0; i--)
59+
{
60+
for (int j = n - 1; j >= 0; j--)
61+
{
62+
int need = min(dp[j], dp[j + 1]) - dungeon[i][j];
63+
dp[j] = need <= 0 ? 1 : need;
64+
}
65+
}
66+
return dp[0];
67+
}
68+
};
69+
70+
int main()
71+
{
72+
// https://leetcode.com/problems/dungeon-game/discuss/52774/C%2B%2B-DP-solution
73+
vector<vector<int>> dungeon{{0, -3}};
74+
cout << Solution().calculateMinimumHP(dungeon) << "\n";
75+
return 0;
76+
}

‎leetcode/174.dungeon_game/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [174. Dungeon Game](https://leetcode.com/problems/dungeon-game/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /