Пространственная сложность

Использование памяти относительно размера входных данных (Big O).

Space complexity describes how an algorithm's memory usage grows with input size (n). Like time complexity, it uses Big O notation.

Space complexity = Input space + Auxiliary space
- Input space: Memory for the input itself
- Auxiliary space: Extra memory used during execution

Common space complexities:
- O(1) Constant: Fixed extra memory (in-place algorithms)
- O(n) Linear: Memory proportional to input
- O(log n) Logarithmic: Recursion depth in binary search
- O(n²) Quadratic: 2D matrix storage

        graph LR
  Center["Пространственная сложность"]:::main
  Pre_algorithm["algorithm"]:::pre --> Center
  click Pre_algorithm "/terms/algorithm"
  Rel_time_complexity["time-complexity"]:::related -.-> Center
  click Rel_time_complexity "/terms/time-complexity"
  Rel_asymptotic_notations["asymptotic-notations"]:::related -.-> Center
  click Rel_asymptotic_notations "/terms/asymptotic-notations"
  Rel_big_o_notation["big-o-notation"]:::related -.-> Center
  click Rel_big_o_notation "/terms/big-o-notation"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 Простыми словами

Представь, что собираешь чемодан в путешествие. Пространственная сложность — это как спросить: 'Сколько чемоданов мне нужно, если я беру больше вещей?' Некоторые методы всегда требуют только одну сумку (O(1)), другие — сумку для каждой вещи (O(n)).

🤓 Expert Deep Dive

Оптимизация хвостовой рекурсии превращает O(n) стекового пространства в O(1). Компромиссы пространство-время фундаментальны: хеш-таблицы обменивают O(n) пространства на O(1) поиск. Потоковые алгоритмы обрабатывают данные в O(1) пространстве.

🔗 Связанные термины

Предварительные знания:

📚 Источники