
카카오 문제는 항상 요구사항이 많아서 겉보기에는 쉬워 보여도 막상 풀다 보면 꽤 고생하게 만든다. 나는 시간을 계산할 때 LocalTime 타입을 사용했다.
먼저 두 개의 Map을 만들었는데 하나는 입출차 정보를 관리하기 위해 다른 하나는 차량별 누적 주차 시간을 계산하기 위해 사용했다.
마지막으로 리스트를 이용해 각 차량의 추가 요금을 계산하였다.
시간복잡도: O(NlogN), 공간복잡도: O(N)
- [ x ] 1회
- 2회
- 3회
import java.util.*;
import java.time.*;
class Solution {
public int[] solution(int[] fees, String[] records) {
int baseTime = fees[0];
int baseFee = fees[1];
int unitTime = fees[2];
int unitFee = fees[3];
HashMap<String,String> hm = new HashMap<>();
HashMap<String,Integer> sum = new HashMap<>();
for(int i=0;i<records.length;i++){
String [] token = records[i].split(" ");
if(!hm.containsKey(token[1]) && token[2].equals("IN")){
hm.put(token[1],token[0]);
}else{
String start = hm.get(token[1]);
String end = token[0];
hm.remove(token[1]);
int diff = timeCalc(start,end);
sum.put(token[1],sum.getOrDefault(token[1],0)+diff);
}
}
for(String car: hm.keySet()){
int diff = timeCalc(hm.get(car), "23:59");
sum.put(car,sum.getOrDefault(car,0)+diff);
}
ArrayList<String> al = new ArrayList<>(sum.keySet());
Collections.sort(al);
int[] answer = new int [al.size()];
for(int i=0;i<answer.length;i++){
int total = sum.get(al.get(i));
int fee = baseFee;
if(total>baseTime){
fee += Math.ceil((total - baseTime) / (double) unitTime) * unitFee;
}
answer[i] = fee;
}
return answer;
}
public static int timeCalc(String start, String end){
LocalTime startTime = LocalTime.parse(start);
LocalTime endTime = LocalTime.parse(end);
return (int) Duration.between(startTime,endTime).toMinutes();
}
}

public static int timeCalc(String start, String end) {
String[] s = start.split(":");
String[] e = end.split(":");
int startMinutes = Integer.parseInt(s[0]) * 60 + Integer.parseInt(s[1]);
int endMinutes = Integer.parseInt(e[0]) * 60 + Integer.parseInt(e[1]);
return endMinutes - startMinutes;
}