3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better.
Hint: given an integer 𝚡 and a sorted array 𝚊[] of n distinct integers, design a linear-time algorithm to determine if there exists two distinct indices 𝚒 and 𝚓 such that 𝚊[𝚒]+𝚊[𝚓]==𝚡 .
Sort the array first. Traverse the array , for each element a, to find the other two element sum to -a, we use two pointers one from the next of a and the other from the end. If the sum of the two numbers pointed is smaller than -a , then increase the lower pointer else if the sum is bigger than -a, decrease the upper pointer. Until we find a sum that is equal to -a, then we increase the lower pointer and increase the upper pointer.
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).
Hints: Standard version. First, find the maximum integer using ∼1lgn compares—this divides the array into the increasing and decreasing pieces.
Signing bonus. Do it without finding the maximum integer.
1. compare mid with mid + 1 element , to see whether it's in the ascending part or descending part to find the turning point. Then do two binary search in each part.
2. no need to find the turning point. compare target with mid , and compare mid with mid + 1, to know how to move the lower bound or the upper bound. e.g. , if a[mid] > target , then if it's ascending part , you need to set upper bound to mid - 1 other wise you need to lower bund to mid + 1. If a[mid] > a[mid +1] you are in descending part otherwise you are in ascending part.
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∼2n‾‾√ tosses. - Version 4:
2 eggs and≤cT‾‾√ tosses for some fixed constantc .
Hints:
- Version 0: sequential search.
- Version 1: binary search.
- Version 2: find an interval containing
T of size≤2T , then do binary search. - Version 3: find an interval of size
n‾‾√ , then do sequential search. Note: can be improved to∼2n‾‾‾√ tosses. - Version 4:
1+2+3+…+t∼12t2 . Aim forc=22‾‾√ .
Version 0 : try from floor 1 until it's broken
Version 1: binary search
Version 2 : try from 1 , 2, 4, ... the first number that is bigger than T , totally ~lgT tosses , then do binary search another ~lgT tosses
Version 3 : try from n^0.5 , 2*n^0.5 , 3*n^0.5 , ... the first number that is bigger than T, used 1 egg and at most ~n^0.5 tosses. then we know T is between k*n^0.5 and (k+1)*n^0.5 , then we try from lower bound to upper bound , at most n^0.5 tosses and use another egg
Version 4: try from 1, 1+2 , 1+2+3 , ..., 1+2+3 + ... +k the first number that is bigger than T, used 1 egg and k tosses , then try in last k floor, so totally 2k tosses, 0.5 *k * (k + 1) = T , so k = 2^0.5 T^0.5
没有评论:
发表评论