如何求解递归 T(n)=T(n-1)+2n?
A general way to solve it (assume array starts from 0):
Let’s expand the recurrence:
T(n)=2n+T(n−1)
Applying the same definition T(n−1)=2n−1+T(n−2) . If we replace T(n−1) in the above equation we obtain:
T(n)=2n+2n−1+T(n−2)
If we continue the process, we get:
T(n)=2n+2n−1+2n−2+…+21+T(0)
Assuming T(0)=1 we have:
T(n)= ∑ i = 0 n