-
Notifications
You must be signed in to change notification settings - Fork 3
/
1847.cpp
42 lines (41 loc) · 1.51 KB
/
1847.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
class Solution {
public:
static bool compare(const vector<int>& v1, const vector<int>& v2) {
if (v1[1] != v2[1]) return v1[1] > v2[1];
return v1[0] > v2[0];
}
vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {
set<int> roomSet;
int n = queries.size();
for (int i = 0; i < n; i++) {
queries[i].push_back(i);
}
sort(queries.begin(), queries.end(), compare);
sort(rooms.begin(), rooms.end(), compare);
int roomsIdx = 0;
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) {
int querySize = queries[i][1];
int queryRoom = queries[i][0];
int queryIdx = queries[i][2];
while (roomsIdx < rooms.size() && rooms[roomsIdx][1] >= querySize) {
roomSet.insert(rooms[roomsIdx][0]);
roomsIdx++;
}
if (roomSet.size() == 0) continue;
auto it = roomSet.lower_bound(queryRoom);
if (it == roomSet.begin()) res[queryIdx] = *it;
else {
auto prevIt = prev(it);
if (it == roomSet.end()) res[queryIdx] = *prevIt;
else {
int diffNext = abs(*it - queryRoom);
int diffPrev = abs(*prevIt - queryRoom);
if (diffPrev <= diffNext) res[queryIdx] = *prevIt;
else res[queryIdx] = *it;
}
}
}
return res;
}
};