슈콩

[프로그래머스] Lv.2 호텔 대실 본문

Algorithms/Programmers

[프로그래머스] Lv.2 호텔 대실

shukong 2025. 10. 22. 23:55

 

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/155651#qna

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

import java.util.*;

class Solution {
    public int solution(String[][] book_time) {
        int answer = 0;
        Arrays.sort(book_time, (a, b) -> a[0].compareTo(b[0]));
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        for (int i = 0; i < book_time.length; i++) {
            String[] startSplit = book_time[i][0].split(":");
            int sTime = Integer.parseInt(startSplit[0]) * 60 + Integer.parseInt(startSplit[1]);

            String[] endSplit = book_time[i][1].split(":");
            int eTime = Integer.parseInt(endSplit[0]) * 60 + Integer.parseInt(endSplit[1]) + 10;

            while (!pq.isEmpty() && pq.peek() <= sTime) {
                pq.poll();
            }

            pq.offer(eTime);
            answer = Math.max(answer, pq.size());
        }

        return answer;
    }
}