-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path432. All O`one Data Structure.py
81 lines (73 loc) · 2.67 KB
/
432. All O`one Data Structure.py
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
class Node:
def __init__(self, count):
self.count = count
self.keys = set()
self.prev = None
self.next = None
class AllOne:
def __init__(self):
self.key_count = {}
self.count_node = {}
self.head = Node(float('-inf'))
self.tail = Node(float('inf'))
self.head.next = self.tail
self.tail.prev = self.head
def _add_node(self, prev_node, new_node):
new_node.prev = prev_node
new_node.next = prev_node.next
prev_node.next.prev = new_node
prev_node.next = new_node
def _remove_node(self, node):
node.prev.next = node.next
node.next.prev = node.prev
del self.count_node[node.count]
def inc(self, key: str) -> None:
if key in self.key_count:
count = self.key_count[key]
self.key_count[key] += 1
self.count_node[count].keys.remove(key)
if count + 1 not in self.count_node:
new_node = Node(count + 1)
self.count_node[count + 1] = new_node
self._add_node(self.count_node[count], new_node)
self.count_node[count + 1].keys.add(key)
if not self.count_node[count].keys:
self._remove_node(self.count_node[count])
else:
self.key_count[key] = 1
if 1 not in self.count_node:
new_node = Node(1)
self.count_node[1] = new_node
self._add_node(self.head, new_node)
self.count_node[1].keys.add(key)
def dec(self, key: str) -> None:
count = self.key_count[key]
self.key_count[key] -= 1
self.count_node[count].keys.remove(key)
if self.key_count[key] == 0:
del self.key_count[key]
else:
if count - 1 not in self.count_node:
new_node = Node(count - 1)
self.count_node[count - 1] = new_node
self._add_node(self.count_node[count].prev, new_node)
self.count_node[count - 1].keys.add(key)
if not self.count_node[count].keys:
self._remove_node(self.count_node[count])
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))
# Example usage:
allOne = AllOne()
allOne.inc("hello")
allOne.inc("hello")
print(allOne.getMaxKey()) # Output: "hello"
print(allOne.getMinKey()) # Output: "hello"
allOne.inc("leet")
print(allOne.getMaxKey()) # Output: "hello"
print(allOne.getMinKey()) # Output: "leet"