Find the minimum time needed to travel from the first room to the last room in a grid of room times.
You are given a rectangular grid of rooms. You start in the top-left room and want to reach the bottom-right room.
Each room has an associated time value. Moving to an adjacent room takes 1 unit of time, and you can only enter a room once you have reached the required time for that room. Your task is to determine the minimum time needed to reach the last room.
If multiple routes are possible, choose the one with the smallest arrival time at the destination.
Input Format
- A 2D grid of integers
moveTime, wheremoveTime[r][c]represents the earliest time the room can be entered. - Start at
(0, 0)and end at(m - 1, n - 1). - You may move up, down, left, or right to an adjacent room.
Output Format
- Return a single integer: the minimum time needed to reach the bottom-right room from the top-left room.
Constraints
1 <= m, n- Grid dimensions are small to moderate for interview-style pathfinding.
- Times are non-negative integers.
- You may assume the answer fits in a 32-bit signed integer.
Example 1
Input
moveTime = [[0,2,1],[1,3,4],[2,1,0]]
Output
4
Explanation
One optimal route is (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2). The arrival time is governed by the movement cost of 1 per step and the room availability times, giving a minimum finish time of 4.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.