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

-·2022년 9월 24일
0
public int[] solution(int[] fees, String[] records) {
    Map<String, String> recordMap = new HashMap<>();
    Map<Integer, Integer> resultMap = new HashMap<>();
    String endTime = "23:59";
    for (int i = 0; i < records.length; i++) {
        String[] arrTmp = records[i].split(" ");
        String strTime = arrTmp[0];
        String carNum = arrTmp[1];
        if (!recordMap.containsKey(carNum)) {
            recordMap.put(carNum, records[i]);
        } else {
            getTime(resultMap, recordMap.get(carNum).split(" ")[0], strTime, carNum);
            recordMap.remove(carNum);
        }
    }
    // 출차가 없으면 23:59에 출차된걸로 간주
    for (String entryKey : recordMap.keySet()) {
        getTime(resultMap, recordMap.get(entryKey).split(" ")[0], endTime, entryKey);
    }
    // 요금계산
    for(Integer key : resultMap.keySet()){
        resultMap.put(key, getFee(fees, resultMap.get(key)));
    }

    // 결과 정렬
    List<Integer> keyList = new ArrayList<>(resultMap.keySet());
    keyList.sort((s1, s2) -> s1.compareTo(s2));
    int[] answer = new int[resultMap.size()];
    for(int i = 0; i < answer.length; i++){
        answer[i] = resultMap.get(keyList.get(i));
    }
    return answer;
}

public void getTime(Map<Integer, Integer> rstMap, String inStr, String outStr, String carNum) {
    // 시간 차이 구하기
    String[] arrIn = inStr.split(":");
    String[] arrOut = outStr.split(":");
    int timeDiff = (Integer.valueOf(arrOut[0]) * 60 + Integer.valueOf(arrOut[1]))
        - (Integer.valueOf(arrIn[0]) * 60 + Integer.valueOf(arrIn[1]));
    // resultMap 채우기
    Integer rstkey = Integer.valueOf(carNum);
    if (rstMap.containsKey(rstkey)) rstMap.put(rstkey, rstMap.get(rstkey) + timeDiff);
    else rstMap.put(rstkey, timeDiff);
}

public int getFee(int[] fees, int timeDiff) {
    int result = fees[1];
    if (timeDiff - fees[0] > 0) {
        result += ((timeDiff - fees[0]) / fees[2]) * fees[3];
        if((timeDiff - fees[0]) % fees[2] != 0) result += fees[3];
    }
    return result;
}

이번껀 좀 길게 나와서 나만 이렇게 길게 했나 싶은데 다른풀이도 긴편이어서 다행

profile
거북이는 오늘도 걷는다

0개의 댓글