i recently gave a test… the question was long story and the solution boiled down to
F(n) = 2*F(n-1) + 2*F(n-2)…
I had an O(n) solution using dynamic programming… However, the examiner wasn’t satisfied…
my solution was to simply store every F(n) in an array as it is calculated. it took O(n) time.
as we need just the previous two elements, by using just two variables, the space problem can be solved.
however O(n) isn’t fast enough…
the function looks like the fibonacci function, and a fibonacci number can be generated in O(lg n) time… but am unable to get a O(lg n) soln for my problem..
so my question is how do i improve the time-complexity of the function?
Exactly the same way. Express your recurrence in matrix form; this reduces the problem to finding the n-th power of a matrix, which can be done in log(n) time.