My Solution
\n/**\n * Time Complexity: O(n_rooms * log(n_rooms) + n_queries * (log(n_rooms) + log(n_queries)))\n * Space Complexity: O(n_rooms + n_queries)\n * where `n_roo
```\n/**\n * Time Complexity: O(n_rooms * log(n_rooms) + n_queries * (log(n_rooms) + log(n_queries)))\n * Space Complexity: O(n_rooms + n_queries)\n * where `n_rooms` is the length of the vector `rooms`\n *       `n_queries` is the length of the vector `queries`\n */\nclass Solution {\n public:\n  vector<int> closestRoom(vector<vector<int>> &rooms, const vector<vector<int>> &queries) {\n    constexpr int invalid_room_id = -1;\n    sort(rooms.begin(), rooms.end(), [](const vector<int> &lhs, const vector<int> &rhs) -> bool {\n      return lhs.back() > rhs.back();\n    });\n    \n    const int n = static_cast<int>(rooms.size());\n    const int n_queries = static_cast<int>(queries.size());\n    int indices[n_queries];\n    iota(indices, indices + n_queries, 0);\n    sort(indices, indices + n_queries, [&queries](const int lhs, const int rhs) -> bool {\n      return queries[lhs].back() > queries[rhs].back();\n    });\n    \n    vector<int> ret(n_queries);\n    set<int> available_rooms;\n    for (int i_room = 0, i_query = 0; i_query < n_queries; ++i_query) {\n      const int index = indices[i_query];\n      const int preferred_room_id = queries[index].front();\n      const int min_room_size = queries[index].back();\n      for (; i_room < n && rooms[i_room].back() >= min_room_size; ++i_room) {\n        available_rooms.emplace(rooms[i_room].front());\n      }\n      \n      if (available_rooms.empty()) {\n        ret[index] = invalid_room_id;\n        continue;\n      }\n      \n      ret[index] = numeric_limits<int>::max();\n      auto ub = available_rooms.upper_bound(preferred_room_id);\n      if (ub != available_rooms.end()) {\n        ret[index] = *ub;\n      }\n      if (ub != available_rooms.begin() && preferred_room_id - *prev(ub) <= ret[index] - preferred_room_id) {\n        ret[index] = *prev(ub);\n      }\n    }\n    return ret;\n  }\n};\n```
[]