-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path853.cpp
More file actions
38 lines (35 loc) · 1.01 KB
/
Copy path853.cpp
File metadata and controls
38 lines (35 loc) · 1.01 KB
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
class Solution {
public:
struct Node {
int position;
int speed;
Node(const int &pos, const int &spe) {
position = pos;
speed = spe;
}
};
struct CompareNode {
bool operator()(const Node &node0, const Node &node1) {
return node0.position > node1.position;
}
};
int carFleet(int target, vector<int> &position, vector<int> &speed) {
int size = position.size();
vector<Node> car;
vector<double> time(size, 0);
for (int i = 0; i < size; ++i)
car.push_back(Node(position[i], speed[i]));
sort(car.begin(), car.end(), CompareNode());
for (int i = 0; i < size; ++i)
time[i] = 1.0 * (target - car[i].position) / car[i].speed;
int i = 0, fleet = 0;
while (i < size) {
double current = time[i];
++i;
while (i < size && current >= time[i])
++i;
++fleet;
}
return fleet;
}
};