In this problem we’re given n cars traveling to the same destination on a one-lane highway. We’re given two arrays of integers position and speed, both of length n.
position[i] is the position of the ith car (in miles)
speed[i] is the speed of the ith car (in miles per hour)
Example
Approach 1
Visualize the problem
Since we’re given the position and speed of the cars we know we calculate the time the cars arrive to the target position
If they arrive at the same time or before another fleet then they are apart of that fleet
We’ll keep a stack to keep track of fleets. each value in stack will be a different car fleet, therefore, we can pop any cars that end up joining with another fleet
Since this is a one way road , we know that the car at the closest position will be leading and cannot be overpassed. Therefore, it would be best to start comparing from right to left.