-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2349-design-a-number-container-system.py
More file actions
37 lines (29 loc) · 945 Bytes
/
2349-design-a-number-container-system.py
File metadata and controls
37 lines (29 loc) · 945 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
35
36
37
# time complexity: O(logn)
# space complexity: O(n)
from collections import defaultdict
from sortedcontainers import SortedSet
class NumberContainers:
def __init__(self):
self.numDict = defaultdict(SortedSet)
self.idxDict = defaultdict(int)
def change(self, index: int, number: int) -> None:
if index in self.idxDict:
lastNum = self.idxDict[index]
self.numDict[lastNum].remove(index)
if not self.numDict[lastNum]:
del self.numDict[lastNum]
self.idxDict[index] = number
self.numDict[number].add(index)
def find(self, number: int) -> int:
if number in self.numDict and self.numDict[number]:
return self.numDict[number][0]
return -1
obj = NumberContainers()
print(obj.find(10))
obj.change(2, 10)
obj.change(1, 10)
obj.change(3, 10)
obj.change(5, 10)
print(obj.find(10))
obj.change(1, 20)
print(obj.find(10))