-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstStep.txt
More file actions
136 lines (102 loc) · 3.73 KB
/
FirstStep.txt
File metadata and controls
136 lines (102 loc) · 3.73 KB
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
129
130
131
132
133
134
Problem 1: Kth Largest Element (Quick Select)
Don't swap equal element which will cause stacker flow.
Follow up (Kth Largest Element II) the Length of the Array is way larger than K (Priority
Queue)
Problem 2: 3Sum (Review)
Most Important is to remove duplicate element in the List.
Problem 3: Triangle Count (Review)
traverse the longest edge of a triangle and check the number smaller than that
Problem 4: Sort Color 2 (Quick Sort Thought)
Pay attention to the colors division
colors[left] <= pivot
colors[right] > pivot
rainbowSort([], from, pivot, left, ...)
rainbowSort([], pivot + 1, to, ...)
Problem 5: Binary Tree Level Order Traversal (Review)
When to visit the node.
Level Order traversal.
******Topological Sort**********
1. 后面出现的会依赖前面出现的,一定是有向图
******Breadth First Search******
1. 无向图
2. 最短路径
3. HashSet
4. Order of Visiting
********************************
Problem 6: Number of Islands
BFS (Union Find)
Coding Style
Problem 7: Course Schedule I && II
Topological Sort
1. Get all of the in degree of all the nodes
2. Push all of the in degree == 0 node into the queue
3. BFS the graph once meet one child node its in degree minus one
4. If meet one in degree == 0 add it to the queue
****Be sure to check if the graph has a topological sort***
Problem 8: Serialize and Deserialize Binary Tree
Level Order BFS
Problem 9: Word Ladder
You cannot iterate the dict to get the wanted word.
Add the end word into the dict.
***Use a set to save all of the visited node in case of loop.***
Problem 10: Clone Graph
Don't add created node into the queue again, use map to store the mapping between original
Graph and Cloned graph. (Memory Limited)
Graph HashSet or Map should be remember.
Runtime Complexity: O(|V| + |E|) Space Complexity: O(|V| + |E|)
Problem 10: Graph Valid Tree (Review)
1. The sequence add to the queue.
2. Separated tree corner case.
3. edges.length > n - 1 also cannot be a tree.
Kafka Hadoop spark cansadra HBase dist
DFS Tree Based
Problem 11: Flatten Binary Tree to Linked List
Two ways to solve:
1. Use stack like iterator
2. Use divide and conquer (Review)
Problem 12: Kth Smallest Element in a BST
1. QuickSelect Solution O(n)
2. O(h + k)
Problem 13: Closest Binary Search Tree Value
Solution 1: Inorder Traversal O(n)
Solution 2: get LowerBound and HigherBound O(h) (Review)
***********************
private TreeNode lowerBound(TreeNode node, double target) {
TreeNode temp = node;
TreeNode result = null;
while (temp != null) {
if (temp.val < target) {
result = temp;
temp = temp.right;
} else {
temp = temp.left;
}
}
return result;
}
************************
Problem 14: Subtree with Maximum Average
The minimum value for the Double is -Double.MAX_VALUE not Double.MIN_VALUE > 0;
Problem 15: Find Missing Number II & I
Use dfs to find the path where there is a number miss;
Corner Case:
09 0
Missing Number I:
Math Algorithm can solve it easily.
Problem 16: Remove Invalid Parentheses
Get the wrong parentheses and remove one by one to see if the string is right
Problem 17: Work Break I and II
1. DFS stack overflow and Memory Limit Exceed
2. Memoization method to improve the algorithm
3. Dynamic Programming techniques needs to be added (***)
Problem 18: Permutations II
i != 0 && nums[I] == nums[I - 1] && !vistied[I - 1]
Problem 19: Merge Sorted Arrays
Use Heap and build a class which implements Comparable<> override compareTo method
Problem 20: Subarray Sum
Build preSum Array to record the sum before the index I.
Problem 21: Interval Questions:
Compare two intervals
Input.end < interval.start
Input.start > interval.end
Merge two interval