programmers_주차 요금 계산_java

2023. 4. 10. 12:53Algorithm/Programmers

728x90

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

 

프로그래머스

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

programmers.co.kr

import java.util.*;

class Solution {
    public int[] solution(int[] fees, String[] records) {
        int[] answer = {};
        
        // <차량 번호, record>
        Map<String, Parking> carRecordMap = new HashMap<>();
        int recordsLength = records.length;
        
        for(int i = 0; i< recordsLength; i++){
            String[] record = records[i].split(" ");
            
            // 이전 기록이 있으면
            if(carRecordMap.containsKey(record[1])){
                Parking before = carRecordMap.get(record[1]);
                
                if(carRecordMap.get(record[1]).status.equals("IN")){;
                    carRecordMap.put(record[1], new Parking(record[0], record[1], record[2], before.parkingTime + calTime(before.time, record[0])));
                }else{
                    carRecordMap.put(record[1], new Parking(record[0], record[1], record[2], before.parkingTime));
                }
            }else{ // 기록이 없으면
                carRecordMap.put(record[1], new Parking(record[0], record[1], record[2], 0));
            }
        }
        
        
        
        // map 순회 및 요금 계산
        int carRecordMapSize = carRecordMap.size();
        int idx= 0;
        answer = new int[carRecordMapSize];
        
        List<String> keyList = new ArrayList<>(carRecordMap.keySet());
        keyList.sort((s1, s2) -> s1.compareTo(s2));
        for (String key : keyList) {
            Parking value = carRecordMap.get(key);
            int additionalTime = 0;
            if(value.status.equals("IN")){ // 아직 출차를 안한 경우               
                additionalTime = calTime(value.time, "23:59");
            }
            answer[idx++] = calFee(fees, value.parkingTime+additionalTime);
        }
        
        return answer;
    }
    
    // 주차 요금 계산
    public int calFee(int[] fees, int parkingTime){
        if(parkingTime <= fees[0]){
            return fees[1];
        }else{
            int overTime = parkingTime - fees[0];
            
            return fees[1] + (int)(Math.ceil((double)overTime/fees[2])) * fees[3] ;
        }
    }
    
    // 시간 차 계산
    public int calTime(String in, String out){
        String[] inTime = in.split(":");
        String[] outTime = out.split(":");
        
        int hour = Integer.parseInt(outTime[0]) - Integer.parseInt(inTime[0]);
        int minute = Integer.parseInt(outTime[1]) - Integer.parseInt(inTime[1]);
        
        return hour*60 + minute;
    }    
    
    public class Parking{
        String time;
        String carNum;
        String status;
        int parkingTime;
        
        public Parking(String time, String carNum, String status, int parkingTime){
            this.time = time;
            this.carNum = carNum;
            this.status = status;
            this.parkingTime = parkingTime;
        }
        
        @Override
        public String toString(){
            return " | " + time + " " + carNum + " " + status + " " + parkingTime;
        }
    }
}

'Algorithm > Programmers' 카테고리의 다른 글

programmers_스킬트리_java  (1) 2023.04.10
programmers_땅따먹기_java  (0) 2023.04.10
programmers_[3차] n진수 게임_java  (0) 2023.04.10
programmers_[3차] 압축_java  (0) 2023.04.10
programmers_할인 행사_java  (0) 2023.04.07