# Leetcode 1847. Nearest room (sort offline calculation + binary search)

2021-05-04 12:07:35

## 1. subject

A hotel has n A room , These rooms are two-dimensional arrays of integers rooms Express , among `rooms[i] = [roomIdi, sizei]` It means there is a room number roomIdi And its area is sizei . Every room number roomIdi Guarantee is unique Of .

At the same time k A query , Use a two-dimensional array queries Express , among `queries[j] = [preferredj, minSizej]` . The first j The answer to the first query is a room that meets the following conditions id ：

• The area of the room At least by minSizej , And
`abs(id - preferredj)` Value Minimum , among abs(x) yes x The absolute value of .
• If the absolute value of the difference is equal Of , choice Minimum Of id . If There are no rooms that meet the requirements , The answer for -1 .

Please return the length of k Array of answer , among answer[j] For the first time j The result of a query .

`````` Example  1：
Input ：rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
Output ：[3,-1,3]
explain ： The answers to the query are as follows ：
Inquire about  [3,1] ： room  3  The area of is  2 , Greater than or equal to  1 , And the number is the closest  3  Of , by  abs(3 - 3) = 0 , So the answer is  3 .
Inquire about  [3,3] ： No room is at least  3 , So the answer is  -1 .
Inquire about  [5,2] ： room  3  The area of is  2 , Greater than or equal to  2 , And the number is the closest  5  Of , by  abs(3 - 5) = 2 , So the answer is  3 .

Example  2：
Input ：rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]
Output ：[2,1,3]
explain ： The answers to the query are as follows ：
Inquire about  [2,3] ： room  2  The area of is  3 , Greater than or equal to  3 , And the number is the closest , by  abs(2 - 2) = 0 , So the answer is  2 .
Inquire about  [2,4] ： room  1  and  3  All of them are at least  4 , The answer for  1  Because it has a smaller room number .
Inquire about  [2,5] ： room  3  Is the only area greater than or equal to  5  Of , So the answer is  3 .

Tips ：
n == rooms.length
1 <= n <= 10^5
k == queries.length
1 <= k <= 10^4
1 <= roomIdi, preferredj <= 10^7
1 <= sizei, minSizej <= 10^7
``````

source ： Power button （LeetCode） link ：https://leetcode-cn.com/problems/closest-room
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

## 2. Problem solving

• First of all rooms Sort , The big ones first , Inquire about q It's also , Check the big ones first （ In subsequent queries , Before the room size is to meet the requirements of ）
• Then query in turn , Will meet the size of the room id Insert set, Conduct Two points search , Find the closest id
``````class Solution {

public:
vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& q) {

//  Room sizes are sorted from large to small
vector<pair<int,int>> R(rooms.size());
int k = 0;
for(auto& r : rooms)
{

R[k] = {
rooms[k][0], rooms[k][1]};
k++;
} // vector  turn   become  pair , Sort , Don't do this , Overtime ...
sort(R.begin(), R.end(),[&](auto a, auto b){

return a.second > b.second;
});
int n = q.size();
vector<int> ans(n, -1);
vector<int> idx(n);
iota(idx.begin(), idx.end(),0);
sort(idx.begin(), idx.end(),[&](auto a, auto b){

return q[a][1] > q[b][1];// Query according to the large to small query
});
set<int> s;
int j = 0, preferred, minSize, closest, minidgap;
for(auto i : idx)
{

preferred = q[i][0];
minSize = q[i][1];
while(j < R.size() && R[j].second >= minSize)
{
//  The dimensions meet the requirements , Insert  id  To  set
s.insert(R[j].first);
j++;
}
closest = -1;
minidgap = INT_MAX;
auto it = s.lower_bound(preferred);// Two points search
if(it != s.end())
{

closest = *it;
minidgap = *it - preferred;
}
if(it != s.begin())
{

--it;
if(preferred - *it <= minidgap)
{

closest = *it;
}
}
ans[i] = closest;
}
return ans;
}
};
``````

608 ms 146.7 MB C++

Long click or sweep code pay attention to my official account （Michael amin ）, Come on together 、 Learn together ！

https://chowdera.com/2021/05/20210504120225670l.html