Algorithm/Programmers

[Programmers] 주차 요금 계산 / ⭕

cks._.hong 2024. 10. 7. 15:45
 

프로그래머스

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

programmers.co.kr

 

1. 제출 코드 (1시간 20분 30초 / 정렬, 구현)

import java.util.*;

class Solution {
    public int[] solution(int[] fees, String[] records) {
        ArrayList<int[]> ans = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        
        // 정렬
        Arrays.sort(records, (o1, o2) -> {
            String[] os1 = o1.split(" ");
            String[] os2 = o2.split(" ");
            if(Integer.parseInt(os1[1]) == Integer.parseInt(os2[1])) {
                String[] ot1 = os1[0].split(":");
                String[] ot2 = os2[0].split(":");
                if(Integer.parseInt(ot1[0]) == Integer.parseInt(ot2[0])) {
                    return Integer.parseInt(ot1[1]) - Integer.parseInt(ot2[1]);
                }
                return Integer.parseInt(ot1[0]) - Integer.parseInt(ot2[0]);
            }
            return Integer.parseInt(os1[1]) - Integer.parseInt(os2[1]);
        });
        
        for(int i = 0; i < records.length; i++) {
            System.out.println(records[i]);
        }
        int idx = 0;
        while(idx < records.length) {
            String[] record1 = records[idx].split(" ");
            String[] record2 = "".split("");
            if(idx != records.length - 1) {
                record2 = records[idx + 1].split(" ");
            }
            
            int h = 0;
            int m = 0;
            if(idx != records.length - 1 && record1[1].equals(record2[1])) {
                String[] rt1 = record1[0].split(":");
                String[] rt2 = record2[0].split(":");
                h = Integer.parseInt(rt2[0]) - Integer.parseInt(rt1[0]);
                m = 0;
                if(h == 0) {
                    m = Integer.parseInt(rt2[1]) - Integer.parseInt(rt1[1]);
                } else {
                    h -= 1;
                    m = 60 - Integer.parseInt(rt1[1]) + Integer.parseInt(rt2[1]);
                }
                idx += 2;
            } else {
                String[] rt1 = record1[0].split(":");
                h = 23 - Integer.parseInt(rt1[0]);
                m = 0;
                if(h == 0) {
                    m = 59 - Integer.parseInt(rt1[1]);
                } else {
                    h -= 1;
                    m = 60 - Integer.parseInt(rt1[1]) + 59;
                }
                idx++;
            }
            map.put(record1[1], map.getOrDefault(record1[1], 0) + h * 60 + m);
        }
        for(String k : map.keySet()) {
            int t = map.get(k);
            int money = 0;
            if(fees[0] >= t) {
                money = fees[1];
            } else {
                t -= fees[0];
                int m = t / fees[2];
                int n = t % fees[2];
                if(n != 0) {
                    m += 1;
                }
                money = fees[1] + (m * fees[3]);
            }
            ans.add(new int[] {Integer.parseInt(k), money});
        }
        ans.sort((o1, o2) -> {
            return o1[0] - o2[0];
        });
        
        int[] answer = new int[ans.size()];
        
        for(int i = 0; i < answer.length; i++) {
            answer[i] = ans.get(i)[1];
        }
        return answer;
    }
}

 

 

2. 구현 로직

  1. records를 차랑 번호, 시간, 분 순으로 정렬을 한다.
  2. 정렬이되어 있으므로 순서대로 i와 i + 1을 비교하면 동일한 차량의 입차와 출차 시간을 비교하게 된다.
  3. 반복문을 통해 i와 i + 1을 비교하게 되는데 차량 번호가 다른 경우가 존재한다. 이 경우에는 출차의 기록이 없는데 23:59분에 출차를 한 것이다.
  4. 위 반복문을 통해서 입차와 출차시간의 시간차를 구하고 MAP을 활용하여 누적 시간을 구한다.
  5. 이후, MAP의 값들을 다 꺼내서 요금을 계산하고 정렬하여 정답을 입력하면 된다.

 

3. 회고

  • 입차와 출차의 시간차를 구하는 것과 마지막 idx를 고려하기 위해 예외 처리하느라 시간을 꽤 쓴 거 같다.