프로그래머스 Level 2 - 주차 요금 계산
📌 문제 설명
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2F172168dc-ff3f-46fc-94ee-247891f296fb%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.26.33.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2F021d779b-0148-4559-9766-f27ac0d3145f%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.26.48.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2Fc097eef9-89f4-4694-9e7f-c1a78d3d8b4a%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.27.14.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2Fbb81735c-29ee-4bf7-a2d4-3305393f7787%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.27.25.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2Fa9588a25-2ee2-4101-b939-35ae525af58d%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.27.49.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2F5425a7ec-ce32-4d54-a805-84efc0f363c3%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.28.03.png)
![](https://velog.velcdn.com/images%2Ftnehd1998%2Fpost%2F6f57a88d-f17d-4d01-8736-148db167b229%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-07%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%207.28.18.png)
📌 생각한 풀이 방법
- 차량을 번호, 시각 순으로 정렬을 한다.
- 현재 차량을 저장한 current와 다음 차량이 존재하거나, 번호가 같은지 확인한다.
- 번호가 같은 경우
3-1. 입차하는 경우 previousTime에 현재 차량의 시각을 저장한다.
3-2. 출차하는 경우 totalCostTime에 현재 시간을 나타내는 totalTime - previousTime을 더한다.
- 번호가 다르거나 다음 차량이 없는 경우
4-1. 입차하는 경우 출차한 기록이 없으므로 23:59에서 totalTime을 뺀 값을 totalCostTime에 더한다.
4-1. 출차하는 경우 totalCostTime에 현재 시간을 나타내는 totalTime - previousTime을 더한다.
- totalCostTime에 따른 값을 answer에 추가한다.
5-1. 기본 시간보다 totalCostTime이 작거나 같을 경우 기본요금을 answer에 push한다.
5-2. 기본 시간보다 클 경우 조건에 맞는 값을 answer에 push한다.
- 2~5를 입/출차 기록이 존재할 때까지 반복한다.
📌 풀이
function solution(fees, records) {
let answer = [];
let recordsArr = [];
for (let i = 0; i < records.length; i++) {
let current = records[i].split(" ");
recordsArr.push(current);
}
recordsArr.sort((a, b) => {
if (a[1] === b[1]) {
return a[0] - b[0];
}
return a[1] - b[1];
});
let previousTime = 0;
let totalCostTime = 0;
while (recordsArr.length) {
let current = recordsArr.shift();
let time = current[0].split(":");
let totalTime = Number(time[0]) * 60 + Number(time[1]);
if (recordsArr.length && recordsArr[0][1] === current[1]) {
if (current[2] === "IN") {
previousTime = totalTime;
} else {
let usedTime = totalTime - previousTime;
totalCostTime += usedTime;
}
} else {
if (current[2] === "IN") {
let usedTime = 1439 - totalTime;
totalCostTime += usedTime;
} else {
let usedTime = totalTime - previousTime;
totalCostTime += usedTime;
}
if (totalCostTime <= fees[0]) {
answer.push(fees[1]);
} else {
let cost =
fees[1] + Math.ceil((totalCostTime - fees[0]) / fees[2]) * fees[3];
answer.push(cost);
}
totalCostTime = 0;
}
}
return answer;
}