[프로그래머스] 주차 요금 계산

Eden·2023년 7월 19일
0

문제 출처
https://school.programmers.co.kr/learn/courses/30/lessons/92341

using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(int[] fees, string[] records) {
        List<string> a = new List<string>(); 번호에 대한 차량 정렬이 쓸 List
        int[] answer;
        Dictionary<string,int> num_period = new Dictionary<string,int>(); //차량이 들어왔을때 시간 저장
        Dictionary<string,int> num_total = new Dictionary<string,int>(); //차량이 머문 총 시간
        Dictionary<string,bool> check_out = new Dictionary<string,bool>(); // 차량이 나갔는지에 대한 bool
        for (int i = 0;i < records.Length; i++)
        {
            string[] splitval = records[i].Split(' ');
            if(splitval[2] == "IN")
            {
                int cal = Convert.ToInt32(splitval[0].Split(':')[0])*60 + Convert.ToInt32(splitval[0].Split(':')[1]); // 분단위로 변환
                num_period[splitval[1]] = cal;
                check_out[splitval[1]] = false; // 들어오고 나가지 않음 상태처리
                if (!num_total.ContainsKey(splitval[1])) // 딕셔너리와 리스트에 최초 한번만 차량 추가하는 부분
                {
                    num_total[splitval[1]] = 0; //최초로 차량 추가
                    a.Add(splitval[1]); // 정렬에 필요한 List에 차량 추가
                }
            }
            else if (splitval[2] == "OUT") // 차량이 나갔을때
            {
                int cal = Convert.ToInt32(splitval[0].Split(':')[0])*60 + Convert.ToInt32(splitval[0].Split(':')[1]);
                num_total[splitval[1]] += cal - num_period[splitval[1]]; // 머문시간 추가
                check_out[splitval[1]] = true; // 나감 표시
            }

        }
        foreach(var i in check_out.Keys) // 나가지 않은 차량 정산
        {
            if(!check_out[i])
            {
                int term = 1439 - num_period[i];
                num_total[i] += term;
            }
        }
        a.Sort(); // 차량번호 작은 순으로 정렬
        answer = new int[a.Count];
        int k = 0;
        foreach(var i in a)
        {
            answer[k++] = CalPrice(fees,num_total[i]);
        }
        
        return answer;
    }
    public int CalPrice(int[] fees,int term)
    {
        if ( term > fees[0])
            return (int)Math.Ceiling(((double)term - (double)fees[0]) / (double)fees[2])*fees[3] + fees[1] ;
        else
            return fees[1];
    }
}
profile
주섬주섬..

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

좋은 글 감사합니다!

답글 달기