Car Fleet LeetCode solution in C++

1
class Car {
2
public:
3
Car(int pos, int speed) {
4
this->pos = pos;
5
this->speed = speed;
6
}
7
int pos;
8
int speed;
9
};
10

11
class Solution {
12
public:
13
int carFleet(int target, vector<int> &position, vector<int> &speed) {
14
vector<Car> cars;
15
int N = position.size();
16
for (int i = 0; i < N; i++) {
17
cars.emplace_back(position.at(i), speed.at(i));
18
}
19

20
sort(cars.begin(), cars.end(), [](const Car &a, const Car &b) { return a.pos < b.pos; });
21

22
stack<float> mono;
23
for (int i = 0; i < N; i++) {
24
float time = (target - cars.at(i).pos) / (float)cars.at(i).speed;
25
while (!mono.empty() && time >= mono.top()) {
26
mono.pop();
27
}
28
mono.push(time);
29
}
30
return mono.size();
31
}
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0