Design Underground System
设计一个check in 和check out 系统, 然后找出在两个地点所有id的平均时间差.
class UndergroundSystem {
Map<Integer, String> checkIn; // <id, startStation t>
Map<String, Map<String, String>> time; // <startStation, <endStation, total count>>
public UndergroundSystem() {
checkIn = new HashMap<>();
time = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
checkIn.putIfAbsent(id, "");
checkIn.put(id, stationName+" "+t);
}
public void checkOut(int id, String stationName, int t) {
String[] strs = checkIn.get(id).split(" ");
time.putIfAbsent(strs[0], new HashMap<String, String>());
time.get(strs[0]).putIfAbsent(stationName, "0 0");
String[] strs1 = time.get(strs[0]).get(stationName).split(" ");
Integer total = Integer.valueOf(strs1[0]);
Integer count = Integer.valueOf(strs1[1]);
count += 1;
total += (t - Integer.valueOf(strs[1]));
time.get(strs[0]).put(stationName, total +" " + count);
}
public double getAverageTime(String startStation, String endStation) {
String[] strs = time.get(startStation).get(endStation).split(" ");
Integer total = Integer.valueOf(strs[0]);
Integer count = Integer.valueOf(strs[1]);
return (double) total / count;
}
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem obj = new UndergroundSystem();
* obj.checkIn(id,stationName,t);
* obj.checkOut(id,stationName,t);
* double param_3 = obj.getAverageTime(startStation,endStation);
*/