LC 1396. Design Underground System

https://leetcode.com/problems/design-underground-system/

Clarification & Assumption

Description: design an underground railway system that keeps track
             of customer travel times between different stations
functions:
    1. void checkIn(int id, String stationName, int t)
        customer with id check in stationName at time t
    2. void checkOut(int id, String stationName, int t)
        customer with id check out stationName at time t
    3. double getAverageTime(String startStation, String endStation)
        calculate the average time it takes to traverl from start to end stations

Result:

high level: 2 HashMaps

use a HashMap to store the id & checkInData
    Map<Integer, CheckInfo> checkInData
use another HashMap to store the startStation->endStation, & the TimeInfo
    Map<String, TimeInfo> averageTimes    

here I used 2 helper classes, CheckInfo and TimeInfo
checkInfo stores the check in station & time
TimeInfo stores the totalTime to travel from start->end station & counts of travels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static class CheckInfo {
String station;
int time;

public CheckInfo(String station, int time) {
this.station = station;
this.time = time;
}
}

private static class TimeInfo {
int totalTime;
int occurrence;

public TimeInfo(int totalTime, int occurrence) {
this.totalTime = totalTime;
this.occurrence = occurrence;
}
}
* when checkIn is called, put the id & checkInfo into the checkInData HashMap * when checkOut is called, get the checkInfo using given id and calculate the travel time, put the stations & TimeInfo into the averageTimes HashMap * when getAverageTime is called, simply get the TimeInfo from averageTimes HashMap and do a math calculation to get the averageTime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class UndergroundSystem {

Map<Integer, CheckInfo> checkInData;
Map<String, TimeInfo> averageTimes;

public UndergroundSystem() {
checkInData = new HashMap<>();
averageTimes = new HashMap<>();
}

// TC: O(1)
public void checkIn(int id, String stationName, int t) {
checkInData.put(id, new CheckInfo(stationName, t));
}

// TC: O(1)
public void checkOut(int id, String stationName, int t) {
CheckInfo checkInfo = checkInData.get(id);
checkInData.remove(id);
String stations = checkInfo.station + "," + stationName;
if (!averageTimes.containsKey(stations)) {
averageTimes.put(stations, new TimeInfo(0, 0));
}
int timeTaken = t - checkInfo.time;
int occurrence = averageTimes.get(stations).occurrence + 1;
int totalTime = averageTimes.get(stations).totalTime + timeTaken;
averageTimes.put(stations, new TimeInfo(totalTime, occurrence));
}

// TC: O(1)
public double getAverageTime(String startStation, String endStation) {
TimeInfo timeInfo = averageTimes.get(startStation + "," + endStation);
return (double)(timeInfo.totalTime + 0.0) / timeInfo.occurrence;
}

private static class CheckInfo {
String station;
int time;

public CheckInfo(String station, int time) {
this.station = station;
this.time = time;
}
}

private static class TimeInfo {
int totalTime;
int occurrence;

public TimeInfo(int totalTime, int occurrence) {
this.totalTime = totalTime;
this.occurrence = occurrence;
}
}
}

Comments