Find Minimum Time to Reach Last Room I - Problem
Time-Locked Dungeon Escape
You find yourself trapped in a mystical n ร m dungeon where each room has a magical lock that opens only at a specific time!
๐ฐ The Challenge: Navigate from the entrance
๐ Given:
โข A 2D array
โข You start at time
โข Moving between adjacent rooms takes exactly 1 second
โข You can move horizontally or vertically to adjacent rooms
๐ฏ Goal: Find the minimum time to reach the bottom-right corner
Note: If a room opens at time
You find yourself trapped in a mystical n ร m dungeon where each room has a magical lock that opens only at a specific time!
๐ฐ The Challenge: Navigate from the entrance
(0, 0) to the exit (n-1, m-1) in minimum time.๐ Given:
โข A 2D array
moveTime[i][j] representing when each room becomes accessibleโข You start at time
t = 0 from room (0, 0)โข Moving between adjacent rooms takes exactly 1 second
โข You can move horizontally or vertically to adjacent rooms
๐ฏ Goal: Find the minimum time to reach the bottom-right corner
(n-1, m-1).Note: If a room opens at time
t, you can enter it at time t or later. You might need to wait or take detours if rooms aren't ready yet! Input & Output
example_1.py โ Basic Grid
$
Input:
moveTime = [[0,1,2],[3,4,5]]
โบ
Output:
6
๐ก Note:
We start at (0,0) at time 0. Moving right to (0,1) at time 1, then (0,2) at time 2, then down to (1,2) at time 3. But (1,2) opens at time 5, so we wait until time 5 to enter, then it takes 1 more second to reach, arriving at time 6.
example_2.py โ Immediate Path
$
Input:
moveTime = [[0,0,0],[0,0,0]]
โบ
Output:
3
๐ก Note:
All rooms are immediately accessible. The shortest path is (0,0) โ (0,1) โ (0,2) โ (1,2), taking exactly 3 seconds since each move takes 1 second.
example_3.py โ High Wait Times
$
Input:
moveTime = [[0,1],[2,1000]]
โบ
Output:
1001
๐ก Note:
We can go (0,0) โ (0,1) โ (1,1). The room (1,1) opens at time 1000. We arrive at time 2, so we need to wait. Since wait time is 998 (even), we can enter exactly at time 1000, then move to destination at time 1001.
Visualization
Tap to expand
Understanding the Visualization
1
Start Journey
Begin at intersection (0,0) at time 0 with green light
2
Check Next Lights
Look at adjacent intersections and their light schedules
3
Choose Optimal Route
Always head to the intersection you can reach earliest
4
Handle Red Lights
If light is red, either wait or take a detour path
5
Reach Destination
Continue until you reach the final intersection
Key Takeaway
๐ฏ Key Insight: This shortest path problem requires Dijkstra's algorithm with time-dependent edge weights, always processing the earliest reachable cell first!
Time & Space Complexity
Time Complexity
O(4^(n*m))
In worst case, we explore 4 directions for each of n*m cells
โ Linear Growth
Space Complexity
O(n*m)
Recursion depth can go up to n*m in worst case path
โก Linearithmic Space
Constraints
- 2 โค n, m โค 1000
- 0 โค moveTime[i][j] โค 109
- moveTime[0][0] = 0 (we can always start)
- Each move between adjacent cells takes exactly 1 second
- We can only move horizontally or vertically
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code