Skip to content

Commit f5e4780

Browse files
committed
Time Based Key-Value Store
1 parent 2051f51 commit f5e4780

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

981-time-based-key-value-store.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/time-based-key-value-store/
3+
4+
Create a timebased key-value store class TimeMap, that supports two operations.
5+
1. set(string key, string value, int timestamp)
6+
Stores the key and value, along with the given timestamp.
7+
2. get(string key, int timestamp)
8+
9+
Returns a value such that set(key, value, timestamp_prev) was called previously,
10+
with timestamp_prev <= timestamp.
11+
If there are multiple such values, it returns the one with the largest timestamp_prev.
12+
If there are no values, it returns the empty string ("").
13+
14+
Example 1:
15+
16+
Input: inputs = ["TimeMap","set","get","get","set","get","get"],
17+
inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
18+
Output: [null,null,"bar","bar",null,"bar2","bar2"]
19+
20+
Explanation:
21+
TimeMap kv;
22+
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1
23+
kv.get("foo", 1); // output "bar"
24+
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3
25+
// and timestamp 2, then the only value is at timestamp 1 ie "bar"
26+
kv.set("foo", "bar2", 4);
27+
kv.get("foo", 4); // output "bar2"
28+
kv.get("foo", 5); //output "bar2"
29+
30+
Example 2:
31+
Input: inputs = ["TimeMap","set","set","get","get","get","get","get"],
32+
inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],
33+
["love",15],["love",20],["love",25]]
34+
Output: [null,null,null,"","high","high","low","low"]
35+
36+
Note:
37+
All key/value strings are lowercase.
38+
All key/value strings have length in the range [1, 100]
39+
The timestamps for all TimeMap.set operations are strictly increasing.
40+
1 <= timestamp <= 10^7
41+
TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
42+
"""
43+
# Time Complexity: set -> O(1), get -> O(logn)
44+
# Space Complexity: O(n)
45+
import collections
46+
class TimeMap:
47+
48+
def __init__(self):
49+
"""
50+
Initialize your data structure here.
51+
"""
52+
self.map = collections.defaultdict(list)
53+
54+
55+
def set(self, key: str, value: str, timestamp: int) -> None:
56+
self.map[key].append((timestamp, value))
57+
58+
def get(self, key: str, timestamp: int) -> str:
59+
keyValues = self.map.get(key)
60+
if not keyValues:
61+
return ""
62+
index = self.binarySearch(keyValues, timestamp)
63+
print(index)
64+
return keyValues[index][1] if keyValues[index][0] <= timestamp else ""
65+
66+
def binarySearch(self, keyValues, timestamp):
67+
start, end = 0, len(keyValues) - 1
68+
while start < end:
69+
mid = start+(end-start+1)//2
70+
if keyValues[mid][0] > timestamp:
71+
end = mid - 1
72+
else:
73+
start = mid
74+
return end
75+
76+
77+
# Your TimeMap object will be instantiated and called as such:
78+
# obj = TimeMap()
79+
# obj.set(key,value,timestamp)
80+
# param_2 = obj.get(key,timestamp)

0 commit comments

Comments
 (0)