시간 기록하기
정산하기
2-1. 미출차 차량 시간 계산
2-2. 정산하기
import java.util.*;
class Solution {
public List<Integer> solution(int[] fees, String[] records) {
Comparator<String> comparator = (s1, s2)->s1.compareTo(s2);
Map<String, Integer> timeMap = new TreeMap<>(comparator);
final int basetime = fees[0]; //기본 시간
final int baseFee = fees[1]; //기본 요금
final int plustime = fees[2]; //추가 시간
final int plusFee = fees[3]; //추가 요금
/* 기록하기
* Map<차번호, 시간>*/
for(String record : records) {
String[] r = record.split(" ");
int time = Integer.parseInt(r[0].split(":")[0]) * 60 + Integer.parseInt(r[0].split(":")[1]);
String carNum = r[1];
//IN 이면 "-시간" OUT이면 "시간"
time = (r[2].equals("IN")) ? time*-1 : time;
timeMap.put(carNum, timeMap.getOrDefault(carNum, 0) + time);
}
/* 정산하기 */
timeMap.forEach((key, value) -> {
//1. 출차 내역 없는 차량(시간이 음수값 or 0 인 차량) 시간 계산
int howlong = (value <= 0) ? value + 1439 : value;
//2. 정산
int howmuch = (howlong < basetime) ? baseFee : baseFee + (int) Math.ceil(((float)(howlong - basetime) / plustime)) * plusFee;
timeMap.put(key, howmuch);
});
return new ArrayList<>(timeMap.values());
}
}