算法课_算法分析_

算法课_算法分析_


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 indices i 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  2lgN compares 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 更好点




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值