Li Lei and Han Meimei have different opinions regarding the linear time complexity in the video lecture.对于视频中线性递归的时间复杂度,A、B两位同学有不同的看法。
Li Lei aggrees to the video, the complexity is O(n) because there are n instances each requiring O(1) execution time. A同学赞同视频中的算法,由于单个递归实例需要O(1)时间完成,共有n个实例,所以整个算法的复杂度是O(n)。
However, Han Meimei believes the time for sum(A,n)
to be O(n) instead of O(1), since it's still executing even when calling sum(A,n-1)
, leading to a total time complexity of (n+n−1+...+3+2+1=O(n2. You agree with 但B同学认为,当sum(A,n)函数中调用sum(A,n-1)时,sum(A,n)仍在执行,因此sum(A,n)的完成时间不是O(1)而是O(n),依此计算,整个算法的复杂度应该为()。n+n−1+...+3+2+1=O(n2)。请问哪位同学对了?
ALi Lei A同学
BHan Meimei B同学