https://school.programmers.co.kr/learn/courses/30/lessons/92341?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
실패한 첫 코드
import java.util.*;
class Solution {
public int[] solution(int[] fees, String[] records) {
/*
결과값은 resultMap 에서 <key : 차량번호, value : 요금> 으로 처리한다.
split() 함수로 문자열을 자른다.
IN 일 경우 Map 에 <Key : 차량번호, value: 시간> 으로 담는다.
OUT 일 경우
Map 에서 해당 차량을 꺼내고
시간들을 모두 분으로 바꿔서 차이를 구한다.
차이에서 기본시간을 뺸다.
0 이하이면 resultMap 에 기본금액만 담는다.
0 초과이면 단위시간으로 나눠서 올림하고 단위금액을 곱한 값을 담는다.
차량번호 작은차부터 요금을 배열에 담아 반환한다.
*/
HashMap<String, String> processMap = new HashMap<>();
HashMap<String, Integer> resultMap = new HashMap<>();
for(String item : records){
String[] strArr = item.split(" ");
if(strArr[2]=="IN"){
processMap.put(strArr[1], strArr[0]);
}
else if(strArr[2]=="OUT"){
String inTime = processMap.get(strArr[1]);
String outTime = strArr[0];
String[] inTimeArr = inTime.split(":");
String[] outTimeArr = outTime.split(":");
int inTimeMinute = Integer.parseInt(inTimeArr[0])*60 + Integer.parseInt(inTimeArr[1]);
int outTimeMinute = Integer.parseInt(outTimeArr[0])*60 + Integer.parseInt(outTimeArr[1]);
int parkingTime = outTimeMinute-inTimeMinute;
if(parkingTime-fees[0]<=0){
if(resultMap.containsKey(inTimeArr[1])){
resultMap.put(inTimeArr[1], resultMap.get(inTimeArr[1])+fees[1]);
}
else{
resultMap.put(inTimeArr[1], fees[1]);
}
}
else {
if(resultMap.containsKey(inTimeArr[1])){
resultMap.put(inTimeArr[1], resultMap.get(inTimeArr[1])+((int)Math.ceil((parkingTime-fees[0])/fees[2])*fees[3]));
}
else{
resultMap.put(inTimeArr[1], (int)Math.ceil((parkingTime-fees[0])/fees[2])*fees[3]);
}
}
}
}
int[] answer = new int[resultMap.size()];
List<String> keyList = new ArrayList<>(resultMap.keySet());
List<Integer> keyIntList = new ArrayList<>();
for(String item : keyList){
keyIntList.add(Integer.parseInt(item));
}
keyIntList.sort((s1, s2) -> s1.compareTo(s2));
for (int i=0; i<resultMap.size(); i++) {
answer[i] = resultMap.get(String.valueOf(keyIntList.get(i)));
}
return answer;
}
}
애초에 로직을 잘못 짰다.
똑같은 차가 입차와 출차를 한번만 한다고 착각하고 풀었다.
1시간 가량 풀었는데 실패했다.
다시 풀어보자.
다시 푼 코드
import java.util.*;
class Solution {
public int[] solution(int[] fees, String[] records) {
// ioMap 는 <차량번호, 입출차시간> 을 저장하는 맵이다.
HashMap<Integer, Integer> ioMap = new HashMap<>();
// resultMap 은 <차량번호, 총 주차시간량> 을 저장하는 맵이다.
HashMap<Integer, Integer> resultMap = new HashMap<>();
for(String record : records){
String[] splitRecordArr = record.split(" ");
if(splitRecordArr[2].equals("IN")){
int carNum = Integer.parseInt(splitRecordArr[1]);
String[] splitTimeArr = splitRecordArr[0].split(":");
int inTime = Integer.parseInt(splitTimeArr[0])*60 + Integer.parseInt(splitTimeArr[1]);
ioMap.put(carNum, inTime);
}else if(splitRecordArr[2].equals("OUT")){
int carNum = Integer.parseInt(splitRecordArr[1]);
String[] splitTimeArr = splitRecordArr[0].split(":");
int outTime = Integer.parseInt(splitTimeArr[0])*60 + Integer.parseInt(splitTimeArr[1]);
int inTime = ioMap.get(carNum);
ioMap.remove(carNum);
if(resultMap.containsKey(carNum)){
resultMap.put(carNum, resultMap.get(carNum)+outTime-inTime);
}else{
resultMap.put(carNum, outTime-inTime);
}
}
}
ArrayList<Integer> ioKeyList= new ArrayList<>(ioMap.keySet());
while(!ioMap.isEmpty()){
for(int ioKey : ioKeyList){
int inTime = ioMap.get(ioKey);
ioMap.remove(ioKey);
int outTime = 23*60+59;
if(resultMap.containsKey(ioKey)){
resultMap.put(ioKey, resultMap.get(ioKey)+outTime-inTime);
}else{
resultMap.put(ioKey, outTime-inTime);
}
}
}
// 반복문이 완료되었으니 resultMap 에는 <차량번호, 총 주차시간량> 이 담겨있다.
ArrayList<Integer> list = new ArrayList<>(resultMap.keySet());
Collections.sort(list);
int[] answer = new int[list.size()];
for(int i=0; i<list.size(); i++){
int calTime = resultMap.get(list.get(i)) - fees[0];
if(calTime<=0){
answer[i] = fees[1];
}else{
if(calTime%fees[2]>0){
answer[i] = fees[1] + (calTime/fees[2]+1)*fees[3];
}else {
answer[i] = fees[1] + calTime / fees[2] * fees[3];
}
}
}
return answer;
}
}
결국 이것도 다시 한시간 가량 걸렸는데, NPE 가 떠서 ㅠㅠ IDE 의 도움을 약간 받아 성공했다.!!
'알고리즘 > 문제' 카테고리의 다른 글
| *[프로그래머스] lv2 주식가격 /java (0) | 2023.05.09 |
|---|---|
| [프로그래머스] lv2 오픈채팅방 / Java (0) | 2023.05.06 |
| [프로그래머스] lv2 전화번호 목록 / java (0) | 2023.04.06 |
| *[프로그래머스] Lv2 k진수에서 소수 개수 구하기 / Java (0) | 2023.03.27 |
| [프로그래머스] Lv2 타겟 넘버 / JAVA (0) | 2023.03.24 |
