Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

Life Engineering

[프로그래머스] 주차 요금 계산 (Java) 본문

Problem Solving

[프로그래머스] 주차 요금 계산 (Java)

흑개 2022. 5. 6. 23:27

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

import java.util.*;

class Solution {
    Map<Integer, ArrayList<String>> times=new TreeMap<>();
    public int[] solution(int[] fees, String[] records) {
        int[] answer;
        int idx=0;
        for (int i=0; i<records.length; i++){
            String[] record=records[i].split(" ");
            int number=Integer.parseInt(record[1]);
            if (!times.containsKey(number)){
                times.put(number, new ArrayList<>());
            }
            times.get(number).add(record[0]);
        }
        answer=new int[times.size()];
        for (int num : times.keySet()){
            int sum=0;
            if (times.get(num).size()%2!=0){
                times.get(num).add("23:59");
            }
            for (int i=0; i<times.get(num).size(); i+=2){
                String[] out=times.get(num).get(i+1).split(":");
                String[] in=times.get(num).get(i).split(":");
                int out_h=Integer.parseInt(out[0]);
                int out_m=Integer.parseInt(out[1]);
                int in_h=Integer.parseInt(in[0]);
                int in_m=Integer.parseInt(in[1]);
                sum+=(out_h*60+out_m)-(in_h*60+in_m);
            }
            if (sum<=fees[0]){
                    answer[idx]=fees[1];
            }
            else{
                    answer[idx]=fees[1];
                    answer[idx]+=(Math.ceil((double)(sum-fees[0])/fees[2])*fees[3]);
            }
            idx++;
            
        }
        return answer;
    }
}

일단 List에 기록들을 쭉 모아준 다음 List의 갯수가 홀수이면 23:59에 출차되었다고 추가한다.

그 이후 로직은 출차-입차 시간을 빼서 문제 조건대로 구현하면 된다.

TreeMap을 이용해서 정렬된 번호가 나오도록 했다.