当前位置:网站首页>leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
2022-07-28 10:44:00 【白速龙王的回眸】

分析
简单dict + 二分
however,要注意分两个dict
超时的陷阱:提取子元素的on
class TimeMap:
def __init__(self):
self.d = defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.d[key].append((timestamp, value))
def get(self, key: str, timestamp: int) -> str:
if key not in self.d:
return ""
if timestamp < self.d[key][0][0]:
return ""
lst = [item[0] for item in self.d[key]]
idx = bisect_right(lst, timestamp)
if idx == 0:
return ""
else:
return self.d[key][idx - 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)
ac code:分开记录
class TimeMap:
def __init__(self):
self.d1 = defaultdict(list)
self.d2 = defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.d1[key].append(value)
self.d2[key].append(timestamp)
def get(self, key: str, timestamp: int) -> str:
idx = bisect_right(self.d2[key], timestamp)
if idx == 0:
return ""
else:
return self.d1[key][idx - 1]
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
总结
注意隐藏的on时间复杂度
边栏推荐
- Installation points and precautions of split angle probe
- 关于结构体指针函数的返回值传递给结构体指针的理解
- CTF skill tree - file upload
- PHP发送移动MAS短信乱码的解决方法
- Ten questions about low code: tell everything about low code!
- GKLinearCongruentialRandomSource
- 剑指 Offer 30. 包含min函数的栈
- 一文学会如何做电商数据分析(附运营分析指标框架)
- 19. Delete the penultimate node of the linked list
- CGAL compilation error
猜你喜欢

为什么传输前要进行编码与调制

keil和IAR中lib库文件的生成和使用

盘点:144个免费学习网站,全网最全资源合集

JS - modify the key name of the object in the array

用手机对电脑进行远程关机

Blue Bridge Cup embedded Hal library LCD

Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter

Yan reports an error: exception message: /bin/bash: line 0: fg: no job control

这里有一份超实用Excel快捷键合集(常用+八大类汇总)

Preliminary understanding of float
随机推荐
Install GMP
nodejs:mongodb 插入成功之后的返回值
Preliminary understanding of float
The future of generating confrontation networks in deepfake
盘点:6本书教会你职场晋升必备技能
Judge whether the nixie tube is a common anode or a common cathode
为什么传输前要进行编码与调制
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
读懂这6本书,学习MySQL更轻松
Configuring raspberry pie, process and problems encountered
表格数据处理软件,除了Excel还有什么?
JS - 修改数组中对象的键名
低代码(aPaas)为什么最近又火了?
Nodejs:mongodb 简单模糊+分页查询实例
Here is a super practical excel shortcut set (common + summary of eight categories)
Blue Bridge Cup embedded Hal library ADC
Ten questions about low code: tell everything about low code!
Three ways for Cortex-M kernel to manage global interrupts
6. MapReduce custom partition implementation
GKLinearCongruentialRandomSource