Skip to content

Commit 9e9ef4f

Browse files
committed
853_Car_Fleet
1 parent 42ebbe0 commit 9e9ef4f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ I have solved quite a number of problems from several topics. See the below tabl
192192
|90| **[1423. Maximum Points You Can Obtain from Cards](https://tinyurl.com/yxry5n5b)** | [Python](https://tinyurl.com/wu6rdaw/1423_Maximum_Points_You_Can_Obtain_from_Cards.py), [Swift](https://tinyurl.com/wuja3c4/1423_Maximum_Points_You_Can_Obtain_from_Cards.swift) | [Art 1](https://tinyurl.com/y5mohcl3), [Art 2](https://tinyurl.com/y4d7p6wh) | Medium | Very important. Learned new ways of sliding window |
193193
|91| [900. RLE Iterator](https://tinyurl.com/yxry5n5b) | [Python](https://tinyurl.com/wu6rdaw/900_RLE_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/900_RLE_Iterator.swift) | --- | Medium | - |
194194
|92| [954. Array of Doubled Pairs](https://tinyurl.com/yftjpl7m) | [Swift](https://tinyurl.com/wuja3c4/954_Array_of_Doubled_Pairs.swift) | --- | Medium | - |
195+
|93| [853. Car Fleet](https://tinyurl.com/y5h6ggg9) | [Swift](https://tinyurl.com/wuja3c4/853_Car_Fleet.swift) | [Vid 1](https://tinyurl.com/yh628kc9), [Art 1](https://tinyurl.com/ykyq2os5) ] | Medium | - |
195196

196197

197198
</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
3+
var (positionSpeedMap, sortedPositions, fleetCount, timeLastCarReached) = ([Int:Int](), position.sorted { $0 > $1 }, 0, -Double.infinity)
4+
for i in 0..<position.count {
5+
positionSpeedMap[position[i]] = speed[i]
6+
}
7+
8+
for carPosition in sortedPositions {
9+
let speed = positionSpeedMap[carPosition]!
10+
let arrivalTime = Double(target - carPosition) / Double(speed)
11+
if arrivalTime > timeLastCarReached {
12+
fleetCount += 1
13+
timeLastCarReached = arrivalTime
14+
}
15+
}
16+
17+
return fleetCount
18+
}
19+
}

0 commit comments

Comments
 (0)