Skip to content

Commit f314454

Browse files
ir2010Chelcy-millenikaudaysrinu
authored
Dungeon Game.cpp added (#74)
* teemo-attacking.cpp added * Comments added * Comments added and README.md updated * Update Best-Time-to-Buy-and-Sell-Stock-II.cpp * number-of-recent-calls.cpp added * dungeon-game.cpp added * Update README.md * Unnecessary file deleted * Create longest arithmetic subsequence longest arithemetic subsequence leetcode * Revert "Update Best-Time-to-Buy-and-Sell-Stock-II.cpp" * Unnecessary files deleted * README.md updated * README.md fixed * README.md updated Co-authored-by: Chelcy-millenika <54374275+Chelcy-millenika@users.noreply.github.com> Co-authored-by: Gidijala uday srinu <55196913+udaysrinu@users.noreply.github.com>
1 parent 6ae9072 commit f314454

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

C++/dungeon-game.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//https://leetcode.com/problems/dungeon-game/
2+
////Difficulty Level: Hard
3+
//Tags: Dynamic Programming
4+
//Time complexity: O(m*n)
5+
//Space complexity: O(m*n)
6+
//bottom-up DP approach
7+
class Solution
8+
{
9+
public:
10+
int calculateMinimumHP(vector<vector<int>>& dungeon)
11+
{
12+
int m = dungeon.size();
13+
if(r==0) //empty dungeon
14+
return 0;
15+
16+
int n = dungeon[0].size();
17+
18+
//dp[i][j] --> min health req to reach the princess with starting cell as (i,j) -1
19+
vector<vector<int> > dp(r, vector<int>(c));
20+
21+
for (int i = m-1; i>=0; i--) //traversing the array from bottom
22+
{
23+
for (int j = n-1; j>=0; j--)
24+
{
25+
//if starting from last cell,
26+
//if value at last cell is -ve, health req. is 1+abs(value)
27+
//if value at last cell is +ve, health req. is 0+1
28+
if (i == m-1 && j == n-1)
29+
{
30+
dp[i][j] = min(0, dungeon[i][j]);
31+
}
32+
33+
//if starting from last row,
34+
//total health req. is sum of curr cell value and health req. at next cell
35+
//if the sum is +ve, health req. is 0+1
36+
//if the sum is -ve, health req. is 1+abs(sum)
37+
else if (i == m-1)
38+
{
39+
dp[i][j] = min(0, dungeon[i][j]+dp[i][j+1]);
40+
}
41+
42+
//if starting from last column,
43+
//total health req. is sum of curr cell value and health req. at next cell
44+
//if the sum is +ve, health req. is 0+1
45+
//if the sum is -ve, health req. is 1+abs(sum)
46+
else if (j == n-1)
47+
{
48+
dp[i][j] = min(0, dungeon[i][j]+dp[i+1][j]);
49+
}
50+
51+
//if starting from any other cell,
52+
//make a choice to go to the cell with less req. health(more positive dp value) after the curr cell
53+
//the req. health is either 0 or sum of the curr cell value and health req. at next chosen cell
54+
else
55+
{
56+
dp[i][j] = min(0, dungeon[i][j]+max(dp[i][j+1],dp[i+1][j]));
57+
}
58+
}
59+
}
60+
//actual starting point is (0,0), so return abs(dp(0,0))+1
61+
//1 is added because the knight needs to have atleast 1 health to survive, else he will die
62+
return abs(dp[0][0])+1;
63+
}
64+
};

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
216216
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | [Tutorial](https://www.youtube.com/watch?v=sbX1Ze9lNQE) |
217217
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Java](./Java/LRU-Cache.java) | | | Medium | | |
218218
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](./Python/contains-duplicate.py) | _O(n)_ | _O(n)_ | | |
219-
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) |
220-
[C++](./C++/brick-walls.cpp)| _O(n)_ | _O(n)_ | Medium | |
219+
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [C++](./C++/brick-walls.cpp)| _O(n)_ | _O(n)_ | Medium | |
221220

222221
<br/>
223222
<div align="right">
@@ -305,8 +304,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
305304
| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](./Python/edit-distance.py) | _O(N\*M)_ | _O(n^2)_ | Medium | Levenshtein Distance | |
306305
| 91 | [Decode ways](https://leetcode.com/problems/decode-ways/) | [Python](./Python/decode-ways.py) | _O(N)_ | _O(N)_ | Easy | DP | |
307306
| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [Python](./Python/divisor-game.py) | _O(N^2)_ | _O(N)_ | Easy | DP | |
307+
| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [C++](./C++/dungeon-game.pp) | _O(M*N)_ | _O(M*N)_ | Hard | Dynamic Programming | |
308308
| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | |
309309

310+
310311
<br/>
311312
<div align="right">
312313
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)