-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCar Fleet.cpp
37 lines (33 loc) · 925 Bytes
/
Car Fleet.cpp
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
// Runtime: 215 ms (Top 30.21%) | Memory: 78.90 MB (Top 67.67%)
class Car{
public:
Car(int pos, int speed){
this->pos = pos;
this->speed= speed;
}
int pos;
int speed;
};
class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
vector<Car> cars;
int N = position.size();
for(int i = 0; i<N; i++){
cars.emplace_back(position.at(i), speed.at(i));
}
sort(cars.begin(), cars.end(), [](const Car& a, const Car& b){
return a.pos<b.pos;
});
stack<float> mono;
for(int i = 0; i<N; i++){
float time =
(target-cars.at(i).pos)/(float)cars.at(i).speed;
while(!mono.empty() && time >= mono.top()){
mono.pop();
}
mono.push(time);
}
return mono.size();
}
};