Life Engineering
[프로그래머스] 주차 요금 계산 (Java) 본문
https://programmers.co.kr/learn/courses/30/lessons/92341
import java.util.*;
class Solution {
Map<Integer, ArrayList<String>> times=new TreeMap<>();
public int[] solution(int[] fees, String[] records) {
int[] answer;
int idx=0;
for (int i=0; i<records.length; i++){
String[] record=records[i].split(" ");
int number=Integer.parseInt(record[1]);
if (!times.containsKey(number)){
times.put(number, new ArrayList<>());
}
times.get(number).add(record[0]);
}
answer=new int[times.size()];
for (int num : times.keySet()){
int sum=0;
if (times.get(num).size()%2!=0){
times.get(num).add("23:59");
}
for (int i=0; i<times.get(num).size(); i+=2){
String[] out=times.get(num).get(i+1).split(":");
String[] in=times.get(num).get(i).split(":");
int out_h=Integer.parseInt(out[0]);
int out_m=Integer.parseInt(out[1]);
int in_h=Integer.parseInt(in[0]);
int in_m=Integer.parseInt(in[1]);
sum+=(out_h*60+out_m)-(in_h*60+in_m);
}
if (sum<=fees[0]){
answer[idx]=fees[1];
}
else{
answer[idx]=fees[1];
answer[idx]+=(Math.ceil((double)(sum-fees[0])/fees[2])*fees[3]);
}
idx++;
}
return answer;
}
}
일단 List에 기록들을 쭉 모아준 다음 List의 갯수가 홀수이면 23:59에 출차되었다고 추가한다.
그 이후 로직은 출차-입차 시간을 빼서 문제 조건대로 구현하면 된다.
TreeMap을 이용해서 정렬된 번호가 나오도록 했다.
'Problem Solving' 카테고리의 다른 글
[프로그래머스] 외벽 점검 (Java) (0) | 2022.05.11 |
---|---|
[프로그래머스] 셔틀버스 (Java) (0) | 2022.05.07 |
[프로그래머스] 카드 짝 맞추기 (Java) (0) | 2022.05.05 |
[BOJ 13460] 구슬 탈출2 (Java) (0) | 2022.04.29 |
[BOJ 19237] 어른 상어 (Java) (0) | 2022.04.27 |