|
| 1 | +""" |
| 2 | +First, we define an 2-D matrix `hp`. |
| 3 | +`hp[i][j]` is, when entering (i, j), the minimum health required, so that we can reach the princess. |
| 4 | +
|
| 5 | +So what is the value of `hp[i][j]`? |
| 6 | +At `(i, j)`, we always go to next place where its minimum health required is lowest. (Either `(i+1, j)` or `(i, j+1)`). |
| 7 | +And if the value is, for example, `-4` at `(i, j)` the requirement will need to add `4` (`- D[i][j]`). |
| 8 | +So the minimum requirement at (i, j) will become: |
| 9 | +``` |
| 10 | +min_required = min(hp[i+1][j], hp[i][j+1]) - D[i][j] |
| 11 | +``` |
| 12 | +Now if `min_required` is smaller or equal to `0`, it means that we don't need any requirement at all, set it to minimum, `1`. |
| 13 | +``` |
| 14 | +hp[i][j] = min_required if min_required>1 else 1 |
| 15 | +``` |
| 16 | +
|
| 17 | +Now we only need to work it backward so that we can deduct from the value we are sure. |
| 18 | +I add extra row and column to the `hp` and set the bottom and the right of the princess to `1`. |
| 19 | +so we don't need to look out for boundaries. |
| 20 | +``` |
| 21 | +hp[N][M-1] = 1 |
| 22 | +hp[N-1][M] = 1 |
| 23 | +``` |
| 24 | +
|
| 25 | +The time complexity is O(NM). Since we only traverse the 2-D matrix twice. |
| 26 | +One is for constructing `hp`. The second is calculate the value in `hp`. |
| 27 | +The space complexity is O(NM), too. |
| 28 | +
|
| 29 | +I learn my anser though [here](https://leetcode.com/problems/dungeon-game/discuss/52826/A-very-clean-and-intuitive-solution-(with-explanation)) which has an awesome explaination, too. |
| 30 | +""" |
| 31 | +class Solution(object): |
| 32 | + def calculateMinimumHP(self, D): |
| 33 | + N = len(D) |
| 34 | + M = len(D[0]) |
| 35 | + hp = [[float('inf')]*(M+1) for _ in xrange(N+1)] |
| 36 | + hp[N][M-1] = 1 |
| 37 | + hp[N-1][M] = 1 |
| 38 | + |
| 39 | + for i in reversed(xrange(N)): |
| 40 | + for j in reversed(xrange(M)): |
| 41 | + min_required = min(hp[i+1][j], hp[i][j+1]) - D[i][j] |
| 42 | + hp[i][j] = min_required if min_required>1 else 1 |
| 43 | + return hp[0][0] |
| 44 | + |
| 45 | + |
| 46 | +#Time Limit Exceed |
| 47 | +class Solution(object): |
| 48 | + def calculateMinimumHP(self, dungeon): |
| 49 | + def canPass(health_init): |
| 50 | + stack = [] |
| 51 | + |
| 52 | + stack.append((0, 0, health_init)) |
| 53 | + while stack: |
| 54 | + i, j, health = stack.pop() |
| 55 | + health_left = health+dungeon[i][j] |
| 56 | + if health<0 or health_left<0: continue |
| 57 | + if i==N-1 and j==M-1: return True |
| 58 | + if i+1<N: stack.append((i+1, j, health_left)) |
| 59 | + if j+1<M: stack.append((i, j+1, health_left)) |
| 60 | + |
| 61 | + return False |
| 62 | + |
| 63 | + |
| 64 | + N = len(dungeon) |
| 65 | + M = len(dungeon[0]) |
| 66 | + l = 0 |
| 67 | + h = 0 |
| 68 | + for row in dungeon: |
| 69 | + for v in row: |
| 70 | + if v<0: |
| 71 | + h += -1*v |
| 72 | + while l<h: |
| 73 | + ans = (l+h)/2 |
| 74 | + |
| 75 | + if canPass(ans): |
| 76 | + h = ans |
| 77 | + else: |
| 78 | + l = ans+1 |
| 79 | + return l+1 |
| 80 | + |
| 81 | +#Time Limit Exceed |
| 82 | +class Solution(object): |
| 83 | + def calculateMinimumHP(self, dungeon): |
| 84 | + stack = [] |
| 85 | + ans = float('inf') |
| 86 | + N = len(dungeon) |
| 87 | + M = len(dungeon[0]) |
| 88 | + |
| 89 | + stack.append((0, 0, 0, 0)) |
| 90 | + while stack: |
| 91 | + i, j, health_curr, health_need = stack.pop() |
| 92 | + health_curr += dungeon[i][j] |
| 93 | + if health_curr<0: |
| 94 | + health_need = max(health_need, -1*health_curr+1) |
| 95 | + if i==N-1 and j==M-1: |
| 96 | + ans = min(ans, health_need) |
| 97 | + if i+1<N: stack.append((i+1, j, health_curr, health_need)) |
| 98 | + if j+1<M: stack.append((i, j+1, health_curr, health_need)) |
| 99 | + return ans |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments