2023.09.21.THU

ronglong·2023년 9월 21일
0

[ 프로그래머스 ]

[ 오픈채팅방 ]

: 약간 난이도 높아지니까, 다들 클래스 만들어서 메서드 분리해서 확장성까지 고려해가며 푼다.
나 너무 막 푸나,,

import java.util.*;

class Solution {
    public String[] solution(String[] record) {
        List<String> answer = new ArrayList<>();
        
        //Map(userId, nickName)
        Map<String, String> map = new HashMap<>();
        
        for(int i=0; i<record.length; i++){
            String[] arr = record[i].split(" ");

            if(arr[0].equals("Enter")){
                map.put(arr[1], arr[2]);
            } else if(arr[0].equals("Change")){
                map.replace(arr[1], arr[2]);
            }
        }
        
        for(int i=0; i<record.length; i++){
            String[] arr = record[i].split(" ");
            String nickName = map.get(arr[1]);
            
            if(arr[0].equals("Enter")){
                answer.add(nickName + "님이 들어왔습니다.");
            } else if(arr[0].equals("Leave")){
                answer.add(nickName + "님이 나갔습니다.");
            }
        }
        
        return answer.toArray(new String[0]);
    }
}

[ 주차 요금 계산 ]

: 리팩토링 없는 날 것 그대로의 코드,,
일단 돌아가게 짜놓고 리팩토링하는 아주 고전적인 나,,

Map key기준 정렬은 TreeMap 사용했다.
https://developer-talk.tistory.com/395

이진트리 원리가 적용되었으며, 해시맵보다 성능이 떨어지지만, 정렬에 용이하다.
https://dev-coco.tistory.com/39
https://seeminglyjs.tistory.com/227

import java.util.*;
import java.lang.Math.*;

class Solution {
    public int[] solution(int[] fees, String[] records) {
        List<Integer> answer = new ArrayList<>();
        
        //요금표 정리
        int defaultTime = fees[0];
        int defaultCost = fees[1];
        int unitTime = fees[2];
        int unitCost = fees[3];
        
        //Map으로 차량번호 별 입차 출차 시간 value에 리스트로 쌓기 
        Map<String, List<String>> map = new HashMap<>();
        
        for(String record : records){
            String[] arr = record.split(" ");
            String carNumber = arr[1];
            
            if(map.containsKey(carNumber)){
                List<String> value = map.get(carNumber);
                value.add(arr[0]);
                map.replace(carNumber, value);
            } else {
                List<String> value = new ArrayList<>();
                value.add(arr[0]);
                map.put(carNumber, value);
            }
        }
        
        //Map key를 기준으로 정렬하기 
        Map<String, List<String>> sortedMap = new TreeMap<>(map);
        
        //value에서 누적시간 계산하기(분)
        for(List<String> value : sortedMap.values()){
            int time = 0;
            int cost = defaultCost;
            
            while(value.size()>0){
                String in = value.remove(0);
                //출차 기록 없으면 23:59 출차로 계산
                String out = "23:59";
                if(value.size()!=0){
                    out = value.remove(0);
                }

                int inTime = 60*Integer.parseInt(in.substring(0,2)) + 
                                Integer.parseInt(in.substring(3));
                int outTime = 60*Integer.parseInt(out.substring(0,2)) + 
                                Integer.parseInt(out.substring(3)); 

                time += outTime - inTime;
            }

            //주차요금 계산하기 (단위 시간으로 나누어 떨어지지 않으면 올림)
            if(time > defaultTime){
                cost += (int) Math.ceil((double)(time-defaultTime)/unitTime)*unitCost;
            } 
            
            answer.add(cost);
        }
            
        return answer.stream().mapToInt(i->i).toArray();
    }
}

[ 느낀 점 ]

이제 1~5단계 문제 다 합치면 195문제 남았다.
풀어야할 문제보다 풀어온 문제 수가 더 많은데,
이제 남은 문제들은 어려워서 무지성으로 풀지 말고 알고리즘 공부하면서 풀어야할 듯,,

내가 재귀 약한 거 알고 추천 문제로 바로 <하노이의 탑> 추천해주는 AI,,^^
추천 잘하네,,

0개의 댓글