Subtopic: 谁小移谁
Question 1: Marge 2 sorted array, Merge k sorted array/LinkedList
Question 4 Given two sorted arrays, get the intersection/union/difference.
// 谁小移谁
if (A[i] < B[j]) {
// union.add(A[i]);
// diff.add(A[i]);
i++;
} else if (A[i] > B[j]) {
// union.add(B[i]);
// diff.add(B[i]);
j++;
}
else { //相同
// intersection.add(A[i]);
// union.add(B[i]);
i++;
j++;
}
//post processing
while (i < lenA) { //j >= len B
union.add(arrayA[i]);
diff.add(arrayA[i]);
i++;
}
while (j < lenB) { //i >= len A
union.add(arrayB[j]);
diff.add(arrayB[j]);
j++;
}以intersection为例证明谁小移谁
Question 4.1 how about duplicates in the arrays
Question 5.0 Three sorted arrays A, B, and C from each of the arrays pick one element, x from A, y form B, z from C, what is the minimum |x-y| + |y- z| + |z- x|
Question 5.1 What about we have k sorted arrays we would like to pick one element from each of them, what is the smallest ranget = (max - min)
Last updated