The Indian Engineer

Problem 815 Bus Routes

Posted on 6 mins

Array Hash-Table Breadth-First Search

Problem Statement

Link - Problem 815

Question

You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats 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:

Solution

class Solution {
public:
    int numBusesToDestination(vector<vector<int>>& routes, int source,int target) {
        if (source == target) {
            return 0;
        }

        unordered_map<int, vector<int>> adjList;
        for (int route = 0; route < routes.size(); route++) {
            for (auto stop : routes[route]) {
                adjList[stop].push_back(route);
            }
        }

        queue<int> q;
        unordered_set<int> vis;
        for (auto route : adjList[source]) {
            q.push(route);
            vis.insert(route);
        }

        int busCount = 1;
        while (q.size()) {
            int size = q.size();

            for (int i = 0; i < size; i++) {
                int route = q.front();
                q.pop();
                for (auto stop : routes[route]) {
                    if (stop == target) {
                        return busCount;
                    }
                    for (auto nextRoute : adjList[stop]) {
                        if (!vis.count(nextRoute)) {
                            vis.insert(nextRoute);
                            q.push(nextRoute);
                        }
                    }
                }
            }
            busCount++;
        }
        return -1;
    }
};

Complexity Analysis

| Algorithm                  | Time Complexity | Space Complexity |
| -------------------------- | --------------- | ---------------- |
| Breadth-First Search (BFS) | O(n _ m _ k)    | O(n + m)         |

Explanation

Intial Thoughts

To solve this problem, think of it like navigating through a city with multiple bus routes. The goal is to find the shortest path from the source to the target. Consider that each bus stop can be a meeting point for multiple buses, and we need to find the least number of buses to take to reach the target. Think about creating a map or a network where bus stops are connected if they are on the same bus route.

Intuitive Analysis

Imagine you are actually traveling through the city. You start at the source and look for buses that stop there. Each bus represents a possible path, and you need to transfer between buses to reach the target. Think of each transfer as moving to a new bus route. The key is to find the sequence of buses that leads to the target with the fewest transfers. Consider using a strategy like breadth-first search to explore all possible paths level by level, starting from the source, to ensure we find the shortest path.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.


Similar Questions:

Title URL Difficulty
Minimum Costs Using the Train Line https://leetcode.com/problems/minimum-costs-using-the-train-line Hard