We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5d93878 commit ab53081Copy full SHA for ab53081
swift/0853-car-fleet.swift
@@ -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