Created
July 6, 2016 17:59
-
-
Save Zhang/7c7d19b0c66adf53c3f7747ee0fa0229 to your computer and use it in GitHub Desktop.
Hang text screen
This file contains hidden or 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
5 | |
/ \ | |
3 8 | |
/ \ / \ | |
2 4 6 9 | |
\ | |
7 | |
def nextLargest(node): | |
if it does not have a right child node: | |
recursively go to parent until reach a larger node | |
parent = node.parent | |
while True and node.parent: | |
if parent.left == node: | |
return parent | |
else: | |
node = node.parent | |
if it has right child node: | |
it should go to the right child node | |
while right_node.left: | |
right_node = right_node.left | |
return right_node | |
class LinkedNode(object): | |
class LRUCache(object): | |
def __init__(self, capacity): | |
self.capacity = capacity | |
self.cache = {} | |
self.key_node = {} | |
self.head = LinkedNode(None) | |
self.tail = LinkedNode(None) | |
def set(self, key, value): | |
if len(self.cache) >= self.capacity: | |
last_recent = self.head.key | |
del self.cache[last_recent] | |
del self.key_node[last_recent] | |
temp = self.head.next | |
self.head.next = None | |
temp.pre = None | |
self.cache[key] = value | |
node = LinkedNone(key) | |
self.tail.next = node | |
node.pre = self.tail | |
self.tail = node | |
self.key_node[key] = node | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment