Muscardinus
주차 요금 계산 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/92341
function solution(fees, records) {
const answer = [];
const carsInfo = {};
for (let record of records) {
const [time, carNum, _] = record.split(" ");
carsInfo[carNum] = carsInfo[carNum] || [];
carsInfo[carNum].push(time);
}
const cars = Object.keys(carsInfo).sort((a, b) => a - b);
// 기본 시간, 기본 요금, 단위 시간, 단위 요금
const [dT, dF, uT, uF] = fees;
const calcInOutTime = (inTime, outTime = "23:59") => {
const start = inTime.split(":");
const end = outTime.split(":");
const startDate = new Date(0, 0, 0, start[0], start[1], 0);
const endDate = new Date(0, 0, 0, end[0], end[1], 0);
const diff = endDate - startDate;
const minutes = Math.floor(diff / 1000 / 60);
return minutes;
}
for (let car of cars) {
let totalTime = 0;
const n = carsInfo[car].length;
for (let i = 0; i < n; i += 2) {
totalTime += calcInOutTime(carsInfo[car][i], carsInfo[car][i + 1]);
}
let fee = dF;
if (totalTime > dT) {
fee += (Math.ceil((totalTime - dT) / uT) * uF);
}
answer.push(fee);
}
return answer;
}
728x90
Comments