일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- windows10
- WPF
- IValueConverter
- File
- git
- C#
- dotNET
- programmers
- logging
- chashtag
- Binding
- algorithm
- Microsoft
- coding-test
- Github
- ListView
- .net
- windows
- mysql
- commit
- Process
- Coding
- convert
- log
- tls
- Visual Studio
- csharp
- 코딩테스트
- string
- nullable
Archives
- Today
- Total
CHashtag
[프로그래머스] [C#] 주차 요금 계산 본문
반응형
안녕하세요.
오늘은 프로그래머스 코딩테스트 연습 문제인 "주차 요금 계산" 을 풀어 보았습니다.
문제 링크
https://programmers.co.kr/learn/courses/30/lessons/92341
코딩테스트 연습 - 주차 요금 계산
[180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000]
programmers.co.kr
문제 풀이 방법
자세한 문제 풀이는 주석으로 남겨두었습니다.
코드
using System;
using System.Linq;
using System.Collections.Generic;
public class Solution {
public int[] solution(int[] fees, string[] records)
{
int[] answer = new int[] { };
List<RecordData> rDatas = new List<RecordData>();
foreach(string record in records)
{
string[] arr = record.Split(' ');
// 기존에 입차/출차된 기록이 있는지 확인
// 출차의 경우 무조건 기록이 존재하고, 입차의 경우 없을 수도 있다.
RecordData rData = rDatas.FirstOrDefault(x => x.CarNum == arr[1]);
if (arr[2] == "IN")
{
// 입차일 때, 기존에 기록이 없을 때는 추가해주도록 한다.
if(rData == null)
{
rData = new RecordData { CarNum = arr[1] };
rDatas.Add(rData);
}
rData.InTime = arr[0];
}
else
{
// 정산은 한번에 진행해야 하기 때문에 지금은 몇분 주차하였는지만 기록한다.
rData.TotalMinute += GetDiffFromTwoTime(rData.InTime, arr[0]);
// 기록 후 입차 기록을 지워준다.
rData.InTime = string.Empty;
}
}
foreach(var rData in rDatas)
{
if (!string.IsNullOrEmpty(rData.InTime))
{
// 아직 출차되지 않은 차량이 있다면 출차 진행
rData.TotalMinute += GetDiffFromTwoTime(rData.InTime);
}
}
// 차량 번호 순으로 정렬
rDatas.Sort((x, y) => x.CarNum.CompareTo(y.CarNum));
return rDatas.Select(x => GetFee(x.TotalMinute, fees)).ToArray();
}
public int GetFee(int minute, int[] fees)
{
int fee = fees[1];
minute -= fees[0];
if (minute > 0)
{
int a = (int)Math.Ceiling((double)minute / fees[2]);
fee += fees[3] * a;
}
return fee;
}
/// <summary>
/// 항상 t2가 크다.
/// t2 - t1을 분으로 return한다.
/// </summary>
public int GetDiffFromTwoTime(string t1, string t2 = "23:59")
{
int t1Minute = ConvertToMinute(t1);
int t2Minute = ConvertToMinute(t2);
return t2Minute - t1Minute;
}
public int ConvertToMinute(string t)
{
string[] temp = t.Split(':');
return int.Parse(temp[0]) * 60 + int.Parse(temp[1]);
}
}
public class RecordData
{
public string CarNum { get; set; }
public string InTime { get; set; }
public int TotalMinute { get; set; }
}
감사합니다.
반응형
'알고리즘' 카테고리의 다른 글
[백준] [C#] 21919. 소수 최소 공배수 (0) | 2022.02.27 |
---|---|
[프로그래머스] [C#] 신고 결과 받기 (0) | 2022.02.27 |
[프로그래머스] [C#] 거리두기 확인하기 (0) | 2022.01.10 |
[프로그래머스] [Java] 없는 숫자 더하기 (0) | 2021.09.14 |
[프로그래머스] [C#] 없는 숫자 더하기 (0) | 2021.09.14 |