There are n cars going to the same destination along a one-lane road. The destination is target miles away.

You are given two integer array position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).

A car can never pass another car ahead of it, but it can catch up to it&nbsp;and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car's speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).

A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.

If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.

Return the number of car fleets that will arrive at the destination.

&nbsp;
Example 1:

Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12.
The car starting at 0 does not catch up to any other car, so it is a fleet by itself.
The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
Note that no other cars meet these fleets before the destination, so the answer is 3.


Example 2:

Input: target = 10, position = [3], speed = [3]
Output: 1
Explanation: There is only one car, hence there is only one fleet.


Example 3:

Input: target = 100, position = [0,2,4], speed = [4,2,1]
Output: 1
Explanation:
The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The fleet moves at speed 2.
Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.


&nbsp;
Constraints:


	n == position.length == speed.length
	1 &lt;= n &lt;= 105
	0 &lt; target &lt;= 106
	0 &lt;= position[i] &lt; target
	All the values of position are unique.
	0 &lt; speed[i] &lt;= 106


class Solution:
    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
        def computeArrivalTime(curr_pos, curr_speed):
            nonlocal target
            return (target - curr_pos) / curr_speed
            # avoid integer division, as a car may arrive at 5.2s and another at 5.6s

        cars = list(zip(position, speed))
        cars.sort(key=lambda x: x[0], reverse=True)
        arrival_bound = None # time upper bound
        fleet = 0
        for pos, sp in cars:
            curr_arrival = computeArrivalTime(pos, sp)
            if not arrival_bound or curr_arrival > arrival_bound:
                arrival_bound = curr_arrival
                fleet += 1
        return fleet
    # time O(n logn): sort = (nlogn); loop = (n)
    # space O(n): depend on sort
class Solution {
    class pair implements Comparable<pair>{
        int pos;
        double time;
        pair(int pos,double time){
            this.pos=pos;
            this.time=time;
        }
        public int compareTo(pair o){
            return o.pos-this.pos;
        }
    }
    public int carFleet(int target, int[] position, int[] speed) {
        double []arr=new double[position.length];
        for(int i=0;i<position.length;i++){
            arr[i]=(target-position[i])*1.0/speed[i];
        }
        PriorityQueue<pair>pq=new PriorityQueue<>();
        for(int i=0;i<position.length;i++){
            pq.add(new pair(position[i],arr[i]));
        }
        double updatetime=0;
        int fleet=0;
        while(pq.size()>0){
            pair rem=pq.remove();
            if(updatetime<rem.time){
               fleet++;
               updatetime=rem.time;
            }
        }
        return fleet;
    }
}
class Solution {
public:
    int carFleet(int target, vector<int>& position, vector<int>& speed) {
        
        int n = position.size();
        vector<pair<int,int>> cars;
        
        for(int i=0; i<n; i++)cars.push_back({position[i], speed[i]});
        
        sort(cars.begin(),cars.end());
        
        int start=0;
        int mid=0;
        int fleet = n;
        
        double times[n];
        
        for(int i=n-1; i>=0; i--){
            times[i] =((double) (target-cars[i].first)/cars[i].second);
            if(i<n-1)times[i] = max(times[i],times[i+1]);
        }
        
        while(mid<n){            
            double timeA = times[start];
            double timeB = times[mid];
            //cout<<timeA<<" "<<timeB<<endl;
            if(mid-start+1 == 2){
                if(timeA<=timeB)fleet--;
                start++;
            }
            mid++;
        }
        
        return fleet;
        
    }
};
var carFleet = function(target, position, speed) {
    for (let i = 0 ; i < position.length ; i ++) {
        position[i] = [target - position[i], speed[i]]
    }
    position.sort((a, b) => { return a[0] - b[0] })
    let count = 1, prev = position[0][0] / position[0][1]
    for (let i = 1 ; i < position.length ; i ++) {
		// if the time taken is longer then it will cause another fleet
        if (position[i][0] / position[i][1] > prev) {
            count ++
            prev = position[i][0] / position[i][1]
        }
    }
    
    return count
};
Car Fleet