티스토리 뷰

프로그래머스 92341번 - 주차 요금 계산

프로그래머스 92341번 - https://programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스 92341번 - https://programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스 92341번 - https://programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스 92341번 - https://programmers.co.kr/learn/courses/30/lessons/92341

 

요구사항

1. 문제가 길기 때문에 본문의 사진을 참고하길 바랍니다.

 

요구사항 분석 및  풀이과정

1. 문제만 길고, 문자열을 분해만 잘하면 단순히 산술문제입니다.

2. 주의해야하는 점은 출차를 하지 않을 경우는 23시 59분에 출차한 것으로 가정한다는 것이다.

3. 그리고 차량은 입/출차를 여러번 할 수 있습니다. 이 경우는 요금을 누적하여 최종 요금을 구해야합니다.

 

소스코드 작성

import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;

class Solution {
    
    private static class Machine {
        private final int baseTime;
        private final int baseFee;
        private final int unitTime;
        private final int unitFee;
        
        public Machine(int baseTime, int baseFee, int unitTime, int unitFee) {
            this.baseTime = baseTime;
            this.baseFee = baseFee;
            this.unitTime = unitTime;
            this.unitFee = unitFee;
        }
        
        public int totalFee(int minuate) {
            int result = 0;
            
            if (minuate <= baseTime) {
                return baseFee;
            }
            
            if (minuate > baseTime) {
                result += baseFee;
                minuate -= baseTime;
            }
            
            result += ((int)Math.ceil((float)minuate / unitTime) * unitFee);
            return result;
        }
    }
    
    private static class CarLog {
        private String carId;
        private int enterTime;
        private int leaveTime;
        
        public CarLog(String carId, int enterTime) {
            this.carId = carId;
            this.enterTime = enterTime;
            this.leaveTime = 23 * 60 + 59; 
        }
        
        public String getCarId() {
            return carId;
        }
        
        public void updateLeaveTime(int leaveTime) {
            this.leaveTime = leaveTime;
        }
        
        public int stayTime() {
            return leaveTime - enterTime;
        }
    }
    
    private int minuate(String time) {
        int hour = Integer.parseInt(time.substring(0, 2));
        int min = Integer.parseInt(time.substring(3));
        
        return hour * 60 + min;
    }

    public int[] solution(int[] fees, String[] records) {
        Map<String, CarLog> logs = new HashMap<>();
        Map<String, Integer> prices = new TreeMap<>();
        
        Machine parking = new Machine(fees[0], fees[1], fees[2], fees[3]);

        for(String record : records) {
            String[] tokens = record.split(" ");
            String carId = tokens[1];
            String command = tokens[2];
            
            if ("IN".equals(command)) {
                logs.put(carId, new CarLog(carId, minuate(tokens[0])));
            } else if ("OUT".equals(command)) {
                CarLog log = logs.get(carId);
                log.updateLeaveTime(minuate(tokens[0]));
                prices.put(carId, prices.getOrDefault(carId, 0) + log.stayTime());
                logs.remove(carId);
            } 
        }
        
        for(CarLog log : logs.values()) {
            prices.put(log.getCarId(), prices.getOrDefault(log.getCarId(), 0) + log.stayTime());
        }
        
        return prices.values().stream().mapToInt(parking::totalFee).toArray();
    }
}

 

결과

 

소스코드 깃허브 주소

링크

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   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
글 보관함