981. Time Based Key-Value Store Medium

@problem@discussion
#Hash Table#String#Binary Search#Design



1/**
2 * [981] Time Based Key-Value Store
3 *
4 * 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.
5 * Implement the TimeMap class:
6 * 
7 * 	TimeMap() Initializes the object of the data structure.
8 * 	void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.
9 * 	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 "".
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input
15 * ["TimeMap", "set", "get", "get", "set", "get", "get"]
16 * [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
17 * Output
18 * [null, null, "bar", "bar", null, "bar2", "bar2"]
19 * Explanation
20 * TimeMap timeMap = new TimeMap();
21 * timeMap.set("foo", "bar", 1);  // store the key "foo" and value "bar" along with timestamp = 1.
22 * timeMap.get("foo", 1);         // return "bar"
23 * 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".
24 * timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
25 * timeMap.get("foo", 4);         // return "bar2"
26 * timeMap.get("foo", 5);         // return "bar2"
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= key.length, value.length <= 100
32 * 	key and value consist of lowercase English letters and digits.
33 * 	1 <= timestamp <= 10^7
34 * 	All the timestamps timestamp of set are strictly increasing.
35 * 	At most 2 * 10^5 calls will be made to set and get.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/time-based-key-value-store/
41// discuss: https://leetcode.com/problems/time-based-key-value-store/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45struct TimeMap {
46        false
47    }
48
49
50/** 
51 * `&self` means the method takes an immutable reference.
52 * If you need a mutable reference, change it to `&mut self` instead.
53 */
54impl TimeMap {
55
56    fn new() -> Self {
57        
58    }
59    
60    fn set(&self, key: String, value: String, timestamp: i32) {
61        
62    }
63    
64    fn get(&self, key: String, timestamp: i32) -> String {
65        
66    }
67}
68
69/**
70 * Your TimeMap object will be instantiated and called as such:
71 * let obj = TimeMap::new();
72 * obj.set(key, value, timestamp);
73 * let ret_2: String = obj.get(key, timestamp);
74 */
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_981() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.