forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request soapyigu#237 from WAMaker/master
[LinkedList] Add a solution to LRU Cache
- Loading branch information
Showing
3 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/lfu-cache/ | ||
* Primary idea: Use linked list and hash map to build the cache. | ||
* Time Complexity Per Action: O(1), Space Complexity: O(K) | ||
* | ||
*/ | ||
|
||
class LFUCache { | ||
|
||
private let capacity: Int | ||
private var nodeMap = [Int: CacheNode]() | ||
private var listMap = [Int: CacheList]() | ||
private var size = 0 | ||
private var leastFrequency = 1 | ||
|
||
init(_ capacity: Int) { | ||
self.capacity = capacity | ||
} | ||
|
||
func get(_ key: Int) -> Int { | ||
guard let node = nodeMap[key], let list = listMap[node.count] else { | ||
return -1 | ||
} | ||
updateExsit(node: node) | ||
return node.val | ||
} | ||
|
||
func put(_ key: Int, _ value: Int) { | ||
if capacity == 0 { | ||
return | ||
} | ||
|
||
if let node = nodeMap[key], let list = listMap[node.count] { | ||
node.val = value | ||
updateExsit(node: node) | ||
} else { | ||
removeCacheIfNeeded() | ||
|
||
let node = CacheNode(key, value) | ||
nodeMap[key] = node | ||
listMap(add: node) | ||
|
||
size += 1 | ||
leastFrequency = 1 | ||
} | ||
} | ||
|
||
private func updateExsit(node: CacheNode) { | ||
guard let list = listMap[node.count] else { | ||
return | ||
} | ||
list.remove(node) | ||
|
||
if list.isEmpty { | ||
listMap[node.count] = nil | ||
|
||
if leastFrequency == node.count { | ||
leastFrequency += 1 | ||
} | ||
} | ||
|
||
node.count += 1 | ||
listMap(add: node) | ||
} | ||
|
||
private func removeCacheIfNeeded() { | ||
guard size >= capacity, let list = listMap[leastFrequency], let key = list.removeLast()?.key else { | ||
return | ||
} | ||
size -= 1 | ||
nodeMap[key] = nil | ||
if list.isEmpty { | ||
listMap[leastFrequency] = nil | ||
} | ||
} | ||
|
||
private func listMap(add node: CacheNode) { | ||
let list = listMap[node.count, default: CacheList()] | ||
list.add(node) | ||
listMap[node.count] = list | ||
} | ||
|
||
} | ||
|
||
class CacheList { | ||
|
||
private let head = CacheNode(0, 0) | ||
private let tail = CacheNode(0, 0) | ||
private var count = 0 | ||
|
||
var isEmpty: Bool { | ||
return count <= 0 | ||
} | ||
|
||
init() { | ||
head.next = tail | ||
tail.pre = head | ||
} | ||
|
||
func add(_ node: CacheNode) { | ||
head.next?.pre = node | ||
node.next = head.next | ||
node.pre = head | ||
head.next = node | ||
count += 1 | ||
} | ||
|
||
|
||
func remove(_ node: CacheNode) { | ||
node.pre?.next = node.next | ||
node.next?.pre = node.pre | ||
node.next = nil | ||
node.pre = nil | ||
count -= 1 | ||
} | ||
|
||
func removeLast() -> CacheNode? { | ||
guard !isEmpty, let node = tail.pre else { | ||
return nil | ||
} | ||
remove(node) | ||
return node | ||
} | ||
|
||
} | ||
|
||
class CacheNode { | ||
let key: Int | ||
var val: Int | ||
var pre: CacheNode? | ||
var next: CacheNode? | ||
var count = 1 | ||
|
||
init(_ key: Int, _ val: Int) { | ||
self.key = key | ||
self.val = val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/lru-cache/ | ||
* Primary idea: Use linked list and hash map to build the cache. | ||
* Time Complexity Per Action: O(1), Space Complexity: O(K) | ||
* | ||
*/ | ||
|
||
class LRUCache { | ||
|
||
private let capacity: Int | ||
private var count = 0 | ||
|
||
private let head = LRUCacheNode(0, 0) | ||
private let tail = LRUCacheNode(0, 0) | ||
|
||
private var dict = [Int: LRUCacheNode]() | ||
|
||
init(_ capacity: Int) { | ||
self.capacity = capacity | ||
|
||
head.next = tail | ||
tail.pre = head | ||
} | ||
|
||
func get(_ key: Int) -> Int { | ||
if let node = dict[key] { | ||
remove(key) | ||
insert(node) | ||
return node.val | ||
} | ||
return -1 | ||
} | ||
|
||
func put(_ key: Int, _ value: Int) { | ||
if let node = dict[key] { | ||
node.val = value | ||
remove(key) | ||
insert(node) | ||
return | ||
} | ||
|
||
let node = LRUCacheNode(key, value) | ||
dict[key] = node | ||
if count == capacity, let tailKey = tail.pre?.key { | ||
remove(tailKey) | ||
} | ||
insert(node) | ||
} | ||
|
||
private func insert(_ node: LRUCacheNode) { | ||
dict[node.key] = node | ||
|
||
node.next = head.next | ||
head.next?.pre = node | ||
node.pre = head | ||
head.next = node | ||
|
||
count += 1 | ||
} | ||
|
||
private func remove(_ key: Int) { | ||
guard count > 0, let node = dict[key] else { | ||
return | ||
} | ||
dict[key] = nil | ||
|
||
node.pre?.next = node.next | ||
node.next?.pre = node.pre | ||
node.pre = nil | ||
node.next = nil | ||
|
||
count -= 1 | ||
} | ||
|
||
} | ||
|
||
fileprivate class LRUCacheNode { | ||
|
||
let key: Int | ||
var val: Int | ||
|
||
var pre: LRUCacheNode? | ||
var next: LRUCacheNode? | ||
|
||
init(_ key: Int, _ val: Int) { | ||
self.key = key | ||
self.val = val | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters