본문 바로가기
programming/알고리즘

[PG, 자바] 주차요금계산

by s2econd.blue 2022. 9. 20.

0. 문제 링크

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

 

프로그래머스

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

programmers.co.kr


1. 문제 핵심

차량이 주차장에 있었던 시간을 기록한 후 단위 시간만큼 나눈 뒤 그만큼 단위 요금을 계산하는 문제.

 


2. 문제 접근

1. 입차와 출차의 차이만큼 시간을 계산해서 적립하고 출차 하지 않았을 때는 23:59분을 출차 시간로 해서 차이만큼 시간을  적립
2. 모든 방문 차량의 번호판을 기록 및 오름차순 출력
3. 한 번이라도 방문했다면 기본 요금 부과
4. 중요 예외상황: 동일시간에 출차와 입차가 동시에 되는 케이스는 없음


3. 베스트 코드 분석


4.  문제 후기

카카오 알고리즘은 본 문제처럼 숫자의 문자열화한 문제나 해쉬 사용을 했을 때 풀기 쉬운 문제가 많아 라이브러리 숙달이 중요하다고 느꼈습니다.


5. 코드

더보기
import java.io.*;
import java.util.*;

class Solution {
    public int[] solution(int[] fees, String[] records) {
        
        //Integer : 차량번호, int[] : 입차시간, 출차시간, 주차시간
        StringTokenizer st;
        Map<String, int[]> io = new TreeMap<>();
        List<String> list = new LinkedList<>();
        String[] timeSplit;
        String carNum = "";
        int time = 0;
        
        String text;
        int[] tmp;
        for(int i = 0; i < records.length; i++){
            time = 0;
            st = new StringTokenizer(records[i]);
            timeSplit = st.nextToken().split(":");

            time += Integer.parseInt(timeSplit[0]) * 60;

            time += Integer.parseInt(timeSplit[1]);
            
            carNum = st.nextToken();
            
            
            //입차
            if(st.nextToken().compareTo("IN") == 0){
                    
                //첫 차량 등록이라면
                if(io.get(carNum) == null){
                    list.add(carNum);
                    io.put(carNum,new int[]{time, 0, 0});
                    // io.put(carNum) = new int[]{time, 0, 0};
                }
                //이전 입차 기록이 있을 때
                else{
                    //기존 데이터 꺼내기
                    tmp = io.get(carNum);
                    io.put(carNum, new int[]{time, 0, tmp[2]});
                }
            }
            //출차
            else {
                //기록 꺼내기
                tmp = io.get(carNum);
                //기존 시간 + (출차시간 - 입차시간)
                io.put(carNum,new int[]{tmp[0], time,tmp[2] + (time - tmp[0])});
            }
        }
        
        Collections.sort(list);
        int[] answer = new int[list.size()];
        int lastOrder = 23 * 60 + 59, value = 0;
        
        int a = 0;
        //가장 작은 주차번호 순회
        for(String i: list){
            System.out.print("차번호: "+i);
            value = 0;
            //작은 순서대로 주차 비용 저장
            //출차되지 않았을 때
            tmp = io.get(i);
            
            //출차시간이 0일 때
            if(tmp[1] == 0){
                //단위 시간으로 나눠서 ceil로 해당 값보다 큰 정수로 변경  
                //기본 시간을 오버했을 때
                value+= fees[1];
                if(lastOrder - tmp[0] + tmp[2] - fees[0] >= 1){
                    //막차시간 - 입차시간 + 누적시간 - 기본시간
                    int b = lastOrder - tmp[0] + tmp[2] - fees[0];
                    System.out.println("  노 출차: "+  tmp[2]);
                    value += (int)Math.ceil((double)(lastOrder - tmp[0] + tmp[2] - fees[0]) / fees[2]) * fees[3];
                    
                }
                
            }
            //정상출차되었을 때
            else {
                value+= fees[1];
                if(tmp[2] - fees[0] >= 1){
                    
                    System.out.println("출차됨: "+ (int)Math.ceil((double)(tmp[2] - fees[0]) / fees[2]));
                    value += (int)Math.ceil((double)(tmp[2] - fees[0]) / fees[2]) * fees[3];      
                }
            }
            answer[a++] = value;
        }
        
        return answer;
    }
}

 

'programming > 알고리즘' 카테고리의 다른 글

[백준, 자바, S2] 연속합  (0) 2022.10.17
[codeTree, 자바] 술래 잡기  (1) 2022.10.14
[PG, 자바] 합승택시요금  (0) 2022.09.20
[PG, 자바] 등산코스정하기  (0) 2022.09.20
[PG, 자바] 피로도  (0) 2022.09.06

댓글