You are given a list routes, where each element represents the circular path followed by a specific bus.
For example, if routes[i] = [2, 8, 10], then bus i moves continuously as:
2 → 8 → 10 → 2 → 8 → 10 → ...
You start at a bus stop source without being on any bus. Your goal is to reach the bus stop target by boarding buses.
You may switch between buses at any stop they both visit.
Your task is to determine the minimum number of buses you need to take to reach the destination.
If there is no possible sequence of buses that allows you to travel from source to target, return -1.
Input: routes = [[2,3,5], [5,7], [7,9,10]], source = 2, target = 10
Output: 3
Explanation:
Take Bus 0 from stop 2 → 5 Switch to Bus 1 at stop 5 → 7 Switch to Bus 2 at stop 7 → 10 Total buses used = 3.
Input: routes = [[1,4,6], [2,5], [6,7,8]], source = 1, target = 8
Output: 2
Explanation:
Take Bus 0 from 1 → 6 Switch to Bus 2 at 6 → 8
Accepted:
Submission: