I had a question about whether I was understanding the time complexity of the len
function in Python correctly. I've seen multiple posts on this topic
CodePudding user response:
We don't count the complexity of all the operations that increment and decrement the length field of the array, because they're not dependent on the current length of the array.
For instance, if you build up an array by appending and popping, you may have done far more than n
iterations before you call len()
.
Furthermore, if you're calculating the complexity of the entire algorithm, you would have already accounted for the complexity of creating the array. You don't want to count it again when calculating the complexity of processing the array.