Question: 3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n^2 in the worst case. You may assume that you can sort the n integers in time proportional to n^2 or better.
问题描述: 这里的问题是想要一个增长数量级是平方级别或更好的3-sum算法
解决方案: 增加一个哈希表来实现,先使用嵌套的2层循环,然后在哈希表中查询-(a[i] + a[j]), 当然要在数据结构里面加入flag数组来表示每个数是否作为嵌套循环里面的基底使用过了,如果哈希表中寻找到这个值且flag为false,那么count加1。
Question: Search in a bitonic array. An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer is in the array.
- Standard version: Use ∼3lgn compares in the worst case.
- Signing bonus: Use ∼2lgn compares in the worst case (and prove that no algorithm can guarantee to perform fewer than ∼2lgn compares in the worst case).
问题描述: 这是关于双调数组搜索的问题,所谓的双调数组的特点是,数组元素先递增,再递减,类似于一个峰的形式。然后标准实现方法的最坏情况是~3lgn的比较次数,然后最好的实现方法的最坏情况比较次数是关于~2lgn的。
解决方案: 这里的标准解决方法很好实现,先使用二分法的变式找到数组的最大值,然后左右分别进行二分查找即可,很容易得到~3lgn的比较次数。对于更有效率的查找方式,这里我选择不查找最大值。具体的d递归实现方式描述如下:
- 先找到mid,将其与key比较,如果相等则返回true,否则进行下一步;
- 将mid值和左右两个值进行比较,判断mid在山峰的左边还是右边亦或是峰顶;
- 如果是峰顶,直接采取左右两边二分查找;如果是峰左,那么判定key值与mid值的相对大小,如果key值大于mid值,那么可以知道key值位于mid的右边,直接递归bitonicSearch(mid+1, hi, key)即可;如果key值小于mid值,那么需要对左边进行二分查找以及对右边进行递归查找;对于mid位于峰右的情况是同理的,具体Java代码见底部。
Question: Egg drop. Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses:
Version 0: 1 egg, ≤T tosses.
Version 1: ∼1lgn eggs and ∼1lgn tosses.
Version 2: ∼lgT eggs and ∼2lgT tosses.
Version 3: 2 eggs and ∼2√n tosses.
Version 4: 2 eggs and ≤c√T tosses for some fixed constant c.问题描述: 这个是关于爬楼扔鸡蛋确定安全楼层的问题。
解决方案: 其实这个就是解决两个子问题进行递归的问题,分别是鸡蛋碎了和鸡蛋没碎两种情况,而这两种情况也分别对应了确定的不安全楼层和安全楼层。
public class Bitonic {
private int[] bArray;
private int size;
public Bitonic(final int[] arr) {
size = arr.length;
bArray = new int[size];
for (int i = 0; i < size; i++) {
bArray[i] = arr[i];
}
}
public boolean search(int low, int hi, int key) {
int mid = low + (hi - low) / 2;
if (bArray[mid] == key) {
return true;
}
if (mid == 0) {
return search(mid + 1, hi, key);
} else if (mid == size - 1) {
return search(low, mid - 1, key);
} else {
if (bArray[mid - 1] < bArray[mid] && bArray[mid] > bArray[mid + 1]) { // mid
return bArray[mid] < key ? false :((BinarySearch.indexOf(bArray, low, mid - 1, key) != -1)
|| (BinarySearch.indexOfReverse(bArray, mid + 1, hi, key) != -1));
} else if (bArray[mid - 1] < bArray[mid]) { //left
return bArray[mid] < key ? search(mid + 1, hi, key) :
((BinarySearch.indexOf(bArray, low, mid - 1, key) != -1) || search(mid + 1, hi, key));
} else { //right
return bArray[mid] < key ? search(low, mid - 1, key) :
((BinarySearch.indexOf(bArray, mid + 1, hi, key) != -1) || search(low, mid - 1, key));
}
}
}