1
from collections import defaultdict16
self._dict[v] = self._len19
def __contains__(self, item):20
return item in self._dict23
if v not in self._dict:28
if idx < self._len - 1:29
self._list[idx] = self._list[-1]30
self._dict[self._list[idx]] = idx37
return iter(self._list)40
assert len(self._dict) == len(self._list)41
for idx, key in enumerate(self._list):42
assert self._dict[key] == idx48
return f"{self._dict}\n{self._list}"53
def __init__(self, value):58
def set_prev(self, other_node):59
self.prev = other_node60
if other_node is not None:61
other_node.next = self63
def set_next(self, other_node):64
self.next = other_node65
if other_node is not None:66
other_node.prev = self69
return str(self.value)72
class SortedKeyList(object):75
self.head = Node(value=0)76
self.tail = Node(value=float("inf"))77
self.head.set_next(self.tail)80
return self.head.next is self.tail86
return self.head.next.value92
return self.tail.prev.value94
def incr_key(self, orig_node):95
orig_value = orig_node.value96
new_value = orig_value + 197
next_node = orig_node.next98
if next_node.value == new_value:101
new_node = Node(new_value)102
new_node.set_prev(orig_node)103
new_node.set_next(next_node)106
def decr_key(self, orig_node):107
orig_value = orig_node.value108
new_value = orig_value - 1109
prev_node = orig_node.prev110
if prev_node.value == new_value:113
new_node = Node(new_value)114
new_node.set_next(orig_node)115
new_node.set_prev(prev_node)118
def delete_node(self, node):119
prev_node = node.prev120
next_node = node.next121
prev_node.set_next(next_node)126
node = self.head.next128
if node is self.tail:131
assert node.next.prev is node132
assert node.prev.next is node142
self.count_list = SortedKeyList()143
self.counter = defaultdict(lambda: self.count_list.head)144
self.keyed_by_count = defaultdict(set)147
return f"count_list={self.count_list}\ncounter={dict(self.counter)}\nkeyed_by_count={dict(self.keyed_by_count)}"149
def inc(self, key: str) -> None:150
orig_count_node = self.counter[key]151
orig_count = orig_count_node.value153
self.count_list.incr_key(orig_count_node)154
new_count_node = orig_count_node.next155
new_count = new_count_node.value156
assert new_count == orig_count + 1158
self.counter[key] = new_count_node160
if key in self.keyed_by_count[orig_count]:161
self.keyed_by_count[orig_count].remove(key)162
self.keyed_by_count[new_count].add(key)164
if len(self.keyed_by_count[orig_count]) == 0:165
del self.keyed_by_count[orig_count]166
if orig_count_node.value > 0:167
self.count_list.delete_node(orig_count_node)169
def dec(self, key: str) -> None:170
orig_count_node = self.counter[key]171
orig_count = orig_count_node.value173
self.count_list.decr_key(orig_count_node)174
new_count_node = orig_count_node.prev175
new_count = new_count_node.value176
assert new_count == orig_count - 1178
self.counter[key] = new_count_node180
if key in self.keyed_by_count[orig_count]:181
self.keyed_by_count[orig_count].remove(key)182
self.keyed_by_count[new_count].add(key)185
del self.counter[key]187
if len(self.keyed_by_count[orig_count]) == 0:188
del self.keyed_by_count[orig_count]189
self.count_list.delete_node(orig_count_node)191
def getMaxKey(self) -> str:192
max_count = self.count_list.max_key()193
if max_count is not None:194
return next(iter(self.keyed_by_count[max_count]))198
def getMinKey(self) -> str:199
min_count = self.count_list.min_key()200
if min_count is not None:201
return next(iter(self.keyed_by_count[min_count]))209
naive_counter = defaultdict(int)211
for method, param in zip(m, p):212
if method in ("inc", "dec"):217
naive_counter[word] += 1219
elif method == "dec":221
naive_counter[word] -= 1223
if naive_counter[word] == 0:224
del naive_counter[word]226
tmp_counter = defaultdict(set)227
for k, v in naive_counter.items():228
tmp_counter[v].add(k)230
sorted_keys = sorted(tmp_counter.keys())231
min_key = sorted_keys[0]232
max_key = sorted_keys[-1]234
if s.getMaxKey() not in tmp_counter[max_key]:236
return s, naive_counter, tmp_counter238
if s.getMinKey() not in tmp_counter[min_key]:240
return s, naive_counter, tmp_counter242
return None, None, None245
if __name__ == "__main__":256
p = [[], ["hello"], ["hello"], [], [], ["leet"], [], []]257
s, naive_counter, tmp_counter = drive(m, p)