|
| 1 | +class Solution(object): |
| 2 | + def minTimeToReach(self, moveTime): |
| 3 | + n = len(moveTime) |
| 4 | + m = len(moveTime[0]) |
| 5 | + dp = [[float('inf')] * m for _ in range(n)] |
| 6 | + minh = [] |
| 7 | + heapq.heappush(minh, (0, 0, 0)) |
| 8 | + moveTime[0][0] = 0 |
| 9 | + directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| 10 | + while minh: |
| 11 | + currTime, currRow, currCol = heapq.heappop(minh) |
| 12 | + if currTime >= dp[currRow][currCol]: |
| 13 | + continue |
| 14 | + if currRow == n - 1 and currCol == m - 1: |
| 15 | + return currTime |
| 16 | + dp[currRow][currCol] = currTime |
| 17 | + for dr, dc in directions: |
| 18 | + nextRow = currRow + dr |
| 19 | + nextCol = currCol + dc |
| 20 | + if 0 <= nextRow < n and 0 <= nextCol < m and dp[nextRow][nextCol] == float('inf'): |
| 21 | + nextTime = max(moveTime[nextRow][nextCol], currTime) + 1 |
| 22 | + heapq.heappush(minh, (nextTime, nextRow, nextCol)) |
| 23 | + return -1 |
0 commit comments