I was wondering about my apparently code for an MIT online edx class practice problem vs the provided answer code.
The assignment in question from the class was
Their answer code is more efficient because it does not repeatedly iterate through subsequences. Given the subsequence ABCDE
, your code separately processes BCDE
, CDE
, and DE
in successive iterations even though they cannot be longest.
Therefore, the worst-case runtime of your answer is O(N^2) vs. O(N) for theirs. Yes, this is related to having a nested for
loop which is not present in their answer.