즉, [1-3] 과 [3-5] 의 강의실과 [2-4]의 강의실 총 2개가 필요함을 알 수 있다.
이러한 순회를 마치고 나서 큐의 size 를 출력해주면 된다는 의미이다.
// 우선순위 큐에는 종료 시간만 들어가고, 오름차순으로 자동정렬
q.offer(L[1].edate);
for(int i=2; i<=N; i++)
{
/*
* peek() : 우선순위가 가장 높은 값을 참조
* 종료시간 <= 시작시간의 경우에 직전의 종료시간을 꺼내고 다음 종료시간을 넣고 계속 비교
*/
if(q.peek() <= L[i].sdate) q.poll();
// 순회하면서 현재 end 값을 넣어준다.
q.offer(L[i].edate);
}
System.out.println(q.size());
이러한 로직의 전체 코드는 아래와 같다.
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();
static PriorityQueue<Integer> q = new PriorityQueue<>();
static Lecture[] L;
static int N;
static class Lecture
{
public int sdate;
public int edate;
Lecture(int startDate, int endDate)
{
this.sdate = startDate;
this.edate = endDate;
}
}
static void input()
{
N = scan.nextInt();
L = new Lecture[N+1];
for(int i=1; i<=N; i++) L[i] = new Lecture(scan.nextInt(), scan.nextInt());
}
static void pro()
{
// 오름차순 정렬
Arrays.sort(L, 1, N+1, new Comparator<Lecture>() {
@Override
public int compare(Lecture o1, Lecture o2) {
if(o1.sdate == o2.sdate) return o1.edate - o2.edate;
return o1.sdate - o2.sdate;
}
});
// 우선순위 큐에는 종료 시간만 들어가고, 오름차순으로 자동정렬
q.offer(L[1].edate);
for(int i=2; i<=N; i++)
{
/*
* peek() : 우선순위가 가장 높은 값을 참조
* 종료시간 <= 시작시간의 경우에 직전의 종료시간을 꺼내고 다음 종료시간을 넣고 계속 비교
*/
if(q.peek() <= L[i].sdate) q.poll();
// 순회하면서 현재 end 값을 넣어준다.
q.offer(L[i].edate);
}
System.out.println(q.size());
}
public static void main(String[] args)
{
input();
pro();
}
급하게 조사하고 정리하다보니 부족하거나 틀린 내용이 있을 수도 있는데 댓글로 달아주시면 감사하겠습니다!