알고리즘/카카오
[프로그래머스] 주차 요금 계산
BSHwan
2022. 8. 4. 22:30
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class Solution {
static int stdTime, stdCost, perHour, perCost;
static HashMap<String, String> hm = new HashMap();
static HashMap<String, Integer> sumTime = new HashMap();
static ArrayList<Car> list = new ArrayList();
static String LAST = "23:59";
static class Car implements Comparable<Car> {
String num;
String cost;
Car(String num, String cost){
this.num=num;
this.cost=cost;
}
@Override
public int compareTo(Car o) {
return num.compareTo(num);
}
}
public static int[] solution(int[] fees, String[] records) {
stdTime = fees[0];
stdCost = fees[1];
perHour = fees[2];
perCost = fees[3];
for(int i=0; i<records.length; i++) {
String[] strArr = records[i].split(" ");
String time = strArr[0];
String carNum = strArr[1];
String record = strArr[2];
if(hm.containsKey(carNum)) {
int sum = calc(hm.get(carNum), time);
if(sumTime.containsKey(carNum)) {
int total = sumTime.get(carNum) + sum;
sumTime.replace(carNum, total);
} else {
sumTime.put(carNum, sum);
}
hm.remove(carNum);
} else {
hm.put(carNum, time);
}
}
List<Entry<String, String>> list = new ArrayList(hm.entrySet());
for(int i=0; i<list.size(); i++) {
Entry e = list.get(i);
String n = (String) e.getKey();
String t = (String) e.getValue();
int sum = calc(t, LAST);
if(sumTime.containsKey(n)) {
int total = sumTime.get(n) + sum;
sumTime.replace(n, total);
} else {
sumTime.put(n, sum);
}
hm.remove(n);
}
List<Entry<String, Integer>> list2 = new ArrayList(sumTime.entrySet());
Collections.sort(list2, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
int[] answer = new int[list2.size()];
for(int i=0; i<list2.size(); i++) {
Entry e = list2.get(i);
int t = (int) e.getValue();
if( t <= stdTime) {
answer[i] = stdCost;
} else {
answer[i] = stdCost + (int)Math.ceil((double)(t-stdTime) / (double)perHour) * perCost;
}
}
// System.out.println(Arrays.toString(answer));
return answer;
}
public static int calc(String s1, String s2) {
// s1 : "07:59" , s2 : "19:09" => return 10분 + 11시간(660분) = 670분
int ans = 0;
String[] fst = s1.split(":");
String[] snd = s2.split(":");
int fst_hour = Integer.parseInt(fst[0]); // 7
int fst_min = Integer.parseInt(fst[1]); // 59
int snd_hour = Integer.parseInt(snd[0]); // 19
int snd_min = Integer.parseInt(snd[1]); // 9
if(fst_min > snd_min) { // 59 > 9
snd_hour -= 1;
snd_min += 60;
}
ans += (snd_min - fst_min);
ans += ((snd_hour-fst_hour)*60);
return ans;
}
}
반응형