본문 바로가기
알고리즘/프로그래머스

프로그래머스 LV2. 주차 요금 계산 (자바)

by reumiii 2022. 3. 6.
🍀 문제

주차장의 요금표와 차량이 들어오고(입차) 나간(출차) 기록이 주어졌을 때, 차량별로 주차 요금을 계산하려고 합니다.

  • 어떤 차량이 입차된 후에 출차된 내역이 없다면, 23:59에 출차된 것으로 간주합니다.
    • 0000번 차량은 18:59에 입차된 이후, 출차된 내역이 없습니다. 따라서, 23:59에 출차된 것으로 간주합니다.
  • 00:00부터 23:59까지의 입/출차 내역을 바탕으로 차량별 누적 주차 시간을 계산하여 요금을 일괄로 정산합니다.
  • 누적 주차 시간이 기본 시간이하라면, 기본 요금을 청구합니다.
  • 누적 주차 시간이 기본 시간을 초과하면, 기본 요금에 더해서, 초과한 시간에 대해서 단위 시간 마다 단위 요금을 청구합니다.
    • 초과한 시간이 단위 시간으로 나누어 떨어지지 않으면, 올림합니다.
    • ⌈a⌉ : a보다 작지 않은 최소의 정수를 의미합니다. 즉, 올림을 의미합니다.

주차 요금을 나타내는 정수 배열 fees, 자동차의 입/출차 내역을 나타내는 문자열 배열 records가 매개변수로 주어집니다. 차량 번호가 작은 자동차부터 청구할 주차 요금을 차례대로 정수 배열에 담아서 return 하도록 solution 함수를 완성해주세요.

 

입출력 예
fees records result
[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]
[120, 0, 60, 591] ["16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN"] [0, 591]
[1, 461, 1, 10] ["00:00 1234 IN"] [14841]

 

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 {
    public int[] solution(int[] fees, String[] records) {
         HashMap<String,ArrayList> map =new HashMap<String,ArrayList>();
        for(int i=0; i<records.length; i++) {
        	String[] record = records[i].split(" ");
        	ArrayList<Integer> arr = new ArrayList<Integer>();
        	if(map.containsKey(record[1])) {
        		arr = map.get(record[1]);
        	}
        	int time = Integer.valueOf(record[0].substring(0, 2))*60 + Integer.valueOf(record[0].substring(3, 5));
        	arr.add(time);
        	map.put(record[1], arr);// 출차 시간 map에 담기
        }
        
        HashMap<String,Integer> feeMap =new HashMap<String,Integer>();
        for(String key:map.keySet()) {
        	// 주차 시간 계산
        	ArrayList<Integer> arr = map.get(key);
        	int time = 0;
        	for(int i=0; i<arr.size()-1 ;i+=2) {
        		time += (arr.get(i+1)-arr.get(i));
        	}
        	if(arr.size()%2 != 0) {
        		time += 1439 - arr.get(arr.size()-1);
        	}
        	
        	// 요금 계산
        	int fee =fees[1] +(int) Math.ceil(Math.max(0, time - fees[0])/(double)fees[2]) * fees[3];
        	feeMap.put(key, fee);
        }
        
        // 차량 번호가 작은 순으로 정렬
        Object[] mapKey  = feeMap.keySet().toArray();
		Arrays.sort(mapKey);
		
		int[] answer = new int[feeMap.size()];
		int index = 0;
		for(Object key:mapKey) {// answer에 계산한 요금 담기
			answer[index++] = feeMap.get(key);
		}

        return answer;
    }
}

 

HashMap을 두 개 사용했다.

map -> key :  차량번호 / value : 출차 시간을 담은 arrayList<Integer>

feeMap -> key : 차량번호 /  value : 주차요금

 

1. map에 담긴 출차시간을 통해 주차시간을 계산하고 요금을 계산한뒤

2. feeMap에 계산한 주차요금 정보를 넣어주었다.

3. 차량번호 순으로 정렬한 뒤 answer에 요금정보를 넣어 리턴!

 

 

 

 

✌ 다시 풀기

import java.util.*;
class Solution {
    public int[] solution(int[] fees, String[] records) {
         //1. 주차 시간 계산
        HashMap<String,Integer> timeMap =new HashMap<String,Integer>();
        for(int i=0; i<records.length; i++) {
        	String[] record = records[i].split(" ");
        	int time = Integer.valueOf(record[0].substring(0, 2))*60 + Integer.valueOf(record[0].substring(3, 5)); //시간 분단위로 계산
        	if(record[2].equals("IN")) {
        		time*=-1;
        	}
        	if(timeMap.containsKey(record[1])) {
        		 time += timeMap.get(record[1]);
        	}
        	timeMap.put(record[1], time);
        }
        
        //2. 요금 계산
        HashMap<String,Integer> feeMap =new HashMap<String,Integer>();
        for(String key:timeMap.keySet()) {
        	int time = timeMap.get(key);
        	if(time <= 0) {//출차된 내역이 없을때 23:59에 출차된 것으로 간주
        		time += 1439;
        	}

        	int fee =fees[1] +(int) Math.ceil(Math.max(0, time - fees[0])/(double)fees[2]) * fees[3];
        	feeMap.put(key, fee);
        }
        
        // 차량 번호가 작은 순으로 정렬
        Object[] mapKey  = feeMap.keySet().toArray();
		Arrays.sort(mapKey);
		
		int[] answer = new int[feeMap.size()];
		int index = 0;
		for(Object key:mapKey) {// answer에 계산한 요금 담기
			answer[index++] = feeMap.get(key);
		}

        return answer;
    }
}

 

처음 풀때는 출차 시간을 arrayList에 담은 후 다시 시간을 계산했는데,

IN이면 주차시간에서 빼고 OUT이면 더하는 식으로 수정했다.

 

 

그리고 HashMap 말고 객체를 저장할때 키를 자동으로 정렬해주는 TreeMap을 쓰면 좋았겠다는 생각을 했다.

 

댓글