-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall-oone-data-structure.py
More file actions
110 lines (95 loc) · 3.66 KB
/
all-oone-data-structure.py
File metadata and controls
110 lines (95 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class Node:
def __init__(self, freq):
self.freq = freq
self.prev = None
self.next = None
self.keys = set()
class AllOne:
def __init__(self):
"""
Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the AllOne class:
AllOne() Initializes the object of the data structure.
inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1.
dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "".
getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".
Note that each function must run in O(1) average time complexity.
"""
self.head = Node(0)
self.tail = Node(0)
self.head.next = self.tail
self.tail.prev = self.head
self.map = {}
def inc(self, key: str) -> None:
if key in self.map:
node = self.map[key]
freq = node.freq
node.keys.remove(key)
nextNode = node.next
if nextNode == self.tail or nextNode.freq != freq + 1:
newNode = Node(freq + 1)
newNode.keys.add(key)
newNode.prev = node
newNode.next = nextNode
node.next = newNode
nextNode.prev = newNode
self.map[key] = newNode
else:
nextNode.keys.add(key)
self.map[key] = nextNode
if not node.keys:
self.removeNode(node)
else:
firstNode = self.head.next
if firstNode == self.tail or firstNode.freq > 1:
newNode = Node(1)
newNode.keys.add(key)
newNode.prev = self.head
newNode.next = firstNode
self.head.next = newNode
firstNode.prev = newNode
self.map[key] = newNode
else:
firstNode.keys.add(key)
self.map[key] = firstNode
def dec(self, key: str) -> None:
if key not in self.map:
return
node = self.map[key]
node.keys.remove(key)
freq = node.freq
if freq == 1:
del self.map[key]
else:
prevNode = node.prev
if prevNode == self.head or prevNode.freq != freq - 1:
newNode = Node(freq - 1)
newNode.keys.add(key)
newNode.prev = prevNode
newNode.next = node
prevNode.next = newNode
node.prev = newNode
self.map[key] = newNode
else:
prevNode.keys.add(key)
self.map[key] = prevNode
if not node.keys:
self.removeNode(node)
def getMaxKey(self) -> str:
if self.tail.prev == self.head:
return ""
return next(
iter(self.tail.prev.keys)
)
def getMinKey(self) -> str:
if self.head.next == self.tail:
return ""
return next(
iter(self.head.next.keys)
)
def removeNode(self, node):
prevNode = node.prev
nextNode = node.next
prevNode.next = nextNode
nextNode.prev = prevNode