当前位置:网站首页>Leetcode:981. time based key value storage [trap of iteration for: on]
Leetcode:981. time based key value storage [trap of iteration for: on]
2022-07-28 11:14:00 【White speed Dragon King's review】

analysis
Simple dict + Two points
however, Pay attention to two dict
Timeout trap : Extract sub elements 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: Separate records
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)
summary
Pay attention to hidden on Time complexity
边栏推荐
- 3. MapReduce explanation and source code analysis
- Related concepts of several databases
- leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
- 使用statement对象执行DDL语句创建表
- The 10th Landbridge cup embedded electronic provincial competition
- A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
- The solution of PHP sending mobile MAS SMS garbled code
- nodemcu之开发环境配置
- Under the platform driven platform, the "dev- > dev.of_node" of the formal parameter dev in the probe function Understanding of
- 蓝桥杯嵌入式-HAL库-ADC
猜你喜欢

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

6. MapReduce custom partition implementation

Here is a super practical excel shortcut set (common + summary of eight categories)

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

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

ctf技能树----文件上传

Learn these analysis methods and models, and no longer have no ideas when encountering problems

Inventory: 144 free learning websites, the most complete collection of resources in the whole network

Inventory: 6 books teach you the necessary skills for career promotion

Ten questions about low code: tell everything about low code!
随机推荐
21. Merge two ordered linked lists
Sword finger offer 30. stack containing min function
JSON初步理解
Header library file
The Xiongguan pass is like an iron road, and now we are going to cross it from the beginning
Use the statement object to execute DDL statements to create tables
JS - modify the key name of the object in the array
Bc35 NB module at instruction development summary
C语言使用二重指针实现简单工厂模式(多态)
Nodejs: mongodb simple fuzzy + paging query instance
BOM part attributes and understanding
nodejs:mongodb 插入成功之后的返回值
float浮动初步理解
为什么传输前要进行编码与调制
offsetof宏与container_of宏分析详解
Zero code | easily realize data warehouse modeling and build Bi Kanban
C language to convert float data into BCD data
Understanding of the return value of the structure pointer function passed to the structure pointer
Combination of const and pointer
JWT 登录认证 + Token 自动续期方案,写得太好了!