sliding window
Resilience Patterns

Meaning

The sliding window technique maintains a contiguous subset of data (a window) that moves linearly through the input. By adjusting the window's start and end indices, you can evaluate constraints efficiently without revisiting elements, turning quadratic brute-force scans into linear-time algorithms.

Primary Function

Algorithm design

Communicative Purpose

Efficiently process contiguous subarrays or substrings by maintaining a window that slides over the data, reducing time complexity from O(n^2) to O(n).

Pattern

Use two pointers (start, end) to represent a window; expand end to include new element, then contract start while condition violated.

Função primária

Algorithm design

Propósito comunicativo

Efficiently process contiguous subarrays or substrings by maintaining a window that slides over the data, reducing time complexity from O(n^2) to O(n).

Situações de gatilho

Finding maximum/minimum subarray sum, longest substring with constraints, counting subarrays satisfying condition.

Contextos

Algorithm interviews, competitive programming, data stream processing, string manipulation.

Padrão

Use two pointers (start, end) to represent a window; expand end to include new element, then contract start while condition violated.

Colocados típicos

  • two pointers
  • hash map
  • prefix sum
  • deque

Substituições comuns

  • brute force enumeration
  • prefix sum with binary search

Erros comuns

Forgetting to update window start after moving end, leading to O(n^2); incorrectly handling edge cases when window size zero.

Similar / contraste

Prefix sum (good for static range queries but not dynamic constraints); Two-pointer technique (similar but often for sorted arrays).

Interferências

Coming from naive brute-force: may overlook linear solution; from fixed-size sliding window: may confuse with variable-size windows.

Família do chunk

  • two pointers
  • prefix sum
  • deque
  • binary search

Nuance

Window can be fixed-size or variable-size; algorithm assumes monotonic condition for contraction; not suitable for non-contiguous selections.

Efeito pragmático

Enables linear-time solutions for many substring/subarray problems, crucial for performance-critical code.

Dica de memória

Think of a sliding glass pane moving over a scene, only showing a portion at a time.

Nota

Sliding window assumes the condition is monotonic so the left pointer only moves forward; otherwise the algorithm may miss valid windows

Upgrade path

Mastering more advanced window variations like monotonic queue or deque for sliding window maximum.

Frequência: HighFormulaicidade: FlexibleTipo de construção: conceptPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.