Search⌘ K
AI Features

Bus Routes

Explore how to apply graph traversal techniques to solve the problem of determining the minimum number of buses required to travel between two stations. This lesson guides you through understanding bus route representations, constraints, and problem-solving strategies to build efficient solutions in coding interviews.

Statement

You are given an array, routes, representing bus routes where routes[i] is a bus route that the ithi^{th} bus repeats forever. Every route contains one or more stations. You have also been given the source station, src, and a destination station, dest. Return the minimum number of buses someone must take to travel from src to dest, or return -1 if there is no route.

Constraints:

  • 11 \le routes.length 50\le 50
  • 11 \le routes[i].length 100\le 100
  • 00 \le routes[i][j] <1000< 1000
  • 00 \le src, dest <1000< 1000

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Technical Quiz
1.

What is the output if the following input is given?

Bus routes = [[4, 2, 12], [3, 26], [1, 10], [4, 26, 6]]

src = 3

dest = 12

A.

2

B.

3

C.

4

D.

-1


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > MinimumBuses.cpp
#include <iostream>
#include <vector>
#include <queue>
int MinimumBuses(vector<vector<int>> busRoutes, int src, int dest)
{
// Replace this placeholder return statement with your code
return -1;
}
Bus Routes