문제 출처: https://school.programmers.co.kr/learn/courses/30/lessons/92341?language=kotlin
문제
나의 풀이
자동차 넘버와 얼마나 머물렀는지 시간을 같이 배열에 넣기 위해 MutableMap을 사용하였다.
import java.util.*
class Solution {
var basicTime = 0
var basicFee = 0
var unitTime = 0
var unitFee = 0
fun solution(fees: IntArray, records: Array<String>): IntArray {
var answer = mutableMapOf<String, Int>()
val map = mutableMapOf<String, Int>()
basicTime = fees[0]
basicFee = fees[1]
unitTime = fees[2]
unitFee = fees[3]
records.map {
var (time, car, inout) = it.split(" ")
var (hour, min) = time.split(":")
var due = hour.toInt() * 60 + min.toInt()
if(inout == "IN") {
map[car] = due
}
else {
var duration = due - map[car]!!
answer[car] = answer.getOrDefault(car, 0) + duration
map.remove(car)
}
}
map.forEach { car, t ->
val max = 23 * 60 + 59
var duration = max - t
answer[car] = answer.getOrDefault(car, 0) + duration
}
val result = IntArray(answer.size)
answer.map { it.key.toInt() }
answer.map {
answer[it.key] = calFee(it.value)
}
return answer.toSortedMap(compareBy{it} ).values.toIntArray()
}
fun calFee(duration: Int) : Int {
var fee = 0
if(duration <= basicTime) {
fee = basicFee
}
else {
var result = (duration - basicTime).toDouble() / unitTime
fee = basicFee + Math.ceil(result).toInt() * unitFee
}
return fee
}
}