leetcode - 815. Bus Routes

Description

You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.

For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.

Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.

Example 1:

Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 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.

Example 2:

Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
Output: -1

Constraints:

1 <= routes.length <= 500.
1 <= routes[i].length <= 10^5
All the values of routes[i] are unique.
sum(routes[i].length) <= 10^5
0 <= routes[i][j] < 10^6
0 <= source, target < 10^6

Solution

Solved after help.

Since the requirement is the minimum bus instead of minimum stops, we use bus as the node, then BFS to find the shortest path.

Code

class Solution:
    def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:
        stop_to_route = {}
        for route_i, each_route in enumerate(routes):
            for each_stop in each_route:
                if each_stop not in stop_to_route:
                    stop_to_route[each_stop] = set()
                stop_to_route[each_stop].add(route_i)
        queue = collections.deque([(source, 0)])
        visited = set()
        while queue:
            node, step = queue.popleft()
            if node == target:
                return step
            visited.add(node)
            for stop_route in stop_to_route[node]:
                for neighbor in routes[stop_route]:
                    if neighbor not in visited:
                        queue.append((neighbor, step + 1))
                routes[stop_route] = []
        return -1

An easier but MLE version:

class Solution:
    def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:
        stop_to_route = {}
        for route_i, each_route in enumerate(routes):
            for each_stop in each_route:
                if each_stop not in stop_to_route:
                    stop_to_route[each_stop] = set()
                stop_to_route[each_stop].add(route_i)
        queue = collections.deque([(source, 0, -1)])
        visited = set()
        while queue:
            node, step, route = queue.popleft()
            if node in visited:
                continue
            visited.add(node)
            if node == target:
                return step
            for stop_route in stop_to_route[node]:
                if stop_route == route:
                    continue
                for neighbor in routes[stop_route]:
                    queue.append((neighbor, step + 1, stop_route))
        return -1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值