풀이 방법✏️
Map<String, List[]>
형태로 주차 정보를 저장 이때 key 값은 차량 번호, value의 0번째 리스트는 입차 시간, 1번째 리스틑 출차 시간을 저장한다.
- 저장한 정보를 바탕으로 총 주차 시간을 계산
- 총 주차 시간을 바탕으로 주차 요금 계산
소스 코드(feat. 알찬 주석)⌨️
import java.util.*;
class Solution {
static Map<String, List[]> parkingInfo;
public int[] solution(int[] fees, String[] records) {
parkingInfo = new TreeMap<>();
for(int i=0; i<records.length; i++){
String[] temp = records[i].split(" ");
String time = temp[0];
String carNum = temp[1];
String type = temp[2];
if(parkingInfo.get(carNum) == null){
List<String> enterInfo = new ArrayList<>();
List<String> outInfo = new ArrayList<>();
List[] input = new List[2];
input[0] = enterInfo;
input[1] = outInfo;
parkingInfo.put(carNum, input);
}
List[] info = parkingInfo.get(carNum);
if(type.equals("IN")){
info[0].add(time);
continue;
}
if(type.equals("OUT")){
info[1].add(time);
}
}
int[] answer = new int[parkingInfo.keySet().size()];
int index = 0;
for(Map.Entry<String, List[]> entry : parkingInfo.entrySet()){
answer[index] = calFee(entry.getValue(), fees);
index++;
}
return answer;
}
private static int calFee(List[] infos, int[] fees){
List<String> enterInfo = infos[0];
List<String> outInfo = infos[1];
int minCnt = Integer.min(enterInfo.size(), outInfo.size());
int stay = 0;
for(int i=0; i<minCnt; i++){
stay += calTime(enterInfo.get(i), outInfo.get(i));
}
if(enterInfo.size() > outInfo.size()){
stay += calTime(enterInfo.get(enterInfo.size() - 1), "23:59");
}
if(stay <= fees[0]){
return fees[1];
}
int fee = fees[1];
fee += Math.ceil((stay - fees[0]) / (double)fees[2]) * fees[3];
return fee;
}
private static int calTime(String enterTime, String outTime){
String[] enter = enterTime.split(":");
String[] out = outTime.split(":");
int enterHour = Integer.parseInt(enter[0]);
int enterMinute = Integer.parseInt(enter[1]);
int outHour = Integer.parseInt(out[0]);
int outMinute = Integer.parseInt(out[1]);
return (outHour - enterHour) * 60 + (outMinute - enterMinute);
}
}