본문 바로가기

Algorithm

[JAVA 알고리즘] 유연근무제

✅ 문제

 

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

 

프로그래머스

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

programmers.co.kr

 

✅ 코드

 

class Solution {
    // hhmm 형식의 정수를 분 단위로 바꿔주는 헬퍼
    private int toMinutes(int hhmm) {
        int hour = hhmm / 100;
        int minute = hhmm % 100;
        return hour * 60 + minute;
    }

    public int solution(int[] schedules, int[][] timelogs, int startday) {
        int answer = 0;
        int n = schedules.length;

        for (int i = 0; i < n; i++) {
            int hopeTimeMinutes = toMinutes(schedules[i]);
            int allowedLimit = hopeTimeMinutes + 10; // 희망 시각 + 10분

            boolean eligible = true;
            // 이벤트 7일차 순회
            for (int j = 0; j < 7; j++) {
                // 현재 날의 요일 계산: startday가 1=월 ... 7=일
                int dayOfWeek = ((startday - 1 + j) % 7) + 1;
                if (dayOfWeek == 6 || dayOfWeek == 7) {
                    // 토/일은 건너뛴다.
                    continue;
                }
                int actualTimeMinutes = toMinutes(timelogs[i][j]);
                if (actualTimeMinutes > allowedLimit) {
                    // 지각한 날이 하나라도 있으면 탈락
                    eligible = false;
                    break;
                }
            }
            if (eligible) answer++;
        }

        return answer;
    }
}