981. Time Based Key-Value Store

Create a timebased key-value store class TimeMap, that supports two operations.

1. set(string key, string value, int timestamp)

  • Stores the key and value, along with the given timestamp.

2. get(string key, int timestamp)

  • Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
  • If there are multiple such values, it returns the one with the largest timestamp_prev.
  • If there are no values, it returns the empty string (""). 

Example 1:

Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
Output: [null,null,"bar","bar",null,"bar2","bar2"]
Explanation:   
TimeMap kv;   
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   
kv.get("foo", 1);  // output "bar"   
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"   
kv.set("foo", "bar2", 4);   
kv.get("foo", 4); // output "bar2"   
kv.get("foo", 5); //output "bar2"   

Example 2:

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]]
Output: [null,null,null,"","high","high","low","low"]

 1 public class TimeMap {
 2 
 3     private Map<String,TreeMap<Integer,String>> map;
 4 
 5     /** Initialize your data structure here. */
 6     public TimeMap() {
 7         map = new HashMap<>();
 8     }
 9 
10     public void set(String key, String value, int timestamp) {
11         if(!map.containsKey(key)) {
12             map.put(key,new TreeMap<>());
13         }
14         map.get(key).put(timestamp,value);
15     }
16 
17     public String get(String key, int timestamp) {
18         TreeMap<Integer,String> treeMap = map.get(key);
19         if(treeMap==null) {
20             return "";
21         }
22         
23         Integer floor = treeMap.floorKey(timestamp);
24         if(floor==null) {
25             return "";
26         }
27         return treeMap.get(floor);
28     }
29 }
30 
31 /**
32  * Your TimeMap object will be instantiated and called as such:
33  * TimeMap obj = new TimeMap();
34  * obj.set(key,value,timestamp);
35  * String param_2 = obj.get(key,timestamp);
36  */
 1 class TimeMap {
 2     Map<String, List<Pair<Integer, String>>> hashMap;
 3 
 4     public TimeMap() {
 5         hashMap = new HashMap();
 6     }
 7 
 8     public void set(String key, String value, int timestamp) {
 9         if (!hashMap.containsKey(key))
10             hashMap.put(key, new ArrayList<Pair<Integer, String>>());
11 
12         hashMap.get(key).add(new Pair(timestamp, value));
13     }
14 
15     public String get(String key, int timestamp) {
16         if (!hashMap.containsKey(key)) {
17             return "";
18         }
19 
20         List<Pair<Integer, String>> A = hashMap.get(key);
21         int idx = binarySearch(A, timestamp);
22         if (idx < 0) {
23             return "";
24         }
25         return A.get(idx).getValue();
26     }
27 
28     private int binarySearch(List<Pair<Integer, String>> list, int time) {
29         int low = 0, high = list.size() - 1;
30         while (low <= high) {
31             int mid = (low + high) >> 1;
32             if (list.get(mid).getKey() <= time) {
33                 low = mid + 1;
34             } else {
35                 high = mid - 1;
36             }
37         }
38         return high;
39     }
40 }

 

 
posted @ 2021-03-12 09:40  北叶青藤  阅读(85)  评论(0)    收藏  举报