Practice/PayPal/Leetcode 815. Bus Routes
CodingMust
You are given a transit network represented by an array of bus routes. Each bus route is a list of stops that the bus visits. Buses follow their routes in a circular pattern, meaning you can board at any stop on the route and get off at any other stop on the same route.
Given a starting stop and a destination stop, determine the minimum number of buses you must take to travel from the source to the target. If it's impossible to reach the target, return -1.
Example 1:
Input: routes = [[1, 2, 7], [3, 6, 7]], source = 1, target = 6 Output: 2 Explanation: Take the first bus (route [1, 2, 7]) from stop 1 to stop 7. Then transfer to the second bus (route [3, 6, 7]) and ride to stop 6. Total buses taken: 2
Example 2:
Input: routes = [[7, 12], [4, 5, 15], [6], [15, 19], [9, 12, 13]], source = 15, target = 12 Output: 3 Explanation: One possible path requires 3 bus transfers to get from stop 15 to stop 12.
Example 3:
Input: routes = [[1, 2, 3], [4, 5, 6]], source = 1, target = 6 Output: -1 Explanation: There's no way to travel from stop 1 to stop 6 since the routes don't connect.