structCar{intpos;doubletime;// Time to reach the target};classSolution{public:intcarFleet(inttarget,vector<int>&position,vector<int>&speed){intans=0;vector<Car>cars(position.size());for(inti=0;i<position.size();++i)cars[i]={position[i],(double)(target-position[i])/speed[i]};sort(begin(cars),end(cars),[](constauto&a,constauto&b){returna.pos>b.pos;});doublemaxTime=0;// The time of the slowest car to reach the targetfor(constCar&car:cars)// A car needs more time to reach the target, so it becomes slowestif(car.time>maxTime){maxTime=car.time;++ans;}returnans;}};
classCar{publicintpos;publicdoubletime;publicCar(intpos,doubletime){this.pos=pos;this.time=time;}}classSolution{publicintcarFleet(inttarget,int[]position,int[]speed){intans=0;Car[]cars=newCar[position.length];for(inti=0;i<position.length;++i)cars[i]=newCar(position[i],(double)(target-position[i])/speed[i]);Arrays.sort(cars,(a,b)->b.pos-a.pos);doublemaxTime=0;// The time of the slowest car to reach the targetfor(Carcar:cars)// A car needs more time to reach the target, so it becomes slowestif(car.time>maxTime){maxTime=car.time;++ans;}returnans;}}
classSolution:defcarFleet(self,target:int,position:List[int],speed:List[int])->int:ans=0times=[float(target-p)/sforp,sinsorted(zip(position,speed),reverse=True)]maxTime=0# The time of the slowest car to reach the targetfortimeintimes:# A car needs more time to reach the target, so it becomes slowestiftime>maxTime:maxTime=timeans+=1returnans