티스토리 뷰
프로그래머스 92341번 - 주차 요금 계산
요구사항
1. 문제가 길기 때문에 본문의 사진을 참고하길 바랍니다.
요구사항 분석 및 풀이과정
1. 문제만 길고, 문자열을 분해만 잘하면 단순히 산술문제입니다.
2. 주의해야하는 점은 출차를 하지 않을 경우는 23시 59분에 출차한 것으로 가정한다는 것이다.
3. 그리고 차량은 입/출차를 여러번 할 수 있습니다. 이 경우는 요금을 누적하여 최종 요금을 구해야합니다.
소스코드 작성
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
class Solution {
private static class Machine {
private final int baseTime;
private final int baseFee;
private final int unitTime;
private final int unitFee;
public Machine(int baseTime, int baseFee, int unitTime, int unitFee) {
this.baseTime = baseTime;
this.baseFee = baseFee;
this.unitTime = unitTime;
this.unitFee = unitFee;
}
public int totalFee(int minuate) {
int result = 0;
if (minuate <= baseTime) {
return baseFee;
}
if (minuate > baseTime) {
result += baseFee;
minuate -= baseTime;
}
result += ((int)Math.ceil((float)minuate / unitTime) * unitFee);
return result;
}
}
private static class CarLog {
private String carId;
private int enterTime;
private int leaveTime;
public CarLog(String carId, int enterTime) {
this.carId = carId;
this.enterTime = enterTime;
this.leaveTime = 23 * 60 + 59;
}
public String getCarId() {
return carId;
}
public void updateLeaveTime(int leaveTime) {
this.leaveTime = leaveTime;
}
public int stayTime() {
return leaveTime - enterTime;
}
}
private int minuate(String time) {
int hour = Integer.parseInt(time.substring(0, 2));
int min = Integer.parseInt(time.substring(3));
return hour * 60 + min;
}
public int[] solution(int[] fees, String[] records) {
Map<String, CarLog> logs = new HashMap<>();
Map<String, Integer> prices = new TreeMap<>();
Machine parking = new Machine(fees[0], fees[1], fees[2], fees[3]);
for(String record : records) {
String[] tokens = record.split(" ");
String carId = tokens[1];
String command = tokens[2];
if ("IN".equals(command)) {
logs.put(carId, new CarLog(carId, minuate(tokens[0])));
} else if ("OUT".equals(command)) {
CarLog log = logs.get(carId);
log.updateLeaveTime(minuate(tokens[0]));
prices.put(carId, prices.getOrDefault(carId, 0) + log.stayTime());
logs.remove(carId);
}
}
for(CarLog log : logs.values()) {
prices.put(log.getCarId(), prices.getOrDefault(log.getCarId(), 0) + log.stayTime());
}
return prices.values().stream().mapToInt(parking::totalFee).toArray();
}
}
결과
소스코드 깃허브 주소
'코딩테스트 > 알고리즘' 카테고리의 다른 글
[알고리즘]프로그래머스 49189번 - 가장 먼 노드 (0) | 2022.01.16 |
---|---|
[알고리즘]프로그래머스 43162번 - 네트워크 (0) | 2022.01.16 |
[알고리즘]프로그래머스 42888번 - 오픈채팅방 (0) | 2022.01.15 |
[알고리즘]프로그래머스 12941번 - 최솟값 만들기 (0) | 2022.01.15 |
[알고리즘]프로그래머스 12953번 - N개의 최소공배수 (0) | 2022.01.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 쓰레드
- 오늘의집
- 탐욕법
- 스택
- 코딩인터뷰
- TDD
- Uber
- Java
- 우선순위큐
- 비트연산
- dfs
- dsu
- kotlin
- 해쉬
- 정렬
- 스트림
- JPA
- BFS
- 코드 스니펫
- 구현
- k8s
- dp
- 알고리즘
- 문자열
- sql
- 프로그래머스
- set
- 카카오
- 회고
- 연결리스트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함