반응형
문제 풀이📝
문자열을 처리하여 푸는 문제였다.
1. 문자열 상태의 시간을 int 형태의 분으로 변환하여 주차 요금을 계산하였따.
2. 차량의 주차 시간을 저장하는 Map을 정의하여 총 주차 시간을 저장하였다.
구현🛠️
import java.util.Map;
import java.util.TreeMap;
public class Solution_주차_요금_계산 {
class Record {
private int entrance;
private int time;
private boolean out;
// 주차장 입장
public void enter(int enterTime) {
this.entrance = enterTime;
this.out = false;
}
// 주차장 퇴장
public void exit(int exitTime) {
this.time += exitTime - entrance; // 총 이용 시간은 쌓인다.
this.out = true;
}
// 총 주차 요금 계산
public int calculateFee() {
int fee = basicFee;
// 기본 시간보다 적게 사용했으면 -> 기본요금
if (time <= basicTime) {
return fee;
}
time -= basicTime;
// 기본 시간 초과 -> 단위 요금 부과
fee += unitFee * Math.ceil((double)time / unitTime);
return fee;
}
}
private Map<Integer, Record> parkingTimes = new TreeMap<>();
private int basicTime, basicFee;
private int unitTime, unitFee;
private int limitTime;
public int[] solution(int[] fees, String[] records) {
basicTime = fees[0];
basicFee = fees[1];
unitTime = fees[2];
unitFee = fees[3];
limitTime = timeToMinute("23:59");
calculate(records);
int[] result = new int[parkingTimes.size()];
int idx = 0;
for (int id : parkingTimes.keySet()) {
result[idx++] = parkingTimes.get(id).calculateFee();
}
return result;
}
public void calculate(String[] records) {
for (String record : records) {
String[] attr = record.split(" ");
int id = Integer.parseInt(attr[1]);
int time = timeToMinute(attr[0]);
parkingTimes.putIfAbsent(id, new Record());
// 주차장 입장
if (attr[2].equals("IN")) {
parkingTimes.get(id).enter(time);
continue;
}
// 주차장 퇴장
parkingTimes.get(id).exit(time);
}
// 자정 전까지 퇴장하지 않은 차량들 계산
calculateRemained();
}
public void calculateRemained() {
for (int id : parkingTimes.keySet()) {
// 출차 시간 불분명 -> 23:59분 출차로 간주
if (!parkingTimes.get(id).out) {
parkingTimes.get(id).exit(limitTime);
}
}
}
public int timeToMinute(String time) {
String[] splitted = time.split(":");
int hour = Integer.parseInt(splitted[0]) * 60;
int minute = Integer.parseInt(splitted[1]);
return hour + minute;
}
}
실행결과✅
테스트 1 〉 통과 (1.00ms, 74.5MB)
테스트 2 〉 통과 (1.29ms, 75.9MB)
테스트 3 〉 통과 (1.29ms, 72.9MB)
테스트 4 〉 통과 (1.71ms, 76.9MB)
테스트 5 〉 통과 (3.36ms, 69.9MB)
테스트 6 〉 통과 (2.26ms, 74.1MB)
테스트 7 〉 통과 (6.53ms, 75.5MB)
테스트 8 〉 통과 (6.39ms, 83.9MB)
테스트 9 〉 통과 (2.47ms, 78.5MB)
테스트 10 〉 통과 (8.76ms, 78.7MB)
테스트 11 〉 통과 (7.43ms, 79MB)
테스트 12 〉 통과 (9.04ms, 86.7MB)
테스트 13 〉 통과 (0.90ms, 73.6MB)
테스트 14 〉 통과 (1.16ms, 79MB)
테스트 15 〉 통과 (0.77ms, 72.7MB)
테스트 16 〉 통과 (0.63ms, 74.5MB)
반응형
문제 풀이📝
문자열을 처리하여 푸는 문제였다.
1. 문자열 상태의 시간을 int 형태의 분으로 변환하여 주차 요금을 계산하였따.
2. 차량의 주차 시간을 저장하는 Map을 정의하여 총 주차 시간을 저장하였다.
구현🛠️
import java.util.Map;
import java.util.TreeMap;
public class Solution_주차_요금_계산 {
class Record {
private int entrance;
private int time;
private boolean out;
// 주차장 입장
public void enter(int enterTime) {
this.entrance = enterTime;
this.out = false;
}
// 주차장 퇴장
public void exit(int exitTime) {
this.time += exitTime - entrance; // 총 이용 시간은 쌓인다.
this.out = true;
}
// 총 주차 요금 계산
public int calculateFee() {
int fee = basicFee;
// 기본 시간보다 적게 사용했으면 -> 기본요금
if (time <= basicTime) {
return fee;
}
time -= basicTime;
// 기본 시간 초과 -> 단위 요금 부과
fee += unitFee * Math.ceil((double)time / unitTime);
return fee;
}
}
private Map<Integer, Record> parkingTimes = new TreeMap<>();
private int basicTime, basicFee;
private int unitTime, unitFee;
private int limitTime;
public int[] solution(int[] fees, String[] records) {
basicTime = fees[0];
basicFee = fees[1];
unitTime = fees[2];
unitFee = fees[3];
limitTime = timeToMinute("23:59");
calculate(records);
int[] result = new int[parkingTimes.size()];
int idx = 0;
for (int id : parkingTimes.keySet()) {
result[idx++] = parkingTimes.get(id).calculateFee();
}
return result;
}
public void calculate(String[] records) {
for (String record : records) {
String[] attr = record.split(" ");
int id = Integer.parseInt(attr[1]);
int time = timeToMinute(attr[0]);
parkingTimes.putIfAbsent(id, new Record());
// 주차장 입장
if (attr[2].equals("IN")) {
parkingTimes.get(id).enter(time);
continue;
}
// 주차장 퇴장
parkingTimes.get(id).exit(time);
}
// 자정 전까지 퇴장하지 않은 차량들 계산
calculateRemained();
}
public void calculateRemained() {
for (int id : parkingTimes.keySet()) {
// 출차 시간 불분명 -> 23:59분 출차로 간주
if (!parkingTimes.get(id).out) {
parkingTimes.get(id).exit(limitTime);
}
}
}
public int timeToMinute(String time) {
String[] splitted = time.split(":");
int hour = Integer.parseInt(splitted[0]) * 60;
int minute = Integer.parseInt(splitted[1]);
return hour + minute;
}
}
실행결과✅
테스트 1 〉 통과 (1.00ms, 74.5MB)
테스트 2 〉 통과 (1.29ms, 75.9MB)
테스트 3 〉 통과 (1.29ms, 72.9MB)
테스트 4 〉 통과 (1.71ms, 76.9MB)
테스트 5 〉 통과 (3.36ms, 69.9MB)
테스트 6 〉 통과 (2.26ms, 74.1MB)
테스트 7 〉 통과 (6.53ms, 75.5MB)
테스트 8 〉 통과 (6.39ms, 83.9MB)
테스트 9 〉 통과 (2.47ms, 78.5MB)
테스트 10 〉 통과 (8.76ms, 78.7MB)
테스트 11 〉 통과 (7.43ms, 79MB)
테스트 12 〉 통과 (9.04ms, 86.7MB)
테스트 13 〉 통과 (0.90ms, 73.6MB)
테스트 14 〉 통과 (1.16ms, 79MB)
테스트 15 〉 통과 (0.77ms, 72.7MB)
테스트 16 〉 통과 (0.63ms, 74.5MB)