๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] 2022 KAKAO BLIND RECRUITMENT ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

by Fomagran ๐Ÿ’ป 2022. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

[180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000]

programmers.co.kr


Solution

 

1. records์˜ ๊ธฐ๋ก๋Œ€๋กœ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•ด ์ €์žฅํ•œ๋‹ค.

 

var timeInfo:[String:Int] = [:]
var parkInfo:[String:Int] = [:]

func calTimeByRecord(_ records:[String],_ parkInfo:inout [String:Int],_ timeInfo:inout [String:Int]) {
    for record in records {
        let split = record.split(separator: " ")
        let time = String(split[0])
        let number = String(split[1])
        let isIN = split[2] == "IN"
        if isIN {
            parkInfo[number] = changeTimeToMinutes(time)
        }else {
            let out = changeTimeToMinutes(time)
            let minutes = out - parkInfo[number]!
            if timeInfo[number] == nil {
                timeInfo[number] = minutes
            }else {
                timeInfo[number]! += minutes
            }
            parkInfo[number] = nil
        }
    }
}

 

* ๋ฌธ์ž๋กœ ๋œ ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜

 

func changeTimeToMinutes(_ time:String) -> Int {
    let split = time.split(separator: ":")
    let hours = Int(split[0])!
    let minutes = Int(split[1])!
    return hours*60 + minutes
}

 

2. ๋งŒ์•ฝ ์ฃผ์ฐจ ์ •๋ณด์— ๋“ค์–ด์˜จ ์ •๋ณด๋งŒ ์žˆ๋‹ค๋ฉด ์ตœ๋Œ€ ์‹œ๊ฐ„(23:59)์—์„œ ๋“ค์–ด์˜จ ์‹œ๊ฐ„์„ ๊ตฌํ•ด์ค€๋‹ค.

 

    let maxTime = 23*60+59

    for (number,time) in parkInfo {
        if timeInfo[number] == nil {
            timeInfo[number] = maxTime - time
        }else {
            timeInfo[number]! += maxTime - time
        }
    }

 

3. ์‹œ๊ฐ„๋Œ€๋กœ ์š”๊ธˆ์„ ๊ตฌํ•œ๋‹ค.

 

  for (number,time) in timeInfo {
        feeInfo[number] = getTotalFee(time,fees)
    }

 

* ์š”๊ธˆ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

 

func getTotalFee(_ minutes:Int,_ fees:[Int]) -> Int {
    let freetime = fees[0]
    let defaultFee = fees[1]
    let perTime = fees[2]
    let fee = fees[3]
    if minutes <= freetime { return defaultFee }
    let c = Int(ceil(Double((minutes - freetime))/Double(perTime)))
    let total = defaultFee + c * fee
    return total
}

 

4. ๋ฒˆํ˜ธํŒ์ด ์ ์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 return feeInfo.sorted{$0.key < $1.key}.map{$0.value}

Source Code

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€