Ctrl K

Scooter Trips Under a Road Limit

Hard
A city has n crossings numbered 0 to n - 1. Two-way roads are listed in roads: an entry [u, v, d] means a road of length d joins crossings u and v. Two crossings may be joined by several roads. A courier rides an e-scooter, and each trip has its own charge. A trip trips[j] = [from, to, limit] allows any chain of roads, but **every** road on the way must be strictly shorter than limit: the scooter cannot handle a longer stretch without recharging. Return a boolean array whose j-th element is true if to can be reached from from on the j-th trip, and false otherwise.
Example 1
n=4, roads=[[0, 1, 3], [1, 2, 7], [2, 3, 2], [0, 3, 9]], trips=[[0, 2, 8], [0, 3, 7], [1, 3, 10]] → [True, False, True]

The first trip goes 0 → 1 → 2 over roads of length 3 and 7, both shorter than 8. The second needs roads shorter than 7: from 0 only the road of length 3 to crossing 1 is usable, and the roads of length 7 and 9 are off-limits, so the answer is false. In the third trip every road is usable.

Example 2
n=3, roads=[[0, 1, 5], [0, 1, 1]], trips=[[0, 1, 2], [1, 2, 100]] → [True, False]

Crossings 0 and 1 are linked by two roads, and the short one (length 1) fits under the limit of 2. Crossing 2 has no roads at all.

Constraints
  • 2 ≤ n ≤ 10⁵
  • 1 ≤ len(roads), len(trips) ≤ 10⁵
  • roads[i] = [u, v, d], trips[j] = [from, to, limit]
  • 0 ≤ u, v, from, to ≤ n - 1
  • u ≠ v, from ≠ to
  • 1 ≤ d, limit ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCell Levels by Rows and Columns HardBusy Server Pairs HardCompress a Grid of Numbers HardRoutes Between Equal Peaks HardHow Far the Water Spreads Hard
loading Python…
Input
n=4, roads=[[0, 1, 3], [1, 2, 7], [2, 3, 2], [0, 3, 9]], trips=[[0, 2, 8], [0, 3, 7], [1, 3, 10]]
Expected
[True, False, True]

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