-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution677.cs
60 lines (54 loc) · 1.73 KB
/
Solution677.cs
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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution677
{
/// <summary>
/// 677. Map Sum Pairs - Medium
/// <a href="https://leetcode.com/problems/map-sum-pairs">See the problem</a>
/// </summary>
public class MapSum
{
private TrieNode root = new();
private Dictionary<string, int> map = [];
/** Insert key-val pair into the Trie */
public void Insert(string key, int val)
{
var delta = val;
if (map.ContainsKey(key))
{
delta -= map[key]; // If key already exists, adjust by the old value
}
map[key] = val; // Update the key-value pair
var node = root;
foreach (var c in key)
{
if (!node.children.ContainsKey(c))
{
node.children[c] = new();
}
node = node.children[c];
node.value += delta; // Update the cumulative sum for this node
}
}
/** Returns the sum of all keys with the given prefix */
public int Sum(string prefix)
{
var node = root;
foreach (var c in prefix)
{
if (!node.children.ContainsKey(c))
{
return 0; // If the prefix doesn't exist, return 0
}
node = node.children[c];
}
return node.value; // Return the cumulative sum for this prefix
}
// Trie Node structure
class TrieNode
{
public Dictionary<char, TrieNode> children = [];
public int value; // Cumulative sum for this node
}
}
}