Professional Documents
Culture Documents
xk+1 1 xk+1 1
= 1 + x + x2 . . . xk = + xk+1 by inductive hypothesis
x1 x1
xk+1 1 + xk+1 (x 1)
=
x1
xk+2 1
=
x1
(f) To start with, we need to determine an interval in which k lives in O(log k) time. To do this, we simply
look for a 1 at indices 1, 2, 4, . . . , 2dlog ke . Index k lives somewhere in the interval [2dlog ke1 , 2dlog ke ]
We can now search this interval for an index which contains 0 and which is followed by a 1, using binary
search. The interval has no more than k items, so binary search incurs an additional O(log k) cost.
1
2. The idea is to keep the smallest element from each list in a heap; each element is augmented with the index
of the lists where it comes from. We can perform a DeleteMin on the heap to find and delete the smallest
element and insert the next element from the corresponding list.
Analysis: It takes O(k) to build the heap; for every element, it takes O(logk) to DeleteMin and O(logk) to
insert the next one from the same list. In total it takes O(k + n logk) = O(n logk).
3. (i) For each i = 1, . . . , k we will compute(solve) two sub-problems:
F (i) : the maximum amout of work that can be done during minutes {1, . . . , i} assuming that minute
i is spent at First Cup.
N (i) : the maximum amout of work that can be done during minutes {1, . . . , i} assuming that minute
i is spent at Jim Nortons.
Overall we want to compute max(F (k), N (k))
(ii) Given that I spend minute i at First Cup there are two possible situations: either I spent the previous
minute at First Cup or I spent minute i 11 at Jim Nortons (I was switching during {i 10, . . . i 1}).
In the first case F (i) = fi + F (i 1). In the second case where I switched, we have F (i) = fi + N (i 11).
Thus:
F (i) = fi + max(F (i 1), N (i 11)), if 1 i k and F (i) = 0, if i 0.
N (i) = ni + max(N (i 1), F (i 11)), if 1 i k and N (i) = 0, if i 0.
(iii) Allocate two arrays of real numbers F and N of length k with indices staring from 10 until k. Intialize
them to 0 for all entries. Then incrementallty compute the functions in the previous question. Clearly
O(k) both time and space complexity.