<p>There are <code>n</code> cars at given miles away from the starting mile 0, traveling to reach the mile <code>target</code>.</p>

<p>You are given two integer array <code>position</code> and <code>speed</code>, both of length <code>n</code>, where <code>position[i]</code> is the starting mile of the <code>i<sup>th</sup></code> car and <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> car in miles per hour.</p>

<p>A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.</p>

<p>A <strong>car fleet</strong> is a car or cars driving next to each other. The speed of the car fleet is the <strong>minimum</strong> speed of any car in the fleet.</p>

<p>If a car catches up to a car fleet at the mile <code>target</code>, it will still be considered as part of the car fleet.</p>

<p>Return the number of car fleets that will arrive at the destination.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<ul>
	<li>The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at <code>target</code>.</li>
	<li>The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.</li>
	<li>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 <code>target</code>.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">target = 10, position = [3], speed = [3]</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>

<p><strong>Explanation:</strong></p>
There is only one car, hence there is only one fleet.</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">target = 100, position = [0,2,4], speed = [4,2,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>

<p><strong>Explanation:</strong></p>

<ul>
	<li>The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.</li>
	<li>Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches <code>target</code>.</li>
</ul>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == position.length == speed.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt; target &lt;= 10<sup>6</sup></code></li>
	<li><code>0 &lt;= position[i] &lt; target</code></li>
	<li>All the values of <code>position</code> are <strong>unique</strong>.</li>
	<li><code>0 &lt; speed[i] &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution {
public:
    bool canMerge(pair<double, double>& car1, pair<double, double>&car2, int target) {
        auto &[pos1, speed1] = car1;
        auto &[pos2, speed2] = car2;
        if (pos1 < pos2 && speed1 <= speed2 || pos1 > pos2 && speed1 >= speed2) {
            return false;
        }
        double distance = abs(pos1 - pos2);
        double hour = (distance) / abs(speed1 - speed2);
        // cout << "hour = " << hour << "\n";
        // cout << "final catch = " << pos1 + hour * speed1 << "\n";
        // cout << "check can merge\n";
        // cout << car1.first << " " << car1.second << "\n";
        // cout << car2.first << " " << car2.second << "\n";

        if (pos1 + hour * speed1 <= target) {
            // cout << "merge\n";
            return true;
        }
        return false; 
    }
    int merge(vector<pair<double, double>> &fleet, int target) {
        int n = fleet.size();
        int cur = n - 2;
        int last = n - 1;
        int res = n;
        while(cur >= 0 && last > cur) {
            if (!canMerge(fleet[cur], fleet[last], target)) {
                last = cur;
                cur = last - 1;
            } else {
                cur --;
                res --;
            }
        }
        return res;

    }
    int carFleet(int target, vector<int>& position, vector<int>& speed) {
        int n = position.size();
        vector<pair<double, double>> car(n);
        for (int i = 0; i < n; i ++) {
            car[i] = {position[i], speed[i]};
        }
        sort(car.begin(), car.end());
        vector<pair<double, double>> fleet;

        for (int i = 0; i < n; i ++) {
            // cout << car[i].first << " " << car[i].second << "\n";
            fleet.push_back(car[i]);
        }
        // if both position and speed of the car is small
        // there are a fleet itself
        // else there may be anther car that could catch it
        // I need find the first that catch it
        // there should i and j < k
        // fleet.push_back(car[0]);

        return merge(fleet, target);
    }
};
94459