Skip to content

Instantly share code, notes, and snippets.

@Zhang
Created July 6, 2016 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zhang/7c7d19b0c66adf53c3f7747ee0fa0229 to your computer and use it in GitHub Desktop.
Save Zhang/7c7d19b0c66adf53c3f7747ee0fa0229 to your computer and use it in GitHub Desktop.
Hang text screen
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