§Binary Tree Level Order Traversal
test case:cover different + corner case
- only left/right child
- full tree
- left child, right child, left child, right child…
Follow Up: output the level to level reversely from bottom to up
- reverse funtion
- add from front
§Intersection of Two Arrays
- If non sort-> Hashmap. Ues extra space. O(m+n)
- If sort-> Two pointers. O(m+n)
- If no extra space / If n>>m, scan the smaller array and find it in larger one via binary search in -> If already sort -> O(m*logn)
Intersection of k arrays? merge 的变种
-
§Dot Production (FB)
Conduct Dot Product of two large sparse Vectors
- find non-zero indexes
- intersection
- dot product
§Find K-th Smallest Pair Distance
-
1
2
3
4
5
6
7
8
9
10
11int lo = 0;
int hi = nums[num.length - 1] - nums[0];
while (lo < hi){
int mid = lo + ((hi - lo) >>> 1);
//...
}
// count function = number of pairs with distance <= guess
if (nums[right] - nums[left] > guess)
continue;
count += right - left;Runtime: N^2*logW
-
Optimization
since there may be duplicate ->
1
2right = upperBound(nums, left+1, n-1, target);
count += right - left -1 -
Optimization II
Use Map<Integer, Integer> to memorize
Binary Search -> N logN * logW
§Others
§410. Split Array Largest Sum
§Copy Books, The Painter’s Partition Problem Part II
§expire map
https://www.quora.com/How-can-I-implement-expiry-key-hashmap-in-Java
cannot remove element while using for loop, but can use interator
https://stackoverflow.com/questions/6092642/how-to-remove-a-key-from-hashmap-while-iterating-over-it
1 | class ExpiringKey implement Comparable<ExpiringKey> { |
optimization: use PriorityQueue
§Trick
-
search
1
2
3
4
5
6
7
8
9
10
11// search in java, return index
if (index>=0){
set.add(num);
l = index;
}
else{
index = -(index+1); //next one who is larger than it
if (index >= r)
break;
l = index;
} -
Difference between >>> and >>
>>>
is unsigned