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: Fast & Slow Pointers
slug: fast-slow-pointers
difficulty_level: 2
pattern_type: algorithm
display_order: 3
description: >
Use two pointers moving at different speeds to detect cycles, find midpoints,
@@ -296,3 +298,188 @@ related_patterns:
prerequisite_patterns:
- two-pointers
visualization_examples:
- id: find-middle-of-list
title: Find Middle of Linked List
input:
list: [1, 2, 3, 4, 5]
code: |
def find_middle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
steps:
- id: step-1
description: "Initialize slow and fast pointers at head (node 1)."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: active }
- { value: 2, state: default }
- { value: 3, state: default }
- { value: 4, state: default }
- { value: 5, state: default }
pointers:
slow: 0
fast: 0
variables: {}
codeHighlight:
startLine: 2
endLine: 2
- id: step-2
description: "Move slow 1 step (to 2), fast 2 steps (to 3)."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: active }
- { value: 3, state: active }
- { value: 4, state: default }
- { value: 5, state: default }
pointers:
slow: 1
fast: 2
variables: {}
codeHighlight:
startLine: 5
endLine: 6
- id: step-3
description: "Move slow 1 step (to 3), fast 2 steps (to 5)."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: visited }
- { value: 3, state: active }
- { value: 4, state: visited }
- { value: 5, state: active }
pointers:
slow: 2
fast: 4
variables: {}
codeHighlight:
startLine: 5
endLine: 6
- id: step-4
description: "fast.next is null, loop ends. slow is at middle (node 3)."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: visited }
- { value: 3, state: found }
- { value: 4, state: visited }
- { value: 5, state: visited }
pointers:
slow: 2
variables:
result: 3
codeHighlight:
startLine: 8
endLine: 8
- id: detect-cycle
title: Detect Cycle in Linked List
input:
list: [1, 2, 3, 4, 5]
cycle_at: 2
code: |
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
steps:
- id: step-1
description: "List has a cycle: 5 points back to 3. Initialize pointers at head."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: active }
- { value: 2, state: default }
- { value: 3, state: default, annotations: ["cycle start"] }
- { value: 4, state: default }
- { value: 5, state: default, annotations: ["→3"] }
pointers:
slow: 0
fast: 0
variables: {}
codeHighlight:
startLine: 2
endLine: 2
- id: step-2
description: "slow moves to 2, fast moves to 3."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: active }
- { value: 3, state: active, annotations: ["cycle start"] }
- { value: 4, state: default }
- { value: 5, state: default, annotations: ["→3"] }
pointers:
slow: 1
fast: 2
variables: {}
codeHighlight:
startLine: 5
endLine: 6
- id: step-3
description: "slow moves to 3, fast moves to 5."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: visited }
- { value: 3, state: active, annotations: ["cycle start"] }
- { value: 4, state: visited }
- { value: 5, state: active, annotations: ["→3"] }
pointers:
slow: 2
fast: 4
variables: {}
codeHighlight:
startLine: 5
endLine: 6
- id: step-4
description: "slow moves to 4, fast loops back to 4 (via 5→3→4). They meet!"
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited }
- { value: 2, state: visited }
- { value: 3, state: visited, annotations: ["cycle start"] }
- { value: 4, state: found, annotations: ["meeting point"] }
- { value: 5, state: visited, annotations: ["→3"] }
pointers:
"slow, fast": 3
variables:
cycle_detected: true
codeHighlight:
startLine: 8
endLine: 9