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
.-2 (K) | -3 | 3 |
-5 | -10 | 1 |
10 | 30 | -5 (P) |
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.
A new LeetCode problem. 2D matrix, minimumHP, naturally, that is DP. The interesting part here is instead of solving from top-left, this time we need to solve from bottom right.
In the room where the princess(p) is imprisoned, the knight(k) needs at least 1 HP. As the note suggests, "Any room can contain threats or power-ups", so the minimumHP needed is 1 + dungeon[m -1][n - 1]. What if the room contains power-ups? That means the minimumHP k needs in that room is 1.
Note that if we need more HP, we need to add the points that will be deducted when fighting with the demons. And since that point is negative, we need to deduct that point. If there is a power-up, that number will be negative, and we take the minimum point needed, which is 1. Take the bottom right room as the example:
minHP[i][n - 1] = Math.max(minHP[i + 1][n - 1] - dungeon[i][n - 1], 1);
The boundary can be calculated by deducting dungeon[m - 1][j] (dungeon[i][n - 1]) from minHP[m - 1][j + 1] (minHP[ i + 1][n - 1]) and taking the minimum between the result and 1.
For the rooms in the middle, since k can only go right and down, we take the minimum between minHP[i + 1][j] and minHP[i][j + 1], i.e., get the minimum HP in order to enter the next room.
minHP[i][j] = Math.min(minHP[i + 1][j], minHP[i][j + 1]) - dungeon[i][j];
minHP[i][j] = (minHP[i][j] <= 0) ? 1 : minHP[i][j];
When we reach minHP[0][0], we get the minimum HP needed for k.
public int calculateMinimumHP(int[][] dungeon) { if (dungeon == null) throw new NullPointerException("Null dungeon...!"); if (dungeon.length == 0 || dungeon[0].length == 0) { System.out.println("Well, apparently there is no threat..."); return 0; } int m = dungeon.length; int n = dungeon[0].length; int[][] minHP = new int[m][n]; minHP[m - 1][n - 1] = Math.max(-dungeon[m - 1][n - 1] + 1, 1); for (int i = m - 2; i >= 0; i--) { minHP[i][n - 1] = Math.max(minHP[i + 1][n - 1] - dungeon[i][n - 1], 1); } for (int j = n - 2; j >= 0; j--) { minHP[m - 1][j] = Math.max(minHP[m - 1][j + 1] - dungeon[m - 1][j], 1); } for (int i = m - 2; i >= 0; i--) { for (int j = n - 2; j >= 0; j--) { minHP[i][j] = Math.min(minHP[i + 1][j], minHP[i][j + 1]) - dungeon[i][j]; minHP[i][j] = (minHP[i][j] <= 0) ? 1 : minHP[i][j]; } } return minHP[0][0]; }
I never like games, no, never...
can you please provide me a bottom approach for this problem. I have tried it but got stuck in few cases.
ReplyDeleteHi, Rajat. Bottom-up approach is not possible for this problem because the direction that we use to build (bottom-up or top-down) our solution matters in this case. This is due to the fact that we try to maintain an invariant of having minimum health at all times. If we switch the direction this invariant will be violated.
Delete