Find Minimum Time to Reach Last Room II - Problem
Escape the Time-Locked Dungeon!

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
ENTRANCETime: 0Moves: 0โœ“ OpenROOM AUnlock: T=1Move cost: 1Arrive: T=1 โœ“ROOM BUnlock: T=2Move cost: 1Wait until T=2EXITUnlock: T=3Best arrival: T=3๐ŸŽฏ GOALCost: 1Cost: 1Cost: 2Cost: 2Algorithm StepsStart: Priority Queue = [(0,0,0,0)]Process (0,0): Add neighbors to PQNext: (0,1) at T=1, moves=1Then: (1,0) at T=2, moves=1 (wait)Path via (0,1): T=1+2=3 โœ“Path via (1,0): T=2+2=4 (worse)Answer: 3 (optimal path found)
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.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
23.7K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen