- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathTT0.java
62 lines (48 loc) · 1.9 KB
/
TT0.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importjava.util.*;
publicclassTT0 {
// Epoch timestamp with list of users who had an action for the respective
// timestamp
privateTreeMap<Integer, Set<Integer>> logs;
publicTT0() {
logs = newTreeMap<>();
}
publicvoidput(intid, inttimestamp) {
logs.putIfAbsent(timestamp, newHashSet<>());
logs.get(timestamp).add(id);
}
publicList<Integer> retrieve(inttotalMinutes, intintervalLength) {
List<Integer> result = newArrayList<>();
intintervalStart = 0, intervalEnd = intervalStart + intervalLength;
while (intervalStart <= totalMinutes) {
intend = Math.min(totalMinutes, intervalEnd + 1);
SortedMap<Integer, Set<Integer>> submap = logs.subMap(intervalStart, end);
result.add(submap.size());
intervalStart += intervalLength;
intervalEnd += intervalLength;
}
returnresult;
}
publicstaticvoidmain(String[] args) {
/*
* [1, 1518290973] [1, 1518291096] [1, 1518291200] [1, 1518291200] [2,
* 1518291032] [3, 1518291095] [3, 1518291178] [4, 1518291120]
*
* 1 -> 3 minutes in total 2 -> 1 minutes in total 3 -> 2 minutes in total 4 ->
* 1 minutes in total
*
* 2 users spend 0-1 minutes in total - users 2 and 4 2 users spend 2-3 minutes
* in total - users 1 and 3
*/
int[][] logs = newint[][] { { 1, 1518290973 }, { 2, 1518291032 }, { 3, 1518291095 }, { 1, 1518291096 },
{ 4, 1518291120 }, { 3, 1518291178 }, { 1, 1518291200 }, { 1, 1518291200 } };
TT0logRetrieval = newTT0();
for (int[] log : logs) {
logRetrieval.put(log[0], log[1]);
}
for (intinterval : logRetrieval.retrieve(1, 2)) {
System.out.print(interval + ", ");
}
System.out.println();
return;
}
}