Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c24216

Browse files
added daily challenge
1 parent 25d61a4 commit 3c24216

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from collections import defaultdict
2+
from queue import Queue
3+
4+
class Solution:
5+
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
6+
adj = defaultdict(list)
7+
for flight in flights:
8+
adj[flight[0]].append((flight[1], flight[2]))
9+
10+
dist = [float('inf')] * n
11+
dist[src] = 0
12+
13+
q = Queue()
14+
q.put((src, 0))
15+
stops = 0
16+
17+
while not q.empty() and stops <= k:
18+
sz = q.qsize()
19+
for _ in range(sz):
20+
node, distance = q.get()
21+
22+
if node not in adj: continue
23+
24+
for neighbour, price in adj[node]:
25+
if price + distance >= dist[neighbour]: continue
26+
dist[neighbour] = price + distance
27+
q.put((neighbour, dist[neighbour]))
28+
29+
stops += 1
30+
31+
return dist[dst] if dist[dst] != float('inf') else -1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /