728x90
๋ฐ์ํ
Problem
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
๋ฐ์ํ
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] 2022 KAKAO BLIND RECRUITMENT ์๊ณผ ๋๋ (0) | 2022.01.19 |
---|---|
[Swift] 2022 KAKAO BLIND RECRUITMENT ์๊ถ๋ํ (1) | 2022.01.19 |
[Swift] 2022 KAKAO BLIND RECRUITMENT k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2022.01.19 |
[Swift] 2022 KAKAO BLIND RECRUITMENT ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (0) | 2022.01.19 |
[Swift] 2020 KAKAO BLIND RECRUITMENT ๊ฐ์ฌ ๊ฒ์ (0) | 2022.01.03 |
๋๊ธ