算法課_算法分析_

算法課_算法分析_


O(n^2),

O(nlgn),

O(n)


t=an^b+c

given two groups of t and n, the b can be calculucated by lg(t2-t1)=blgn+lga, assuming that t and n are large enough.


空間複雜度


Question 1

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.
Your Answer   Score Explanation
Total   0.00 / 0.00  
Question Explanation

Hint: given an integer x and a sorted array a[] of N distinct integers, design a linear-time algorithm to determine if there exists two distinct indicesi and j such that a[i] + a[j] == x.
即:sort 數組a,對a裏面的每一個小於等於0的數a[j],找出-a[j]是否在數組中,用lgn的時間,找出最接近-a[j]的位置i,滿足a[i]+a[j]<=0,然後再在i同時向左右看,l=i,r=i+1,a[j]+a[l]+a[r],如果<0,則向右移r=r+1;>0,則向左移,l=l-1;=0,則左右都移。

Question 2

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 2lgNcompares in the worst case).
Your Answer   Score Explanation
Total   0.00 / 0.00  
Question Explanation

Hints:

Standard version. First, find the maximum integer using 1lgN compares—this divides the array into the increasing and decreasing pieces.

找最大值時只需 less(a[i-1],a[i]) == true 向右 false 向左,直到最後

Signing bonus. Do it without finding the maximum integer.這個太複雜,實現很麻煩。考慮單調性和大小兩個邏輯。而且我認爲2lgn這個有錯,應該是3lgn。worsest  case  3lgn:      51...,100,49,...1,找50是否存在,a[50]=100,a[49]=99,則左邊單調增,朝下找50,用1*lg50的時間,右邊a[75]=25,a[74]=26,單調減,要判斷單調性和a[75]與50的大小,2lg50。綜合起來3lg50,worse case,吃力不討好。上面一段是我一開始的想法,在論壇裏看到了別人的2lnN的解法。在另一篇文檔裏

Question 3

Egg drop. Suppose that you have an N-story building 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 constant c.
Your Answer   Score Explanation
Total   0.00 / 0.00  
Question Explanation

Hints:

Version 0: sequential search. 一個一個對,1,2,3,4,5的找

Version 1: binary search. 二分

Version 2: find an interval containing T of size 2T, then do binary search.          1,2,4,8,..,2^n search,找到sizeT的區間containingT,然後做二分。 對於T<N/2比較好。比較好是次數比較少。Version 3: find an interval of size N, then do sequential search. Note: can be improved to 2N tosses. 1,sqrt(N),2*sqrt(N),,,sqrt(N)*sqrt(N)這樣的序列找T所在的區間

Version 4: 1+2+3++t12t2. Aim for c=22. 對於 T>N/2 更好點




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章