LeetCode 815. Bus Routes

  Рет қаралды 8,826

Happy Coding

Happy Coding

Күн бұрын

Пікірлер: 6
@sandeepradhakrishnapadhi6366
@sandeepradhakrishnapadhi6366 3 жыл бұрын
Thankyou sir..!! Sir can you suggest some good hard problems along similar lines?
@veilmk5
@veilmk5 3 жыл бұрын
讲的非常好 谢谢
@deedee_358
@deedee_358 3 жыл бұрын
Really liked the explanation for the approach taken. Understood the problem and the solution quite well. Thanks a lot Sir for this wonderful video. Subscribed to your channel and would love to share the info of this channel with my other coder friends. Looking forward for more such videos.
@happycoding1093
@happycoding1093 3 жыл бұрын
thank you for your support!
@alammahtab08
@alammahtab08 2 жыл бұрын
Any idea how to get all the possible routes from A to B.
@arupdas2210
@arupdas2210 11 ай бұрын
Thank you very much for the explanation. Here is my C++ code based on above logic. Wishing you all a very happy Diwali. class Solution { public: // We will perform BFS on the source node and continue traversing // until we reach the target node. The number of different routes we // encounter is the answer. // Time Complexity: O(mn) // Space Complexity: O(mn) int numBusesToDestination(vector& routes, int source, int target) { int m; // no. of routes int n; // no. of stops in a bus route int ans; queue q; unordered_set seen_stops; unordered_set seen_routes; unordered_map graph; ans = 0; m = routes.size(); // Time Complexity: O(mn) for(int route_id = 0; route_id < m; route_id++) { for(auto stop : routes[route_id]) { graph[stop].push_back(route_id); } } q.push(source); seen_stops.insert(source); while(!q.empty()) { // Time Complexity: O(m) int cnt = q.size(); for(int j = 0; j < cnt; j++) { // Time Complexity: O(n) int bus_stop = q.front(); q.pop(); if(bus_stop == target) { return ans; } for(int route_id : graph[bus_stop]) { if(seen_routes.find(route_id) == seen_routes.end()) { // mark route_id as visited seen_routes.insert(route_id); n = routes[route_id].size(); // visit all stops in the given route for(int i = 0; i < n; i++) { int stop = routes[route_id][i]; if(seen_stops.find(stop) == seen_stops.end()) { // mark bus stop as visited seen_stops.insert(stop); q.push(stop); } } } } } ans++; } return -1; } };
UBER'S #1 INTERVIEW QUESTION | BUS ROUTES | PYTHON BFS SOLUTION
18:03
Cracking FAANG
Рет қаралды 4,1 М.
the balloon deflated while it was flying #tiktok
00:19
Анастасия Тарасова
Рет қаралды 34 МЛН
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 9 МЛН
НИКИТА ПОДСТАВИЛ ДЖОНИ 😡
01:00
HOOOTDOGS
Рет қаралды 3 МЛН
LeetCode 815. Bus Routes Detailed Explanation + Coding (Python)
31:53
Algorithms Simplified
Рет қаралды 3,4 М.
LeetCode 490. The Maze
19:37
Happy Coding
Рет қаралды 8 М.
Leetcode 79 Problem 4 - Bus Routes
11:17
code_report
Рет қаралды 15 М.
Go Has Exceptions??
16:58
ThePrimeTime
Рет қаралды 72 М.
How to Start Leetcode (as a beginner)
8:45
Ashish Pratap Singh
Рет қаралды 951 М.
you will never ask about pointers again after watching this video
8:03
815. Bus Routes | Tricky BFS
19:27
Aryan Mittal
Рет қаралды 4,1 М.
LeetCode 723. Candy Crush
25:39
Happy Coding
Рет қаралды 9 М.
basics of CODING in 10 minutes
15:34
The StudyTube Project
Рет қаралды 1,5 МЛН
the balloon deflated while it was flying #tiktok
00:19
Анастасия Тарасова
Рет қаралды 34 МЛН