huequad / swift-algorithm

1 stars 0 forks source link

72414. 광고 삽입 #49

Open ghis22130 opened 3 years ago

ghis22130 commented 3 years ago

https://programmers.co.kr/learn/courses/30/lessons/72414

지크 문제 다푼거 같은데 한문제 더 ㄱ?

ghis22130 commented 3 years ago

https://ghis22130.github.io/2021-10-11-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4_%EA%B4%91%EA%B3%A0-%EC%82%BD%EC%9E%85/

zekexros commented 3 years ago

이문제 진짜 킹받네... 테케 14번이 안되는데 짜증나서 못하겠슴다

import Foundation

func solution(_ play_time:String, _ adv_time:String, _ logs:[String]) -> String {
    var playTime = Array<Int>.init(repeating: 0, count: convertSS(date: play_time)+1)
    let advTime = convertSS(date: adv_time)
    logs.forEach { log in
        let log = log.components(separatedBy: "-")
        let converted = log.map { convertSS(date: $0) }
        let start = converted[0], end = converted[1]
        playTime[start] += 1
        playTime[end] -= 1
    }

    var stack = 0
    var accumulated = 0
    var maxTime = 0
    var insertTime = 0

    let calculatedPlayTime = playTime.map { time -> Int in
        stack += time
        return stack
    }.map { time -> Int in
        accumulated += time
        return accumulated
    }

    for i in 0...calculatedPlayTime.count {
        if advTime + i >= calculatedPlayTime.count {
            break
        }
        let time = calculatedPlayTime[advTime + i] - calculatedPlayTime[i]

        if maxTime < time {
            insertTime = i + 1
            maxTime = time
        }
    }

    return convertHHMMSS(date: insertTime)
}

func convertSS(date: String) -> Int {
    let playTime = date.components(separatedBy: ":").map { Int($0)! }
    return playTime[0] * 3600 + playTime[1] * 60 + playTime[2]
}

func convertHHMMSS(date: Int) -> String {
    var hour = String(date / 3600), remainder = date % 3600
    var minute = String(remainder / 60)
    var second = String(remainder % 60)
    if hour.count == 1 {
        hour = "0" + hour
    }
    if minute.count == 1 {
        minute = "0" + minute
    }

    if second.count == 1 {
        second = "0" + second

        if second == "01" {
            second = "00"
        }
    }
    return "\(hour):\(minute):\(second)"

}