We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.
We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.
Example:
Input: 
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation: 
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Constraints:
1 <= routes.length <= 500.
1 <= routes[i].length <= 10^5.
0 <= routes[i][j] < 10 ^ 6.
class Solution:
    # O(n_stops x n_buses) time, O(n_stops x n_buses) space
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
        num_buses = len(routes)
        bus_to_stop = defaultdict(set)
        for bus, stops in enumerate(routes):
            bus_to_stop[bus] = set(stops)
        
        def update_buses_used():
            for bus in range(num_buses):
                if bus in buses_used:
                    continue
                if stops_reached & bus_to_stop[bus]:
                    buses_used.add(bus)
        
        def update_stops_reached():
            for bus in buses_used:
                stops_reached.update(bus_to_stop[bus])
        
        buses_used = set()
        stops_reached = {S}
        pre_stop_count = 0
        bus_count = 0
        while len(stops_reached) > pre_stop_count:
            if T in stops_reached:
                return bus_count
            pre_stop_count = len(stops_reached)
            update_buses_used()
            update_stops_reached()
            bus_count += 1
            
        return -1

        
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
        stop_to_bus = defaultdict(set)
        for bus, stops in enumerate(routes):
            for stop in stops:
                stop_to_bus[stop].add(bus)
        
        q = deque([(S, 0)])
        seen = {S}
        while q:
            stop, bus_count = q.popleft()
            if stop == T:
                return bus_count
            for bus in stop_to_bus[stop]:
                for stop_new in routes[bus]:
                    if stop_new in seen:
                        continue
                    seen.add(stop_new)
                    q.append((stop_new, bus_count + 1))
        return -1
        

class Solution:
    # O(n_stops x n_buses) time, O(n_stops x n_buses) space
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
        num_buses = len(routes)
        bus_to_stop = defaultdict(set)
        for bus, stops in enumerate(routes):
            bus_to_stop[bus] = set(stops)
        
        def update_buses_used():
            for bus in range(num_buses):
                if bus in buses_used:
                    continue
                if stops_reached & bus_to_stop[bus]:
                    buses_used.add(bus)
        
        def update_stops_reached():
            for bus in buses_used:
                stops_reached.update(bus_to_stop[bus])
        
        buses_used = set()
        stops_reached = {S}
        pre_stop_count = 0
        bus_count = 0
        while len(stops_reached) > pre_stop_count:
            if T in stops_reached:
                return bus_count
            pre_stop_count = len(stops_reached)
            update_buses_used()
            update_stops_reached()
            bus_count += 1
            
        return -1

        
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
        stop_to_bus = defaultdict(set)
        for bus, stops in enumerate(routes):
            for stop in stops:
                stop_to_bus[stop].add(bus)
        
        q = deque([(S, 0)])
        seen = {S}
        while q:
            stop, bus_count = q.popleft()
            if stop == T:
                return bus_count
            for bus in stop_to_bus[stop]:
                for stop_new in routes[bus]:
                    if stop_new in seen:
                        continue
                    seen.add(stop_new)
                    q.append((stop_new, bus_count + 1))
        return -1
        
