int[] fees //길이 = 4
기본 시간, 기본 요금, 단위 시간, 단위 요금
String[] records //1 <= records <= 1000
시각, 차량번호, 내역
"HH:MM 0000 IN/OUT"
시각 기준 오름차순
출차 내역 x => 23:59 출차로 간주
초과 시간이 단위 시간으로 나누어 떨어지지 않으면 ((올림))한다.
요금 = 기본요금 + (주차시간 - 기본 시간)/단위시간 * 단위요금
int[] answer
차량 번호 오름차순으로 주차 요금 return
fees 토대로 상수 선언
final TIME
final FEE
final UNIT_TIME
final UNIT_FEE
records에서 정보 읽어오기
->IN이면
HashMap<Integer, String> beforeCal
크기는 records의 길이
{챠량 번호, 시간}이 저장
substring()사용
->OUT이면
계산하기
0넣기
HashMap<Integer, Integer> afterCal
차량 번호, 총 주차 시간(분) 필요
이 정보를 토대로 요금 계산
차량번호 정렬 후 주차요금 answer에 저장
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class Solution {
public int[] solution(int[] fees, String[] records) {
final int TIME = fees[0];
final int FEE = fees[1];
final int UNIT_TIME = fees[2];
final int UNIT_FEE = fees[3];
HashMap<Integer, String> beforeCal = new HashMap<>();
HashMap<Integer, Integer> afterCal = new HashMap<>();
HashMap<Integer, Integer> totalFee = new HashMap<>();
for(int i = 0; i < records.length; i++) {
String time = records[i].substring(0, 5);
int carNum = Integer.parseInt(records[i].substring(6, 10));
boolean in = records[i].substring(11).equals("IN");
if(in)
beforeCal.put(carNum, time);
else {
String timeBefore = beforeCal.get(carNum);
int dif = calcTime(timeBefore, time);
if(afterCal.get(carNum) == null)
afterCal.put(carNum, dif);
else
afterCal.replace(carNum, afterCal.get(carNum) + dif);
beforeCal.replace(carNum, "");
}
}
for(int car : beforeCal.keySet()) {
if(!beforeCal.get(car).isEmpty()) {
String timeBefore = beforeCal.get(car);
String time = "23:59";
int dif = calcTime(timeBefore, time);
if(afterCal.get(car) == null)
afterCal.put(car, dif);
else
afterCal.replace(car, afterCal.get(car) + dif);
beforeCal.replace(car, "");
}
}
for(int car : afterCal.keySet()) {
int total = afterCal.get(car) - TIME;
if(total < 0)
total = 0;
if(total % UNIT_TIME == 0)
total = FEE + total / UNIT_TIME * UNIT_FEE;
else
total = FEE + (total / UNIT_TIME + 1)* UNIT_FEE;
totalFee.put(car, total);
}
Object[] cars = totalFee.keySet().toArray();
Arrays.sort(cars);
int[] answer = new int[cars.length];
for(int i = 0; i < answer.length; i++)
answer[i] = totalFee.get(cars[i]);
return answer;
}
static int calcTime(String timeBefore, String time) {
try{
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date1 = sdf.parse(timeBefore);
Date date2 = sdf.parse(time);
long timeMil1 = date1.getTime();
long timeMil2 = date2.getTime();
int dif = (int)(timeMil2 - timeMil1) / (1000 * 60);
return dif;
}
catch(ParseException e) {
e.printStackTrace();
}
return -1;
}
}