ํ๋ก๊ทธ๋๋จธ์ค - ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ(Swift)
2022. 10. 18. 00:38ใํ๋ก๊ทธ๋๋จธ์ค-Swift
ํ๋ก๊ทธ๋๋จธ์ค - ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ(Swift)
๋ฌธ์ ์ค๋ช
https://school.programmers.co.kr/learn/courses/30/lessons/92341
๋์ ํ์ด
๋ฌธ์ ๋ฅผ ํ๋ฉด์ ์๊ฒ๋ ์ ์ ๋์ ๋๋ฆฌ๋ฅผ for๋ฌธ ๋๋ฉด ํํํํ๋ก key์ value๊ฐ ์๊ธด๋ค๋ ๊ฒ์ด๋ค.
์ด ์ ์ ์ด์ฉํด์ ๋ฌธ์ ๋ฅผ ํ์ด๋ด๋ฉด ๋๋ค.
import Foundation
func solution(_ fees:[Int], _ records:[String]) -> [Int] {
func calculate(_ minute: Int) -> Int {
if minute <= fees[0] {
return fees[1]
} else {
let price = fees[1] + Int(ceil(Double(minute - fees[0]) / Double(fees[2]))) * fees[3]
return price
}
}
var resultDic: [String: Int] = [:]
var timeDic: [String: String] = [:]
for record in records {
let res = record.components(separatedBy: " ")
let time = res[0], carNum = res[1], signal = res[2]
if timeDic[carNum] != nil && signal == "OUT" {
let inTimeArr = timeDic[carNum]!.components(separatedBy: ":")
let inHour = Int(inTimeArr[0])!, inMinute = Int(inTimeArr[1])!
let outTimeArr = time.components(separatedBy: ":")
let outHour = Int(outTimeArr[0])!, outMinute = Int(outTimeArr[1])!
let accTime = (outHour - inHour) * 60 + (outMinute - inMinute)
if resultDic[carNum] != nil {
resultDic[carNum]! += accTime
} else {
resultDic[carNum] = accTime
}
timeDic[carNum] = nil
} else {
timeDic[carNum] = time
}
}
print(timeDic)
for x in timeDic {
print(x)
let inTime = x.1.components(separatedBy: ":")
let inHour = Int(inTime[0])!, inMinute = Int(inTime[1])!
let outHour = 23, outMinute = 59
let accTime = (outHour - inHour) * 60 + (outMinute - inMinute)
if resultDic[x.0] != nil {
resultDic[x.0]! += accTime
} else {
resultDic[x.0] = accTime
}
timeDic[x.0] = nil
}
let arr = resultDic.sorted(by: <).map{ $0.value }
return arr.map{ calculate($0) }
}
'ํ๋ก๊ทธ๋๋จธ์ค-Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - n์ง์ ๊ฒ์(Swift) (0) | 2022.10.18 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2022.10.18 |
ํ๋ก๊ทธ๋๋จธ์ค - ์์ถ(Swift) (0) | 2022.10.18 |
ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ ๋ฐฉ(Swift) (0) | 2022.10.17 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋ด์ค ํด๋ฌ์คํฐ๋ง(Swift) (1) | 2022.10.15 |