-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
853. Car Fleet I
46 lines (35 loc) · 1.58 KB
/
853. Car Fleet I
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*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 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.
class Solution {
class Car{
public:
int pos,speed;
Car(int p,int s):pos(p), speed(s){};
};
static bool mycomp(Car&a,Car&b){
return a.pos<b.pos;
}
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
vector<Car>cars;
for(int i=0;i<position.size();i++){
Car car(position[i],speed[i]);
cars.push_back(car);
}
sort(cars.begin(),cars.end(),mycomp);
stack<float>st;
for(auto car:cars){
float time = (target-car.pos)/((float)car.speed);
while(!st.empty() && time>= st.top()){
st.pop();
}
st.push(time);
}
return st.size();
}
};
//medium but tough though