基于时间的键值存储
“基于时间的键值存储”问题的详细指南和 Python 实现。
1. 学习
“基于时间的键值存储”问题是二分搜索部分的一个关键挑战。
此实现侧重于 Python 中的简单级逻辑。
在我们提供的解决方案中,我们优先考虑技术准确性和代码可读性。
2. Real-World Applications
3. Visual Intuition
可视化基于时间的键值存储的逻辑流程。
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
仔细阅读基于时间的键值存储的问题陈述。
2. Formulate brute force
起草一个简单的迭代解决方案。
3. Identify inefficiency
寻找冗余计算。
4. Optimize search path
使用散列或排序来加速该过程。
5. Final Implementation
清理生产标准代码。
问题陈述
设计一个基于时间的键值数据结构,可以存储同一键在不同时间戳的多个值,并在某个时间戳检索该键的值。
实现 TimeMap 类:
- TimeMap() 初始化对象。
- set(key: str, value: str, timestamp: int) 在给定时间 timestamp 存储键 key 和值 value。
- get(key: str, timestamp: int) -> str 返回一个值,使得 set 之前使用 timestamp_prev <= timestamp 被调用。如果有多个这样的值,则返回与最大的 timestamp_prev 关联的值。如果没有值,则返回 ""。
- •1 <= key.length, value.length <= 100
- •key and value consist of lowercase English letters and digits
- •1 <= timestamp <= 10^7
- •All timestamps of set are strictly increasing for each key
- •At most 2 * 10^5 calls will be made to set and get
示例
["TimeMap", "set", "get", "get", "set", "get", "get"] [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
[None, None, "bar", "bar", None, "bar2", "bar2"]
set("foo", "bar", 1): stores bar at time 1. get("foo", 1): returns "bar". get("foo", 3): returns "bar" (latest value at or before time 3). set("foo", "bar2", 4): stores bar2 at time 4. get("foo", 4): returns "bar2". get("foo", 5): returns "bar2".
Need a Hint?
Edge Cases to Watch
- 空输入结构
- 单元素输入
- 大数值范围
准备好解决了吗?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
面试见解和变化
复杂性分析分解
为什么时间: Directly evaluates all possibilities.
为什么选择太空: Uses standard local memory.
为什么时间: Optimized paths reduce total operations.
为什么选择太空: May trade memory for speed.
优化解决方案Python代码
优化解决方案Python代码
class TimeMapOpt:
def __init__(self):
self.store = {}
def set(self, key: str, value: str, timestamp: int) -> None:
if key not in self.store: self.store[key] = []
self.store[key].append([value, timestamp])
def get(self, key: str, timestamp: int) -> str:
res = ""
values = self.store.get(key, [])
l, r = 0, len(values) - 1
while l <= r:
m = (l + r) // 2
if values[m][1] <= timestamp:
res = values[m][0]
l = m + 1
else:
r = m - 1
return res暴力破解代码(剧透保护)
暴力破解代码(剧透保护)
class TimeMapBrute:
def __init__(self):
self.store = {}
def set(self, key: str, value: str, timestamp: int) -> None:
if key not in self.store: self.store[key] = []
self.store[key].append([value, timestamp])
def get(self, key: str, timestamp: int) -> str:
res = ""
values = self.store.get(key, [])
for v, t in values:
if t <= timestamp: res = v
return resAlgorithm Pattern Checklist
When dealing with Binary Search data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推荐的 Python 资源
通过相关的交互式教程、备忘单和代码比较来扩展您的知识。
Python 日期时间:使用日期和时间
了解如何在 Python 中处理日期、时间、时区和计算。使用日期时间和时间增量掌握格式化、解析和算术。
如何在 Python 中按值对字典进行排序
了解如何按 Python 字典的值对字典进行排序。探索使用排序()、自定义键 lambda 进行排序,以及构建有序字典结构。
Python 日期时间格式备忘单
了解如何在 Python 中使用 datetime、strftime 和 strptime 解析和格式化日期和时间。
Python 与 JavaScript:哪种编程语言最好?
Python 和 JavaScript 的全面比较。探索语法差异、性能、用例(后端与前端)和编码示例。