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.
- Loading branch information
1 parent
5af0fa4
commit 5acf57b
Showing
2 changed files
with
80 additions
and
0 deletions.
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,75 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/lru-cache/ | ||
* Primary idea: Use Doubly linked list and hash table to build the LRU cache. | ||
* Time Complexity: O(1), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class DoublyLinkedList{ | ||
var key: Int | ||
var value: Int | ||
var previous: DoublyLinkedList? | ||
var next: DoublyLinkedList? | ||
var hashValue: Int | ||
|
||
init(_ key: Int, _ value: Int) { | ||
self.key = key | ||
self.value = value | ||
self.hashValue = key | ||
} | ||
} | ||
|
||
class LRUCache{ | ||
var maxCapacity: Int | ||
var head: DoublyLinkedList | ||
var tail: DoublyLinkedList | ||
var cache: [Int: DoublyLinkedList] | ||
|
||
init(_ maxCapacity: Int) { | ||
self.maxCapacity = maxCapacity | ||
self.cache = [Int: DoublyLinkedList]() | ||
self.head = DoublyLinkedList(Int.min, Int.min) | ||
self.tail = DoublyLinkedList(Int.max, Int.max) | ||
self.head.next = self.tail | ||
self.tail.previous = self.head | ||
} | ||
|
||
func add(_ node: DoublyLinkedList){ | ||
let next = head.next | ||
head.next = node | ||
node.previous = head | ||
node.next = next | ||
next?.previous = node | ||
} | ||
|
||
func remove(_ node: DoublyLinkedList){ | ||
let previous = node.previous | ||
let next = node.next | ||
previous?.next = next | ||
next?.previous = previous | ||
} | ||
|
||
func get(_ key: Int) -> Int{ | ||
if let node = cache[key]{ | ||
remove(node) | ||
add(node) | ||
return node.value | ||
} | ||
return -1 | ||
} | ||
|
||
func put(_ key: Int, _ value: Int){ | ||
if let node = cache[key]{ | ||
remove(node) | ||
cache.removeValue(forKey: key) | ||
}else if cache.keys.count >= maxCapacity{ | ||
if let leastNode = tail.previous{ | ||
remove(leastNode) | ||
cache.removeValue(forKey: leastNode.key) | ||
} | ||
} | ||
let newNode = DoublyLinkedList(key, value) | ||
cache[key] = newNode | ||
add(newNode) | ||
} | ||
} |
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