Programmers - 주차 요금 계산 (C#)

장민제·2025년 6월 12일

Programmers

목록 보기
30/31
post-thumbnail

📝 문제 설명

아래는 프로그래머스에서 제공한 문제 설명입니다.

주차장의 요금표차량의 입/출차 기록이 주어졌을 때, 차량별로 주차 요금을 계산하세요.

요금표

기본 시간(분)기본 요금(원)단위 시간(분)단위 요금(원)
180500010600

입/출차 기록

시각차량 번호내역
05:345961입차
06:000000입차
06:340000출차
07:595961출차
07:590148입차
18:590000입차
19:090148출차
22:595961입차
23:005961출차

자동차별 주차 요금

차량 번호누적 주차 시간(분)주차 요금(원)
000034 + 300 = 3345000 + ⌈(334 - 180) / 10⌉ × 600 = 14600
01486705000 + ⌈(670 - 180) / 10⌉ × 600 = 34400
5961145 + 1 = 1465000
  1. 입차 후 출차 내역이 없으면 23:59에 출차된 것으로 간주합니다.

  2. 누적 주차 시간이 기본 시간 이하인 경우: 기본 요금만 청구합니다.

  3. 초과 시간이 있는 경우:

    • 초과 시간은 (누적 시간 - 기본 시간)
    • 요금 = 기본 요금 + ⌈초과 시간 / 단위 시간⌉ × 단위 요금
  4. ⌈a⌉는 올림 함수, 즉 a보다 크거나 같은 최소 정수


❌ 제한사항

  • fees.Length == 4
  • 1 ≤ fees[0] ≤ 1439 (기본 시간)
  • 0 ≤ fees[1] ≤ 100000 (기본 요금)
  • 1 ≤ fees[2] ≤ 1439 (단위 시간)
  • 1 ≤ fees[3] ≤ 10000 (단위 요금)
  • 1 ≤ records.Length ≤ 1000
  • records[i]"HH:MM 차량번호 IN/OUT" 형식
  • 차량 번호는 "0000" ~ "9999" 사이 문자열
  • records는 시각 오름차순 정렬

💻 나의 풀이 (C#)

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;
        }
    }
}
profile
Unity, C#

0개의 댓글