글
프로그래머스 레벨2 C# 주차 요금 계산(array.where)
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] fees, string[] records) {
List<string> newRecords = new List<string>();
Dictionary<string, List<string>> dicList = new Dictionary<string, List<string>>();
List<int> answerList = new List<int>();
for (var i=0; i< records.Length; i++)
{
var tmp = records[i].Split(' ');
var list = records.Where(w => w.Substring(6, 4) == tmp[1]).OrderBy(o => o);
if(!dicList.ContainsKey(tmp[1]))
{
dicList.Add(tmp[1], list.ToList());
}
}
var result = dicList.OrderBy(o => o.Key);
double totalMinute = 0;
foreach(var dic in result)
{
DateTime inTime = DateTime.Now;
DateTime outTime = DateTime.Now;
for (var i=0;i< dic.Value.Count;i++ )
{
var time = dic.Value[i].Split(' ')[0];
var gb = dic.Value[i].Split(' ')[2];
if (gb == "IN")
{
inTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + time);
}
else
{
outTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + time);
TimeSpan dateDiff = outTime - inTime;
totalMinute += Convert.ToInt32(dateDiff.TotalMinutes);
}
}
// 출차기록이 없으면 23:59 분 출차로 기록
if (dic.Value.Count % 2 != 0)
{
outTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59");
TimeSpan dateDiff2 = outTime - inTime;
totalMinute += Convert.ToInt32(dateDiff2.TotalMinutes);
}
double parkingPrice = 0;
if (totalMinute <= fees[0])
{
parkingPrice = fees[1];
}
else
{
parkingPrice = fees[1] + Math.Ceiling((totalMinute - fees[0]) / fees[2]) * fees[3];
}
answerList.Add(Convert.ToInt32(parkingPrice));
totalMinute = 0;
}
var answer = answerList.ToArray();
return answer;
}
}
//////////////////
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] fees, string[] records) {
List<string> cars = new List<string>();
Dictionary<string, int> recDic = new Dictionary<string, int>();
Dictionary<string, int> tempDic = new Dictionary<string, int>();
for(int i = 0; i < records.Length; i++)
{
string[] rec = records[i].Split(' ');
if(rec[2] == "IN")
{
int inTime = int.Parse(rec[0].Substring(0, 2)) * 60 + int.Parse(rec[0].Substring(3, 2));
if(!recDic.ContainsKey(rec[1]))
{
cars.Add(rec[1]);
tempDic.Add(rec[1], inTime);
}
else
tempDic[rec[1]] = inTime;
}
else
{
int outTime = int.Parse(rec[0].Substring(0, 2)) * 60 + int.Parse(rec[0].Substring(3, 2));
if(!recDic.ContainsKey(rec[1]))
recDic.Add(rec[1], outTime - tempDic[rec[1]]);
else
recDic[rec[1]] += outTime - tempDic[rec[1]];
tempDic[rec[1]] = -1;
}
}
cars.Sort();
int[] answer = new int[cars.Count];
for(int i = 0; i < cars.Count; i++)
{
int time = 0;
if(!recDic.TryGetValue(cars[i], out time) || tempDic[cars[i]] != -1)
time += 1439 - tempDic[cars[i]];
answer[i] = fees[1];
if(time > fees[0])
{
time -= fees[0];
answer[i] += (time / fees[2]) * fees[3];
if(time % fees[2] != 0)
answer[i] += fees[3];
}
}
return answer;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨0 안전지대(C#, JAVA) try catch(Exception e) (0) | 2023.02.09 |
---|---|
다항식 더하기 JAVA (0) | 2023.02.09 |
프로그래머스 C# 푸드 파이트 대회(for문, 문자열 합하기, P, J) (0) | 2023.02.06 |
프로그래머스 C# 신고 결과 받기(Dictionary, HashMap, P, J) (0) | 2023.02.06 |
프로그래머스 C# 콜라 문제(P, J) (0) | 2023.02.06 |