-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0853.cpp
More file actions
44 lines (39 loc) · 1.08 KB
/
Copy path0853.cpp
File metadata and controls
44 lines (39 loc) · 1.08 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
39
40
41
42
43
44
class Solution {
public:
int carFleet(int target, vector<int> &position, vector<int> &speed) {
int n = position.size();
if (!n) return 0;
vector<pair<int, double>> vp(n + 1);
for (int i = 0; i < n; i++)
vp[i] = {position[i], (double)(target - position[i]) / speed[i]};
sort(vp.rbegin(), vp.rend());
int res = 0;
double ct = 0;
for (int i = 0; i < n; i++) {
auto [_, time] = vp[i];
if (time > ct) {
res++;
ct = time;
}
}
return res;
}
};
// Using map for the heavy lifting
class Solution {
public:
int carFleet(int target, vector<int> &position, vector<int> &speed) {
map<int, double> mp;
for (int i = 0; i < speed.size(); i++)
mp[-position[i]] = (double)(target - position[i]) / speed[i];
int res = 0;
double ct = 0;
for (auto [_, time] : mp) {
if (time > ct) {
res++;
ct = time;
}
}
return res;
}
};