-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
48 lines (42 loc) · 1.18 KB
/
Solution.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
class LogSystem {
private List<Log> logs = new ArrayList<>();
private Map<String, Integer> d = new HashMap<>();
public LogSystem() {
d.put("Year", 4);
d.put("Month", 7);
d.put("Day", 10);
d.put("Hour", 13);
d.put("Minute", 16);
d.put("Second", 19);
}
public void put(int id, String timestamp) {
logs.add(new Log(id, timestamp));
}
public List<Integer> retrieve(String start, String end, String granularity) {
List<Integer> ans = new ArrayList<>();
int i = d.get(granularity);
String s = start.substring(0, i);
String e = end.substring(0, i);
for (var log : logs) {
String t = log.ts.substring(0, i);
if (s.compareTo(t) <= 0 && t.compareTo(e) <= 0) {
ans.add(log.id);
}
}
return ans;
}
}
class Log {
int id;
String ts;
Log(int id, String ts) {
this.id = id;
this.ts = ts;
}
}
/**
* Your LogSystem object will be instantiated and called as such:
* LogSystem obj = new LogSystem();
* obj.put(id,timestamp);
* List<Integer> param_2 = obj.retrieve(start,end,granularity);
*/