Find Minimum Time to Reach Last Room II - Problem
Escape the Time-Locked Dungeon!
You find yourself trapped in a mysterious
The Challenge: Starting from room
Movement Rules:
Return the minimum time needed to reach the final room. If a room's time lock hasn't expired, you must wait or find alternative paths!
You find yourself trapped in a mysterious
n x m dungeon where each room has a magical time lock. You're given a 2D array moveTime[i][j] that represents the earliest time you can enter room (i, j).The Challenge: Starting from room
(0, 0) at time t = 0, reach the exit at room (n-1, m-1) in minimum time.Movement Rules:
- You can only move to adjacent rooms (up, down, left, right)
- Movement time alternates: 1 second for odd-numbered moves, 2 seconds for even-numbered moves
- You cannot enter a room before its
moveTime[i][j]
Return the minimum time needed to reach the final room. If a room's time lock hasn't expired, you must wait or find alternative paths!
Input & Output
example_1.py โ Basic 2x2 Grid
$
Input:
moveTime = [[0,1],[2,3]]
โบ
Output:
3
๐ก Note:
Start at (0,0) at time 0. Move to (0,1) at time 1 (cost 1). Move to (1,1): arrival time 1+2=3, which equals moveTime[1][1]=3, so final time is 3.
example_2.py โ Waiting Required
$
Input:
moveTime = [[0,0,99],[1,1,1]]
โบ
Output:
6
๐ก Note:
Optimal path: (0,0)โ(1,0)โ(1,1)โ(1,2). Times: 0โ2(wait)โ4โ6. The direct path through (0,1) is blocked by high moveTime.
example_3.py โ Single Cell
$
Input:
moveTime = [[5]]
โบ
Output:
5
๐ก Note:
Already at destination (0,0). Need to wait until time 5 when the room unlocks.
Constraints
- 1 โค n, m โค 750
- 0 โค moveTime[i][j] โค 109
- moveTime[0][0] = 0 (can always start immediately)
- The grid is connected (solution always exists)
Visualization
Tap to expand
Understanding the Visualization
1
Start Journey
Begin at entrance with your time-alternating pass
2
Check Room Access
Each room requires waiting until its unlock time
3
Calculate Movement
Movement cost alternates: 1 unit for odd moves, 2 for even
4
Priority Decision
Always choose the path that gets you anywhere fastest
5
Handle Waiting
If you arrive early, wait until the room opens
6
Reach Destination
First arrival at exit is guaranteed optimal
Key Takeaway
๐ฏ Key Insight: This is a shortest path problem with variable edge weights. Dijkstra's algorithm naturally handles both the alternating movement costs and waiting times by always processing the most promising path first.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code