기타/코딩테스트

(코테) 프로그래머스_셔틀버스 *시물레이션 정렬

불광동 물주먹 2025. 7. 30. 05:42

 

 

 

 

 

 

 

 

import java.util.*;

class Solution {
    public String solution(int n, int t, int m, String[] timetable) {
        List<Integer> wait = new ArrayList<>();
        
        for (String time : timetable) {
            String[] parts = time.split(":");
            int minute = Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
            wait.add(minute);
        }
        
        Collections.sort(wait);
        
        int crewIdx = 0;
        int shuttleTime = 540; // 09:00 in minutes
        int lastBoarded = 0;
        
        for (int i = 0; i < n; i++) {
            int count = 0;
            
            while (crewIdx < wait.size() && wait.get(crewIdx) <= shuttleTime && count < m) {
                lastBoarded = wait.get(crewIdx);
                crewIdx++;
                count++;
            }
            
            if (i == n - 1) {
                // 마지막 셔틀
                if (count == m) {
                    // 만석이면 마지막 탄 사람보다 1분 빨리 와야 함
                    return toTimeString(lastBoarded - 1);
                } else {
                    // 자리 남았으면 도착 시간에 타면 됨
                    return toTimeString(shuttleTime);
                }
            }
            
            shuttleTime += t;
        }
        
        return ""; // 도달 불가
    }

    private String toTimeString(int minutes) {
        int hour = minutes / 60;
        int min = minutes % 60;
        return String.format("%02d:%02d", hour, min);
    }
}

 

 

 

 

 

1. String.format("%02d:%02d", hour,min);  stringformat으로 0채워주기