1396. Design Underground System
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.
Implement the UndergroundSystem class:
- void checkIn(int id, string stationName, int t)
- A customer with a card ID equal to id, checks in at the station stationName at time t.
- A customer can only be checked into one place at a time.
- void checkOut(int id, string stationName, int t)
- A customer with a card ID equal to id, checks out from the station stationName at time t.
- double getAverageTime(string startStation, string endStation)
- Returns the average time it takes to travel from startStation to endStation.
- The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation.
- The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
- There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.
지하철 시스템은 서로 다른 역 사이의 고객 이동 시간을 추적하고 있습니다. 그들은 이 데이터를 사용하여 한 역에서 다른 역으로 이동하는 데 걸리는 평균 시간을 계산합니다.
UndergroundSystem 클래스를 구현합니다.
- void checkIn(int id, 문자열 stationName, int t)
- id와 동일한 카드 ID를 가진 고객이 시간 t에 stationName 역에 체크인합니다.
- 고객은 한 번에 한 곳에서만 체크인할 수 있습니다.
- void checkOut(int id, 문자열 stationName, int t)
- id와 동일한 카드 ID를 가진 고객이 t 시간에 stationName 역에서 체크아웃합니다.
- double getAverageTime(문자열 startStation, 문자열 endStation)
- startStation에서 endStation까지 이동하는 데 걸리는 평균 시간을 반환합니다.
- 평균 시간은 startStation에서 endStation까지 직접 발생한 이전의 모든 이동 시간에서 계산됩니다. 즉, startStation에서 체크인한 후 endStation에서 체크아웃하는 것을 의미합니다.
- startStation에서 endStation까지 이동하는 데 걸리는 시간은 endStation에서 startStation까지 이동하는 데 걸리는 시간과 다를 수 있습니다.
- getAverageTime이 호출되기 전에 startStation에서 endStation까지 이동한 고객이 한 명 이상 있습니다.
checkIn 및 checkOut 메서드에 대한 모든 호출이 일관적이라고 가정할 수 있습니다. 고객이 t1 시간에 체크인한 다음 t2 시간에 체크아웃하면 t1 < t2입니다. 모든 사건은 시간순으로 발생합니다.
Example 1:
Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
Output
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000
undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
Example 2:
Input
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
Output
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(10, "Leyton", 3);
undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
undergroundSystem.checkIn(5, "Leyton", 10);
undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
undergroundSystem.checkIn(2, "Leyton", 21);
undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
Constraints:
- 1 <= id, t <= 10^6
- 1 <= stationName.length, startStation.length, endStation.length <= 10
- All strings consist of uppercase and lowercase English letters and digits.
- There will be at most 2 * 10^4 calls in total to checkIn, checkOut, and getAverageTime.
- Answers within 10-5 of the actual value will be accepted.
코드 1
class UndergroundSystem() {
private val hm1 = HashMap<Int, Pair<String, Int>>()
private val hm2 = HashMap<Pair<String, String>, MutableList<Int>>()
fun checkIn(id: Int, stationName: String, t: Int) {
hm1[id] = Pair(stationName, t)
}
fun checkOut(id: Int, stationName: String, t: Int) {
val checkInInfo = hm1[id] ?: return
val travelTime = t - checkInInfo.second
val route = Pair(checkInInfo.first, stationName)
if (route !in hm2) {
hm2[route] = mutableListOf<Int>()
}
hm2[route]!!.add(travelTime)
}
fun getAverageTime(startStation: String, endStation: String): Double {
val times = hm2[Pair(startStation, endStation)] ?: return 0.0
return times.average()
}
}
코드 2
class UndergroundSystem() {
val checkIn = HashMap<Int, Pair<String, Int>>()
val averageTime = HashMap<String, Pair<Double, Int>>()
fun checkIn(id: Int, stationName: String, t: Int) {
checkIn[id] = Pair(stationName, t)
}
fun checkOut(id: Int, stationName: String, t: Int) {
val (start, startTime) = checkIn[id]!!
val fromTo = "$start:$stationName"
val (total, count) = averageTime.getOrDefault(fromTo, 0.0 to 0)
averageTime[fromTo] = (total + t - startTime) to (count + 1)
}
fun getAverageTime(start: String, end: String): Double {
val (total, count) = averageTime.getOrDefault("$start:$end", 0.0 to 0)
return total / count
}
}
'LeetCode > Array & Hashing' 카테고리의 다른 글
[LeetCode][Kotlin] 2405. Optimal Partition of String (0) | 2024.10.19 |
---|---|
[LeetCode][Kotlin] 1249. Minimum Remove to Make Valid Parentheses (0) | 2024.10.19 |
[LeetCode][Kotlin] 2306. Naming a Company (0) | 2024.10.19 |