Skip to content

Commit 8ceb091

Browse files
authored
Create 787-Cheapest-Flights-within-K-stops.py
1 parent 246e49d commit 8ceb091

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
3+
prices = [float("inf")] * n
4+
prices[src] = 0
5+
6+
for i in range(k + 1):
7+
tmpPrices = prices.copy()
8+
9+
for s, d, p in flights: # s=source, d=dest, p=price
10+
if prices[s] == float("inf"):
11+
continue
12+
if prices[s] + p < tmpPrices[d]:
13+
tmpPrices[d] = prices[s] + p
14+
prices = tmpPrices
15+
return -1 if prices[dst] == float("inf") else prices[dst]

0 commit comments

Comments
 (0)