|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | + -* Time Based Key-Value Store *- |
| 5 | +
|
| 6 | +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. |
| 7 | +
|
| 8 | +Implement the TimeMap class: |
| 9 | +
|
| 10 | +TimeMap() Initializes the object of the data structure. |
| 11 | +void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp. |
| 12 | +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 "". |
| 13 | +
|
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +Input |
| 18 | +["TimeMap", "set", "get", "get", "set", "get", "get"] |
| 19 | +[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]] |
| 20 | +Output |
| 21 | +[null, null, "bar", "bar", null, "bar2", "bar2"] |
| 22 | +
|
| 23 | +Explanation |
| 24 | +TimeMap timeMap = new TimeMap(); |
| 25 | +timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. |
| 26 | +timeMap.get("foo", 1); // return "bar" |
| 27 | +timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 28 | +timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. |
| 29 | +timeMap.get("foo", 4); // return "bar2" |
| 30 | +timeMap.get("foo", 5); // return "bar2" |
| 31 | +
|
| 32 | +
|
| 33 | +Constraints: |
| 34 | +
|
| 35 | +1 <= key.length, value.length <= 100 |
| 36 | +key and value consist of lowercase English letters and digits. |
| 37 | +1 <= timestamp <= 107 |
| 38 | +All the timestamps timestamp of set are strictly increasing. |
| 39 | +At most 2 * 105 calls will be made to set and get. |
| 40 | +
|
| 41 | +*/ |
| 42 | + |
| 43 | +//Your TimeMap object will be instantiated and called as such: |
| 44 | + |
| 45 | +// class Pair<T1,T2> { |
| 46 | +// final T1 first; |
| 47 | +// final T2 second; |
| 48 | +// Pair(this.first, this.second); |
| 49 | + |
| 50 | +// bool operator == (final Pair other) { |
| 51 | +// return first == other.first && second == other.second; |
| 52 | +// } |
| 53 | +// int get hashCode => hash(first.hashCode,second.hashCode); |
| 54 | +// } |
| 55 | + |
| 56 | +// class Pair { |
| 57 | +// late String value; |
| 58 | +// late int time; |
| 59 | +// Pair(String _value, int _timestamp) { |
| 60 | +// time = _timestamp; |
| 61 | +// value = _value; |
| 62 | +// } |
| 63 | +// } |
| 64 | + |
| 65 | +// class F { |
| 66 | +// List<Pair> list = <Pair>[]; |
| 67 | + |
| 68 | +// F(String _value, int _timestamp) { |
| 69 | +// list.add(Pair(_value, _timestamp)); |
| 70 | +// } |
| 71 | +// F.from(List<Pair> c) { |
| 72 | +// list = c; |
| 73 | +// } |
| 74 | +// } |
| 75 | + |
| 76 | +class TimeMap { |
| 77 | + Map map = {}; |
| 78 | + TimeMap() { |
| 79 | + this.map; |
| 80 | + } |
| 81 | + |
| 82 | + void set(String key, String value, int timestamp) {} |
| 83 | + |
| 84 | + String get(String key, int timestamp) { |
| 85 | + return ""; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TimeMap obj = TimeMap(); |
| 90 | +// obj.set(key,value,timestamp); |
| 91 | +// String param2 = obj.get(key,timestamp); |
0 commit comments