Movatterモバイル変換


[0]ホーム

URL:


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

Commit60dbe88

Browse files
committed
Create 0787-cheapest-flights-within-k-stops.swift
1 parent7fbec15 commit60dbe88

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+
classSolution{
2+
func findCheapestPrice(_ n:Int, _ flights:[[Int]], _ src:Int, _ dst:Int, _ k:Int)->Int{
3+
varprices=Array(repeating:Int.max, count: n)
4+
prices[src]=0
5+
6+
foriin0..<(k+1){
7+
vartmp= prices
8+
9+
forfin flights{
10+
let(s, d, p)=(f[0],f[1],f[2])
11+
ifprices[s]==Int.max{
12+
continue
13+
}
14+
ifprices[s]+ p<tmp[d]{
15+
tmp[d]=prices[s]+ p
16+
}
17+
}
18+
19+
prices= tmp
20+
}
21+
22+
returnprices[dst]==Int.max?-1:prices[dst]
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp