詳解leetcode146題【LRU (最近最少使用) 快取機制】(附js最優解法!)
運用你所掌握的資料結構,設計和實現一個LRU (最近最少使用) 快取機制。它應該支援以下操作: 獲取資料 get 和 寫入資料 put 。 獲取資料 get(key) - 如果金鑰 (key) 存在於快取中,則獲取金鑰的值(總是正數),否則返回 -1。 寫入資料 put(key, value) - 如果金鑰不存在,則寫入其資料值。當快取容量達到上限時,它應該在寫入新資料之前刪除最近最少使用的資料值,從而為新的資料值留出空間。 進階: 你是否可以在 O(1) 時間複雜度內完成這兩種操作? 示例: LRUCache cache = new LRUCache( 2 /* 快取容量 */ ); cache.put(1, 1); cache.put(2, 2); cache.get(1);// 返回1 cache.put(3, 3);// 該操作會使得金鑰 2 作廢 cache.get(2);// 返回 -1 (未找到) cache.put(4, 4);// 該操作會使得金鑰 1 作廢 cache.get(1);// 返回 -1 (未找到) cache.get(3);// 返回3 cache.get(4);// 返回4 複製程式碼
解題思路:
搞清楚兩個問題就行,執行put操作的時候關注
一、快取資料的變化
分為兩種情況:
1.快取不滿
命中快取(快取中存在該值),則快取無任何變化
未命中快取(快取中不存在該值),快取中加入該值
2.快取已滿
命中快取,快取無變化
未命中快取,刪掉快取末尾的值,之後快取中加入該值
從以上分析,要想找到快取末尾的值
,我想到兩個辦法。
(1)將快取有序化(有序化涉及到排序,增加演算法複雜度,所以我不用這個方法)
(2)設定一個指標從記憶體第一個數開始跟蹤快取末尾的值
二、記憶體中增加資料時記憶體的變化
也是兩種情況 1.記憶體中不存在該值(新資料)
直接將該值置於記憶體首部
2.記憶體中已經存在該值(舊資料)
更新記憶體中值的順序,規則是將改值的前一個節點的下一個節點設定為該值的下一個節點,然後該值置於記憶體首部(基本連結串列操作)
這裡需要考慮部分特殊情況,比如記憶體為空的情況下連續執行以下操作
put(1, 1); put(1, 1); 複製程式碼
所以更新的規則要相容以上情況
執行get的時候,如果快取中存在get的資料,則更新快取順序,跟以上一樣。
程式碼:
let LRUCache = function(capacity) { this.cacheSize = capacity; // 快取計數器 this.cacheIndex = 0; this.cacheSet = new Set(); // 記憶體頭節點 this.head = null; // 快取尾節點 this.cacheShift = null; this.memory = {}; }; LRUCache.prototype.get = function(key) { let val; const { cacheSet, memory } = this; if (cacheSet.has(key)) { val = memory[key].value; console.log(memory[key].value) // get 最後一個節點 if (memory[key].next == null) { return val; } if (memory.cacheShift === memory[key] && memory.cacheShift.next) { memory.cacheShift = memory.cacheShift.next; } this.memorySort(key); } else { val = -1; console.log(-1); } return val; }; LRUCache.prototype.put = function(key, value) { const { cacheSet, memory } = this; if (this.cacheIndex < this.cacheSize) { !cacheSet.has(key) && this.cacheIndex++; cacheSet.add(key) } else { if (!cacheSet.has(key)) { cacheSet.delete(memory.cacheShift.key); memory.cacheShift.next && (memory.cacheShift = memory.cacheShift.next); cacheSet.add(key); } } // 記憶體中有值 if (memory.head) { // 記憶體中不存在該節點 if (!memory[key]) { memory[key] = { prev: memory.head, next: null } memory.head.next = memory[key]; memory.head = memory[key]; } else { // 記憶體中存在節點 if (memory.cacheShift === memory[key] && memory.cacheShift.next) { memory.cacheShift = memory[key].next; } this.memorySort(key); } } else {// 記憶體為空,該節點為第一個節點 memory[key] = { prev: null, next: null }; memory.cacheShift = memory.head = memory[key]; } memory[key].key = key; memory[key].value = value; }; LRUCache.prototype.memorySort = function(key) { const { memory } = this; // get 的不是最後一個節點 if (memory[key].next != null) { if (memory[key].prev != null) { memory[key].prev.next = memory[key].next; } else {// 第一個節點 memory[key].next.prev = null; } memory[key].next.prev = memory[key].prev; memory[key].prev = memory.head; memory[key].next = null; memory.head.next = memory[key]; memory.head = memory[key]; } } 複製程式碼
以上,是我第一感覺的做法。為什麼說是第一感覺,首先,題目要求O(1)
的複雜度,所以我不能用js
中較為方便操作的陣列。期次,不能用物件,因為用物件無法知道快取的順序。所以我只能想到連結串列的操作,一旦用到連結串列,免不了各種增刪改查的操作,所以程式碼上會複雜不少。
然而,我的一位同事的做法,讓我震驚了。如下:
class LRUCache { constructor(capacity) { this.capacity = capacity this.map = new Map() } get(key) { let val = this.map.get(key) if (typeof val === 'undefined') { return -1 } this.map.delete(key) this.map.set(key, val) return val } put(key, value) { if (this.map.has(key)) { this.map.delete(key) } this.map.set(key, value) let keys = this.map.keys() while (this.map.size > this.capacity) { this.map.delete(keys.next().value) } } } 複製程式碼
這裡他用的是map
,為什麼map
可以。這裡分析下。
map.keys().next()
可以取得到他排位第一的鍵值,map.put()
操作類似陣列的push
操作,將值儲存在最頂的位置,這兩點就是最關鍵的,也正是我沒想到的。這樣一來,就能清除的排序而且對map
的操作複雜度為O(1)
,比操作物件還快。不僅在程式碼上及其優美,演算法複雜度也是最優的。
感觸:js
為我們封裝了不少函式,熟練掌握,你就能如虎添翼。