|
26 | 26 | * [done] HARD 295 Find Median from Data Stream https://leetcode.com/problems/find-median-from-data-stream |
27 | 27 | * MEDIUM 1631 Path With Minimum Effort https://leetcode.com/problems/path-with-minimum-effort/ |
28 | 28 |
|
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +```python= |
| 33 | +
|
| 34 | +import heapq |
| 35 | +class Solution: |
| 36 | + def minimumEffortPath(self, heights: List[List[int]]) -> int: |
| 37 | + # use dfs + heap |
| 38 | + # time analysis |
| 39 | + # operation of heappop and heappush: O(logH), where H is the size of heap |
| 40 | + # visited all heights: O(N), where N is the size of given heights (H*W) |
| 41 | + # overall: O(N)*O(logH) |
| 42 | + |
| 43 | + H, W = len(heights), len(heights[0]) |
| 44 | + hp = [(0,0,0)] # diff, i, j |
| 45 | + effort = 0 |
| 46 | + visited = set() |
| 47 | +
|
| 48 | + while hp: |
| 49 | + diff, i, j = heapq.heappop(hp) |
| 50 | + effort = max(effort, diff) |
| 51 | + print(i,j,heights[i][j], diff, hp) # log |
| 52 | + if i == H-1 and j == W-1: |
| 53 | + return effort |
| 54 | +
|
| 55 | + visited.add((i,j)) |
| 56 | + for a, b in [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]: |
| 57 | + if 0<=a<H and 0<=b<W and (a,b) not in visited: |
| 58 | + print(a,b) # log |
| 59 | + newDiff = abs(heights[a][b]-heights[i][j]) |
| 60 | + heapq.heappush(hp, (newDiff, a, b)) |
| 61 | + |
| 62 | + return effort |
| 63 | +``` |
| 64 | + |
| 65 | +```text |
| 66 | +[[1,2,2],[3,8,2],[5,3,5]] |
| 67 | +log |
| 68 | +0 0 1 0 [] |
| 69 | +1 0 |
| 70 | +0 1 |
| 71 | +0 1 2 1 [(2, 1, 0)] |
| 72 | +1 1 |
| 73 | +0 2 |
| 74 | +0 2 2 0 [(2, 1, 0), (6, 1, 1)] |
| 75 | +1 2 |
| 76 | +1 2 2 0 [(2, 1, 0), (6, 1, 1)] |
| 77 | +2 2 |
| 78 | +1 1 |
| 79 | +1 0 3 2 [(3, 2, 2), (6, 1, 1), (6, 1, 1)] |
| 80 | +2 0 |
| 81 | +1 1 |
| 82 | +2 0 5 2 [(3, 2, 2), (5, 1, 1), (6, 1, 1), (6, 1, 1)] |
| 83 | +2 1 |
| 84 | +2 1 3 2 [(3, 2, 2), (5, 1, 1), (6, 1, 1), (6, 1, 1)] |
| 85 | +1 1 |
| 86 | +2 2 |
| 87 | +2 2 5 2 [(3, 2, 2), (5, 1, 1), (6, 1, 1), (6, 1, 1), (5, 1, 1)] |
| 88 | +
|
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
29 | 94 | --- |
| 95 | +Y.J. Lee晚上8:14 |
| 96 | +比第一個 |
| 97 | +Y.J. Lee晚上8:26 |
| 98 | +用visited控制拜訪過的格子 |
| 99 | +每次拜訪新格子 |
| 100 | +一定是使用目前最小的effort |
| 101 | +拜訪新格子的時候 |
| 102 | +把新格子跟對應的新effort推進heap裡面 |
| 103 | +Y.J. Lee晚上8:30 |
| 104 | +總而言之就是 |
| 105 | +永遠選最好走的路 |
| 106 | +跟走的距離無關 |
| 107 | +Y.J. Lee晚上8:34 |
| 108 | +比較關鍵的地方 |
| 109 | +在更新visited 的時機 |
| 110 | +我覺得啦 |
| 111 | +Y.J. Lee晚上8:35 |
| 112 | +以題目給的example 3為例子 |
| 113 | +(3,1)會被加進去heap很多次 |
| 114 | +但是到終點都不會pop |
| 115 | +Y.J. Lee晚上8:37 |
| 116 | +你看(1,1)其實被加進去很多次 |
| 117 | +Y.J. Lee晚上8:39 |
| 118 | +222 走到底發現滿難走的 |
| 119 | +回頭走3那格 |
| 120 | +Y.J. Lee晚上8:41 |
| 121 | +這邊是無腦走看看 會產生困難的路線 |
| 122 | +困難的路線會跑到heap底部 |
| 123 | +yes |
| 124 | +Y.J. Lee晚上8:44 |
| 125 | +XD |
| 126 | +Y.J. Lee晚上8:47 |
| 127 | +要真的試著做做看比較能理解 |
| 128 | +Y.J. Lee晚上8:53 |
| 129 | +dijkstra 就是heap |
| 130 | +dijkstra 就是heap+dfs |
| 131 | +時間複雜度我po在comment |
| 132 | +Y.J. Lee晚上8:54 |
| 133 | +sry hackmd comment |
| 134 | +L37 |
| 135 | +嗯 worst case scenario |
| 136 | +Y.J. Lee晚上8:55 |
| 137 | +就很衰 走來走去都越走effort越高 |
| 138 | +Y.J. Lee晚上8:57 |
| 139 | +可能不是到所有的點啦 但是正比於面積 |
| 140 | +而且你走來走去搞不好同個點被加很多遍 |
| 141 | +Y.J. Lee晚上8:58 |
| 142 | +不會再走 可是會被加進去heap裡面 |
| 143 | +Y.J. Lee晚上8:59 |
| 144 | +visited最多就是存全部的點 所以確定是O(N) |
| 145 | +heap你沒辦法控制存多少東西進去 但是是正比於面積 所以heapsize ~= N |
0 commit comments