#
# lru design
# @param operators int整型二维数组 the ops
# @param k int整型 the k
# @return int整型一维数组
#
class Solution:
def LRU(self , operators , k ):
# write code here
lru = {} # 利用字典作为双向队列,左侧作为最不常用的,右侧作为最常用的
l_return = []
for opt in operators:
if opt[0] == 1 :
if len(lru) == k:#当满的时候,删除最不常用的
key = list(lru.keys())[0]
lru.pop(key)
#若没满,则添加当前元素
key = opt[1]
value = opt[2]
lru[key] = value
if opt[0] == 2:
key = opt[1]
if key in lru:
value = lru[key]
l_return.append(value)
# 更新顺序
lru.pop(key) # 删除
lru[key] = value #移动到最右端
else:
l_return.append(-1)
return l_return