-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathTime Based Key-Value Store.java
64 lines (57 loc) · 1.94 KB
/
Time Based Key-Value Store.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
63
64
class TimeMap {
private Map<String, List<Entry>> map;
private final String NOT_FOUND = "";
public TimeMap() {
map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
List<Entry> entries = map.getOrDefault(key, new ArrayList<>());
entries.add(new Entry(value, timestamp));
map.put(key, entries);
}
public String get(String key, int timestamp) {
List<Entry> entries = map.get(key);
if (entries == null) {
return NOT_FOUND;
}
return binarySearch(entries, timestamp);
}
private String binarySearch(List<Entry> entries, int timestamp) {
int lo = 0, hi = entries.size() - 1, mid = -1;
String ans = "";
// Base cases - if value is not set, return empty
if (entries.get(lo).timestamp > timestamp) {
return NOT_FOUND;
}
// If timestamp is equal or greater, return the last value saved in map against this key, since that will have the largest timestamp
else if (entries.get(hi).timestamp <= timestamp) {
return entries.get(hi).value;
}
// Else apply binary search to get correct value
while (lo <= hi) {
mid = lo + (hi-lo)/2;
Entry entry = entries.get(mid);
// System.out.println("mid: "+mid);
if (entry.timestamp == timestamp) {
return entry.value;
}
// Save ans, and look for ans on right half to find greater timestamp
else if (entry.timestamp < timestamp) {
ans = entry.value;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
return ans;
}
}
class Entry {
String value;
int timestamp;
public Entry(String value, int timestamp) {
this.value = value;
this.timestamp = timestamp;
}
}