Skip to content

Commit 60dbe88

Browse files
committed
Create 0787-cheapest-flights-within-k-stops.swift
1 parent 7fbec15 commit 60dbe88

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func findCheapestPrice(_ n: Int, _ flights: [[Int]], _ src: Int, _ dst: Int, _ k: Int) -> Int {
3+
var prices = Array(repeating: Int.max, count: n)
4+
prices[src] = 0
5+
6+
for i in 0..<(k + 1) {
7+
var tmp = prices
8+
9+
for f in flights {
10+
let (s, d, p) = (f[0], f[1], f[2])
11+
if prices[s] == Int.max {
12+
continue
13+
}
14+
if prices[s] + p < tmp[d] {
15+
tmp[d] = prices[s] + p
16+
}
17+
}
18+
19+
prices = tmp
20+
}
21+
22+
return prices[dst] == Int.max ? -1 : prices[dst]
23+
}
24+
}

0 commit comments

Comments
 (0)