# CSES Room Allocation TLE

Again, I have TLE on CSES using Java. Please let me know if I have the correct algorithm and my TLE is just because of Java? https://cses.fi/problemset/task/1164

What I am doing is using a min-heap (priority queue) in Java to store the end times of customers who already checked in. Then, for each new customer, I will check whether an existing room has opened up. (The end time in the priority queue should be less than the arrival times of the new customer). If there exists such a room, then I poll the element and assign that room ID to the new customer. The total rooms used is just the max room ID I go up to (or the max size of the priority queue of all time).

``````import java.io.*;
import java.util.*;

public class cses {
static int n;
static boolean[] used;
public static void main(String[] args) throws IOException{
PriorityQueue<Point> pq = new PriorityQueue<>();
int[][] times = new int[n];
int[] ans = new int[n];
for (int i=0; i<n; i++){
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
times[i] = start;
times[i] = end;
times[i] = i;
}
Arrays.sort(times, (a, b) -> a-b);
int rooms = 0;
int max = 0;
for (int i=0; i<n; i++){
int start = times[i];
int end = times[i];
if (pq.isEmpty()){
rooms++;
ans[times[i]] = rooms;
}else{
if (pq.peek().t < start){
int currid = pq.poll().id;
ans[times[i]] = currid;
}else{
rooms++;
ans[times[i]] = rooms;
}
}
max = Math.max(pq.size(), max);
}
System.out.println(max);
for (int i : ans){
System.out.print(i + " ");
}
}
static class Point implements Comparable<Point>{
int id;
int t;
public Point(int t, int id){
this.t = t;
this.id = id;
}

@Override
public int compareTo(Point o) {
return this.t - o.t;
}
}
}
``````