forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndergroundTunnel.java
43 lines (38 loc) · 1.25 KB
/
UndergroundTunnel.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
/*https://binarysearch.com/problems/Underground-Tunnel*/
import java.util.*;
class UndergroundTunnel
{
private Map<String, Map<Integer, Integer>> checkinMap;
private Map<String, Map<Integer, Integer>> checkoutMap;
public UndergroundTunnel()
{
checkinMap = new HashMap();
checkoutMap = new HashMap();
}
public void checkIn(int userId, String station, int timestamp)
{
if (!checkinMap.containsKey(station))
checkinMap.put(station, new HashMap());
checkinMap.get(station).put(userId, timestamp);
}
public void checkOut(int userId, String station, int timestamp)
{
if (!checkoutMap.containsKey(station))
checkoutMap.put(station, new HashMap());
checkoutMap.get(station).put(userId, timestamp);
}
public double averageTime(String start, String end)
{
double totalSum = 0;
double totalUsers = 0;
for (Integer userId : checkoutMap.get(end).keySet())
{
if (checkinMap.get(start).containsKey(userId))
{
totalUsers++;
totalSum += (checkoutMap.get(end).get(userId)-checkinMap.get(start).get(userId));
}
}
return totalSum / totalUsers;
}
}