当前位置:网站首页>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时间复杂度
边栏推荐
猜你喜欢

Relevant knowledge points of hash table

5. Implement MapReduce program on window side to complete wordcount function

CTF skill tree - file upload

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

GKCylindersNoiseSource

剑指 Offer 35. 复杂链表的复制

The 10th Landbridge cup embedded electronic provincial competition

Blue Bridge Cup embedded Hal library USART_ RX

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

Reading these six books makes learning MySQL easier
随机推荐
盘点:144个免费学习网站,全网最全资源合集
Blue Bridge Cup embedded Hal library ADC
Samba learning
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
ThinkPad指纹验证在win7无法使用的解决方法
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
Arduino Basics
Blue Bridge Cup embedded Hal library USART_ RX
Array related knowledge points
Nodejs: set up the express service, set up the session and realize the exit operation
Explanation of JDBC classes
GKARC4RandomSource
蓝桥杯电子类嵌入式第十届省赛
Learn these analysis methods and models, and no longer have no ideas when encountering problems
Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter
Judge whether the nixie tube is a common anode or a common cathode
_ HUGE and __ IMP__ HUGE in “math.h“
JSON preliminary understanding
数组相关的知识点
Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market