From 9c2b71e1e30bf2fbb3ff1db499a37f86bbe062c4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 4 May 2021 22:58:19 +0800 Subject: [PATCH] feat: add solutions to leetcode problem: No.0981. Time Based Key-Value Store --- .../0981.Time Based Key-Value Store/README.md | 56 ++++++++++++- .../README_EN.md | 78 ++++++++++++------- .../Solution.java | 30 +++++++ .../Solution.py | 24 ++++++ 4 files changed, 161 insertions(+), 27 deletions(-) create mode 100644 solution/0900-0999/0981.Time Based Key-Value Store/Solution.java create mode 100644 solution/0900-0999/0981.Time Based Key-Value Store/Solution.py diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/README.md b/solution/0900-0999/0981.Time Based Key-Value Store/README.md index 74fee95a96..03835677ff 100644 --- a/solution/0900-0999/0981.Time Based Key-Value Store/README.md +++ b/solution/0900-0999/0981.Time Based Key-Value Store/README.md @@ -57,11 +57,12 @@ kv.get("foo", 5); // 输出 "bar2"  
  • TimeMap.set 和 TimeMap.get 函数在每个测试用例中将(组合)调用总计 120000 次。
  • - ## 解法 +嵌套哈希表实现。 + ### **Python3** @@ -69,7 +70,31 @@ kv.get("foo", 5); // 输出 "bar2"   ```python +class TimeMap: + def __init__(self): + """ + Initialize your data structure here. + """ + self.ktv = collections.defaultdict(list) + + def set(self, key: str, value: str, timestamp: int) -> None: + self.ktv[key].append((timestamp, value)) + + def get(self, key: str, timestamp: int) -> str: + if key not in self.ktv: + return '' + tv = self.ktv[key] + # #查找第一个大于timestamp的 + i = bisect.bisect_right(tv, (timestamp, chr(127))) + return tv[i - 1][1] if i else '' + + + +# Your TimeMap object will be instantiated and called as such: +# obj = TimeMap() +# obj.set(key,value,timestamp) +# param_2 = obj.get(key,timestamp) ``` ### **Java** @@ -77,7 +102,36 @@ kv.get("foo", 5); // 输出 "bar2"   ```java +class TimeMap { + private Map> ktv; + /** Initialize your data structure here. */ + public TimeMap() { + ktv = new HashMap<>(); + } + + public void set(String key, String value, int timestamp) { + TreeMap tv = ktv.getOrDefault(key, new TreeMap<>()); + tv.put(timestamp, value); + ktv.put(key, tv); + } + + public String get(String key, int timestamp) { + if (!ktv.containsKey(key)) { + return ""; + } + TreeMap tv = ktv.get(key); + Integer t = tv.floorKey(timestamp); + return t == null ? "" : tv.get(t); + } +} + +/** + * Your TimeMap object will be instantiated and called as such: + * TimeMap obj = new TimeMap(); + * obj.set(key,value,timestamp); + * String param_2 = obj.get(key,timestamp); + */ ``` ### **...** diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md b/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md index 7cd66f26f4..2c4b958a5c 100644 --- a/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md +++ b/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md @@ -6,40 +6,26 @@

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

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

    - -
     
     Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
    @@ -66,14 +52,10 @@ kv.get("foo", 5); //output "bar2"  
     
     
    - -

    Example 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]]
    @@ -86,16 +68,10 @@ kv.get("foo", 5); //output "bar2"  
     
     
    - -

     

    - -

    Note:

    - -
    1. All key/value strings are lowercase.
    2. All key/value strings have length in the range [1, 100]
    3. @@ -104,8 +80,6 @@ kv.get("foo", 5); //output "bar2"  
    4. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
    - - ## Solutions @@ -113,13 +87,65 @@ kv.get("foo", 5); //output "bar2"   ### **Python3** ```python +class TimeMap: + def __init__(self): + """ + Initialize your data structure here. + """ + self.ktv = collections.defaultdict(list) + + def set(self, key: str, value: str, timestamp: int) -> None: + self.ktv[key].append((timestamp, value)) + + def get(self, key: str, timestamp: int) -> str: + if key not in self.ktv: + return '' + tv = self.ktv[key] + i = bisect.bisect_right(tv, (timestamp, chr(127))) + return tv[i - 1][1] if i else '' + + + +# Your TimeMap object will be instantiated and called as such: +# obj = TimeMap() +# obj.set(key,value,timestamp) +# param_2 = obj.get(key,timestamp) ``` ### **Java** ```java +class TimeMap { + private Map> ktv; + /** Initialize your data structure here. */ + public TimeMap() { + ktv = new HashMap<>(); + } + + public void set(String key, String value, int timestamp) { + TreeMap tv = ktv.getOrDefault(key, new TreeMap<>()); + tv.put(timestamp, value); + ktv.put(key, tv); + } + + public String get(String key, int timestamp) { + if (!ktv.containsKey(key)) { + return ""; + } + TreeMap tv = ktv.get(key); + Integer t = tv.floorKey(timestamp); + return t == null ? "" : tv.get(t); + } +} + +/** + * Your TimeMap object will be instantiated and called as such: + * TimeMap obj = new TimeMap(); + * obj.set(key,value,timestamp); + * String param_2 = obj.get(key,timestamp); + */ ``` ### **...** diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/Solution.java b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.java new file mode 100644 index 0000000000..6689807756 --- /dev/null +++ b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.java @@ -0,0 +1,30 @@ +class TimeMap { + private Map> ktv; + + /** Initialize your data structure here. */ + public TimeMap() { + ktv = new HashMap<>(); + } + + public void set(String key, String value, int timestamp) { + TreeMap tv = ktv.getOrDefault(key, new TreeMap<>()); + tv.put(timestamp, value); + ktv.put(key, tv); + } + + public String get(String key, int timestamp) { + if (!ktv.containsKey(key)) { + return ""; + } + TreeMap tv = ktv.get(key); + Integer t = tv.floorKey(timestamp); + return t == null ? "" : tv.get(t); + } +} + +/** + * Your TimeMap object will be instantiated and called as such: + * TimeMap obj = new TimeMap(); + * obj.set(key,value,timestamp); + * String param_2 = obj.get(key,timestamp); + */ \ No newline at end of file diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py new file mode 100644 index 0000000000..479e1a12c3 --- /dev/null +++ b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py @@ -0,0 +1,24 @@ +class TimeMap: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.ktv = collections.defaultdict(list) + + def set(self, key: str, value: str, timestamp: int) -> None: + self.ktv[key].append((timestamp, value)) + + def get(self, key: str, timestamp: int) -> str: + if key not in self.ktv: + return '' + tv = self.ktv[key] + i = bisect.bisect_right(tv, (timestamp, chr(127))) + return tv[i - 1][1] if i else '' + + + +# Your TimeMap object will be instantiated and called as such: +# obj = TimeMap() +# obj.set(key,value,timestamp) +# param_2 = obj.get(key,timestamp) \ No newline at end of file