Skip to content

Commit 1407e39

Browse files
committed
May 08
1 parent eea6b7a commit 1407e39

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from heapq import heappop, heappush
2+
3+
4+
class Solution:
5+
DIRS = ((0, -1), (0, 1), (1, 0), (-1, 0))
6+
7+
def minTimeToReach(self, moveTime: list[list[int]]) -> int:
8+
"""Dijkstra's algorithm-based implementation."""
9+
n, m = len(moveTime), len(moveTime[0])
10+
heap = [(0, (0, 0), True)] # time, (x, y), flag: False 1, True 2
11+
visited = {(0, 0)}
12+
while heap:
13+
time, (x, y), flag = heappop(heap)
14+
if x == n - 1 and y == m - 1:
15+
return time
16+
for dx, dy in self.DIRS:
17+
nx, ny = x + dx, y + dy
18+
if 0 <= nx < n and 0 <= ny < m and (nx, ny) not in visited:
19+
leave_time = max(moveTime[nx][ny], time) + (int(not flag) + 1)
20+
heappush(heap, (leave_time, (nx, ny), not flag))
21+
visited.add((nx, ny))
22+
return 0
23+
24+
25+
def main():
26+
moveTime = [[0, 4],
27+
[4, 4]]
28+
assert Solution().minTimeToReach(moveTime) == 7
29+
30+
moveTime = [[0, 0, 0],
31+
[0, 0, 0]]
32+
assert Solution().minTimeToReach(moveTime) == 6
33+
34+
35+
if __name__ == '__main__':
36+
main()

2025-05-May-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| May 05 | [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/) | Medium | Solved |
1212
| May 06 | [1920. Build Array from Permutation](https://leetcode.com/problems/build-array-from-permutation/) | Easy | Solved |
1313
| May 07 | [3341. Find Minimum Time to Reach Last Room I](https://leetcode.com/problems/find-minimum-time-to-reach-last-room-i/) | Medium | Solved |
14-
| May 08 | []() | | |
14+
| May 08 | [3342. Find Minimum Time to Reach Last Room II](https://leetcode.com/problems/find-minimum-time-to-reach-last-room-ii/) | Medium | Solved |
1515
| May 09 | []() | | |
1616
| May 10 | []() | | |
1717
| May 11 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 2 | 2 | 0 |
44-
| Medium | 4 | 4 | 0 |
44+
| Medium | 5 | 5 | 0 |
4545
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)