Ctrl K

Rooms on Schedule, Limping Steps

Medium
A building is an n × m grid of rooms. For each room you know opens[i][j], the moment (in seconds) from which you are allowed to **start moving into it** from a neighbouring room. You stand in room (0, 0) at time t = 0 and move into rooms that share a wall with yours (up, down, left or right). You have twisted an ankle, so move durations alternate: the first move takes 1 second, the second takes 2, the third 1 again, the fourth 2, and so on. You may wait in place as long as you like; waiting does not reset the pattern. Return the earliest moment you can be in room (n - 1, m - 1).
Example 1
opens=[[0, 3], [1, 9]] → 11

The first move, down to (1, 0), starts at 1 and lasts 1 second, so we arrive at 2. The door to (1, 1) opens at 9, and the second move lasts 2 seconds: we arrive at 11.

Example 2
opens=[[0, 0, 0], [0, 0, 0], [0, 0, 0]] → 6

Four moves lasting 1, 2, 1 and 2 seconds: 6 seconds in total.

Example 3
opens=[[0, 5, 0], [2, 7, 1]] → 9
Constraints
  • 2 ≤ n, m ≤ 750
  • 0 ≤ opens[i][j] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Path of a Rolling Ball MediumAcross the Hazard Field MediumRooms Unlocked on Schedule MediumBall Into the Hole HardRepaint Arrows to Reach the Exit Hard
loading Python…
Input
opens=[[0, 3], [1, 9]]
Expected
11

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.