-
Notifications
You must be signed in to change notification settings - Fork 106
/
campus-bikes.cpp
42 lines (39 loc) · 1.41 KB
/
campus-bikes.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
// Time: O((w * b) * log(w * b))
// Space: O(w * b)
class Solution {
public:
vector<int> assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {
using P = vector<int>;
vector<vector<P>> distances(workers.size());
for (int i = 0; i < workers.size(); ++i) {
for (int j = 0; j < bikes.size(); ++j) {
distances[i].push_back({manhattan(workers[i], bikes[j]), i, j});
}
sort(distances[i].begin(), distances[i].end(), greater<P>());
}
vector<int> result(workers.size());
unordered_set<int> lookup;
priority_queue<P, vector<P>, greater<P>> min_heap;
for (int i = 0; i < workers.size(); ++i) {
min_heap.emplace(distances[i].back());
distances[i].pop_back();
}
while (lookup.size() < workers.size()) {
const auto worker = min_heap.top()[1];
const auto bike = min_heap.top()[2];
min_heap.pop();
if (!lookup.count(bike)) {
result[worker] = bike;
lookup.emplace(bike);
} else {
min_heap.emplace(distances[worker].back());
distances[worker].pop_back();
}
}
return result;
}
private:
int manhattan(const vector<int>& p1, const vector<int>& p2) {
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]);
}
};