Ctrl K

Stops That Close

Medium
A city has stop_count stops numbered 0 to stop_count - 1. The list roads describes the roads: [u, v, t] is a two-way road between stops u and v that takes t units of time to travel. A pair of stops may be joined by several roads, and the city may fall apart into disconnected pieces. Stop i closes at time closing[i]: you can only get there strictly before that moment, and a closed stop cannot be passed through. You start at stop 0 at time 0. Return a list holding, for every stop, the earliest time you can get there, or -1 if it cannot be reached in time.
Example 1
stop_count=4, roads=[[0, 1, 3], [1, 2, 2], [0, 2, 7], [2, 3, 1]], closing=[5, 4, 6, 10] → [0, 3, 5, 6]

Stop 1 is reached at 3 (it closes at 4, in time), stop 2 via stop 1 at 3 + 2 = 5 (closes at 6), stop 3 one unit later at 6.

Example 2
stop_count=4, roads=[[0, 1, 3], [1, 2, 2], [0, 2, 7], [2, 3, 1]], closing=[5, 3, 6, 10] → [0, -1, -1, -1]

We would reach stop 1 exactly as it closes, which is too late. The direct road to stop 2 takes 7, but it closes at 6. With stops 1 and 2 lost, stop 3 is out of reach.

Example 3
stop_count=3, roads=[], closing=[2, 2, 2] → [0, -1, -1]

There are no roads, so only the starting stop is reachable.

Constraints
  • 1 ≤ stop_count ≤ 5·10⁴
  • 0 ≤ len(roads) ≤ 10⁵
  • 0 ≤ u, v < stop_count
  • 1 ≤ t ≤ 10⁵
  • len(closing) = stop_count
  • 1 ≤ closing[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Path of a Rolling Ball MediumMost Reliable Relay Route MediumSouvenir Round Trip MediumA Trip Through Tunnels MediumNearest Flagged Node Medium
loading Python…
Input
stop_count=4, roads=[[0, 1, 3], [1, 2, 2], [0, 2, 7], [2, 3, 1]], closing=[5, 4, 6, 10]
Expected
[0, 3, 5, 6]

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