Hotel Queries

There are n hotels, and each hotel has some free rooms.

For every tourist group in queries, assign the group to the first hotel that has enough free rooms.

After assigning, reduce that hotel’s free rooms. If no hotel can fit the group, return 0 for that group.

Return the hotel index (1-based) assigned to each group.

Example 1:

Input: hotels = [3, 1, 4, 2], queries = [2, 3, 1]

Output: [1, 3, 1]

Explanation:

  • Group 1 needs 2 rooms. The first hotel with at least 2 free rooms is hotel 1. Remaining rooms: [1, 1, 4, 2]
  • Group 2 needs 3 rooms. The first hotel with at least 3 free rooms is hotel 3. Remaining rooms: [1, 1, 1, 2]
  • Group 3 needs 1 room. The first hotel with at least 1 free room is hotel 1. Remaining rooms: [0, 1, 1, 2]

So, the answer is [1, 3, 1].

Example 2:

Input: hotels = [2, 2, 2], queries = [3, 2, 2, 1]

Output: [0, 1, 2, 3]

Explanation:

  • Group 1 needs 3 rooms. No hotel has 3 free rooms, so answer is 0.
  • Group 2 needs 2 rooms. Hotel 1 gets assigned. Remaining: [0, 2, 2]
  • Group 3 needs 2 rooms. Hotel 2 gets assigned. Remaining: [0, 0, 2]
  • Group 4 needs 1 room. Hotel 3 gets assigned. Remaining: [0, 0, 1]

So, the answer is [0, 1, 2, 3].

Now Your Turn!

Pick the correct output for the given input

Input: hotels = [5, 3, 4], queries = [4, 2, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 2 * 105
  • 1 <= m <= 2 * 105
  • hotels.length == n
  • queries.length == m
  • 1 <= hotels[i] <= 109
  • 1 <= queries[j] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> hotelQueries(vector<int> &hotels, vector<int> &queries) {
// Your code goes here
}
};
 
Test Case

Input:

Hotels
Queries