Easy: Solve T(n)=T(n-1)+n by Iteration Method

前端 未结 5 2230
灰色年华
灰色年华 2021-02-19 19:59

Can someone please help me with this ?

Use iteration method to solve it. T(n) = T(n-1) +n

Explanation of steps

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 20:59

    Another solution:

    T(n) = T(n-1) + n
         = T(n-2) + n-1 + n
         = T(n-3) + n-2 + n-1 + n
         // we can now generalize to k
         = T(n-k) + n-k+1 + n-k+2 + ... + n-1 + n
         // since n-k = 1 so T(1) = 1
         = 1 + 2 + ... + n    //Here 
         = n(n-1)/2
         = n^2/2 - n/2
         // we take the dominating term which is n^2*1/2 therefor 1/2 = big O
         = big O(n^2)
    

提交回复
热议问题