Skip to content

Commit 7cd9fbd

Browse files
add 981
1 parent 9f43bc6 commit 7cd9fbd

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,5 @@ LeetCode
311311
|0978|[Longest Turbulent Subarray](https://leetcode.com/problems/longest-turbulent-subarray/) | c | [c++](./src/0978-Longest-Turbulent-Subarray/0978.cpp) |[python](./src/0978-Longest-Turbulent-Subarray/0978.py)|||Medium|
312312
|0979|[Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | c | [c++](./src/0979-Distribute-Coins-in-Binary-Tree/0979.cpp) |[python](./src/0979-Distribute-Coins-in-Binary-Tree/0979.py)|||Medium|
313313
|0980|[Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | c | [c++](./src/0980-Unique-Paths-III/0980.cpp) |[python](./src/0980-Unique-Paths-III/0980.py)|||Medium|
314-
|0984|[String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | c | [c++](./src/0984-String-Without-AAA-or-BBB/0984.cpp) |[python](./src/0984-String-Without-AAA-or-BBB/0984.py)|||Medium|
314+
|0981|[Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | c | [c++](./src/0981-Time-Based-Key-Value-Store/0981.cpp) |[python](./src/0981-Time-Based-Key-Value-Store/0981.py)|||Medium|
315+
|0984|[String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | c | [c++](./src/0984-String-Without-AAA-or-BBB/0984.cpp) |[python](./src/0984-String-Without-AAA-or-BBB/0984.py)|||Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string>
2+
#include <unordered_map>
3+
#include <map>
4+
using namespace std;
5+
6+
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
7+
class TimeMap
8+
{
9+
public:
10+
/** Initialize your data structure here. */
11+
TimeMap() {}
12+
13+
void set(string key, string value, int timestamp)
14+
{
15+
m[key].insert({timestamp, value});
16+
}
17+
18+
string get(string key, int timestamp)
19+
{
20+
auto it = m[key].upper_bound(timestamp);
21+
return it == m[key].begin() ? "" : prev(it)->second;
22+
}
23+
private:
24+
unordered_map<string, map<int, string>> m;
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import collections
2+
import bisect
3+
class TimeMap:
4+
def __init__(self):
5+
"""
6+
Initialize your data structure here.
7+
"""
8+
self.data = collections.defaultdict(list)
9+
10+
def set(self, key: 'str', value: 'str', timestamp: 'int') -> 'None':
11+
self.data[key].append((timestamp, value))
12+
13+
def get(self, key: 'str', timestamp: 'int') -> 'str':
14+
if key not in self.data:
15+
return ''
16+
idx = bisect.bisect_right(self.data[key], (timestamp, 'z'*101))
17+
return '' if idx == 0 else self.data[key][idx-1][1]

0 commit comments

Comments
 (0)