Python算法题集_LRU 缓存

Python算法题集_LRU 缓存

本文为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 ,则应该 逐出 最久未使用的关键字。

    函数 getput 必须以 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 <= 3000
    • 0 <= key <= 10000
    • 0 <= value <= 105
    • 最多调用 2 * 105getput

2. 题目解析

- 题意分解

  1. 本题为设计一个整形缓存类,可以指定缓存大小
  2. 基本的设计思路是采用队列控制使用次序,字典进行缓存【哈希】

- 优化思路

  1. 通常优化:减少循环层次

  2. 通常优化:增加分支,减少计算集

  3. 通常优化:采用内置算法来提升计算速度

  4. 分析题目特点,分析最优解

    1. 可以考虑采用有序字典设计缓存类

    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 ~

相关推荐
汤姆yu30 分钟前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越32 分钟前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
如何原谅奋力过但无声1 小时前
TensorFlow 1.x常用函数总结(持续更新)
人工智能·python·tensorflow
晚风吹人醒.1 小时前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件
翔云 OCR API1 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
小白程序员成长日记1 小时前
2025.11.24 力扣每日一题
算法·leetcode·职场和发展
有一个好名字1 小时前
LeetCode跳跃游戏:思路与题解全解析
算法·leetcode·游戏
AndrewHZ2 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
蓝牙先生2 小时前
简易TCP C/S通信
c语言·tcp/ip·算法
2501_941870562 小时前
Python在高并发微服务数据同步与分布式事务处理中的实践与优化
leetcode