There is a hotel with n
rooms. The rooms are represented by a 2D integer array rooms
where rooms[i] = [roomIdi, sizei]
denotes that there is a room with room number roomIdi
and size equal to sizei
. Each roomIdi
is guaranteed to be unique.
You are also given k
queries in a 2D array queries
where queries[j] = [preferredj, minSizej]
. The answer to the jth
query is the room number id
of a room such that:
- The room has a size of at least
minSizej
, and abs(id - preferredj)
is minimized, whereabs(x)
is the absolute value ofx
.
If there is a tie in the absolute difference, then use the room with the smallest such id
. If there is no such room, the answer is -1
.
Return an array answer
of length k
where answer[j]
contains the answer to the jth
query.
Example 1:
Input: rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]] Output: [3,-1,3] Explanation: The answers to the queries are as follows: Query = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3. Query = [3,3]: There are no rooms with a size of at least 3, so the answer is -1. Query = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. 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] Explanation: The answers to the queries are as follows: Query = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2. Query = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller. Query = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.
Constraints:
n == rooms.length
1 <= n <= 105
k == queries.length
1 <= k <= 104
1 <= roomIdi, preferredj <= 107
1 <= sizei, minSizej <= 107
Solution: Off Processing: Sort + Binary Search


Time complexity: O(nlogn + mlogm)
Space complexity: O(n + m)
Sort queries and rooms by size in descending order, only add valid rooms (size >= min_size) to the treeset for binary search.
C++
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 |
// Author: Huahua class Solution { public: vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) { const int n = rooms.size(); const int m = queries.size(); for (int i = 0; i < m; ++i) queries[i].push_back(i); // Sort queries by size DESC. sort(begin(queries), end(queries), [](const auto& q1, const auto& q2){ return q1[1] > q2[1]; }); // Sort room by size DESC. sort(begin(rooms), end(rooms), [](const auto& r1, const auto& r2) { return r1[1] > r2[1]; }); vector<int> ans(m); int j = 0; set<int> ids; for (const auto& q : queries) { while (j < n && rooms[j][1] >= q[1]) ids.insert(rooms[j++][0]); if (ids.empty()) { ans[q[2]] = -1; continue; } const int id = q[0]; auto it = ids.lower_bound(id); int id1 = (it != end(ids)) ? *it : INT_MAX; int id2 = id1; if (it != begin(ids)) id2 = *prev(it); ans[q[2]] = abs(id1 - id) < abs(id2 - id) ? id1 : id2; } return ans; } }; |
Solution 2: Fenwick Tree

Time complexity: O(nlogS + mlogSlogn)
Space complexity: O(nlogS)
C++
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 |
class Solution { public: vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) { S = 0; for (const auto& r : rooms) S = max(S, r[1]); for (const auto& r : rooms) update(r[1], r[0]); vector<int> ans; for (const auto& q : queries) ans.push_back(query(q[1], q[0])); return ans; } private: int S; void update(int s, int id) { for (s = S - s + 1; s <= S; s += s & (-s)) tree[s].insert(id); } int query(int s, int id) { int ans = -1; for (s = S - s + 1; s > 0; s -= s & (-s)) { if (!tree.count(s)) continue; int id1 = INT_MAX; int id2 = INT_MAX; auto it = tree[s].lower_bound(id); if (it != end(tree[s])) id1 = *it; if (it != begin(tree[s])) id2 = *prev(it); int cid = (abs(id1 - id) < abs(id2 - id)) ? id1 : id2; if (ans == -1 || abs(cid - id) < abs(ans - id)) ans = cid; else if (abs(cid - id) == abs(ans - id)) ans = min(ans, cid); } return ans; } unordered_map<int, set<int>> tree; }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment