호텔대실

Seongjin Jo·2023년 7월 2일
0

프로그래머스 LV2

목록 보기
15/28

문제

풀이

import java.util.*;
class Solution {
    public int solution(String[][] book_time) {
        int answer = 1;
        
        int[][] time = new int[book_time.length][2];
        for(int i=0; i<book_time.length; i++){
            int st = Integer.parseInt(book_time[i][0].replace(":",""));
            int et = Integer.parseInt(book_time[i][1].replace(":",""));
            
            et = et+10;
            if(et%100>=60) et+=40;
            
            time[i][0] = st;
            time[i][1] = et;
        }
        
        // 예약시간 기준으로 정렬, (동일한 경우 종료시간 기준 정렬)
        Arrays.sort(time, (o1, o2) -> {
            if(o1[0]==o2[0]) return o1[1]-o2[1];
            return o1[0]-o2[0];
        });
        
        
        PriorityQueue<Integer> pq = new PriorityQueue();
        
        
        for(int i=0; i<time.length; i++){
            if(pq.size()==0) {
                pq.add(time[0][1]);
                continue;
            }
            
            if(time[i][0] < pq.peek()){
                pq.add(time[i][1]);
                answer++;
            }
            else{
                pq.poll();
                pq.add(time[i][1]);
            }
        }
        
        return answer;
    }
}

문제의 핵심
1-1. 정렬을 위해서 int타입으로 변경한 후에 새로운 배열에 저장.
1-2. 근데 여기서 객실 종료시간이후에 10분동안 청소시간이 주어진다고 했음.
2. Arrays.sort에서 람다를 이용한 정렬
3. PrioritiyQueue를 이용한 시작,종료 시간 비교하면서 객실 체크

0개의 댓글