Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

๊ฐ์ž ๐Ÿฅ” 2022. 5. 10. 00:45
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

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

 

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

[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

 

๋ฌธ์ œ ํ•ด์„ ๋ฐ ํ’€์ด

  • fees ์—์„œ ์ฒซ๋ฒˆ์งธ๋Š” ๊ธฐ๋ณธ ์ฃผ์ฐจ ์‹œ๊ฐ„, ๋‘๋ฒˆ์งธ๋Š” ๊ธฐ๋ณธ ์ฃผ์ฐจ ์š”๊ธˆ, ์„ธ๋ฒˆ์งธ๋Š” ์ฃผ์ฐจ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋‹จ์œ„ (๋ถ„), ๋„ค๋ฒˆ์งธ๋Š” ๋‹จ์œ„๋‹น ์–ผ๋งˆ?๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • records์—์„œ "__" ์ด ํ•œ ๋”ฐ์˜ดํ‘œ๋ฅผ " " ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฒˆํ˜ธํŒ๊ณผ ์ž…์ถœ์ฐจ์‹œ๊ฐ„์„ ๊ตฌ๋ณ„ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.
    • ์ƒ๊ฐํ•ด๋ณด๋ฉด IN ๊ณผ OUT ์€ ๊ทธ๋‹ฅ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ์–ด์ฐจํ”ผ records๋Š” ์‹œ๊ฐ„์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—, IN๋‹ค์Œ์€ ๋ฌด์กฐ๊ฑด OUT์ผ ๊ฒƒ์ด๋‹ค. ์ด ๊ฐœ๋…๋Œ€๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด, records์—์„œ ๋ฒˆํ˜ธํŒ 0000์˜ ์ž…์ถœ์ฐจ์‹œ๊ฐ„ ๊ธฐ๋ก ๊ฐฏ์ˆ˜๊ฐ€ ํ™€์ˆ˜๊ฐœ์ด๋ฉด, OUT ๊ธฐ๋ก์ด ์—†๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ ๊ฐฏ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฒƒ์—๋งŒ append("23:59")๋ฅผ ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค!
  • ์ฃผ์˜ํ•ด์•ผํ•  ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๊ฐ€, ๋ฒˆํ˜ธํŒ ์˜ค๋ฆ„์ฐจ์ˆœ ์ˆœ์„œ๋Œ€๋กœ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
  • "๋ฒˆํ˜ธํŒ ๋ณ„"๋กœ ์ž…์ถœ์ฐจ๋ฅผ ๊ตฌ๋ถ„ํ•ด์•ผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, dictionary๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

1.  ํ•„์š”ํ•œ ๋ณ€์ˆ˜  ๋ฐ dictionary ์ •์˜

  • records์—์„œ ์ž…๋ ฅ๋ฐ›์€ ํ•˜๋‚˜์”ฉ ํ•˜๋‚˜์”ฉ์„ for๋ฌธ์„ ํ†ตํ•ด ๋Œ๋ฆฌ๋ฉด์„œ dictionary์— ๋„ฃ์–ด์ค€๋‹ค.
  • temp์—๋Š” record๋ฅผ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋‘ ์งค๋ผ์„œ ๋„ฃ๋Š”๋‹ค. 
    • ์œ„์— ์‚ฌ์ง„์—์„œ ์ž…์ถœ๋ ฅ ๋งจ ์ฒซ๋ฒˆ์งธ records๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด, ["05:34", "5961", "IN"] ์ด๋ ‡๊ฒŒ ์ €์žฅ๋  ๊ฒƒ์ด๋‹ค.
  • recordDic์—๋Š” key ๊ฐ’์€ int๋กœ, value๋Š” int ๋ฐฐ์—ด ํ˜•์‹์œผ๋กœ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค.
    • [์ฐจ๋Ÿ‰๋ฒˆํ˜ธ: [์ž…์ฐจ์‹œ๊ฐ„, ์ถœ์ฐจ์‹œ๊ฐ„, ์ž…์ฐจ์‹œ๊ฐ„]] ์ด๋ ‡๊ฒŒ ํ•  ์˜ˆ์ •์ด๋‹ค.
    • if ๋ฌธ์„ ํ†ตํ•ด์„œ, carName์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•œ ๋’ค, ์—†๋‹ค๋ฉด key:value๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ์žˆ๋‹ค๋ฉด value.append() ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
let basicTime = fees[0]
let basicFee = fees[1]
let perTime = fees[2]  // ๋‹จ์œ„ ์‹œ๊ฐ„
let perFee = fees[3]  //๋‹จ์œ„์š”๊ธˆ
var result: [[Int]] = []
var recordDic: [Int: [Int]] = [:]

for record in records {
    let temp = record.split(separator: " ").map{ String($0) }
    let carName = Int(temp[1])!

    if recordDic[carName] == nil {
        recordDic[carName] = [calculateTime(time: temp[0])]
    } else {
        recordDic[carName]!.append(calculateTime(time: temp[0]))
    }
}

 

2. recordDic ์—์„œ ์ถœ์ฐจ์‹œ๊ฐ„ - ์ž…์ฐจ์‹œ๊ฐ„ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ

  • ์—ฌ๊ธฐ์„œ ์ œ์‹œ๋œ ๊ธˆ์•ก์€ ์ „๋ถ€ '๋ถ„'๋‹จ์œ„๋กœ ๊ณ„์‚ฐ๋œ๋‹ค. 
  • ๋”ฐ๋ผ์„œ ์ž…์ถœ์ฐจ์‹œ๊ฐ„์„ ๋ชจ๋‘ ๋ถ„์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
    • time์—๋Š” stringํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค. "23:59" ์ด๋Ÿฐ ํ˜•ํƒœ
    • ํ•ด๋‹น ์‹œ๊ฐ„์„ ":" ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์งค๋ผ์„œ, 23๊ณผ 59๋กœ ๋‚˜๋ˆˆ๋‹ค.
    • 23์€ hour์ด๊ธฐ ๋–„๋ฌธ์— 60์„ ๊ณฑํ•ด์ฃผ๊ณ , 59๋Š” ๊ทธ๋ƒฅ ๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋†”๋‘”๋‹ค.
    • ํ•ด๋‹น ๋‘ ๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด, 23:59์ด ๋ถ„์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค.
func calculateTime(time: String) -> Int {
    let tempTime = time.split(separator: ":").map{Int(String($0))!}
    return tempTime[0]*60 + tempTime[1]
}

 

  • recordDic์—์„œ ์ถœ์ฐจ์‹œ๊ฐ„ - ์ž…์ฐจ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ for๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค.
  • ์ฒ˜์Œ for๋ฌธ์„ ๋Œ๋ฆฌ๊ธฐ ์ „์—, value์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด์„œ out์‹œ๊ฐ„์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋“ค์—๊ฒŒ๋Š” "23:59"๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ์ž‘์—…์„ ๊ฑฐ์นœ๋‹ค.
  • while๋ฌธ ์„ค๋ช…
    • nowIdx๋ฅผ 1๋กœ ์„ค์ •ํ•ด์ฃผ๊ณ , 2์”ฉ ๋”ํ•ด์ฃผ๋ฉด์„œ ์ถœ์ฐจ์‹œ๊ฐ„ - ์ž…์ฐจ์‹œ๊ฐ„ ์„ ๊ณ„์‚ฐํ•ด์ค€๋‹ค. 
    • 2๋ฅผ ๋”ํ•˜๋Š” ์ด์œ ๋Š”, ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 1, 3, 5,,, ์œ„์น˜์— ์žˆ๋Š” ๊ฐ’๋“ค์ด ์ถœ์ฐจ์‹œ๊ฐ„์ผ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ 2์”ฉ ๋”ํ•ด์ฃผ๋ฉด์„œ, ํ•œ์นธ ์•ž์—์žˆ๋Š” ์ž…์ฐจ์‹œ๊ฐ„์„ ๋นผ์ค€๋‹ค.
  • while ๋ฌธ์ด ๋๋‚˜๋ฉด result์— ์ž๋™์ฐจ ๋ฒˆํ˜ธํŒ์„ ๊ฐ€๋ฅดํ‚ค๋Š” key์™€ ๊ณ„์‚ฐ๋˜์–ด ๋‚˜์˜จ ์ฃผ์ฐจ์‹œ๊ฐ„(resultTotalTime)์„ [result]์— ๋„ฃ์–ด์ค€๋‹ค.
    • 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•œ ์ด์œ ๋Š”, ๋‚˜์ค‘์— ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ๋–„ "์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธํŒ" ๊ธฐ์ค€์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ตฌ๋ถ„์„ ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
for (key, value) in recordDic {
    var resultTotalTime = 0
    var nowIdx = 1

    var tempValue: [Int] = value
    // out์ด ์—†๋Š” ๊ฒฝ์šฐ 23์‹œ 59๋ถ„ ์ถ”๊ฐ€
    if value.count % 2 != 0 {
        tempValue.append(calculateTime(time: "23:59"))
    }

    // 2, 4, 6,,, ๋ฒˆ์งธ์—์„œ ์•ž๊บผ ๋นผ๊ธฐ
    while nowIdx < tempValue.count {
        let calTime = tempValue[nowIdx] - tempValue[nowIdx-1]
        resultTotalTime += calTime
        nowIdx += 2
    }
    result.append([key, resultTotalTime])
}

 

3. ์ตœ์ข… ์ฃผ์ฐจ๋น„ ๊ณ„์‚ฐ

  • ์ตœ์ข… ์ฃผ์ฐจ๋น„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
  • total ํƒ€์ž„์ด ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š” ์ฃผ์ฐจ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ์ฐจ๋ฅผ ๊ตฌํ•˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ๊ณ„์‚ฐํ•  ์ฃผ์ฐจ์‹œ๊ฐ„์€ 0์ด๋œ๋‹ค.
  • basicFee๋Š” ํ•„์ˆ˜์ ์œผ๋กœ ๋‚ด์•ผํ•˜๋Š” ๊ธˆ์•ก์ด๊ธฐ ๋•Œ๋ฌธ์—, result์— ๊ณ„์‹ผ๋œ ๊ธˆ์•ก๋“ค์„ ๋”ํ•ด์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.
func calculateResult(basicFee: Int, basicTime: Int, perFee: Int, perTime: Int,arr: [Int]) -> Int {
    var total = arr[1]
    var result = basicFee
    
    // ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ๋บ€ ์ฃผ์ฐจ์š”๊ธˆ์„ ๋‚ผ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ
    if total > basicTime {
        total = total - basicTime
    } else {
        total = 0
    }
    
    if total > 0 {
        if total % perTime != 0 {
            result += Int((total / perTime + 1) * perFee)
        } else {
            result += Int(total / perTime * perFee)
        }
    }
    return result
}

 

  • ์ตœ์ข…์ ์œผ๋กœ ๊ตฌ์„ฑ๋œ result ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 
    • [[์ฐจ๋Ÿ‰๋ฒˆํ˜ธ, ๊ธˆ์•ก], [์ฐจ๋Ÿ‰๋ฒˆํ˜ธ, ๊ธˆ์•ก]....]
  • ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ, ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ํ›„, ๊ธˆ์•ก๋“ค์„ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.
    • ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ, ๊ทธ๋‹ˆ๊นŒ ์ธ๋ฑ์Šค 0๋ฒˆ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ๋‹ค. (์•Œ์•„๋‘๋ฉด ์ข‹์„๋“ฏ!)
let realResult = result.sorted(by: {$0[0] < $1[0]})

var returnValue: [Int] = []
for i in 0..<realResult.count {
    returnValue.append(calculateResult(basicFee: basicFee, basicTime: basicTime, perFee: perFee, perTime: perTime, arr: realResult[i]))
}

 

 

์ตœ์ข…์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EC%A3%BC%EC%B0%A8%20%EC%9A%94%EA%B8%88%20%EA%B3%84%EC%82%B0/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

func solution(_ fees:[Int], _ records:[String]) -> [Int] {
    let basicTime = fees[0]
    let basicFee = fees[1]
    let perTime = fees[2]  // ๋‹จ์œ„ ์‹œ๊ฐ„
    let perFee = fees[3]  //๋‹จ์œ„์š”๊ธˆ
    var result: [[Int]] = []
    var recordDic: [Int: [Int]] = [:]
    
    for record in records {
        let temp = record.split(separator: " ").map{ String($0) }
        let carName = Int(temp[1])!
        
        if recordDic[carName] == nil {
            recordDic[carName] = [calculateTime(time: temp[0])]
        } else {
            recordDic[carName]!.append(calculateTime(time: temp[0]))
        }
    }
    
    
    for (key, value) in recordDic {
        var resultTotalTime = 0
        var nowIdx = 1
        
        var tempValue: [Int] = value
        // out์ด ์—†๋Š” ๊ฒฝ์šฐ 23์‹œ 59๋ถ„ ์ถ”๊ฐ€
        if value.count % 2 != 0 {
            tempValue.append(calculateTime(time: "23:59"))
        }
        
        // 2, 4, 6,,, ๋ฒˆ์งธ์—์„œ ์•ž๊บผ ๋นผ๊ธฐ
        while nowIdx < tempValue.count {
            let calTime = tempValue[nowIdx] - tempValue[nowIdx-1]
            resultTotalTime += calTime
            nowIdx += 2
        }
        result.append([key, resultTotalTime])
    }
    
    let realResult = result.sorted(by: {$0[0] < $1[0]})
    
    var returnValue: [Int] = []
    for i in 0..<realResult.count {
        returnValue.append(calculateResult(basicFee: basicFee, basicTime: basicTime, perFee: perFee, perTime: perTime, arr: realResult[i]))
    }
    return returnValue
}
    
func calculateTime(time: String) -> Int {
    let tempTime = time.split(separator: ":").map{Int(String($0))!}
    return tempTime[0]*60 + tempTime[1]
}

func calculateResult(basicFee: Int, basicTime: Int, perFee: Int, perTime: Int,arr: [Int]) -> Int {
    var total = arr[1]
    var result = basicFee
    
    // ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ๋บ€ ์ฃผ์ฐจ์š”๊ธˆ์„ ๋‚ผ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ
    if total > basicTime {
        total = total - basicTime
    } else {
        total = 0
    }
    
    if total > 0 {
        if total % perTime != 0 {
            result += Int((total / perTime + 1) * perFee)
        } else {
            result += Int(total / perTime * perFee)
        }
    }
    return result
}
๋ฐ˜์‘ํ˜•