feat(patterns): pattern taxonomy + is_optimal

This commit is contained in:
2025-09-08 16:03:14 +01:00
parent 5b768f6a21
commit 13bab63618
28 changed files with 1434 additions and 26 deletions

View File

@@ -1,6 +1,8 @@
name: Sliding Window
slug: sliding-window
difficulty_level: 2
pattern_type: algorithm
display_order: 2
description: >
Maintain a window of elements that slides through the data, tracking a
@@ -192,3 +194,129 @@ related_patterns:
prerequisite_patterns:
- two-pointers
visualization_examples:
- id: max-sum-subarray
title: Maximum Sum Subarray of Size K
input:
nums: [2, 1, 5, 1, 3, 2]
k: 3
code: |
def max_sum_subarray(nums, k):
window_sum = sum(nums[:k])
max_sum = window_sum
for i in range(k, len(nums)):
window_sum += nums[i] - nums[i - k]
max_sum = max(max_sum, window_sum)
return max_sum
steps:
- id: step-1
description: "Initialize window with first k=3 elements: [2, 1, 5]. Sum = 8"
structures:
nums:
type: array
values:
- { value: 2, state: active, annotations: ["window"] }
- { value: 1, state: active, annotations: ["window"] }
- { value: 5, state: active, annotations: ["window"] }
- { value: 1, state: default }
- { value: 3, state: default }
- { value: 2, state: default }
pointers:
left: 0
right: 2
variables:
window_sum: 8
max_sum: 8
codeHighlight:
startLine: 2
endLine: 3
- id: step-2
description: "Slide window: remove 2, add 1. New window [1, 5, 1]. Sum = 7"
structures:
nums:
type: array
values:
- { value: 2, state: visited }
- { value: 1, state: active, annotations: ["window"] }
- { value: 5, state: active, annotations: ["window"] }
- { value: 1, state: active, annotations: ["window"] }
- { value: 3, state: default }
- { value: 2, state: default }
pointers:
left: 1
right: 3
variables:
window_sum: 7
max_sum: 8
i: 3
codeHighlight:
startLine: 5
endLine: 7
- id: step-3
description: "Slide window: remove 1, add 3. New window [5, 1, 3]. Sum = 9. New max!"
structures:
nums:
type: array
values:
- { value: 2, state: visited }
- { value: 1, state: visited }
- { value: 5, state: found, annotations: ["window"] }
- { value: 1, state: found, annotations: ["window"] }
- { value: 3, state: found, annotations: ["window"] }
- { value: 2, state: default }
pointers:
left: 2
right: 4
variables:
window_sum: 9
max_sum: 9
i: 4
codeHighlight:
startLine: 5
endLine: 7
- id: step-4
description: "Slide window: remove 5, add 2. New window [1, 3, 2]. Sum = 6"
structures:
nums:
type: array
values:
- { value: 2, state: visited }
- { value: 1, state: visited }
- { value: 5, state: visited }
- { value: 1, state: active, annotations: ["window"] }
- { value: 3, state: active, annotations: ["window"] }
- { value: 2, state: active, annotations: ["window"] }
pointers:
left: 3
right: 5
variables:
window_sum: 6
max_sum: 9
i: 5
codeHighlight:
startLine: 5
endLine: 7
- id: step-5
description: "Loop complete. Maximum sum found is 9 (window [5, 1, 3])"
structures:
nums:
type: array
values:
- { value: 2, state: visited }
- { value: 1, state: visited }
- { value: 5, state: found }
- { value: 1, state: found }
- { value: 3, state: found }
- { value: 2, state: visited }
variables:
max_sum: 9
codeHighlight:
startLine: 9
endLine: 9