Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the TimeMap class:
TimeMap() Initializes the object of the data structure. void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp. String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".
class TimeMap { | |
class Node{ | |
private String value; | |
private int timestamp; | |
public Node(String value, int timestamp){ | |
this.value = value; | |
this.timestamp = timestamp; | |
} | |
public String getValue(){ | |
return value; | |
} | |
public int getTimestamp(){ | |
return timestamp; | |
} | |
} | |
Map<String, List<Node>> map; | |
public TimeMap() { | |
this.map = new HashMap<>(); | |
} | |
public void set(String key, String value, int timestamp) { | |
List<Node> nodes = map.getOrDefault(key, null); | |
if (nodes == null){ | |
nodes = new ArrayList<>(); | |
} | |
Node node = new Node(value, timestamp); | |
nodes.add(node); | |
map.put(key, nodes); | |
} | |
public String get(String key, int timestamp) { | |
List<Node> nodes = map.getOrDefault(key, null); | |
if (nodes == null){ | |
return ""; | |
} | |
int maxTimestamp = Integer.MIN_VALUE; | |
String maxValue = ""; | |
for (Node node : nodes){ | |
if (node.getTimestamp() == timestamp){ | |
return node.getValue(); | |
} | |
if (node.getTimestamp() <= timestamp && node.getTimestamp() > maxTimestamp){ | |
maxValue = node.getValue(); | |
maxTimestamp = node.getTimestamp(); | |
} | |
} | |
return maxValue; | |
} | |
} |
It failed with error : Time Limit Exceeded.
Instead of using List, I can try using TreeMap
. A TreeMap is a Red-Black tree-based NavigableMap implementation that stores keys in sorted order.
class TimeMap { | |
Map<String, TreeMap<Integer, String>> map; | |
public TimeMap() { | |
this.map = new HashMap<>(); | |
} | |
public void set(String key, String value, int timestamp) { | |
map | |
.computeIfAbsent(key, k -> new TreeMap<>()) | |
.put(timestamp, value); | |
} | |
public String get(String key, int timestamp) { | |
if (!map.containsKey(key)) { | |
return ""; | |
} | |
TreeMap<Integer, String> treeMap = map.get(key); | |
Map.Entry<Integer, String> entry = treeMap.floorEntry(timestamp); | |
return entry == null ? "" : entry.getValue(); | |
} | |
} |
We use the floorEntry
method of the TreeMap to get the closest timestamp lower than or equal to the requested timestamp.
Consider a TreeMap with the following key-value pairs: {1: "a", 3: "b", 5: "c"}. Let's assume you call the floorEntry method with the argument 4. Since 4 is greater than 3 but less than 5, the method will return the entry with the key 3 and value "b".