2022 KAKAO BLIND RECRUITMENT > 주차요금계산
https://programmers.co.kr/learn/courses/30/lessons/92341
https://tech.kakao.com/2022/01/14/2022-kakao-recruitment-round-1/
문제에 대한 해설은 카카오 신입공채 테크블로그의 해설이 워낙 잘 설명되어 있으므로, 이 글로 대체하겠습니다.
static int BaseTime, BaseFee, UnitTime, UnitFee;
static Function<String, Integer> stoi = Integer::parseInt;
public static int[] solution(int[] fees, String[] records) {
BaseTime = fees[0];
BaseFee = fees[1];
UnitTime = fees[2];
UnitFee = fees[3];
StringTokenizer st;
Queue<Record> inList = new ArrayDeque<>();
List<Record> outList = new ArrayList<>();
for (String record : records) {
st = new StringTokenizer(record);
int minute = chHtoM(st.nextToken());
String carNum = st.nextToken();
String InOut = st.nextToken();
if (InOut.equals("IN")) {
inList.add(new Record(carNum, minute));
} else {
outList.add(new Record(carNum, minute));
}
}
Map<String, Integer> sum = new HashMap<>();
while (!inList.isEmpty()) {
Record in = inList.poll();
Record out = null;
for (Record o : outList) {
if (in.carNum.equals(o.carNum)) {
out = o;
break;
}
}
outList.remove(out);
if (out != null) {
int time = out.m - in.m;
sum.merge(in.carNum, time, (ori, init) -> time + ori); //computeIfAbsent , merge
} else {
int time = 1439 - in.m;
sum.merge(in.carNum, time, (ori, init) -> time + ori);
}
}
List<Fee> answer = new ArrayList<>();
for (String key : sum.keySet()) {
int fee = 0;
int acc = sum.get(key);
if (acc <= BaseTime) {
fee = BaseFee;
} else {
fee = (int) (BaseFee + Math.ceil((float) (acc - BaseTime) / UnitTime) * UnitFee);
}
answer.add(new Fee(Integer.parseInt(key), fee));
}
Collections.sort(answer);
int[] ans = new int[answer.size()];
int idx = 0;
for (Fee f : answer) {
ans[idx++] = f.cost;
}
return ans;
}
private static int chHtoM(String s) {
String[] time = s.split(":");
int h = stoi.apply(time[0]);
int m = stoi.apply(time[1]);
return h * 60 + m;
}
static class Record implements Comparable<Record> {
String carNum;
int m;
public Record(String carNum, int m) {
this.carNum = carNum;
this.m = m;
}
@Override
public int compareTo(Record o) {
return this.m - o.m;
}
}
static class Fee implements Comparable<Fee> {
int num;
int cost;
public Fee(int num, int cost) {
this.num = num;
this.cost = cost;
}
@Override
public int compareTo(Fee o) {
return this.num - o.num;
}
}