Skip to content

Commit cdfcdd9

Browse files
authored
Create 1396-design-underground-system.kt
1 parent f02c591 commit cdfcdd9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: kotlin/1396-design-underground-system.kt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class UndergroundSystem() {
2+
3+
val checkIn = HashMap<Int, Pair<String, Int>>() // id -> (startStation to time)
4+
val averageTime = HashMap<String, Pair<Double, Int>>() // startion -> (TotalTime to countOfTrips)
5+
6+
fun checkIn(id: Int, start: String, startTime: Int) {
7+
checkIn[id] = start to startTime
8+
}
9+
10+
fun checkOut(id: Int, end: String, endTime: Int) {
11+
val (start, startTime) = checkIn[id]!!
12+
val fromTo = "$start:$end"
13+
val (total, count) = averageTime.getOrDefault(fromTo, 0.0 to 0)
14+
averageTime[fromTo] = (total + endTime - startTime) to (count + 1)
15+
}
16+
17+
fun getAverageTime(start: String, end: String): Double {
18+
val (total, count) = averageTime.getOrDefault("$start:$end", 0.0 to 0)
19+
return total / count
20+
}
21+
22+
}

0 commit comments

Comments
 (0)