|
| 1 | +## 981. Time Based Key-Value Store |
| 2 | + |
| 3 | +### Question: |
| 4 | +Create a timebased key-value store class TimeMap, that supports two operations. |
| 5 | + |
| 6 | +1. set(string key, string value, int timestamp) |
| 7 | + |
| 8 | + Stores the key and value, along with the given timestamp. |
| 9 | + |
| 10 | +2. get(string key, int timestamp) |
| 11 | + |
| 12 | + Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp. |
| 13 | + If there are multiple such values, it returns the one with the largest timestamp_prev. |
| 14 | + If there are no values, it returns the empty string (""). |
| 15 | + |
| 16 | + |
| 17 | +··· |
| 18 | +Example 1: |
| 19 | + |
| 20 | +Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] |
| 21 | +Output: [null,null,"bar","bar",null,"bar2","bar2"] |
| 22 | +Explanation: |
| 23 | +TimeMap kv; |
| 24 | +kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1 |
| 25 | +kv.get("foo", 1); // output "bar" |
| 26 | +kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar" |
| 27 | +kv.set("foo", "bar2", 4); |
| 28 | +kv.get("foo", 4); // output "bar2" |
| 29 | +kv.get("foo", 5); //output "bar2" |
| 30 | + |
| 31 | +Example 2: |
| 32 | + |
| 33 | +Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]] |
| 34 | +Output: [null,null,null,"","high","high","low","low"] |
| 35 | +··· |
| 36 | + |
| 37 | +Note: |
| 38 | +1. All key/value strings are lowercase. |
| 39 | +2. All key/value strings have length in the range [1, 100] |
| 40 | +3. The timestamps for all TimeMap.set operations are strictly increasing. |
| 41 | +4. 1 <= timestamp <= 10^7 |
| 42 | +5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +### Solution: |
| 48 | +* Method 1: TreeMap |
| 49 | + ```Java |
| 50 | + class TimeMap { |
| 51 | + private Map<String, TreeMap<Integer, String>> map; |
| 52 | + /** Initialize your data structure here. */ |
| 53 | + public TimeMap() { |
| 54 | + map = new HashMap<>(); |
| 55 | + } |
| 56 | + public void set(String key, String value, int timestamp) { |
| 57 | + TreeMap<Integer, String> treeMap = map.containsKey(key) ? map.get(key): new TreeMap<>(); |
| 58 | + treeMap.put(timestamp, value); |
| 59 | + map.put(key, treeMap); |
| 60 | + } |
| 61 | + public String get(String key, int timestamp) { |
| 62 | + if(!map.containsKey(key)) return ""; |
| 63 | + else{ |
| 64 | + TreeMap treeMap = map.get(key); |
| 65 | + Map.Entry<Integer, String> entry = treeMap.floorEntry(timestamp); |
| 66 | + if(entry == null) return ""; |
| 67 | + else return entry.getValue(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + /** |
| 72 | + * Your TimeMap object will be instantiated and called as such: |
| 73 | + * TimeMap obj = new TimeMap(); |
| 74 | + * obj.set(key,value,timestamp); |
| 75 | + * String param_2 = obj.get(key,timestamp); |
| 76 | + */ |
| 77 | + ``` |
| 78 | + |
| 79 | + |
0 commit comments