Back to problems

Bus Routes (LC 815)

Algorithm · Uber · Hard

Requirements Input: routes[i] contains the stops served by bus i; each bus repeats that sequence without end (for example, routes[0] = [2,8,11] represents 2 -> 8 -> 11 -> 2 -> 8 -> 11 -> ...). You are also given integer stop IDs source and target. Output: return the fewest buses required to travel from source to target. Return -1 when no such trip exists. Edge case: when source == target, answer 0 because no ride is necessary. Limits: there may be as many as 500 routes. One…

Checking your access…