-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-minimum-time-to-reach-last-room-i.py
34 lines (30 loc) · 1.24 KB
/
find-minimum-time-to-reach-last-room-i.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Time: O(n * m * logn(n * m))
# Space: O(n * m)
import heapq
# dijkstra's algorithm
class Solution(object):
def minTimeToReach(self, moveTime):
"""
:type moveTime: List[List[int]]
:rtype: int
"""
def dijkstra(start, target):
DIRECTIONS = [(1, 0), (0, 1), (-1, 0), (0, -1)]
dist = [[float("inf")]*len(moveTime[0]) for _ in xrange(len(moveTime))]
dist[start[0]][start[1]] = 0
min_heap = [(dist[start[0]][start[1]], start[0], start[1])]
while min_heap:
curr, i, j = heapq.heappop(min_heap)
if curr != dist[i][j]:
continue
if (i, j) == target:
break
for di, dj in DIRECTIONS:
ni, nj = i+di, j+dj
c = 1
if not (0 <= ni < len(moveTime) and 0 <= nj < len(moveTime[0]) and dist[ni][nj] > max(moveTime[ni][nj], curr)+c):
continue
dist[ni][nj] = max(moveTime[ni][nj], curr)+c
heapq.heappush(min_heap, (dist[ni][nj], ni, nj))
return dist[target[0]][target[1]]
return dijkstra((0, 0), (len(moveTime)-1, len(moveTime[0])-1))