Cheapest Flights Within K Stops | Graph | [CODE + Explaination] | Amazon | GFG 🔥
Vložit
- čas přidán 17. 03. 2021
- #graph #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Cheapest Flights Within K Stops'.
Join our Telegram Channel for more Information
🔰 Telegram Channel Link = t.me/CodeLibrary1
🔰 Array question Playlist = • Love Babbar DSA 450 Qu...
🔰 String question Playlist = • Love Babbar DSA 450 Qu...
🔰 Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
🔰 Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
🔰 Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
🔰 RoadMap of Web Development = • 🔴 Web Development Road...
🔰 Roadmap for Dynamic Programming = • Complete Roadmap for D...
🔰 Great Strategy to solve DSA = • Great Strategy to solv...
🔰 My Journey to 5 star at codechef = • My Journey to 5 Star a...
🔰 Love Babbar DSA Sheet : drive.google.com/file/d/1FMdN...
Follow us on Instagram:
🔰 Shailesh Yogendra : / shaileshyogendra
🔰 Yogesh Yogendra : / i_am_yogesh_here
Follow us on LinkedIn:
🔰 Yogesh Yogendra : / yogesh-yogendra-26bbb518a
🔰 Shailesh Yogendra : / shailesh-yogendra-8b13...
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
Update
Recently Leetcode has updated its test cases so the Priority queue solution is not getting Accepted it's giving TLE. So I have to do this question by some different approach.
I will update the solution in the next video.
bana do bhaiya naya vide ispe plz
To tackle TLE just use a map saying seen_stop, idea is whenever we see some already visited node we check whether st that time the curr stops was lesser or not - if less then set that stops to that node at seen_stop else don't add it into pq again.
@@AbhishekKumar-be7tx thanks mate :)
Abe TLE de rha hai
bro i think hindi + english combination is good...just like the previous videos.
okay
thanks buddy, you have saved my entire night.
Thanks bro
giving tle
Wether we can use bellman for algorithm?
simple use Bellman-Ford algo.
and compare previous cost vector
# define MAX 1e9
class Solution {
public:
int findCheapestPrice(int n, vector& flights, int src, int dst, int k) {
vector cost(n,MAX);
vector pre_cost;
cost[src]=0;
k++;
while(k--){
pre_cost.assign(cost.begin(),cost.end());
for(auto i:flights){
int a=i[0];
int b=i[1];
int w=i[2];
cost[b]=min(cost[b],pre_cost[a]+w);
}
}
if(cost[dst]==MAX) return -1;
return cost[dst];
}
};
U r using dijkstras algo which will not work here bcoz lets say we have connections 0-->1 cost 100, 0-->2 cost 400, 1--->2 cost 100, 2-->3 cost 100 and we want to go from 0 to 3 with 1 stops here dijkstras will go from 0-->1 and then 1-->2 and then it will realize that now stops are not available as we have taken max 1 stops until now but as we have now updated the distance with 200 at node 2 we will not be able to get correct ans.As correct ans is 0-->2 and then 2-->3 with cost 400+ 100.
I think that this testcase will work because it will return at A because steps are greater than given and we didn't find the value
Tell me one thing anyone plz . Though this approach is giving tle now , but why in this solution we have not used distance vector as we usually do in djikstra algo
Hello Brother
One optimize tip : All node which we pop out from queue (Node which are relaxed ) should' we mark processed using array .
Since processed node finish there job there is no point to put them again in queue.
But with one condition node which not able to process properly because of exceed boundary condition should not be mark processed.
If we think of it like Dijkstra with min heap
yaa like this we can improve little bit.........let me go through once
is the algo used is some sort of dijkshtra?
bro please create a video on the updated solution to this problem
if there is cycle in graph them????????
Bhai yeh same code tle de raha hai 😐
tle aa rha
This approach gives TLE!!
yess, then what to do ?
Ya recently leetcode has updated the testcases
@@CodeLibrary bro please make a video on new solution, don't know how to solve this tle;
which softwar and device do you use to write
HINDI MAI HI KARUUUUUUUUUUUU
Bhai coding woding to sab theek hai
Par ek baar shailesh ka ek beatboxing ka video v daal yaahan 😜😜
Abey hmko pehchante to ho na be k bhul gye😜
Haa yaar...... Shailesh wale channel me upload karte hai beatboxing wala😀
@@CodeLibrary oohh acha acha channel ka name kya hai??
Shailesh Yogendra
Which slide is this ques slide..?? Can you send it
Love Babar 450
@@tusharshaily can you send link
@@shubhampokhriyal8491 drive.google.com/file/d/1FMdN_OCfOI0iAeDlqswCiC2DZzD4nPsb/view
it is giving tle
Yes
@@snehabaser3155 yes
can plz anyone tell , how to fix this TLE
HINDI IS BEST BRO
ye sb baad m btate hn explore krke.......let explore it's very funy😂 bhai
why have we not used visited array..like for eg what if the node 2 was connected to node 0 and node 5 simultaneousy. Plz answer!!
you should use boolean array , but in this case even if we dont use boolean array it will work because youll be terminating when stops>k
my previous reply is not correct , i tried submitting it using visited array and it failed , then i tried submitting it without using visited array it passed all testcases , the reason why we are not using visited array is because if u do use visited array , you will not be able to visit the already visited node , and you may want to visit already visited node in this question because there might still exist a possible path having shorter length
we want multiple occurrences of node and then filter out using the stops
@@tusharshaily use visited array and try submitting on leetcode and you will find a counter example
@@LegitGamer2345 If you want to use visited array, you can use a 2d array, with vertex and the number of stops left as coordinates of the 2d matrix. But that wont be of much benefit, and will use up a lot of space as well, hence its not of much use
Bro this is giving TLE
This gives tle
Ya recently leetcode has updated the testcases
Absolutely wrong solution, Will get into infinite loop in case of directed cycle. What a waste of time..!
bellman wala batao bhai , ye TLE de rha hai, comment wagera nhi padhte ho kya ab tum log.
Accha mai bhul gya tha , bada channel ho gya hai !!! 😅
are comments read karta hu bro.......dusra approach soch ke banata hu naya video
giving tle