TIL Java
내일배움캠프 51일차 TIL _ 11주 3일차
mad038
2024. 6. 26. 23:00
- 오늘 있었던 일
- 스프링 공부
- 알고리즘
알고리즘 문제 풀기
● 주차 요금 계산
더보기
●주차 요금 계산//링크
import java.util.*;
class Solution {
public int[] solution(int[] fees, String[] records) {
Map<String, Integer> inTimes = new HashMap<>();
Map<String, Integer> totalTimes = new HashMap<>();
for (String record : records) {
String[] parts = record.split(" ");
String[] timeParts = parts[0].split(":");
int time = Integer.parseInt(timeParts[0]) * 60 + Integer.parseInt(timeParts[1]);
String carNumber = parts[1];
String action = parts[2];
if (action.equals("IN")) {
inTimes.put(carNumber, time);
} else {
int inTime = inTimes.remove(carNumber);
int duration = time - inTime;
totalTimes.put(carNumber, totalTimes.getOrDefault(carNumber, 0) + duration);
}
}
int endOfDay = 23 * 60 + 59;
for (String carNumber : inTimes.keySet()) {
int inTime = inTimes.get(carNumber);
int duration = endOfDay - inTime;
totalTimes.put(carNumber, totalTimes.getOrDefault(carNumber, 0) + duration);
}
List<String> carNumbers = new ArrayList<>(totalTimes.keySet());
Collections.sort(carNumbers);
int[] answer = new int[carNumbers.size()];
for (int i = 0; i < carNumbers.size(); i++) {
String carNumber = carNumbers.get(i);
int totalTime = totalTimes.get(carNumber);
int fee = fees[1];
if (totalTime > fees[0]) {
fee += Math.ceil((totalTime - fees[0]) / (double) fees[2]) * fees[3];
}
answer[i] = fee;
}
return answer;
}
}
- 마지막 태스트가 틀렸다.
- 문제 지문에
- 마지막 태스트문의 한번 들어오고 아에 안나 가는 문제가 있었다.
- ["00:00 1234 IN"]
-
- 나갈때만 체크하는 로직을 짜서 틀렸다.
- 들어오는 것으로 체크해서 다시 짜니 통과했다.
- 문제 지문에
- 밑은 다른 사람의 풀이
-
import java.util.*; class Solution { public int timeToInt(String time) { String temp[] = time.split(":"); return Integer.parseInt(temp[0])*60 + Integer.parseInt(temp[1]); } public int[] solution(int[] fees, String[] records) { TreeMap<String, Integer> map = new TreeMap<>(); for(String record : records) { String temp[] = record.split(" "); int time = temp[2].equals("IN") ? -1 : 1; time *= timeToInt(temp[0]); map.put(temp[1], map.getOrDefault(temp[1], 0) + time); } int idx = 0, ans[] = new int[map.size()]; for(int time : map.values()) { if(time < 1) time += 1439; time -= fees[0]; int cost = fees[1]; if(time > 0) cost += (time%fees[2] == 0 ? time/fees[2] : time/fees[2]+1)*fees[3]; ans[idx++] = cost; } return ans; } }
- 나랑 비슷하지만 코드 자체가 더 간결하고 좋다
당일 회고
- 오늘은 몸이 안좋아서 강의 코드카타하고 강의만 들었다..