
아래는 프로그래머스에서 제공한 문제 설명입니다.
주차장의 요금표와 차량의 입/출차 기록이 주어졌을 때, 차량별로 주차 요금을 계산하세요.
| 기본 시간(분) | 기본 요금(원) | 단위 시간(분) | 단위 요금(원) |
|---|---|---|---|
| 180 | 5000 | 10 | 600 |
| 시각 | 차량 번호 | 내역 |
|---|---|---|
| 05:34 | 5961 | 입차 |
| 06:00 | 0000 | 입차 |
| 06:34 | 0000 | 출차 |
| 07:59 | 5961 | 출차 |
| 07:59 | 0148 | 입차 |
| 18:59 | 0000 | 입차 |
| 19:09 | 0148 | 출차 |
| 22:59 | 5961 | 입차 |
| 23:00 | 5961 | 출차 |
| 차량 번호 | 누적 주차 시간(분) | 주차 요금(원) |
|---|---|---|
| 0000 | 34 + 300 = 334 | 5000 + ⌈(334 - 180) / 10⌉ × 600 = 14600 |
| 0148 | 670 | 5000 + ⌈(670 - 180) / 10⌉ × 600 = 34400 |
| 5961 | 145 + 1 = 146 | 5000 |
입차 후 출차 내역이 없으면 23:59에 출차된 것으로 간주합니다.
누적 주차 시간이 기본 시간 이하인 경우: 기본 요금만 청구합니다.
초과 시간이 있는 경우:
(누적 시간 - 기본 시간)기본 요금 + ⌈초과 시간 / 단위 시간⌉ × 단위 요금⌈a⌉는 올림 함수, 즉 a보다 크거나 같은 최소 정수
fees.Length == 41 ≤ fees[0] ≤ 1439 (기본 시간)0 ≤ fees[1] ≤ 100000 (기본 요금)1 ≤ fees[2] ≤ 1439 (단위 시간)1 ≤ fees[3] ≤ 10000 (단위 요금)1 ≤ records.Length ≤ 1000records[i]는 "HH:MM 차량번호 IN/OUT" 형식"0000" ~ "9999" 사이 문자열records는 시각 오름차순 정렬using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] fees, string[] records) {
List<int> answer = new List<int>();
Dictionary<string, TimeSpan> timeDict = new Dictionary<string, TimeSpan>();
Dictionary<string, int> feeDict = new Dictionary<string, int>();
for(int i = 0; i < records.Length; i++)
{
string[] recordInfo = records[i].Split(' ');
TimeSpan time = TimeSpan.Parse(recordInfo[0]);
string carNum = recordInfo[1];
if(timeDict.ContainsKey(carNum))
{
TimeSpan outTime = time;
TimeSpan inTime = timeDict[carNum];
TimeSpan totalTime = outTime - inTime;
int totalM = (int)totalTime.TotalMinutes;
if(feeDict.ContainsKey(carNum))
feeDict[carNum] += totalM;
else
feeDict.Add(carNum, totalM);
timeDict.Remove(carNum);
}
else
{
timeDict.Add(carNum, time);
}
}
if(timeDict.Count > 0)
{
foreach(var carNum in timeDict.Keys)
{
TimeSpan outTime = TimeSpan.Parse("23:59");
TimeSpan inTime = timeDict[carNum];
TimeSpan totalTime = outTime - inTime;
int totalM = (int)totalTime.TotalMinutes;
if(feeDict.ContainsKey(carNum))
feeDict[carNum] += totalM;
else
feeDict.Add(carNum, totalM);
}
}
foreach(var carNum in new List<string>(feeDict.Keys).OrderBy(k => k))
{
feeDict[carNum] = CalculateFee(feeDict[carNum], fees);
answer.Add(feeDict[carNum]);
}
return answer.ToArray();
}
public int CalculateFee(int minutes, int[] fees)
{
int basicTime = fees[0];
int basicFee = fees[1];
int unitTime = fees[2];
int unitFee = fees[3];
if(minutes <= basicTime)
return basicFee;
else
{
int extraTime = minutes - basicTime;
int extraUnits = (int)Math.Ceiling((double)extraTime / unitTime);
return basicFee + extraUnits * unitFee;
}
}
}