-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path677 Map Sum Pairs.py
128 lines (94 loc) · 3.4 KB
/
677 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
#!/usr/bin/python3
"""
Implement a MapSum class with insert, and sum methods.
For the method insert, you'll be given a pair of (string, integer). The string
represents the key and the integer represents the value. If the key already
existed, then the original key-value pair will be overridden to the new one.
For the method sum, you'll be given a string representing the prefix, and you
need to return the sum of all the pairs' value whose key starts with the prefix.
Example 1:
Input: insert("apple", 3), Output: Null
Input: sum("ap"), Output: 3
Input: insert("app", 2), Output: Null
Input: sum("ap"), Output: 5
"""
class MapSum:
def __init__(self):
"""
Initialize your data structure here.
Trie
update using delta
"""
from collections import defaultdict
class TrieNode:
def __init__(self, chr, sum, val):
self.chr = chr
self.sum = sum
self.val = val
self.children = defaultdict(lambda: None)
class Trie:
def __init__(self):
self.root = TrieNode(None, 0, 0) # dummy root
def insert(self, cur, key, i, val):
if not cur:
cur = TrieNode(key[i], 0, 0)
if i == len(key) - 1:
delta = val - cur.val
cur.val = val
else:
cur.children[key[i+1]], delta = self.insert(cur.children[key[i+1]], key, i + 1, val)
cur.sum += delta
return cur, delta
self.trie = Trie()
def insert(self, key: str, val: int) -> None:
root = self.trie.root
root.children[key[0]], _ = self.trie.insert(root.children[key[0]], key, 0, val)
def sum(self, prefix: str) -> int:
node = self.trie.root
for a in prefix:
if a not in node.children:
return 0
node = node.children[a]
return node.sum
class MapSum2:
def __init__(self):
"""
Initialize your data structure here.
Trie
update using delta
"""
class TrieNode:
def __init__(self, chr, sum, val):
self.chr = chr
self.sum = sum
self.val = val
self.children = {}
class Trie:
def __init__(self):
self.root = TrieNode(None, 0, 0) # dummy root
def insert(self, pi, key, i, val):
if key[i] not in pi.children:
cur = TrieNode(key[i], 0, 0)
pi.children[key[i]] = cur
cur = pi.children[key[i]]
if i + 1 < len(key):
cur.children[key[i+1]], delta = self.insert(cur, key, i + 1, val)
else:
delta = val - cur.val
cur.val = val
cur.sum += delta
return cur, delta
self.trie = Trie()
def insert(self, key: str, val: int) -> None:
self.trie.insert(self.trie.root, key, 0, val)
def sum(self, prefix: str) -> int:
node = self.trie.root
for a in prefix:
if a not in node.children:
return 0
node = node.children[a]
return node.sum
# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)