X3_Binary Search

§Binary Tree Level Order Traversal

test case:cover different + corner case

  1. only left/right child
  2. full tree
  3. left child, right child, left child, right child…

Follow Up: output the level to level reversely from bottom to up

  1. reverse funtion
  2. add from front

§Intersection of Two Arrays

  1. If non sort-> Hashmap. Ues extra space. O(m+n)
  2. If sort-> Two pointers. O(m+n)
  3. 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

  1. find non-zero indexes
  2. intersection
  3. dot product

§Find K-th Smallest Pair Distance

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    int 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

  2. Optimization

    since there may be duplicate ->

    1
    2
    right = upperBound(nums, left+1, n-1, target);
    count += right - left -1
  3. 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
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
class ExpiringKey implement Comparable<ExpiringKey> {
V val;
long expirationTime; // ttl
K k;
}

class ExpringMap {
Map<K, ExpiringKey> m = new HashMap<>();
PriorityQueue q

V get(Key k) {

ExpiringKey v = this.m.get(k);
if (v == null) {
return null;
}

if (v.expirationTime < System.currentTimeMillis()) {
m.remove(k);
return null;
}

return v.val;
}

void put(Key k, V val, long expirationTime) {
ExpiringKey expiringKey = new ExpiringKey(val, expirationTime);
this.m.put(k, expiringKey);
this.q.add(expiringKey); // log(k)
}

void cleanup() {
// q.poll() log(k)
List<K> keys = new ArrayList<>();
for (ExpiringKey entry : this.m.entrySet()) {
if (entry.getValue().expirationTime < System.currentTimeMillis()) {
keys.add(entry.getKey());
}
}

for (K key : keys) // delete
}

// PriorityQueue<>() minHeap
}

optimization: use PriorityQueue

§Trick

  1. 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;
    }
  2. Difference between >>> and >>

    >>> is unsigned