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 (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
STARTt=0๐ŸŸขt=5๐Ÿ”ดt=10๐ŸŸขt=3๐ŸŸขGOALt=12๐ŸŸขNavigation Strategy:๐Ÿš— Always choose the earliest reachable intersection๐Ÿšฆ Red lights force waiting or detoursโฐ Timing is everything - plan ahead!๐Ÿ›ฃ๏ธ Sometimes longer paths are fasterOptimal Path Found!1. Start โ†’ Down (avoid red light ahead)2. Down โ†’ Right (perfect timing)3. Arrive at destination efficiently๐Ÿ’ก Key InsightUse Dijkstra's algorithm with time-awaretransitions - always explore earliest reachable!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n*m)

Recursion depth can go up to n*m in worst case path

n
2n
โšก 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
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
52.0K Views
High Frequency
~25 min Avg. Time
1.8K 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