Skip to content

Commit d059766

Browse files
committed
Add T1.java
1 parent 31bc870 commit d059766

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Miscellaneous/T1.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
public class T1 {
4+
// Epoch timestamp with list of users who had an action for the respective
5+
// timestamp
6+
private TreeMap<Integer, Set<Integer>> logs;
7+
8+
public T1() {
9+
logs = new TreeMap<>();
10+
}
11+
12+
public void put(int id, int timestamp) {
13+
logs.putIfAbsent(timestamp, new HashSet<>());
14+
logs.get(timestamp).add(id);
15+
}
16+
17+
public List<Integer> retrieve(int totalMinutes, int intervalLength) {
18+
List<Integer> result = new ArrayList<>();
19+
20+
int intervalStart = 0, intervalEnd = intervalStart + intervalLength;
21+
22+
while (intervalStart <= totalMinutes) {
23+
int end = Math.min(totalMinutes, intervalEnd + 1);
24+
SortedMap<Integer, Set<Integer>> submap = logs.subMap(intervalStart, end);
25+
result.add(submap.size());
26+
27+
intervalStart += intervalLength;
28+
intervalEnd += intervalLength;
29+
}
30+
31+
return result;
32+
}
33+
34+
public static void main(String[] args) {
35+
/*
36+
* [1, 1518290973]
37+
* [1, 1518291096]
38+
* [1, 1518291200]
39+
* [1, 1518291200]
40+
* [2, 1518291032]
41+
* [3, 1518291095]
42+
* [3, 1518291178]
43+
* [4, 1518291120]
44+
*
45+
* 1 -> 3 minutes in total
46+
* 2 -> 1 minutes in total
47+
* 3 -> 2 minutes in total
48+
* 4 -> 1 minutes in total
49+
*
50+
* 2 users spend 0-1 minutes in total - users 2 and 4
51+
* 2 users spend 2-3 minutes in total - users 1 and 3
52+
*/
53+
int[][] logs = new int[][] { { 1, 1518290973 }, { 2, 1518291032 }, { 3, 1518291095 }, { 1, 1518291096 },
54+
{ 4, 1518291120 }, { 3, 1518291178 }, { 1, 1518291200 }, { 1, 1518291200 } };
55+
56+
T1 logRetrieval = new T1();
57+
58+
for (int[] log : logs) {
59+
logRetrieval.put(log[0], log[1]);
60+
}
61+
62+
for (int interval : logRetrieval.retrieve(1, 2)) {
63+
System.out.print(interval + ", ");
64+
}
65+
66+
System.out.println();
67+
68+
return;
69+
}
70+
}

0 commit comments

Comments
 (0)