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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code