문제 풀기/C#

96. 주차 요금 계산(TimeSpan 자료형)

kagan-draca 2025. 3. 19. 16:21

using System;

public class Solution {
    public int[] solution(int[] fees, string[] records) {
        int[] answer = new int[] {};
        return answer;
    }
}

 

나름 재미있고 유익한 문제를 풀었다.

 

자동차 번호에 따른 입차, 출차 시간을 저장할 필요가 있어 보였기 때문에

 

Dictionary를 사용해서 자동차 번호를 key로 시간 기록을 value로 사용하기로 했다.

 

이때, 시간 기록은 정해진 크기가 아닌 유동적이므로 List를 사용해 저장할 수 있도록 해줬다.

 

List의 타입으로는 TimeSpan이라는 자료형을 사용하기로 결정했다.

 

그 이유로는, DateTime(특정 시점)과 달리 두 시간 간격(기간, 차이)를 표현할 때 좋은 구조체이기 때문이다.

(TimeSpan 사용 방법을 검색해보면서 아래의 모든 코드를 작성함)

 

        Dictionary<string, List<TimeSpan>> dictionary = new Dictionary<string, List<TimeSpan>>();
        
        for(int i = 0; i < records.Length; i++)
        {
            string[] info = records[i].Split(' ');
            if(!dictionary.ContainsKey(info[1])) dictionary[info[1]] = new List<TimeSpan>();
            dictionary[info[1]].Add(TimeSpan.Parse(info[0]));
        }

 

 

반복문을 사용해 records를 조회하면서 Split(' ')로 붙어있는 정보들을 끊어준 후

 

dictionary에 key(자동차 번호)가 존재하지 않으면 dictionary에 key 값과 value를 새로 추가하고

 

value 값을 저장해줬다.

 

그 후, 문제는 자동차 번호가 작은 순으로 요금을 출력하길 원하기 때문에

 

		// dictionary key(차량 번호) 순으로 오름차순 정렬
        dictionary = dictionary.OrderBy(X=> X.Key).ToDictionary(X => X.Key, X => X.Value);
        
        int[] priceRecords = new int[dictionary.Count];

 

dictionary를 Key에 따라 오름차순으로 정렬하고

 

반환에 사용할 배열을 dictionary.Count로 크기를 지정해줬다.

 

그리고 dictionary를 조회하면서

        for(int i = 0; i < dictionary.Count; i++)
        {
            var entity = dictionary.ElementAt(i);
            
            // 출차 기록 없으면 23시 59분 강제 출차로 만들기
            if(entity.Value.Count % 2 == 1) entity.Value.Add(TimeSpan.Parse("23:59"));
            
            TimeSpan sumMinutes = TimeSpan.Zero;
            
            // in, out 순으로 반복이라 2씩 증가로 탐색
            for(int j = 0; j < entity.Value.Count; j+=2)
                // 출차 - 입차 시간
                sumMinutes += entity.Value[j + 1] - entity.Value[j];
            
            // 기본 요금
            int totalPrise = fees[1];
            
            // 기본 시간 초과
            if(sumMinutes.TotalMinutes > fees[0])
            {
                // 초과 한 시간 계산
                sumMinutes -= TimeSpan.FromMinutes(fees[0]);
                // 시간 당 금액 계산
                totalPrise += (int)Math.Ceiling((double)sumMinutes.TotalMinutes / fees[2]) * fees[3];
            }
            
            priceRecords[i] = totalPrise;
        }

 

key에 따른 value가 홀수인 경우 "23:59"에 강제로 출차 기록을 만들어줬다.

 

그리고 in, out 순으로 정보가 반복되기 때문에 j를 2씩 증가시키면서 

 

주차장에 머무른 총 시간을 계산해주고, 기본 시간을 초과한 경우

 

초과한 시간에 따른 금액을 계산해줬다.

 

using System;
using System.Linq;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] fees, string[] records) 
    {
        Dictionary<string, List<TimeSpan>> dictionary = new Dictionary<string, List<TimeSpan>>();
        
        for(int i = 0; i < records.Length; i++)
        {
            string[] info = records[i].Split(' ');
            if(!dictionary.ContainsKey(info[1])) dictionary[info[1]] = new List<TimeSpan>();
            dictionary[info[1]].Add(TimeSpan.Parse(info[0]));
        }
        
        // dictionary key(차량 번호) 순으로 오름차순 정렬
        dictionary = dictionary.OrderBy(X=> X.Key).ToDictionary(X => X.Key, X => X.Value);
        
        int[] priceRecords = new int[dictionary.Count];
        
        for(int i = 0; i < dictionary.Count; i++)
        {
            var entity = dictionary.ElementAt(i);
            
            // 출차 기록 없으면 23시 59분 강제 출차로 만들기
            if(entity.Value.Count % 2 == 1) entity.Value.Add(TimeSpan.Parse("23:59"));
            
            TimeSpan sumMinutes = TimeSpan.Zero;
            
            // in, out 순으로 반복이라 2씩 증가로 탐색
            for(int j = 0; j < entity.Value.Count; j+=2)
                // 출차 - 입차 시간
                sumMinutes += entity.Value[j + 1] - entity.Value[j];
            
            // 기본 요금
            int totalPrise = fees[1];
            
            // 기본 시간 초과
            if(sumMinutes.TotalMinutes > fees[0])
            {
                // 초과 한 시간 계산
                sumMinutes -= TimeSpan.FromMinutes(fees[0]);
                // 시간 당 금액 계산
                totalPrise += (int)Math.Ceiling((double)sumMinutes.TotalMinutes / fees[2]) * fees[3];
            }
            
            priceRecords[i] = totalPrise;
        }
        
        return priceRecords;
    }
}

'문제 풀기 > C#' 카테고리의 다른 글

98. 뒤에 있는 큰 수 찾기  (0) 2025.03.21
97. 모음 사전  (0) 2025.03.20
95. k진수에서 소수 개수 구하기  (0) 2025.03.18
94. 타겟 넘버  (0) 2025.03.18
93. 피로도  (0) 2025.03.14