-
Notifications
You must be signed in to change notification settings - Fork 43
/
map-sum-pairs.py
166 lines (143 loc) · 3.76 KB
/
map-sum-pairs.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# V0
# V1
# http://bookshadow.com/weblog/2017/09/17/leetcode-map-sum-pairs/
class TrieNode:
# Initialize your data structure here.
def __init__(self):
self.children = dict()
self.sum = 0
self.val = 0
class MapSum(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
def insert(self, key, val):
"""
:type key: str
:type val: int
:rtype: void
"""
node = self.root
nodes = []
for letter in key:
child = node.children.get(letter)
if child is None:
child = TrieNode()
node.children[letter] = child
nodes.append(child)
node = child
if node.val != val:
delta = val - node.val
node.val = val
for node in nodes:
node.sum += delta
def sum(self, prefix):
"""
:type prefix: str
:rtype: int
"""
node = self.root
for letter in prefix:
node = node.children.get(letter)
if node is None: return 0
return node.sum
# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)
# V1'
# https://www.jiuzhang.com/solution/map-sum-pairs/#tag-highlight-lang-python
class TrieNode:
def __init__(self):
self.son = {}
self.val = 0
class Trie:
root = TrieNode()
def insert(self, s, val):
cur = self.root
for i in range(0, len(s)):
if s[i] not in cur.son:
cur.son[s[i]] = TrieNode()
cur = cur.son[s[i]]
cur.val += val
def find(self, s):
cur = self.root
for i in range(0, len(s)):
if s[i] not in cur.son:
return 0
cur = cur.son[s[i]]
return cur.val
class MapSum:
def __init__(self):
"""
Initialize your data structure here.
"""
self.d = {}
self.trie = Trie()
def insert(self, key, val):
"""
:type key: str
:type val: int
:rtype: void
"""
if key in self.d:
self.trie.insert(key, val - self.d[key])
else:
self.trie.insert(key, val)
self.d[key] = val
def sum(self, prefix):
"""
:type prefix: str
:rtype: int
"""
return self.trie.find(prefix)
# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)
# V2
# Time: O(n), n is the length of key
# Space: O(t), t is the number of nodes in trie
import collections
class MapSum(object):
def __init__(self):
"""
Initialize your data structure here.
"""
_trie = lambda: collections.defaultdict(_trie)
self.__root = _trie()
def insert(self, key, val):
"""
:type key: str
:type val: int
:rtype: void
"""
# Time: O(n)
curr = self.__root
for c in key:
curr = curr[c]
delta = val
if "_end" in curr:
delta -= curr["_end"]
curr = self.__root
for c in key:
curr = curr[c]
if "_count" in curr:
curr["_count"] += delta
else:
curr["_count"] = delta
curr["_end"] = val
def sum(self, prefix):
"""
:type prefix: str
:rtype: int
"""
# Time: O(n)
curr = self.__root
for c in prefix:
if c not in curr:
return 0
curr = curr[c]
return curr["_count"]