Python算法题集_LRU 缓存
- [题146:LRU 缓存](#题146:LRU 缓存)
 - [1. 示例说明](#1. 示例说明)
 - [2. 题目解析](#2. 题目解析)
 - 
- [- 题意分解](#- 题意分解)
 - [- 优化思路](#- 优化思路)
 - [- 测量工具](#- 测量工具)
 
 - [3. 代码展开](#3. 代码展开)
 - 
- 1) 标准求解【队列+字典】 标准求解【队列+字典】)
 - 2) 改进版一【有序字典】 改进版一【有序字典】)
 - 3) 改进版二【双向链表+字典】 改进版二【双向链表+字典】)
 
 - [4. 最优算法](#4. 最优算法)
 
本文为Python算法题集之一的代码示例
题146:LRU 缓存
1. 示例说明
- 
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现
LRUCache类:LRUCache(int capacity)以 正整数 作为容量capacity初始化 LRU 缓存int get(int key)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。void put(int key, int value)如果关键字key已经存在,则变更其数据值value;如果不存在,则向缓存中插入该组key-value。如果插入操作导致关键字数量超过capacity,则应该 逐出 最久未使用的关键字。
函数
get和put必须以O(1)的平均时间复杂度运行。示例:
输入 ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] 输出 [null, null, null, 1, null, -1, null, -1, 3, 4] 解释 LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // 缓存是 {1=1} lRUCache.put(2, 2); // 缓存是 {1=1, 2=2} lRUCache.get(1); // 返回 1 lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3} lRUCache.get(2); // 返回 -1 (未找到) lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3} lRUCache.get(1); // 返回 -1 (未找到) lRUCache.get(3); // 返回 3 lRUCache.get(4); // 返回 4提示:
1 <= capacity <= 30000 <= key <= 100000 <= value <= 105- 最多调用 
2 * 105次get和put 
 
2. 题目解析
- 题意分解
- 本题为设计一个整形缓存类,可以指定缓存大小
 - 基本的设计思路是采用队列控制使用次序,字典进行缓存【哈希】
 
- 优化思路
- 
通常优化:减少循环层次
 - 
通常优化:增加分支,减少计算集
 - 
通常优化:采用内置算法来提升计算速度
 - 
分析题目特点,分析最优解
- 
可以考虑采用有序字典设计缓存类
 - 
可以考虑采用双向链表设计使用队列,缓存还是采用字典
 
 - 
 
- 测量工具
- 本地化测试说明:LeetCode网站测试运行时数据波动很大,因此需要本地化测试解决这个问题
 CheckFuncPerf(本地化函数用时和内存占用测试模块)已上传到CSDN,地址:Python算法题集_检测函数用时和内存占用的模块- 本题本地化超时测试用例自己生成,详见【最优算法章节】
 
3. 代码展开
1) 标准求解【队列+字典】
队列控制使用次序,字典保存键值对
勉强通关,超过05% 
            
            
              python
              
              
            
          
          import CheckFuncPerf as cfp
class LRUCache_base:
def __init__(self, capacity):
   self.queue, self.dict, self.capacity, self.queuelen = [], {}, capacity, 0
def get(self, key):
   if key in self.queue:
       self.queue.remove(key)
       self.queue.append(key)
       return self.dict[key]
   else:
       return -1
def put(self, key, value):
   if key in self.queue:
       self.queue.remove(key)
   else:
       if self.queuelen == self.capacity:
           self.dict.pop(self.queue.pop(0))
       else:
           self.queuelen += 1
   self.queue.append(key)
   self.dict[key] = valu
tmpLRUCache = LRUCache_base(5)
result = cfp.getTimeMemoryStr(testLRUCache, tmpLRUCache, actions)
print(result['msg'], '执行结果 = {}'.format(result['result']))
# 运行结果
函数 testLRUCache 的运行时间为 561.12 ms;内存使用量为 4.00 KB 执行结果 = 99
        2) 改进版一【有序字典】
采用有序字典【Python3.6之后支持】,同时支持使用顺序和保存键值对
性能卓越,超越93% 
            
            
              python
              
              
            
          
          import CheckFuncPerf as cfp
class LRUCache_ext1:
 def __init__(self, capacity):
     self.data = dict()
     self.capacity = capacity
 def get(self, key):
     keyval = self.data.get(key, -1)
     if keyval != -1:
         self.data.pop(key)
         self.data[key] = keyval
     return keyval
 def put(self, key, value)
     if key in self.data:
         self.data.pop(key)
         self.data[key] = value
     else:
         if len(self.data) < self.capacity:
             self.data[key] = value
         else:
             firstpop = next(iter(self.data))
             self.data.pop(firstpop)
             self.data[key] = value
tmpLRUCache = LRUCache_ext1(5)
result = cfp.getTimeMemoryStr(testLRUCache, tmpLRUCache, actions)
print(result['msg'], '执行结果 = {}'.format(result['result']))
# 运行结果
函数 testLRUCache 的运行时间为 420.10 ms;内存使用量为 0.00 KB 执行结果 = 99
        3) 改进版二【双向链表+字典】
采用双向链表维护使用顺序,字典保存键值对,要首先定义双向链表类
性能卓越,超过92% 
            
            
              python
              
              
            
          
          import CheckFuncPerf as cfp
class ListNodeDouble:
 def __init__(self, key=None, value=None):
     self.key = key
     self.value = value
     self.prev = None
     self.next = None
class LRUCache_ext2:
 def __init__(self, capacity):
     self.capacity = capacity
     self.dict = {}
     self.head = ListNodeDouble()
     self.tail = ListNodeDouble()
     self.head.next = self.tail
     self.tail.prev = self.head
 def move_to_tail(self, key):
     tmpnode = self.dict[key]
     tmpnode.prev.next = tmpnode.next
     tmpnode.next.prev = tmpnode.prev
     tmpnode.prev = self.tail.prev
     tmpnode.next = self.tail
     self.tail.prev.next = tmpnode
     self.tail.prev = tmpnode
 def get(self, key: int):
     if key in self.dict:
         self.move_to_tail(key)
     result = self.dict.get(key, -1)
     if result == -1:
         return result
     else:
         return result.value
 def put(self, key, value):
     if key in self.dict:
         self.dict[key].value = value
         self.move_to_tail(key)
     else:
         if len(self.dict) == self.capacity:
             self.dict.pop(self.head.next.key)
             self.head.next = self.head.next.next
             self.head.next.prev = self.head
         newkeyval = ListNodeDouble(key, value)
         self.dict[key] = newkeyval
         newkeyval.prev = self.tail.prev
         newkeyval.next = self.tail
         self.tail.prev.next = newkeyval
         self.tail.prev = newkeyval
tmpLRUCache = LRUCache_ext2(5)
result = cfp.getTimeMemoryStr(testLRUCache, tmpLRUCache, actions)
print(result['msg'], '执行结果 = {}'.format(result['result']))
# 运行结果
函数 testLRUCache 的运行时间为 787.18 ms;内存使用量为 0.00 KB 执行结果 = 99
        4. 最优算法
根据本地日志分析,最优算法为第2种方式【有序字典】 LRUCache_ext1
            
            
              python
              
              
            
          
          def testLRUCache(lrucache, actiions):
    for act in actiions:
        if len(act) > 1:
            lrucache.put(act[0], act[1])
        else:
            lrucache.get(act[0])
    return 99
import random
actions = []
iLen = 1000000
for iIdx in range(10):
    actions.append([iIdx, random.randint(1, 10)])
iturn = 0
for iIdx in range(iLen):
    if iturn >= 2:
        actions.append([random.randint(1,10)])
    else:
        actions.append([random.randint(1,10), random.randint(1, 1000)])
    iturn += 1
    if iturn >= 3:
        iturn = 0
tmpLRUCache = LRUCache_base(5)
result = cfp.getTimeMemoryStr(testLRUCache, tmpLRUCache, actions)
print(result['msg'], '执行结果 = {}'.format(result['result']))
# 算法本地速度实测比较
函数 testLRUCache 的运行时间为 561.12 ms;内存使用量为 4.00 KB 执行结果 = 99
函数 testLRUCache 的运行时间为 420.10 ms;内存使用量为 0.00 KB 执行结果 = 99
函数 testLRUCache 的运行时间为 787.18 ms;内存使用量为 0.00 KB 执行结果 = 99
        一日练,一日功,一日不练十日空
may the odds be ever in your favor ~