S和T是两个排序的数组,每个都有n个元素(整数),描述了一个算法,用于在两个数组的联合中找到第k个最小数(假设没有重复数),时间复杂度为(logn)^ 2。请注意,找到复杂logn的算法相当容易。
答案 0 :(得分:0)
针对您的问题的优化 O(min(log(k),log(m + n))工作解决方案粘贴在下面
public class KthSmallestIterative {
public static int kthSmallest(int[] A, int[] B, int k) {
if (A == null || B == null) {
throw new IllegalArgumentException("Arrays cannot be null!");
}
int a = A.length;
int b = B.length;
if (k < 1 || k > a + b) {
throw new IllegalArgumentException("k is not within range!");
}
int minSizeA = Math.max(0, k - b);
int maxSizeA = Math.min(a, k);
while (minSizeA <= maxSizeA) {
int sizeA = minSizeA + (maxSizeA - minSizeA) / 2;
int sizeB = k - sizeA;
int indexA = sizeA - 1;
int indexB = sizeB - 1;
int indexANext = syzeA;
int indexBNext = sizeB;
int valA = (indexA < 0) ? Integer.MIN_VALUE : A[indexA];
int valB = (indexB < 0) ? Integer.MIN_VALUE : B[indexB];
int valANext = (indexANext >= a) ? Integer.MAX_VALUE : A[indexANext];
int valBNext = (indexBNext >= b) ? Integer.MAX_VALUE: B[indexBNext];
if (valA <= valBNext && valB <= valANext) {
return Math.max(valA, valB);
} else if (valA > valBNext) {
maxSizeA = sizeA - 1;
} else {
minSizeA = sizeA + 1;
}
}
}
public static void main(String[] args) {
int[] list1 = new int[] { 3, 4, 10, 23, 45, 55, 56, 58, 60, 65 };
int[] list2 = new int[] { 3, 3, 3, 15, 16, 28, 50, 70, 71, 72 };
int k = 13;
int kthSmallest = kthSmallest(list1, list2, k);
System.out.println(k + "th smallest is " + kthSmallest);
}
return 0;
}
}