File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
432-all-oone-data-structure Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ class AllOne :
2
+
3
+ def __init__ (self ):
4
+ self .freq = {}
5
+
6
+ def inc (self , key : str ) -> None :
7
+ self .freq [key ] = self .freq .get (key , 0 ) + 1
8
+
9
+ def dec (self , key : str ) -> None :
10
+ self .freq [key ] = self .freq .get (key , 0 ) - 1
11
+ if self .freq [key ] == 0 :
12
+ del self .freq [key ]
13
+
14
+ def getMaxKey (self ) -> str :
15
+ count , res = 0 , ""
16
+ for k , v in self .freq .items ():
17
+ if v > count :
18
+ count = v
19
+ res = k
20
+ return res
21
+
22
+ def getMinKey (self ) -> str :
23
+ count , res = inf , ""
24
+ for k , v in self .freq .items ():
25
+ if v < count :
26
+ count = v
27
+ res = k
28
+ return res
29
+
30
+
31
+ # Your AllOne object will be instantiated and called as such:
32
+ # obj = AllOne()
33
+ # obj.inc(key)
34
+ # obj.dec(key)
35
+ # param_3 = obj.getMaxKey()
36
+ # param_4 = obj.getMinKey()
You can’t perform that action at this time.
0 commit comments