File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun carFleet (target : Int , position : IntArray , speed : IntArray ): Int {
3+ val sortedPairs = position
4+ .zip(speed)
5+ .sortedBy { (position, _) -> position }
6+ var numberOfFleets = 1
7+ var timeRequiredForCarInFrontToReachInTarget =
8+ (target - sortedPairs[sortedPairs.lastIndex].first) / sortedPairs[sortedPairs.lastIndex].second.toFloat()
9+ var timeRequiredForCurrentCarToReachTarget: Float
10+ for (i in (sortedPairs.lastIndex - 1 ) downTo 0 ) {
11+ timeRequiredForCurrentCarToReachTarget = (target - sortedPairs[i].first) / sortedPairs[i].second.toFloat()
12+ if (timeRequiredForCurrentCarToReachTarget > timeRequiredForCarInFrontToReachInTarget) {
13+ // the current car requires more time to reach the destination
14+ // than the car in front of it.
15+ numberOfFleets++
16+ timeRequiredForCarInFrontToReachInTarget = timeRequiredForCurrentCarToReachTarget
17+ }
18+ }
19+ return numberOfFleets
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments