Cheapest Flights Within K Stops - Leetcode 787 Python | Algorithm explained | Dijkstra Algorithm

preview_player
Показать описание
🔴 Question Link -

✅Connect with me

🔴 Question with Example
There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w.

Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.

Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Рекомендации по теме
Комментарии
Автор

These days this modified BFS returns Time Limit Exceeded :/ solution is correct though

dlobatog
Автор

Thanks your great work! One thing I would like to ask, in the explanation there is a addition node “3”, which are not existed, why do you want to add this node? A little confused to follow when comparing to the original question. Anyway, awesome work!

threeche
Автор

Nicely explained each step of logic
Thanks

sharifmansuri
Автор

Awesome explanation! I had a doubt tho.... Can we use python modules like collections in interviews?

dinkaranand
welcome to shbcf.ru