Leetcode 883 Solution

This article provides solution to leetcode question 883 (car-fleet)

https://leetcode.com/problems/car-fleet

Solution

class Solution {
public:
    int carFleet(int target, vector<int>& position, vector<int>& speed) {
        vector<pair<int, double>> a;

        for (int i = 0; i < position.size(); i++)
            a.push_back(make_pair(position[i], (target - position[i]) * 1.0 / speed[i]));

        sort(a.begin(), a.end());

        double last_t = 0;
        int count = 0;

        for (int i = a.size() - 1; i >= 0; i--)
        {
            if (last_t < a[i].second)
            {
                count++;
                last_t = a[i].second;
            }
        }

        return count;
    }
};