Skip to content

Commit eea6b7a

Browse files
committed
May 07
1 parent e0cfa5c commit eea6b7a

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+
visited = {(0, 0)}
11+
heap = [(0, (0, 0))] # time, (x, y)
12+
while heap:
13+
time, (x, y) = 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) + 1
20+
heappush(heap, (leave_time, (nx, ny)))
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) == 6
29+
30+
moveTime = [[0, 0, 0],
31+
[0, 0, 0]]
32+
assert Solution().minTimeToReach(moveTime) == 3
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
@@ -10,7 +10,7 @@
1010
| May 04 | [1128. Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/) | Easy | Solved |
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 |
13-
| May 07 | []() | | |
13+
| 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 |
1414
| May 08 | []() | | |
1515
| May 09 | []() | | |
1616
| May 10 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 2 | 2 | 0 |
44-
| Medium | 3 | 3 | 0 |
44+
| Medium | 4 | 4 | 0 |
4545
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)