【每天一道算法题(75/150)】LRU 缓存
思路:一道中等写一下午
首先,LRU 是最近最少使用算法,也被称为页面置换算法。是怎么实现的呢。给每一个页面一个访问字段,记为 T,T 为这个页面自上次被访问以来所经历的时间。当必须要淘汰一个页面时,找出所有 T 中最大的,即最少使用的那个页面淘汰。
其次,题目要求所实现的 LRU 算法的类(后文简称 cache)的 get 和 put 方法的时间复杂度为 o(1)。
那么问题来了,get 还好说,map 的 get 方法时间复杂度本身就为 o(1),put 方法难在需要:
1. 先找到最大的 T
2. 将新的 key, value 放入 cache
那么无论是哈希表,还是链表,双向链表,有序链表及有序双向链表都无法做到时间复杂度为 0(1),除非
哈希表 + 双向链表。
下面操作步骤讲讲双向链表的 get 方法:
操作步骤:
put [1,1]
put[2,2]
get[1]
初始化双向链表为:
head <-> tail
第一步:put [1,1]
head <-> (1,1) <-> tail
第二步:put [2,2]
head <-> (2,2) <-> (1,1) <-> tail
第三步:get [1]
a. 先通过哈希表找到链表所在位置,返回 node
b. 在链表中将 [1,1] 移除
此时链表状态:head <-> (2,2) <-> tail
c. 在链表中将 [1,1] 插入到链表头部
此时链表状态:had <-> (1,1) <-> (2,2) <-> tail
返回 node.val = 1;
经过上述操作,把 get 的 Key 的 value 返回了,链表也更新了,保证了最新使用节点(页面)的在最前面(链表头部)。
思路:一道中等写一下午
首先,LRU 是最近最少使用算法,也被称为页面置换算法。是怎么实现的呢。给每一个页面一个访问字段,记为 T,T 为这个页面自上次被访问以来所经历的时间。当必须要淘汰一个页面时,找出所有 T 中最大的,即最少使用的那个页面淘汰。
其次,题目要求所实现的 LRU 算法的类(后文简称 cache)的 get 和 put 方法的时间复杂度为 o(1)。
那么问题来了,get 还好说,map 的 get 方法时间复杂度本身就为 o(1),put 方法难在需要:
1. 先找到最大的 T
2. 将新的 key, value 放入 cache
那么无论是哈希表,还是链表,双向链表,有序链表及有序双向链表都无法做到时间复杂度为 0(1),除非
哈希表 + 双向链表。
下面操作步骤讲讲双向链表的 get 方法:
操作步骤:
put [1,1]
put[2,2]
get[1]
初始化双向链表为:
head <-> tail
第一步:put [1,1]
head <-> (1,1) <-> tail
第二步:put [2,2]
head <-> (2,2) <-> (1,1) <-> tail
第三步:get [1]
a. 先通过哈希表找到链表所在位置,返回 node
b. 在链表中将 [1,1] 移除
此时链表状态:head <-> (2,2) <-> tail
c. 在链表中将 [1,1] 插入到链表头部
此时链表状态:had <-> (1,1) <-> (2,2) <-> tail
返回 node.val = 1;
经过上述操作,把 get 的 Key 的 value 返回了,链表也更新了,保证了最新使用节点(页面)的在最前面(链表头部)。
#早安心语##外国文学#
THE WIND AND THE LEAVES
风儿和树叶
作者:乔治·库柏
"Come, little leaves,"
said the wind one day.
一天,风儿说:
“过来,小叶子
"Come o'er the meadows with me,
and play;
和我一起去牧场玩吧
Put on your dress
of red and gold
穿上你红色金色的裙子
Summer is gone,
and the days grow cold."
夏天过去
天气变凉了
Soon as the leaves
heard the wind's loud call,
叶子听到风儿大声召唤
Down they came fluttering,
one and all;
所有的叶子都落了下来
Over the brown fields
they danced and flew,
在棕色的原野上
它们飞舞着
Singing the soft little songs
they knew.
唱着它们会唱的柔美的歌
"Cricket, good-by,
we've been friends so long;
蟋蟀,再见了
我们是老朋友
Little brook, sing us
your farewell song, —
小溪为我们唱着告别曲
Say you are sorry
to see us go
看到我们离开很难过
Ah! you will miss us,
right well we know.
啊,你要离开我们了
我们知道
"Dear little lambs,
in your fleecy fold,
“亲爱的小羊羔
在毛茸茸的羊群里
Mother will keep you
from harm and cold;
羊妈妈会保护你们
不受伤、不着凉
Fondly we've watched you
in vale and glade;
我们会在山谷和林间
深情地望着你们
Say, will you dream
of our loving shade?"
告诉我
你们会梦到我们美好的荫凉吗?”
Dancing and whirling,
the little leaves went;
飞舞着,旋转着
小叶子走了
Winter had called them,
and they were content.
冬天在召唤
它们心甘情愿
Soon fast asleep
in their earthy beds,
它们很快入睡
大地为床
The snow laid a coverlet
over their heads.
雪花为被
轻轻盖在它们的头上
— E·N·D —
THE WIND AND THE LEAVES
风儿和树叶
作者:乔治·库柏
"Come, little leaves,"
said the wind one day.
一天,风儿说:
“过来,小叶子
"Come o'er the meadows with me,
and play;
和我一起去牧场玩吧
Put on your dress
of red and gold
穿上你红色金色的裙子
Summer is gone,
and the days grow cold."
夏天过去
天气变凉了
Soon as the leaves
heard the wind's loud call,
叶子听到风儿大声召唤
Down they came fluttering,
one and all;
所有的叶子都落了下来
Over the brown fields
they danced and flew,
在棕色的原野上
它们飞舞着
Singing the soft little songs
they knew.
唱着它们会唱的柔美的歌
"Cricket, good-by,
we've been friends so long;
蟋蟀,再见了
我们是老朋友
Little brook, sing us
your farewell song, —
小溪为我们唱着告别曲
Say you are sorry
to see us go
看到我们离开很难过
Ah! you will miss us,
right well we know.
啊,你要离开我们了
我们知道
"Dear little lambs,
in your fleecy fold,
“亲爱的小羊羔
在毛茸茸的羊群里
Mother will keep you
from harm and cold;
羊妈妈会保护你们
不受伤、不着凉
Fondly we've watched you
in vale and glade;
我们会在山谷和林间
深情地望着你们
Say, will you dream
of our loving shade?"
告诉我
你们会梦到我们美好的荫凉吗?”
Dancing and whirling,
the little leaves went;
飞舞着,旋转着
小叶子走了
Winter had called them,
and they were content.
冬天在召唤
它们心甘情愿
Soon fast asleep
in their earthy beds,
它们很快入睡
大地为床
The snow laid a coverlet
over their heads.
雪花为被
轻轻盖在它们的头上
— E·N·D —
#金地雄语音鉴定书##金地雄##金地雄澄清#不看澄清继续冤枉人是你的武器吗?投我那一够加那 那加咪那 内呢gin 木漏 打啦哇Put you on my back seatBack seat Back seat uh woo uh一 gi不露内泼You don't have to fight 不漏哥我哈几马Put you on my back seat Back seat Back seatNow let's get it on on my back seat
✋热门推荐