티스토리 뷰

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/92341

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

2022 KAKAO BLIND RECRUITMENT의 Level 2 문제다.

 

 

 

특별히 고려해야 할 것 없이 문제에서 요구하는 대로 구현하면 된다.

TreeMap에 차 번호를 key값으로, time을 담은 ArrayList<>를 value로 저장했다.

그리고 map의 value를 하나씩 꺼내 시간을 계산했다.

value인 ArrayList<>의 size가 홀수라면, 입차 후 출차 내역이 없는 것이므로

시간을 계산할 때 23:59으로 설정했다.

 

차 번호가 작은 순서대로 비용을 배열에 담아야 하므로

Map에서 꺼낼 때 작은 순서대로 꺼내야 한다.

정렬된 Map을 유지하기 위해 HashMap이 아닌 TreeMap을 사용했다.

 

 

지금까지는 HashMap에 키와 값을 저장하고 stream.Collectors를 통해 정렬을 했었다.

HashMap과 동일한 기능을 가지면서 자동으로 정렬이 되는 TreeMap의 존재를 완전히 잊고 있었다..

HashMap에 저장하고 정렬하는 코드보다, TreeMap을 사용하는 게 속도가 훨씬 빨리나온다.

나중에는 꼭 잊지 말고 TreeMap을 바로 사용해야겠다.

 

필요 이상으로 코드를 길게 작성한 것 같아서

다른 분들 코드를 보면서 많이 배워야겠다..

다른 사람들은 어떻게 코드를 짰나 구경하는 것도 재밌다. 감탄만 나옴..

 

 

import java.util.*;

class Solution {

    static int[] solution(int[] fees, String[] records) {
        TreeMap<Integer, ArrayList<String>> map = new TreeMap<>();
        StringTokenizer st;
        
        for(int i=0;i<records.length;i++) {
            st = new StringTokenizer(records[i]);
            
            String time = st.nextToken();
            int car = Integer.parseInt(st.nextToken());
            String type = st.nextToken();
            
            if(map.containsKey(car))
                map.get(car).add(time);
            else {
                ArrayList<String> list = new ArrayList<>();
                list.add(time);
                map.put(car, list);
            }
        }
        
        int[] result = new int[map.size()];
        
        int idx = 0;
        for (ArrayList<String> list : map.values()) {
            int time = 0;
            int size = list.size();
            
            for(int i=0;i<size;i+=2) {
                String in = list.get(i);
                String out;
                if(size % 2 != 0 && i == size - 1) // 출차된 내역 없으면
                    out = "23:59";
                else 
                    out = list.get(i + 1);
                
                time += timeCalcute(in, out);
            }
            
            if(time <= fees[0])
                result[idx++] = fees[1];
            else 
                result[idx++] = fees[1] + (int)Math.ceil((double)(time - fees[0]) / fees[2]) * fees[3];

        }
        
        return result;
    }
    
        static int timeCalcute(String in, String out) {
        StringTokenizer st = new StringTokenizer(in, ":");
        int inH = Integer.parseInt(st.nextToken());
        int inM = Integer.parseInt(st.nextToken());
        
        st = new StringTokenizer(out, ":");
        int outH = Integer.parseInt(st.nextToken());
        int outM = Integer.parseInt(st.nextToken());
        
        int H, M;
        if(outM < inM) {
            M = 60 - (inM - outM);
            H = outH - inH - 1;
        }
        else {
            M = outM - inM;
            H = outH - inH;
        }
        
        return H * 60 + M;
    }
}
728x90
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/10   »
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
글 보관함