Pergunta

I have this method

arr // input


new_ seq = []

for i in arr:
  new_seq.append(i)
  __new_seq = [x for i, x in enumerate(arr) if x not in new_seq]
  for j in __new_seq:
      new_seq.append(j)
      __new_seq = [x for i, x in enumerate(arr) if x not in new_seq]
      for k in __new_seq:
          new_seq.append(k)

How to calculate the time complexity for this method Please note that each loop has a smaller length than the one before

Foi útil?

Solução

Ordinarily, a loop that executes only part of the time counts as a normal loop - complexity analysis ignores constant factors. That would make it cubic time complexity.

In this case, though, the inner loops never execute, so it should be quadratic time (the middle loop body never executes, but you still spend time iterating the second for...in).

Licenciado em: CC-BY-SA com atribuição
scroll top