문제 호텔 대실
import java.util.*;
class Solution {
public int solution(String[][] book_time) {
int answer = 0;
Time []books = new Time[book_time.length];
for(int i=0;i<book_time.length;i++)
{
String []s =book_time[i];
books[i] = new Time(dayTime(s[0]),dayTime(s[1])+10);
}
//대실 시작시간으로 오름차순 정렬
Arrays.sort(books,(x,y) -> (x.start - y.start));
//대실 종료시간으로 오름차순 정렬
PriorityQueue<Time> pq = new PriorityQueue<>((x,y)->(x.end-y.end));
for(Time book : books)
{
//큐가 비어있다면 방하나 추가
if(pq.isEmpty())
{
answer++;
pq.offer(book);
continue;
}
Time prev = pq.peek();
//대실 중인 방과 현재 시간의 예약
if(book.start >= prev.end )
{
pq.poll();//기존에 있던 대실은 끝
pq.offer(book);
}else{
answer++;//방추가
pq.offer(book);
}
}
return answer;
}
public int dayTime(String s){
String temp[] = s.split(":");
return Integer.parseInt(temp[0])*60 + Integer.parseInt(temp[1]);
}
class Time{
int start;
int end;
public Time(int start,int end)
{
this.start = start;
this.end = end;
}
}
}