Java

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

프로버티기 2025. 4. 12. 16:26

문제

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

 

프로그래머스

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

programmers.co.kr

 

 

 

입출력

 

 


아이디어

 

- 시간 (String) 비교할 때 분으로 바꿔서 계산하기 !

- PriorityQueue 사용 (그리디) 생각해보면 가장 짧은 시간 먼저 찾으면 됨

 

 

코드 

 

import java.util.*;
class Solution {
    public int solution(String[][] book_time) {
        int answer = 0;
        
        Arrays.sort(book_time, 
                    Comparator.comparing((String[] x) -> x[0])
                   .thenComparing((String[] x) -> x[1])
        );
       
        PriorityQueue<String> endTime = new PriorityQueue<>(); // 제일 짧은 시간 먼저 찾기 
        for (String[] book : book_time) {
            System.out.println(Arrays.toString(book));
            if (!endTime.isEmpty() && compareTime(endTime.peek(), book[0])) {
                endTime.poll();
            }
            endTime.add(book[1]);
        }
        
        return endTime.size();
    }
    
    static boolean compareTime(String a, String b) {
        int aMinutes = toMinutes(a) + 10;
        int bMinutes = toMinutes(b);
        return aMinutes <= bMinutes;
    }

    static int toMinutes(String time) { // 시간 계산할 때 분으로 비교하기 
        String[] parts = time.split(":");
        return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
    }
    
}

'Java' 카테고리의 다른 글

[codetree] 언덕 깎기  (0) 2025.05.18
[프로그래머스] 튜플  (0) 2025.04.12
[백준] 3020 : 개똥벌레  (0) 2025.04.06
[백준] 2098 : 외판원 순회  (0) 2025.04.06
[프로그래머스] 후보키  (0) 2025.04.03