Map Sum Pairs
Master this topic with zero to advance depth.
Map Sum Pairs
Calculate sum of all values whose key starts with a specific prefix.
Approach 1
Level III: Trie with Pre-calculated Sums (Optimal)
Intuition
Each trie node stores the sum of values for all words passing through it. When updating a key, we first find its old value to calculate the delta and update all nodes in the path.
⏱ Insert: O(L), Sum: O(L).💾 O(N * L * 26).
Detailed Dry Run
Insert('apple', 3): Nodes a,p,p,l,e each get +3 sum. Insert('app', 2): Nodes a,p,p get +2. Sum('ap') -> 5.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.