240213 주차 요금 계산

Jongleee·2024년 2월 13일
0

TIL

목록 보기
494/737
public int[] solution(int[] fees, String[] records) {
	int lastTime = getMinutes("23:59");
	Map<String, Integer> parking = new HashMap<>();
	Map<String, Integer> times = new HashMap<>();
	List<String> cars = new ArrayList<>();

	for (String r : records) {
		String[] parts = r.split(" ");
		int time = getMinutes(parts[0]);
		String car = parts[1];

		if (!cars.contains(car)) {
			cars.add(car);
			times.put(car, 0);
		}

		if (parking.containsKey(car)) {
			times.put(car, times.get(car) + (time - parking.get(car)));
			parking.remove(car);
		} else {
			parking.put(car, time);
		}
	}

	int[] answer = new int[cars.size()];
	Collections.sort(cars);

	for (int i = 0; i < cars.size(); i++) {
		String car = cars.get(i);
		int time = times.get(car);

		if (parking.containsKey(car)) {
			time += (lastTime - parking.get(car));
		}

		if (time > fees[0]) {
			answer[i] += fees[1] + Math.ceil((time - fees[0]) / (fees[2] * 1.0)) * fees[3];
		} else {
			answer[i] += fees[1];
		}
	}

	return answer;
}

public int getMinutes(String time) {
	String[] t = time.split(":");
	return Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1]);
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/92341

0개의 댓글