TIL #59

loci·2024년 6월 28일
0

TIL

목록 보기
58/103

주차 요금 계산

주차요금을 계산할때 들어온시간과 나간시간을 구해서 해당 번호의 차량이 기본시간보다 적으면 기본요금을 부과하고 초과되면 단위시간당 요금이 추가 부과된다. 번호가 작은 자동차부터 부과된 요금을 배열로 리턴해야한다.


코드

class Solution {
    fun solution(fees: IntArray, records: Array<String>): List<Int> {
        var parkingTime = mutableMapOf<String, Int>()
        val answer = ArrayList<Int>()
        records.forEach { record ->
            val (timeStr, id, check) = record.split(" ")
            var time = timeStr.split(":")[0].toInt() * 60 + timeStr.split(":")[1].toInt()
            if (check == "IN") time = -time
            parkingTime[id] = parkingTime.getOrDefault(id, 0) + time
        }
        parkingTime = parkingTime.toSortedMap()
        parkingTime.values.forEach { answer.add(if (it <= 0) it + 1439 else it) }
        return answer.map {
            val a = Math.ceil((it - fees[0]).toDouble() / fees[2].toDouble()).toInt() * fees[3]
            if (a > 0) fees[1] + a else fees[1]
        }
    }
}
profile
편리한 개발자

0개의 댓글