LeetCode677. Map Sum Pairs
来源:互联网 发布:函授和网络教育区别 编辑:程序博客网 时间:2024/06/14 00:55
Analysis
Brute-force solution
Apparently, a straightforward brute-force solution would be to store every {key, value}
pair into a HashSet. And once the sum(String prefix)
function is called, we scan the whole HashSet’s key set, and use String.startswith()
to select keys that satisfiy our prefix requirement. Then we add up all the values and return.
Complexity
For each insert operation, the time complexity is O(1)
. For each sum operation, the time complexity is O(m*n)
where m
is the total number of keys being stored and n
is the length of input prefix.
The space used by the HashMap is liner to the size of all input keys and values.
Optimization1: Use a Trie to store the {key:value}
relationship
Considering that we are compare prefix, a Trie (prefix tree) is naturally appropriate for this task. So for each insert operation, we can use a Trie to store the input keys and at the end Trie node of each key, we store the value in this node.
For every sum operation, we just start from the root of the Trie and looking for the first TrieNode that has a prefix matches the given prefix. And from this node, we perform a BFS to get every key in its child branch and sum up the values.
Complexity
Each insert operation will take O(n)
time where n
is the length of the input key.
Each sum operation will take O(m*k)
time where m
is the length of the prefix and k is the number of words start with this this prefix.
The space used by the Trie is liner to the size of the total input.
Implementation
A Java implementation is showed as following:
class MapSum { class TrieNode { char curr; TrieNode[] child; boolean isEnd; int value; TrieNode(char curr) { this.curr = curr; this.child = new TrieNode[26]; } } TrieNode root; /** Initialize your data structure here. */ public MapSum() { // setup a dummy root node this.root = new TrieNode(' '); } public void insert(String key, int val) { char[] charKey = key.toCharArray(); TrieNode pointer = this.root; for (char c : charKey) { if (pointer.child[c - 'a'] == null) { TrieNode curr = new TrieNode(c); pointer.child[c - 'a'] = curr; } pointer = pointer.child[c - 'a']; } pointer.isEnd = true; pointer.value = val; } public int sum(String prefix) { int ret = 0; if (prefix == null || prefix.isEmpty()) { return ret; } char[] charPrefix = prefix.toCharArray(); TrieNode pointer = this.root; for (char c : charPrefix) { if (pointer.child[c - 'a'] == null) { return ret; } pointer = pointer.child[c - 'a']; } Queue<TrieNode> queue = new LinkedList<TrieNode>(); queue.offer(pointer); while (!queue.isEmpty()) { TrieNode curr = queue.poll(); ret += curr.value; for (TrieNode t : curr.child) { if (t == null) { continue; } queue.offer(t); } } return ret; }}
Optimization2: Store the sum directly
Actually there is still some unnecessary work we have done in the above approach. Since we only cares about the prefix sum rather than the exact value that is related to this key, we do not need to store the values corresponding to each input key. Instead, we can store the sum
of current prefix in the Trie. In this way, the insert is still the same with the above process, but during the insert process, rather than only store the value at the end TrieNode of current key, we store the sum of every prefix along the way.
Complexity
The time complexity of insert operation is O(n)
where n
is the length of the input key.
The time complexity of sum operation is O(m)
where m
is the length of the input prefix.
The space used is still linear to the total input size.
Implementation
A Java implementation is showed as following:
class MapSum { class TrieNode { TrieNode[] child = new TrieNode[26]; int val; } HashMap<String, Integer> map; TrieNode root; /** Initialize your data structure here. */ public MapSum() { this.map = new HashMap<>(); this.root = new TrieNode(); } public void insert(String key, int val) { int delta = val - map.getOrDefault(key, 0); map.put(key, val); TrieNode curr = this.root; curr.val += delta; for (char c : key.toCharArray()) { if (curr.child[c - 'a'] == null) { curr.child[c - 'a'] = new TrieNode(); curr.child[c - 'a'].val = delta; } else { curr.child[c - 'a'].val += delta; } curr = curr.child[c - 'a']; } } public int sum(String prefix) { if (prefix == null || prefix.isEmpty()) { return 0; } TrieNode curr = this.root; for (char c : prefix.toCharArray()) { if (curr.child[c - 'a'] == null) { return 0; } curr = curr.child[c - 'a']; } return curr.val; }}
- LeetCode677. Map Sum Pairs
- 677. Map Sum Pairs
- 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- 677. Map Sum Pairs(JAVA)
- leetcode 677. Map Sum Pairs
- LeetCode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- Map Sum Pairs问题及解法
- LWC 50:677. Map Sum Pairs
- 第14周Map Sum Pairs
- 未完成--Sum of Pairs
- Rust : codewars的Sum of Pairs
- pairs
- pairs
- Two Sum 的map函数
- Incorrect inline parameter map format (missmatched name=value pairs)
- Segue回传数据
- 安装Windows + Linux Ubuntu双系统
- 1039. 到底买不买(20)
- php+ajax实现web客服系统
- 如何使用python计算2个毫秒级字符串的计算
- LeetCode677. Map Sum Pairs
- 云监控插件安装指南
- 白话阿里巴巴Java开发手册高级篇
- Python Request库小实例
- MySQL存储引擎--MyISAM与InnoDB区别
- 自然语言处理Word2Vec词向量模型
- springboot分布式事务管理(atomikos)
- Swift工程中引用OC文件
- InputStream To String