Skip to content

Commit ab53081

Browse files
committed
Create 0853-car-fleet.swift
1 parent 5d93878 commit ab53081

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

swift/0853-car-fleet.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/car-fleet/
3+
*/
4+
5+
class Solution {
6+
func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
7+
var stack = [Double]()
8+
9+
let pair = zip(position, speed)
10+
.map { ($0, $1) }
11+
.sorted { $0.0 > $1.0 }
12+
13+
for (p, s) in pair {
14+
stack.append(Double(target - p) / Double(s))
15+
16+
guard stack.count >= 2 else { continue }
17+
18+
let suffix = stack.suffix(2)
19+
if suffix.last! <= suffix.first! {
20+
stack.removeLast()
21+
}
22+
}
23+
24+
return stack.count
25+
}
26+
}

0 commit comments

Comments
 (0)