一些简单但是常见的算法题目(笔记类)

来源:互联网 发布:知乎 域名备案选择 编辑:程序博客网 时间:2024/05/07 01:29

(1) Let A1, A2, and A3 be three sorted arrays of n real numbers (all distinct). In the comparison  model, constructing a balanced binary search tree of the set A1 UA2 U A3 

需要时间O(n),首先对A1 A2 A3进行Merge,这里需要O(n),然后构建平衡二叉树时,简单地选取中位数,然后插入到树中,然后递归调用这个过程来构建左子树和右子树。 T(n)=2T(n/2)+O(1) =O(lgn),所以总的时间是O(n)。

(2) Given an unsorted array A[1  .  . n] of n integers, building a max-heap out of the elements of  A can be performed asymptotically faster than building a red-black tree out of the elements of A.

这里想记录的是红黑数的构建时间是Ω(nlgn)。方法是首先排序,需要Ω(nlgn),然后用中序遍历的方法在O(n)的时间内构建红黑树。

(3)Suppose we use a hash function h  to hash n distinct keys  into an array T  of  length m. Assuming simple uniform hashing, the expected number of colliding pairs of elements is Θ(n的平方/m).

 

原创粉丝点击