|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class Solution { |
| 5 | + public int[] solution(int[] fees, String[] records) { |
| 6 | + Map<String, Integer> totalTime = new HashMap<>(); |
| 7 | + Map<String, String> inTime = new HashMap<>(); |
| 8 | + |
| 9 | + for (String record : records) { |
| 10 | + String[] parts = record.split(" "); |
| 11 | + String time = parts[0]; |
| 12 | + String car = parts[1]; |
| 13 | + String type = parts[2]; |
| 14 | + |
| 15 | + if (type.equals("IN")) { |
| 16 | + inTime.put(car, time); |
| 17 | + } else { |
| 18 | + int diff = getTimeDiff(inTime.get(car), time); |
| 19 | + totalTime.put(car, totalTime.getOrDefault(car, 0) + diff); |
| 20 | + inTime.remove(car); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + for (Map.Entry<String, String> entry : inTime.entrySet()) { |
| 25 | + int diff = getTimeDiff(entry.getValue(), "23:59"); |
| 26 | + totalTime.put(entry.getKey(), totalTime.getOrDefault(entry.getKey(), 0) + diff); |
| 27 | + } |
| 28 | + |
| 29 | + TreeMap<String, Integer> result = new TreeMap<>(); |
| 30 | + for (Map.Entry<String, Integer> entry : totalTime.entrySet()) { |
| 31 | + int time = entry.getValue(); |
| 32 | + int fee = fees[1]; |
| 33 | + if (time > fees[0]) { |
| 34 | + fee += (int)Math.ceil((time - fees[0]) / (double)fees[2]) * fees[3]; |
| 35 | + } |
| 36 | + result.put(entry.getKey(), fee); |
| 37 | + } |
| 38 | + |
| 39 | + int[] answer = new int[result.size()]; |
| 40 | + int i = 0; |
| 41 | + for (int fee : result.values()) { |
| 42 | + answer[i++] = fee; |
| 43 | + } |
| 44 | + |
| 45 | + return answer; |
| 46 | + } |
| 47 | + |
| 48 | + private static int getTimeDiff(String start, String end) { |
| 49 | + String[] s = start.split(":"); |
| 50 | + String[] e = end.split(":"); |
| 51 | + int startMin = Integer.parseInt(s[0]) * 60 + Integer.parseInt(s[1]); |
| 52 | + int endMin = Integer.parseInt(e[0]) * 60 + Integer.parseInt(e[1]); |
| 53 | + return endMin - startMin; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +``` |
0 commit comments