Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
667 B
2
Indexable
Never
class Solution {
public:
    static bool cmp(const vector<double>a,const vector<double>b){
        return a[0]<b[0];
    }

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

        stack<double>s;
    
        for(int i=car.size()-1;i>=0;i--){
            double time=(target-car[i][0])/car[i][1];
            if(s.empty() || time>s.top()) s.push(time);
        }
        return s.size();
    }
};
Leave a Comment