-
Notifications
You must be signed in to change notification settings - Fork 1
/
DesignUndergroundSystem.java
46 lines (35 loc) · 1.34 KB
/
DesignUndergroundSystem.java
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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
// https://leetcode.com/problems/design-underground-system/
public class DesignUndergroundSystem {
private final HashMap<String, List<Integer>> stations = new HashMap<>();
private final HashMap<Integer, CheckIn> checkIns = new HashMap<>();
public DesignUndergroundSystem() {
}
public void checkIn(int id, String stationName, int t) {
checkIns.put(id, new CheckIn(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
var checkIn = checkIns.remove(id);
stations.computeIfAbsent(checkIn.stationName + " -> " + stationName, (v) -> new ArrayList<>())
.add(t - checkIn.time);
}
public double getAverageTime(String startStation, String endStation) {
int totalTime = 0;
List<Integer> times = stations.get(startStation + " -> " + endStation);
for (int time : times) {
totalTime += time;
}
return (double) totalTime / times.size();
}
private static class CheckIn {
public final String stationName;
public final int time;
private CheckIn(String stationName, int time) {
this.stationName = stationName;
this.time = time;
}
}
}