VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-room-allocation/

⇱ CSES Solutions - Room Allocation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Room Allocation

Last Updated : 23 Jul, 2025

There is a large hotel, and N customers will arrive soon. Each customer wants to have a single room. You know each customer's arrival and departure day as timings[][] such that timings[i][0] is the arrival time and timings[i][1] is the departure time of ith person. Two customers can stay in the same room if the departure day of the first customer is earlier than the arrival day of the second customer.

What is the minimum number of rooms that are needed to accommodate all customers? And how can the rooms be allocated?

Note: If k is the minimum number of rooms, then the rooms are numbered from 1 to k. Return any valid solution.

Examples:

Input: N = 3, timings[][] = {{1, 2}, {2, 4}, {4, 4}}
Output:
2
1 2 1
Explanation:

  • The first person arrives at time = 1 and gets room 1.
  • The second person arrives at time = 2. Since room 1 is already occupied by the first person, second person will get room 2.
  • The third person arrives at time = 4. Since the first person has already left, third person will get room 1.

Input: N = 4, timings[][] = {{6, 6}, {5, 5}, {6, 6}, {5, 10}}
Output:
3
1 1 3 2
Explanation:

  • The second person arrivesat time = 5 and gets room 1.
  • The fourth person arrives at time = 5. Since room 1 is already occupied by second person, fourth person will get room 2.
  • The first person arrives at time = 6. Since the second person has already left, the first person will get room 1.
  • The third person arrives at time = 6. Since room1 is already occupied by first person and room2 is already occupied by fourth person, the third person will get room 3.

Approach: To solve the problem, follow the below idea:

The problem can be solved using Greedy Approach. Sort all the customers in increasing order of their arrival times. This way, a customer who arrives earlier gets a room before a customer who arrives later. Also, keep track of the departure times of the customers we have already given rooms to, using a Priority Queue (Min Heap).

For each new customer, we check if any of the current customers will leave before the new customer arrives by comparing the arrival time with the top of the priority queue.

  • If any of the current customers will leave, then the new customer can take the room of the customer who is leaving. So, we update the departure time in our system with the departure time of the new customer.
  • If no customer leaves, that means all the rooms are currently occupied and will still be occupied when the new customer arrives. So, we need to provide a new room for this customer and add their departure time to our system.

The minimum number of rooms needed is the largest number of rooms we find ourselves using at any point in this process.


Output
2
1 2 1 

Time Complexity: O(n * log n), where n is the number of customers.
Auxiliary Space: O(n)


Comment
Article Tags: