https://programmers.co.kr/learn/courses/30/lessons/92341
코딩테스트 연습 - 주차 요금 계산
[180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000]
programmers.co.kr
[문제]
[풀이]
입력이 IN 이면 주차를 시켜야하고 OUT이면 출차시키고 주차된 시간을 누적시킨다음 요금을 계산해야한다.
현재 주차 상태를 나타내는 HashMap
총 시간누적을 나타내고 차량번호가 적은순대로 출력시키기위한 TreeMap 을 사용
1. IN 일 경우 => 차번호, 주차한시각 으로 주차상태 HashMap에 저장
2. OUT 일 경우 => 출차시각-주차한시각 의 값을 시간누적 TreeMap에 저장후 주차상태를 제거
3. 주차상태 Map에 아직 남아있는 경우 => 23:59분을 출차 기점으로 2번 과정을 다시 반복.
4. 주차시간누적 Map을 순회하여 요금을 계산
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import java.util.*;
import java.io.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
class Solution {
public int[] solution(int[] fees, String[] records) {
ParkingLot parkingLot = new ParkingLot(fees);
parkingLot.setDashBoard(records);
return parkingLot.getResult();
}
static class ParkingLot{
private int basicFee,basicTime,overFee,overTime;
// carNumber, time
private TreeMap<String,Integer> timeStatus;
private HashMap<String,String> parkingStatus;
public ParkingLot(int[] fees) {
timeStatus = new TreeMap<>();
parkingStatus = new HashMap<>();
this.basicTime = fees[0];
this.basicFee = fees[1];
this.overTime = fees[2];
this.overFee = fees[3];
}
public void setDashBoard(String[] records){
for (String record : records) {
String[] log = record.split(" ");
if(log[2].equals("IN")){ // 주차
parkingStatus.put(log[1], log[0]);
}else{ // 출차
accTime(log[0], log[1]);
parkingStatus.remove(log[1]);
}
}
// 23:59분 까지 빠져나가지 않은 나머지 차들 시간계산
parkingStatus.keySet().iterator()
.forEachRemaining(number->accTime("23:59",number));
}
private void accTime(String time, String number) { // 시간누적
String[] inTime = parkingStatus.get(number).split(":");
String[] outTime = time.split(":");
int hourDiff = Integer.parseInt(outTime[0])-Integer.parseInt(inTime[0]);
int minDiff = Integer.parseInt(outTime[1])-Integer.parseInt(inTime[1]);
if(minDiff<0){
minDiff=minDiff+60;
hourDiff--;
}
if(!timeStatus.containsKey(number))
timeStatus.put(number,minDiff+hourDiff*60);
else
timeStatus.replace(number,timeStatus.get(number)+minDiff+hourDiff*60);
}
public int[] getResult(){ //요금 계산
ArrayList<Integer> result = new ArrayList<>();
for (Integer totalTime : timeStatus.values()) {
int fee = totalTime<=basicTime ?
basicFee : basicFee+(int)Math.ceil((double) (totalTime-basicTime)/overTime)*overFee;
result.add(fee);
}
return result.stream().mapToInt(i->i).toArray();
}
}
}
|
cs |
'알고리즘,PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스] [Level2] 양궁대회 JAVA (0) | 2022.02.14 |
---|---|
[프로그래머스] [Level3] 양과늑대 JAVA (0) | 2022.02.08 |
[프로그래머스] [Level2] k진수에서 소수 개수 구하기JAVA (0) | 2022.02.04 |
[프로그래머스] [Level1] 신고결과받기 JAVA (0) | 2022.02.02 |
[프로그래머스] [Level2] 더 맵게 JAVA (0) | 2022.01.12 |