-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar Fleet.cpp
36 lines (33 loc) · 897 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
// say Alhamdulillah
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_multiset;
class Solution {
public:
int carFleet(int target, vector<int> &position, vector<int> &speed) {
indexed_multiset ms;
for (int i = 0; i < position.size(); i++) {
ms.insert({position[i], speed[i]});
}
int ans = 1;
pair<int, int> pr;
double last;
pr = *(--ms.end());
last = (target - pr.first) / (float)pr.second;
ms.erase(--ms.end());
while (!ms.empty()) {
pr = *(--ms.end());
ms.erase(--ms.end());
double time = (target - pr.first) / (float)pr.second;
if (time > last) {
ans++;
last = time;
}
}
return ans;
}
};