1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Array time complexity when modifying elements in Python

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I was reading a bit on DS/A and found this cheat sheet on Leet Code..

    Add or remove element from arbitrary index: O(n) Access or modify element at arbitrary index: O(1)

    Intuitively I would think they would both be O(n). Why is one O(1) and the other O(n)?

    Why is adding or removing an element linear, while accessing or modifying an element constant? Does it have to do with re-indexing the array?

    Would adding or removing the last element of an array be O(1) since you wouldn't be adjusting the index of that array.

    Assume we're talking about Python here in case that matters for this specific question.

    Continue reading...

Compartilhe esta Página