You're planning a journey through a complex city bus system! ๐
You have an array routes where each routes[i] represents the stops that bus i visits in order. Each bus follows its route in a circular pattern forever. For example, if routes[0] = [1, 5, 7], then bus 0 travels: 1 โ 5 โ 7 โ 1 โ 5 โ 7 โ ... infinitely.
You start at bus stop source (not on any bus initially) and want to reach bus stop target. You can only travel by taking buses - no walking between stops!
Goal: Find the minimum number of buses you need to take to get from source to target. Return -1 if it's impossible.
Think of this as finding the shortest path in a graph where nodes are bus stops and edges represent bus connections! ๐ฏ
Input & Output
Constraints
- 1 โค routes.length โค 500
- 1 โค routes[i].length โค 105
- All values of routes[i] are unique
- Sum of routes[i].length โค 105
- 0 โค routes[i][j] โค 106
- 0 โค source, target โค 106