1129. Shortest Path with Alternating Colors | LeetCode Medium | Graphs | BFS

  Рет қаралды 3,226

DeepCodes

DeepCodes

Күн бұрын

Пікірлер
@deepcodes
@deepcodes Жыл бұрын
class Solution { public: //red->0 and blue->1 vector shortestAlternatingPaths(int n, vector& redEdges, vector& blueEdges) { vectoradj(n); for (auto &it : redEdges) { adj[it[0]].push_back({it[1], 0}); } for (auto &it : blueEdges) { adj[it[0]].push_back({it[1], 1}); } vectorans(n, -1); vectorvisited(n, vector(2)); queueq; q.push({0, 0, -1}); //node, steps, color visited[0][0] = true; visited[0][1] = true; ans[0] = 0; while (!q.empty()) { vectorcurr = q.front(); q.pop(); int node = curr[0], steps = curr[1], prevColor = curr[2]; for (auto& [neighbour, color] : adj[node]) { if (!visited[neighbour][color] && color != prevColor) { visited[neighbour][color] = true; q.push({neighbour, 1+steps, color}); if (ans[neighbour] == -1) { ans[neighbour] = 1+steps; } } } } return ans; } };
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
after searching a lot of articles and videos I found this one. Awesome explanation✨
@peter9759
@peter9759 Жыл бұрын
Good Explanation Bro everything connects
@acceleratorlevel645
@acceleratorlevel645 Жыл бұрын
great approach , i also had a similar approach by pairing up nodes and color in adj list, had difficulty in updating the distance array, your second if statement in the for loop was something i didnt think of , helped me a lot! thanks
@deepcodes
@deepcodes Жыл бұрын
Thanks, you 're welcome.
@CleverSuree
@CleverSuree Жыл бұрын
I started to follow you from 1 of Feb. day by day your explanation style improving. Good Work .
@deepcodes
@deepcodes Жыл бұрын
Thanks a lot 😊
@sadikulhaquesadi6003
@sadikulhaquesadi6003 Жыл бұрын
Thanks man!!! Thanks for the effort!! After trying DFS and normal visited array I was about to give up. Then Saw this video and the idea of using BFS and visited[node][color] trick has been accepted
@deepcodes
@deepcodes Жыл бұрын
Great to hear, thank you!
@mdkashifraza2603
@mdkashifraza2603 Жыл бұрын
Thank You for nice explanation😁
@deepcodes
@deepcodes Жыл бұрын
You're welcome 😊, thanks!
@prajitbanerjee8226
@prajitbanerjee8226 Жыл бұрын
nice bro!!
@ITArchanaGK
@ITArchanaGK Жыл бұрын
Thank you. Very much understandable👍
@deepcodes
@deepcodes Жыл бұрын
Thanks, You are welcome!
@himashisbiswas1339
@himashisbiswas1339 Жыл бұрын
great explanation . that modified visited array thing was really smart. how did you came up with that idea ? i was solving using using a queue of node,steps,and previous color but the visited array thing was making it wrong.
@deepcodes
@deepcodes Жыл бұрын
From the hint, parallel edges. Thank you!
@abhishek__anand__
@abhishek__anand__ Жыл бұрын
Great Code 😉
@deepcodes
@deepcodes Жыл бұрын
Thanks 😁
@mohamedhesham6008
@mohamedhesham6008 Жыл бұрын
Very good keep going (: thank you
@deepcodes
@deepcodes Жыл бұрын
Thank you! 😃
@girikgarg8
@girikgarg8 Жыл бұрын
How do you think of the approach to such questions? I mean how do you develop the intuition to such questions
@deepcodes
@deepcodes Жыл бұрын
Usually I do by generating test cases.
@CricketGalaxy07-18
@CricketGalaxy07-18 Жыл бұрын
Thanks bhai 🙏🏻🙏🏻
@deepcodes
@deepcodes Жыл бұрын
Thanks!
@KrishnaSharma-bv5ys
@KrishnaSharma-bv5ys Жыл бұрын
Nice.
@deepcodes
@deepcodes Жыл бұрын
Thanks!
@thedarkknight1865
@thedarkknight1865 Жыл бұрын
thanks undestood
@deepcodes
@deepcodes Жыл бұрын
Thanks, You are welcome!
864. Shortest Path to Get All Keys | LeetCode Daily Challenge
19:59
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 57 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 154 МЛН
Shortest Path with Alternating Colors - Leetcode 1129 - Python
13:43
Coding Unbreakable Encryption in C | One-Time Pad
17:42
HirschDaniel
Рет қаралды 4,2 М.
Shortest Path with Alternating Colors | Leetcode -1129
11:16
Algorithms Made Easy
Рет қаралды 2,6 М.