Find Minimum Time to Reach Last Room II - Problem

There is a dungeon with n x m rooms arranged as a grid. You are given a 2D array moveTime of size n x m, where moveTime[i][j] represents the minimum time in seconds when you can start moving to that room.

You start from the room (0, 0) at time t = 0 and can move to an adjacent room. Moving between adjacent rooms takes one second for one move and two seconds for the next, alternating between the two.

Return the minimum time to reach the room (n - 1, m - 1).

Two rooms are adjacent if they share a common wall, either horizontally or vertically.

Input & Output

Example 1 — Basic 2x2 Grid
$ Input: moveTime = [[0,1],[2,3]]
Output: 3
💡 Note: Start at (0,0) at time 0. Move right to (0,1) with cost 1 second (first move), arriving at time max(0+1, 1) = 1. Then move down to (1,1) with cost 2 seconds (second move), arriving at time max(1+2, 3) = 3. Total time is 3 seconds.
Example 2 — Larger Grid
$ Input: moveTime = [[0,0,0],[0,0,0],[0,0,0]]
Output: 6
💡 Note: In a 3x3 grid with no time restrictions, the shortest path from (0,0) to (2,2) requires 4 moves. With alternating costs of 1,2,1,2 seconds per move, the total time is 1+2+1+2 = 6 seconds.
Example 3 — High Time Constraints
$ Input: moveTime = [[0,5],[10,15]]
Output: 15
💡 Note: Must wait until time 5 to enter (0,1), then wait until time 15 to enter (1,1). The alternating movement costs don't matter much here due to high time constraints.

Constraints

  • 1 ≤ n, m ≤ 750
  • 0 ≤ moveTime[i][j] ≤ 109

Visualization

Tap to expand
Find Minimum Time to Reach Last Room II INPUT 2x2 Dungeon Grid (0,0) 0 START (0,1) 1 (1,0) 2 (1,1) 3 GOAL moveTime Array: [[0, 1], [2, 3]] Move cost alternates: 1s --> 2s --> 1s --> 2s Values = min start time ALGORITHM STEPS 1 Use Dijkstra's Algorithm Priority queue by arrival time 2 Track Move Parity Even move=1s, Odd move=2s 3 Wait if Needed max(time, moveTime[i][j]) 4 Explore Neighbors Update dist if shorter path Execution Trace: t=0: At (0,0), move#1 t=1: At (0,1), 1s cost t=2: At (0,0), move#2 t=4: At (1,0), 2s cost Wait at (0,1) until t=3 t=3+1=4: Reach (1,1) Optimal path: (0,0)-->(0,1)-->(1,1) FINAL RESULT Optimal Path Found t=0 START t=1 1s unused t=4 GOAL Output: 4 Path Breakdown: (0,0)-->(0,1): 0+1=1 Wait until t=3, then (0,1)-->(1,1): 3+1=4 [OK] Key Insight: This problem requires modified Dijkstra with state = (row, col, parity). The alternating move cost (1s, 2s) means we must track which move number we're on. We also must wait at a cell if we arrive before its moveTime value allows entry. Time Complexity: O(nm log(nm)) | Space Complexity: O(nm) TutorialsPoint - Find Minimum Time to Reach Last Room II | Dijkstra with Alternating Costs
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.4K Views
Medium Frequency
~35 min Avg. Time
890 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