§Lec 1 - Trie / Prefix Tree
§Materials
https://en.wikipedia.org/wiki/Trie
https://www.geeksforgeeks.org/trie-insert-and-search/
§Tries
-
Variable length keys
-
The decision on what path to follow is taken based on portion of the key
-
Static environment, fast retrieval but large space overhead
-
Applications: dictionaries, text searching
-
Trade space for runtime
-
Costly operation to build Trie: assume updating or rebuilding is rare
§Problem
§Implement Trie (Prefix Tree)
-
Use Array/HashMap
1
2
3
4
5
6
7
8
9
10
11class TrieNode{
//boolean isWord;
//String word; //pointer to word
List<String> words; //contains pointers to passing words
//TrieNode[] children;
Map<Character, TrieNode> children;
public TrieNode(){
this.children = new HashMap<>();
}
} -
In trie implementation, we should confirm that we have frequent search but less frequent add operation
-
谷歌变种
- compress: apply, apple; a->p->p->l->… => app->…
- compound word: cat, copy, copycat; root->cat, root->copy->cat=> copy -back-> cat
§Longest Word in Dictionary
1 | int index = -1; //4bits, save space than pointer(use string, 4bits-32 / 8bits-64) |
§Runtime
Trie1, boolean isWord, 需要StringBuilder,平均每个node有k child,有l层
Tire2, Stirng word,same runtime
Tire3,List
§Prefix and Suffix Search
1 | apple |
1 | space: |
https://leetcode.com/problems/map-sum-pairs/
https://leetcode.com/problems/word-search-ii/
§Lec 2/3 - Graph: Graph Search, Union Find & Topological Sort
§Materials
Graph: Data Structure and Algorithms
§Graph
edges, Adjacency Matrix, Adjacency List (use map)
§Problem
§谷歌难题:maximum number of edges in graph without a cycle
undirected graph, n vertexes: n-1
directed graph, n vertexes: n-1 + n-2 + … + 1 = (n-1)n/2
Number of Connected Components in an Undirected Graph
//bfs. iterate: use heap
//dfs. recursive: stack overflow
//union-find: less memory
1 | //dfs | bfs |
§Clone Graph
FB&GG high frequency. 谷歌Variation: Currency Exchange
- Process equations and values to build graph
numMap: map<string, map<string, double>>
visitedSet: set<string>
visitKey = num + “:” + denom //如果分子分母有:,这里handle - Process query by the graph
Follow Up: currency rate update->concurrency同步->synchronization/multi-thread
ConcurrentHashMap - https://quip.com/aCoTAb1SQfnD - Lec 16: Multithreading (English)
CopyOnWroteMap (suit for write Rarely)
§Topological Sort
To create a topological sort from a DAG
- Remove vertex that has indegree of 0
- Update other indegrees of other vertices who are neighbors of removed vertex
- Go to 1
cyclic graph -> halt
1 | class Solution { |
https://leetcode.com/problems/course-schedule/
https://leetcode.com/problems/course-schedule-ii/
§Union-Find
1 | class UnionFind { |
1 | class Solution { |
Redundant Connection, Redundant Connection II
-
If there is a node having two parents. Store them as candidates A and B, and set the second edge invalid.
[5,1], 5 is parent of 1 -
Perform normal union find.
-
-
It is now valid tree => return candidate B.
-
It is still not valid tree
-
- Candidates not existing => there is a loop, return current edge.
[5,1],[1,2],[2,3],[3,4],[4,5], loop => current[4,5] - Candidates existing => return candidate A instead of B.
[1,5],[4,5],[1,2],[2,3],[3,4], 5 has 2 parents => remove[4,5]
=> union find, f(1)=5, f(2)=5, f(3)=5, f(4)=5, f(5)=5 => remove[1,5] instead
- Candidates not existing => there is a loop, return current edge.
-
【Amazon-OA2面经】城市连接问题,即MST
给十几个城市供电,连接不同城市的花费不同,让花费最小同时连到所有的边。给出一系列connection类,里面是edge两端的城市名和它们之间的一个cost,找出要你挑一些边,把所有城市连接起来并且总花费最小。不能有环,最后所以城市要连成一个连通块。
Algorithm Steps: (Kruskal’s Algorithm)
- Sort the graph edges with respect to their weights
- Start adding edges to the MST from the edge with the smallest weight until the edge of the largest weight
- Only add edges which doesn’t form a cycle , edges which connect only disconnected components
§Optimal Account Balancing, Social Network Graph Database
pre-process; non-zero debt -> DFS
1 | class Solution { |
§Minimum Height Tree
keep deleting leaves layer-by-layer, until reach the root.
Specifically, first find all the leaves, then remove them. After removing, some nodes will become new leaves. So we can continue remove them. Eventually, there is only 1 or 2 nodes left. If there is only one node left, it is the root. If there are 2 nodes, either of them could be a possible root.
Time Complexity: Since each node will be removed at most once, the complexity is O(n).
§Implement HashMap
https://www.geeksforgeeks.org/implementing-our-own-hash-table-with-separate-chaining-in-java/