寻找2个有序数组中,topK的值
private static int recFindKthNumber(int[] a, int al, int ar, int[] b, int bl, int br, int k) {
if (ar > br) {
return recFindKthNumber(b, bl, br, a, al, ar, k);
}
if (ar == 0) {
return b[bl + k - 1];
}
if (k == 1) {
return Math.min(a[al], b[bl]);
}
// 开始剪枝
int pa = Math.min(k / 2, ar);
int pb = k - pa;
if (a[al + pa - 1] == b[bl + pb - 1]) {
return a[al + pa - 1];
} else if (a[al + pa - 1] > b[bl + pb - 1]) {
return recFindKthNumber(a, al, ar, b, bl + pb, br - pb, k - pb);
} else {
return recFindKthNumber(a, al + pa, ar - pa, b, bl, br, k - pa);
}
}