【题目1】
public int getLessIndex(int[] arr) {
if (arr == null || arr.length == 0) {
return -1; // no exist
}
if (arr.length == 1 || arr[0] < arr[1]) {
return 0;
}
if (arr[arr.length - 1] < arr[arr.length - 2]) {
return arr.length - 1;
}
int left = 1;
int right = arr.length - 2;
int mid = 0;
while (left < right) {
mid = (left + right) / 2;
if (arr[mid] > arr[mid - 1]) {
right = mid - 1;
} else if (arr[mid] > arr[mid + 1]) {
left = mid + 1;
} else {
return mid;
}
}
return left;
}
【题目2】
时间 O(n)
额外空间O(1)
【题目3】
时间复杂度O(h^2) = [log(n)]^2
public int nodeNum(Node head) {
if (head == null) {
return 0;
}
return bs(head, 1, mostLeftLevel(head, 1));
}
public int bs(Node node, int l, int h) {
if (l == h) {
return 1;
}
if (mostLeftLevel(node.right, l + 1) == h) {
return (1 << (h - l)) + bs(node.right, l + 1, h);
} else {
return (1 << (h - l - 1)) + bs(node.left, l + 1, h);
}
}
public int mostLeftLevel(Node node, int level) {
while (node != null) {
level++;
node = node.left;
}
return level - 1;
}
【题目4】
public static int getUpMedian(int[] arr1, int[] arr2) {
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
throw new RuntimeException("Your arr is invalid!");
}
return findProcess(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1);
}
//arr1的 start1~end1范围上 、arr2的 start2~end2范围上
public static int findProcess(int[] arr1, int start1, int end1, int[] arr2, int start2, int end2) {
if (start1 == end1) {
return Math.min(arr1[start1], arr2[start2]);
}
// 元素个数为奇数,则offset为0;元素个数为偶数,则offset为1;
int offset = ((end1 - start1 + 1) & 1) ^ 1;
int mid1 = (start1 + end1) / 2;
int mid2 = (start2 + end2) / 2;
if (arr1[mid1] > arr2[mid2]) {
return findProcess(arr1, start1, mid1, arr2, mid2 + offset, end2);
} else if (arr1[mid1] < arr2[mid2]) {
return findProcess(arr1, mid1 + offset, end1, arr2, start2, mid2);
} else { //arr1[mid1] = arr2[mid2]
return arr1[mid1];
}
}
【题目5】
Ⅰ. arr1拿出k个数,arr2拿出k个数,求arr1和arr2的上中位数
Ⅱ.
Ⅲ .找 1’~27’ 该淘汰哪一段
public static int findKthNum(int[] arr1, int[] arr2, int kth) {
if (arr1 == null || arr2 == null) {
throw new RuntimeException("Your arr is invalid!");
}
if (kth < 1 || kth > arr1.length + arr2.length) {
throw new RuntimeException("K is invalid!");
}
int[] longArr = arr1.length >= arr2.length ? arr1 : arr2;
int[] shortArr = arr1.length < arr2.length ? arr1 : arr2;
int lenL = longArr.length;
int lenS = shortArr.length;
if (kth <= lenS) {
return getUpMedian(shortArr, 0, kth - 1, longArr, 0, kth - 1);
}
if (kth > lenL) {
if (shortArr[kth - lenL - 1] >= longArr[lenL - 1]) {
return shortArr[kth - lenL - 1];
}
if (longArr[kth - lenS - 1] >= shortArr[lenS - 1]) {
return longArr[kth - lenS - 1];
}
return getUpMedian(shortArr, kth - lenL, lenS - 1, longArr, kth - lenS, lenL - 1);
}
if (longArr[kth - lenS - 1] >= shortArr[lenS - 1]) {
return longArr[kth - lenS - 1];
}
return getUpMedian(shortArr, 0, lenS - 1, longArr, kth - lenS, kth - 1);
}
public static int getUpMedian(int[] arr1, int start1, int end1, int[] arr2,
int start2, int end2) {
if (start1 == end1) {
return Math.min(arr1[start1], arr2[start2]);
}
int offset = ((end1 - start1 + 1) & 1) ^ 1;
int mid1 = (start1 + end1) / 2;
int mid2 = (start2 + end2) / 2;
if (arr1[mid1] > arr2[mid2]) {
return getUpMedian(arr1, start1, mid1, arr2, mid2 + offset, end2);
} else if (arr1[mid1] < arr2[mid2]) {
return getUpMedian(arr1, mid1 + offset, end1, arr2, start2, mid2);
} else {
return arr1[mid1];
}
}