Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions C++/dungeon-game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//https://leetcode.com/problems/dungeon-game/
////Difficulty Level: Hard
//Tags: Dynamic Programming
//Time complexity: O(m*n)
//Space complexity: O(m*n)
//bottom-up DP approach
class Solution
{
public:
int calculateMinimumHP(vector<vector<int>>& dungeon)
{
int m = dungeon.size();
if(r==0) //empty dungeon
return 0;

int n = dungeon[0].size();

//dp[i][j] --> min health req to reach the princess with starting cell as (i,j) -1
vector<vector<int> > dp(r, vector<int>(c));

for (int i = m-1; i>=0; i--) //traversing the array from bottom
{
for (int j = n-1; j>=0; j--)
{
//if starting from last cell,
//if value at last cell is -ve, health req. is 1+abs(value)
//if value at last cell is +ve, health req. is 0+1
if (i == m-1 && j == n-1)
{
dp[i][j] = min(0, dungeon[i][j]);
}

//if starting from last row,
//total health req. is sum of curr cell value and health req. at next cell
//if the sum is +ve, health req. is 0+1
//if the sum is -ve, health req. is 1+abs(sum)
else if (i == m-1)
{
dp[i][j] = min(0, dungeon[i][j]+dp[i][j+1]);
}

//if starting from last column,
//total health req. is sum of curr cell value and health req. at next cell
//if the sum is +ve, health req. is 0+1
//if the sum is -ve, health req. is 1+abs(sum)
else if (j == n-1)
{
dp[i][j] = min(0, dungeon[i][j]+dp[i+1][j]);
}

//if starting from any other cell,
//make a choice to go to the cell with less req. health(more positive dp value) after the curr cell
//the req. health is either 0 or sum of the curr cell value and health req. at next chosen cell
else
{
dp[i][j] = min(0, dungeon[i][j]+max(dp[i][j+1],dp[i+1][j]));
}
}
}
//actual starting point is (0,0), so return abs(dp(0,0))+1
//1 is added because the knight needs to have atleast 1 health to survive, else he will die
return abs(dp[0][0])+1;
}
};
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 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) |
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Java](./Java/LRU-Cache.java) | | | Medium | | |
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](./Python/contains-duplicate.py) | _O(n)_ | _O(n)_ | | |
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) |
[C++](./C++/brick-walls.cpp)| _O(n)_ | _O(n)_ | Medium | |
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [C++](./C++/brick-walls.cpp)| _O(n)_ | _O(n)_ | Medium | |

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


<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down