forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-a-number-container-system.py
More file actions
34 lines (28 loc) · 939 Bytes
/
Copy pathdesign-a-number-container-system.py
File metadata and controls
34 lines (28 loc) · 939 Bytes
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
# Time: ctor: O(1)
# change: O(logn)
# find: O(1)
# Space: O(n)
from sortedcontainers import SortedList
# sorted list
class NumberContainers(object):
def __init__(self):
self.__idx_to_num = {}
self.__num_to_idxs = collections.defaultdict(SortedList)
def change(self, index, number):
"""
:type index: int
:type number: int
:rtype: None
"""
if index in self.__idx_to_num:
self.__num_to_idxs[self.__idx_to_num[index]].remove(index)
if not self.__num_to_idxs[self.__idx_to_num[index]]:
del self.__num_to_idxs[self.__idx_to_num[index]]
self.__idx_to_num[index] = number
self.__num_to_idxs[number].add(index)
def find(self, number):
"""
:type number: int
:rtype: int
"""
return self.__num_to_idxs[number][0] if number in self.__num_to_idxs else -1