PS/문제 풀이

[프로그래머스/Swift] 388351번 - 유연근무제

시르베어 2025. 4. 14. 09:59

🔗 문제 링크

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

💡 구현 아이디어

  1. 월~금요일 시간을 뽑아냄
  2. 시간 비교는 분단위로 진행
  3. 분단위 시간 = (표현된 시간 / 100 * 60) + (표현된 시간 % 100) = 시의 분 + 분
  4. 희망 시간이 실제 시간보다 작거나 같으면 통과, 크면 실패
  5. 한 사람에 대해 전체 통과하면 1 아니면 0
  6. 결과 반환

💻 코드

import Foundation

func solution(_ schedules:[Int], _ timelogs:[[Int]], _ startday:Int) -> Int {
    var result: Int = 0
    var logs: [[Int]] = []
    
   
    for i in 0..<timelogs.count {
        var idx = (8 - startday) % 7
        var temp: [Int] = []
        
        for _ in 0..<5 {
            temp.append(timelogs[i][idx])
            idx += 1
            if idx == 7 { idx = 0 }
        }
        
        logs.append(temp)
    }
    
    for idx in 0..<schedules.count {
        result += check(hope: schedules[idx], reals: logs[idx])
    }
    
    return result
}

func check(hope: Int, reals: [Int]) -> Int {
    let total = (hope / 100 * 60) + (hope % 100) + 10 // 분으로 통일
    
    for real in reals {
        let compReal = (real / 100 * 60) + (real % 100)
        // 출근한 시간이 희망한 시간보다 늦으면
        if total < compReal {
            return 0
        }
    }
    return 1
}

❌ 틀린 이유 및 틀린 부분

⏳ 시간 복잡도

O(N * M) = 사람수 * 5일 = (최대) 1000 * 5 = 5000

📌 풀이 또는 기억할 정보