当前位置:网站首页>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
边栏推荐
- 吊打面试官的问题
- Samba learning
- 理解Oracle的几个概念
- Office2013 input mathematical formula above
- Explanation of JDBC classes
- Learn these analysis methods and models, and no longer have no ideas when encountering problems
- Zero code | easily realize data warehouse modeling and build Bi Kanban
- Use the statement object to execute DDL statements to create tables
- 做数据分析,你还不懂RFM分析方法(模型)?
- JSON preliminary understanding
猜你喜欢

leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】

Blue Bridge Cup embedded Hal library systick

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

MySQL Architecture Principle

零代码 | 轻松实现数据仓库建模,搭建BI看板

leetcode:1300. 转变数组后最接近目标值的数组和【二分】

Bc35 NB module at instruction development summary

21. Merge two ordered linked lists

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

Why is low code (apaas) popular again recently?
随机推荐
Question of hanging the interviewer
这里有一份超实用Excel快捷键合集(常用+八大类汇总)
2021-03-24
3. MapReduce explanation and source code analysis
Stacks and queues
Two dimensional prefix and
Reading these six books makes learning MySQL easier
Preliminary understanding of float
Learn how to do e-commerce data analysis (with operation analysis index framework)
Blue Bridge Cup embedded Hal library systick
Samba learning
Explanation of JDBC classes
Header library file
理解Oracle的几个概念
CRM+零代码:轻松实现企业信息化
网络文件系统服务(NFS)
1. Sum of two numbers
Three ways for Cortex-M kernel to manage global interrupts
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
做数据分析,你还不懂RFM分析方法(模型)?