-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-time-to-repair-cars.cpp
61 lines (58 loc) · 1.87 KB
/
minimum-time-to-repair-cars.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Time: O(mx * log(mn * c^2)) = O(mx * (logc + log(mn))), c = cars, mx = max(ranks), mn = min(ranks)
// Space: O(mx)
// freq table, binary search
class Solution {
public:
long long repairCars(vector<int>& ranks, int cars) {
unordered_map<int, int> cnt;
for (const auto& r : ranks) {
++cnt[r];
}
const auto& check = [&](int64_t x) {
return accumulate(cbegin(cnt), cend(cnt), 0LL, [&](const auto& total, const auto& v) {
return total + static_cast<int64_t>(sqrt(x / v.first)) * v.second;
}) >= cars;
};
const int64_t mn = min_element(cbegin(cnt), cend(cnt), [](const auto& a, const auto& b) {
return a.first < b.first;
})->first;
int64_t left = 1, right = mn * cars * cars;
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};
// Time: O(c * log(mx)), c = cars, mx = max(ranks)
// Space: O(mx)
// freq table, heap, simulation
class Solution2 {
public:
long long repairCars(vector<int>& ranks, int cars) {
unordered_map<int, int> cnt;
for (const auto& r : ranks) {
++cnt[r];
}
vector<pair<int64_t, int>> tmp;
for (const auto& [r, _] : cnt) {
tmp.emplace_back(r * 1 * 1, 1);
}
using P = pair<int64_t, int>;
priority_queue<P, vector<P>, greater<P>> max_heap(cbegin(tmp), cend(tmp));
int64_t result;
while (cars > 0) {
auto [t, k] = max_heap.top(); max_heap.pop();
const auto& r = t / k / k;
cars -= cnt[r];
++k;
max_heap.emplace(r * k * k, k);
result = t;
}
return result;
}
};