当前位置:网站首页>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时间复杂度
边栏推荐
- Header library file
- CRM+零代码:轻松实现企业信息化
- 用手机对电脑进行远程关机
- 蓝桥杯嵌入式-HAL库-USART_RX
- MySQL Architecture Principle
- Stacks and queues
- If you don't climb mountains, you don't know the height of the sky; If you don't face deep streams, you don't know the thickness of the earth
- 盘点:令人心动的数据可视化图表
- Preliminary understanding of float
- Jianzhi offer 09. realize queue with two stacks
猜你喜欢

The 10th Landbridge cup embedded electronic provincial competition

剑指 Offer 30. 包含min函数的栈

ICML 2022 | graph represents the structure aware transformer model of learning

I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime

19. Delete the penultimate node of the linked list

表格数据处理软件,除了Excel还有什么?

Sword finger offer 30. stack containing min function

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

分体式测斜探头安装要点及注意事项

Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award
随机推荐
关于结构体指针函数的返回值传递给结构体指针的理解
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
Blue Bridge Cup embedded Hal library USART_ RX
Reading these six books makes learning MySQL easier
JS - modify the key name of the object in the array
nodemcu之开发环境配置
Yan reports an error: exception message: /bin/bash: line 0: fg: no job control
platform驱动平台下,关于probe函数中,形参dev的“dev->dev.of_node;”的理解
C语言使用二重指针实现简单工厂模式(多态)
Inventory: exciting data visualization chart
蓝桥杯嵌入式-HAL库-USART_TX
Stacks and queues
Sword finger offer 30. stack containing min function
const与指针的组合使用
Inventory: 6 books teach you the necessary skills for career promotion
Nodejs: return value of mongodb after successful insertion
The solution of PHP sending mobile MAS SMS garbled code
c语言链表的使用
吊打面试官的问题
Combination of const and pointer