Problem description:

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.
  1. 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:

1
2
3
4
5
6
7
8
9
10
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:

1
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"]

Note:

  1. All key/value strings are lowercase.
  2. All key/value strings have length in the range [1, 100]
  3. The timestamps for all TimeMap.set operations are strictly increasing.
  4. 1 <= timestamp <= 10^7
  5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.

Solution:

For this kind of key-value pair, using dictionary would help us find item in shortest time.
However, this question have several timestamp we need to consider. We could use a list to store all the timestamp-value.

  • Notice in Note 3, the timestamp is strictly increasing. So we don’t have to sort the element in list before searching

In get(), we could use bisect_right or write own binary search to find the value that is greater than timestamp. The reason we’re finding greater than timestamp is because we use right as boundary, then we could return nums[right-1] as it’s the value that smaller than timestamp but closest to timestamp.

For example:

1
2
3
nums: [5,10,15,20,25], timestamp: 8
at the last round, right would be idx:1, nums[1] = 10
then we return nums[right-1] = nums[0], as it's the one smaller than 8 but fulfilled timestamp_prev <= timestamp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class TimeMap:
def __init__(self):
self.dic = defaultdict(list)

def set(self, key: str, value: str, timestamp: int) -> None:
self.dic[key].append([timestamp, value])

def get(self, key: str, timestamp: int) -> str:
if key not in self.dic:
return ""
arr = self.dic[key]
left, right = 0, len(arr)
while left < right:
mid = (left+ right) //2
if arr[mid][0] > timestamp:
right = mid
else:
left = mid+1

return "" if right == 0 else arr[right-1][1]

# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class TimeMap:

def __init__(self):
self.dic = defaultdict(list)
self.data = defaultdict(list)

def set(self, key: str, value: str, timestamp: int) -> None:
self.dic[key].append(timestamp)
self.data[key].append(value)

def get(self, key: str, timestamp: int) -> str:
idx = bisect.bisect(self.dic[key], timestamp)
if idx == 0:
return ""
return self.data[key][idx-1]

time complexity:

  • set(): $O(1)$ for single operation, $O(n)$ for total. Note: assuming timestamps are only increasing. If not, it’s $O(n log n)$.
  • get(): $O(logn)$, n is length of list in nums[key], and total O(m log n), m is number of get() request.
    space complexity: $O(n)$
    reference:
    related problem: