-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeBasedKeyValueStore.java
More file actions
65 lines (49 loc) · 1.85 KB
/
TimeBasedKeyValueStore.java
File metadata and controls
65 lines (49 loc) · 1.85 KB
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
63
64
65
class TimeMap {
private Map<String, List<Timestamped<String>>> map;
public TimeMap() {
map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
map.computeIfAbsent(key, k -> new ArrayList<>())
.add(new Timestamped<>(value, timestamp));
}
public String get(String key, int timestamp) {
List<Timestamped<String>> values = map.getOrDefault(key, Collections.emptyList());
if(values.isEmpty()) return "";
int start = 0;
int end = values.size() - 1;
while(start <= end) {
int mid = start + (end - start ) / 2;
if(values.get(mid).timestamp == timestamp){
return values.get(mid).value;
}else if(values.get(mid).timestamp > timestamp){
end = mid - 1;
}else {
start = mid + 1;
}
}
if (end >= 0 && values.get(end).timestamp <= timestamp) {
return values.get(end).value;
}
return "";
}
private static class Timestamped<T>{
T value;
int timestamp;
public Timestamped(T value, int timestamp){
this.value = value;
this.timestamp = timestamp;
}
}
}
/**
Approach: Use List<Timestamped> as value container for every key
where we hold pair of each value and corresponding timestamp
as All the timestamps timestamp of set are strictly increasing
we always add new values to the tail of list;
then for retrieving of elements we use binary search and seek for element
with given timestamp, if it could not be found we return closest value with highest
timestamp using 'end' pointer (if timestamp of value[end] is <= to searchable timestamp)
Time Complexity: set O(1), get O( n log n )
Space Complexity: O(n)
*/