{"id":"7d600327-c227-4d1b-9324-6b97d9788943","name":"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.\r\n\r\nFor example, if routes[1] = [1, 5, 7], this means that the 1st bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.\r\n\r\nYou 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.\r\nReturn the least number of buses you must take to travel from source to target. Return -1 if it is not possible.","inputFormat":"Input is already handled for you.","outputFormat":"return theleast number of buses you need to take.","constraints":"1 &lt;= routes.length &lt;= 500.\r\n1 &lt;= routes[i].length &lt;= 10^5.\r\nAll the values of routes[i] are unique.\r\n0 &lt;= routes[i][j] &lt; 10^6.\r\n0 &lt;= source, target &lt; 10^6.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numBusesToDestination(vector<vector<int>>& routes,int S, int T){\n //Write code here\n}\n\nint main(){\n int n,m;\n cin>>n>>m;\n \n vector<vector<int>>arr(n,vector<int>(m));\n \n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n int x;\n cin>>x;\n arr[i][j]=x;\n \n }\n }\n \n int src,dest;\n cin>>src>>dest;\n \n cout<<numBusesToDestination(arr, src, dest);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\nclass Main {\r\n public static void main(String[] args) throws NumberFormatException, IOException {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int m = Integer.parseInt(br.readLine());\r\n\r\n int[][] arr = new int[n][m];\r\n\r\n for (int i = 0; i < n; i++) {\r\n String[] st = br.readLine().split(\" \");\r\n for (int j = 0; j < m; j++) {\r\n arr[i][j] = Integer.parseInt(st[j]);\r\n }\r\n }\r\n\r\n String[] st1 = br.readLine().split(\" \");\r\n int src = Integer.parseInt(st1[0]);\r\n int dest = Integer.parseInt(st1[1]);\r\n System.out.println(numBusesToDestination(arr, src, dest));\r\n\r\n }\r\n\r\n public static int numBusesToDestination(int[][] routes, int S, int T) {\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2\r\n3\r\n1 2 7\r\n3 6 7\r\n1 6","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"c61f4f06-e44c-4338-8b82-a0e7a82bfb69","name":"Bus Routes","slug":"bus-routes","type":1}],"next":{"id":"a9bd6288-fc03-41dc-96a7-6fe924514b8a","name":"Bus Routes mcq","type":0,"slug":"bus-routes-mcq"},"prev":{"id":"f48f88b5-4981-423d-a2aa-1c7aad2e7d70","name":"Shortest Bridge","type":3,"slug":"shortest-bridge"}}}

Bus Routes

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[1] = [1, 5, 7], this means that the 1st 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.

{"id":"7d600327-c227-4d1b-9324-6b97d9788943","name":"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.\r\n\r\nFor example, if routes[1] = [1, 5, 7], this means that the 1st bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.\r\n\r\nYou 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.\r\nReturn the least number of buses you must take to travel from source to target. Return -1 if it is not possible.","inputFormat":"Input is already handled for you.","outputFormat":"return theleast number of buses you need to take.","constraints":"1 &lt;= routes.length &lt;= 500.\r\n1 &lt;= routes[i].length &lt;= 10^5.\r\nAll the values of routes[i] are unique.\r\n0 &lt;= routes[i][j] &lt; 10^6.\r\n0 &lt;= source, target &lt; 10^6.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numBusesToDestination(vector<vector<int>>& routes,int S, int T){\n //Write code here\n}\n\nint main(){\n int n,m;\n cin>>n>>m;\n \n vector<vector<int>>arr(n,vector<int>(m));\n \n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n int x;\n cin>>x;\n arr[i][j]=x;\n \n }\n }\n \n int src,dest;\n cin>>src>>dest;\n \n cout<<numBusesToDestination(arr, src, dest);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\nclass Main {\r\n public static void main(String[] args) throws NumberFormatException, IOException {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int m = Integer.parseInt(br.readLine());\r\n\r\n int[][] arr = new int[n][m];\r\n\r\n for (int i = 0; i < n; i++) {\r\n String[] st = br.readLine().split(\" \");\r\n for (int j = 0; j < m; j++) {\r\n arr[i][j] = Integer.parseInt(st[j]);\r\n }\r\n }\r\n\r\n String[] st1 = br.readLine().split(\" \");\r\n int src = Integer.parseInt(st1[0]);\r\n int dest = Integer.parseInt(st1[1]);\r\n System.out.println(numBusesToDestination(arr, src, dest));\r\n\r\n }\r\n\r\n public static int numBusesToDestination(int[][] routes, int S, int T) {\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2\r\n3\r\n1 2 7\r\n3 6 7\r\n1 6","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"c61f4f06-e44c-4338-8b82-a0e7a82bfb69","name":"Bus Routes","slug":"bus-routes","type":1}],"next":{"id":"a9bd6288-fc03-41dc-96a7-6fe924514b8a","name":"Bus Routes mcq","type":0,"slug":"bus-routes-mcq"},"prev":{"id":"f48f88b5-4981-423d-a2aa-1c7aad2e7d70","name":"Shortest Bridge","type":3,"slug":"shortest-bridge"}}}
plane

Editor


Loading...

Bus Routes

easy

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[1] = [1, 5, 7], this means that the 1st 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.

Constraints

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

Format

Input

Input is already handled for you.

Output

return theleast number of buses you need to take.

Example

Sample Input

2 3 1 2 7 3 6 7 1 6

Sample Output

2

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode