|
2 | 2 | * 981
|
3 | 3 | * Time Based Key-Value Store
|
4 | 4 | **
|
5 |
| - * Design a time-based key-value data structure that |
6 |
| - * can store multiple values for the same key at different time stamps |
7 |
| - * and retrieve the key's value at a certain timestamp. |
| 5 | + * Design a time-based key-value data structure that can store multiple values |
| 6 | + * for the same key at different time stamps and retrieve the key's value at a certain timestamp. |
8 | 7 | *
|
9 | 8 | * Implement the TimeMap class:
|
10 |
| - * TimeMap() Initializes the object of the data structure. |
| 9 | + * • TimeMap() Initializes the object of the data structure. |
11 | 10 | *
|
12 |
| - * void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp. |
| 11 | + * • void set(String key, String value, int timestamp) |
| 12 | + * Stores the key key with the value value at the given time timestamp. |
13 | 13 | *
|
14 |
| - * String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. |
15 |
| - * If there are multiple such values, it returns the value associated with the largest timestamp_prev. |
| 14 | + * • String get(String key, int timestamp) |
| 15 | + * Returns a value such that set was called previously, |
| 16 | + * with timestamp_prev <= timestamp. |
| 17 | + * If there are multiple such values, |
| 18 | + * it returns the value associated with the largest timestamp_prev. |
16 | 19 | * If there are no values, it returns "".
|
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * Input |
| 23 | + * ["TimeMap", "set", "get", "get", "set", "get", "get"] |
| 24 | + * [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]] |
| 25 | + * |
| 26 | + * Output |
| 27 | + * [null, null, "bar", "bar", null, "bar2", "bar2"] |
| 28 | + * |
| 29 | + * Explanation |
| 30 | + * TimeMap timeMap = new TimeMap(); |
| 31 | + * timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. |
| 32 | + * timeMap.get("foo", 1); // return "bar" |
| 33 | + * 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". |
| 34 | + * timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. |
| 35 | + * timeMap.get("foo", 4); // return "bar2" |
| 36 | + * timeMap.get("foo", 5); // return "bar2" |
| 37 | + * |
| 38 | + * Constraints: |
| 39 | + * • 1 <= key.length, value.length <= 100 |
| 40 | + * • key and value consist of lowercase English letters and digits. |
| 41 | + * • 1 <= timestamp <= 10^7 |
| 42 | + * • All the timestamps timestamp of set are strictly increasing. |
| 43 | + * • At most 2 * 10^5 calls will be made to set and get. |
17 | 44 | **
|
18 | 45 | * https://leetcode.com/problems/time-based-key-value-store/
|
19 | 46 | ***/
|
|
0 commit comments