sequenzia/agent-alchemy/dp-patterns

Claude Code and agent skill by sequenzia/agent-alchemy

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Reference for dynamic programming patterns — recognition signals, constraint mapping, Python templates, edge cases, and common mistakes for 8 core DP techniques

Install

npx vskill@latest install sequenzia/agent-alchemy/dp-patterns

SKILL.md

The same SKILL.md was published earlier as sequenzia/agent-alchemy/dp-patterns.

Dynamic Programming Patterns

This reference covers eight foundational dynamic programming patterns commonly encountered in algorithmic problem solving. Each pattern includes recognition signals to identify when a problem maps to the technique, a working Python template, and specific edge cases and mistakes to watch for. Load this skill when solving optimization, counting, or subsequence problems that exhibit optimal substructure and overlapping subproblems.

General DP Approach

Before selecting a specific pattern, apply this checklist:

  1. Identify the state: What information do you need to uniquely describe a subproblem? (index, remaining capacity, bitmask of visited nodes)
  2. Define the recurrence: How does the answer for the current state relate to answers for smaller states?
  3. Establish base cases: What are the trivial subproblems with known answers?
  4. Determine iteration order: Ensure every state is computed before it is needed (bottom-up) or use memoization (top-down)
  5. Optimize space: If dp[i] depends only on dp[i-1] (or a small window), use rolling arrays instead of the full table

Pattern Recognition Table

Trigger SignalsTechniqueTypical Complexity
"count the number of ways", "how many distinct paths"Fibonacci / Climbing StairsO(N) time, O(1) space
"maximize value with weight limit", "partition into two equal subsets"0/1 KnapsackO(N * W) time, O(W) space
"longest common subsequence", "minimum edit operations"LCS / Edit DistanceO(N * M) time, O(min(N, M)) space
"longest increasing subsequence", "minimum number of envelopes"LISO(N log N) time, O(N) space
"unique paths in grid", "minimum cost to reach bottom-right"Grid DPO(R * C) time, O(C) space
"maximum subarray sum", "best contiguous segment"Kadane's AlgorithmO(N) time, O(1) space
"fewest coins to make amount", "ways to combine denominations"Coin Change / Unbounded KnapsackO(N * amount) time, O(amount) space
"visit all nodes exactly once", "assign N items optimally", N <= 20Bitmask DPO(2^N * N) time, O(2^N) space

Constraint-to-Technique Mapping

Use input constraints to narrow the viable DP approach:

Constraint RangeViable Approaches
N <= 20Bitmask DP, brute-force with memoization
N <= 500O(N^3) DP (interval DP, Floyd-Warshall)
N <= 5,000O(N^2) DP (LIS quadratic, 2D tables)
N <= 100,000O(N log N) DP (LIS with binary search, segment tree optimization)
N <= 1,000,000O(N) DP (Fibonacci-style, Kadane, sliding window)
N * W <= 10^7Knapsack variants (0/1 or unbounded)
Grid R * C <= 10^6Standard grid DP with rolling array
Fibonacci / Climbing Stairs

Recognition Signals

  • "How many ways to reach step N"
  • "Count distinct ways to combine steps of size 1, 2, ..."
  • "Decode ways" (how many ways to decode a digit string into letters)
  • Recurrence follows f(n) = f(n-1) + f(n-2) or a generalized k-step variant

Core Idea The answer at position N depends only on a fixed number of prior positions. Build the result iteratively from the base cases, keeping only the last k values in memory. This generalizes beyond two-step Fibonacci to any linear recurrence. When the recurrence has a fixed number of terms, space reduces to O(k) where k is the number of terms. For very large N (e.g., N > 10^18), matrix exponentiation can compute the result in O(k^3 log N).

Python Template

python
def climb_stairs(n: int, steps: list[int] | None = None) -> int:
"""Count ways to reach step n using allowed step sizes."""
if steps is None:
steps = [1, 2]
max_step = max(steps)
# dp[i] = number of ways to reach step i
dp: list[int] = [0] * (max_step + 1)
dp[0] = 1 # base case: one way to stay at ground
for i in range(1, n + 1):
total = 0
for s in steps:
if i - s >= 0:
total += dp[(i - s) % (max_step + 1)]
dp[i % (max_step + 1)] = total
return dp[n % (max_step + 1)]

Key Edge Cases

  • n = 0: exactly 1 way (do nothing)
  • n = 1 with only step size 2: 0 ways (unreachable)
  • Large n with modular arithmetic required (typically mod 10^9 + 7)
  • Negative step sizes are not valid; guard input

Common Mistakes

  • Forgetting the base case dp[0] = 1 and getting all zeros
  • Off-by-one in the loop range (should go up to n inclusive)
  • Not applying modular arithmetic early, causing integer overflow in languages with fixed-width integers
0/1 Knapsack

Recognition Signals

  • "Maximize total value without exceeding capacity W"
  • "Can you partition the array into two subsets with equal sum"
  • "Select a subset such that the sum equals target"
  • Each item can be used at most once

Core Idea For each item, decide to include or exclude it. Build a 1D DP array where dp[w] indicates the best achievable value (or a boolean for subset sum) using capacity w. Iterate items in the outer loop and capacity in reverse to ensure each item is used at most once. Common variants include subset sum (boolean dp), partition equal subset sum (check if total/2 is achievable), and target sum with +/- signs (offset the DP index by a constant).

Python Template

python
def knapsack_01(weights: list[int], values: list[int], capacity: int) -> int:
"""Return maximum value achievable within capacity."""
n = len(weights)
dp: list[int] = [0] * (capacity + 1)
for i in range(n):
# Traverse capacity in reverse to avoid reusing item i
for w in range(capacity, weights[i] - 1, -1):
dp[w] = max(dp[w], dp[w - weights[i]] + values[i])
return dp[capacity]

Key Edge Cases

  • All items heavier than capacity: answer is 0
  • Capacity is 0: answer is 0 regardless of items
  • Single item that exactly matches capacity
  • Subset sum variant: dp is boolean, dp[0] = True

Common Mistakes

  • Iterating capacity forward instead of reverse, which allows items to be picked multiple times (that is unbounded knapsack)
  • Confusing 0/1 knapsack with unbounded knapsack when items are reusable
  • Not handling the case where weights[i] > capacity (skip the inner loop for that item)
Longest Common Subsequence (LCS)

Recognition Signals

  • "Find the longest common subsequence of two strings"
  • "Minimum number of insertions and deletions to transform A into B"
  • "Edit distance between two strings"
  • Two sequences compared element by element

Core Idea Build a 2D table where dp[i][j] represents the LCS length of the first i characters of A and first j characters of B. If characters match, extend the diagonal; otherwise take the max of skipping from either side. Edit distance uses the same table structure with insertion, deletion, and substitution costs, where each operation adds 1 (or a weighted cost) and the goal switches from maximizing to minimizing. The "minimum deletions to make two strings equal" variant is directly: len(A) + len(B) - 2 * LCS(A, B).

Python Template

python
def lcs(a: str, b: str) -> int:
"""Return length of the longest common subsequence."""
m, n = len(a), len(b)
# Use two rows to reduce space from O(m*n) to O(min(m,n))
if m < n:
a, b, m, n = b, a, n, m
prev: list[int] = [0] * (n + 1)
curr: list[int] = [0] * (n + 1)
for i in range(1, m + 1):
for j in range(1, n + 1):
if a[i - 1] == b[j - 1]:
curr[j] = prev[j - 1] + 1
else:
curr[j] = max(prev[j], curr[j - 1])
prev, curr = curr, [0] * (n + 1)
return prev[n]

Key Edge Cases

  • One or both strings empty: LCS is 0
  • Strings are identical: LCS equals the full length
  • No common characters at all: LCS is 0
  • Very long strings (space optimization with rolling rows is critical)

Common Mistakes

  • Mixing up 0-indexed and 1-indexed access between the string and the DP table
  • Forgetting to reset the curr row between iterations when using rolling arrays
  • Reconstructing the actual subsequence requires storing the full table or backtracking pointers, which the space-optimized version does not support
Longest Increasing Subsequence (LIS)

Recognition Signals

  • "Find the length of the longest strictly increasing subsequence"
  • "Minimum number of envelopes that nest inside each other"
  • "Longest chain of pairs"
  • Ordering constraint on a single sequence

Core Idea The O(N^2) approach builds dp[i] as the LIS ending at index i by checking all j < i where nums[j] < nums[i]. The O(N log N) approach (patience sorting) maintains a tails array where tails[k] is the smallest tail element of any increasing subsequence of length k+1. For each element, binary search for its position in tails to either extend or replace. To reconstruct the actual subsequence, maintain a parent pointer array alongside the tails.

Python Template

python
import bisect
def lis_length(nums: list[int]) -> int:
"""Return length of longest strictly increasing subsequence. O(N log N)."""
tails: list[int] = []
for num in nums:
pos = bisect.bisect_left(tails, num)
if pos == len(tails):
tails.append(num) # extend the longest subsequence
else:
tails[pos] = num # replace to keep smallest tail
return len(tails)

Key Edge Cases

  • Empty array: LIS is 0
  • All elements equal: LIS is 1 (strictly increasing requires distinct values)
  • Already sorted in increasing order: LIS equals array length
  • Non-strictly increasing (allow equal): use bisect_right instead of bisect_left

Common Mistakes

  • Using bisect_right for strictly increasing (gives non-decreasing subsequence instead)
  • Assuming the tails array is the actual LIS (it is not; it only gives the correct length)
  • Forgetting to handle the 2D variant (e.g., Russian doll envelopes) by sorting one dimension ascending and the second dimension descending before applying LIS on the second dimension
  • Confusing "longest non-decreasing subsequence" with "longest strictly increasing subsequence" when duplicate values exist
Grid DP

Recognition Signals

  • "How many unique paths from top-left to bottom-right"
  • "Minimum cost path in a grid moving only right or down"
  • "Fill a matrix where each cell depends on its top and left neighbors"
  • "Maximum cherries/gold you can collect traversing a grid" (multi-path grid DP)

Core Idea Process cells row by row (or column by column). Each cell's value depends on its top neighbor and left neighbor. Use a single row array to reduce space: the current value at dp[c] is the "top" neighbor, and dp[c-1] is the "left" neighbor already updated in this row. For "unique paths" counting, the value is the sum of top and left; for "minimum path sum", take the min of top and left plus the current cell cost. Obstacles are handled by zeroing out blocked cells.

Python Template

python
def min_path_sum(grid: list[list[int]]) -> int:
"""Return minimum path sum from top-left to bottom-right."""
rows, cols = len(grid), len(grid[0])
dp: list[int] = [0] * cols
dp[0] = grid[0][0]
# First row: can only come from the left
for c in range(1, cols):
dp[c] = dp[c - 1] + grid[0][c]
# Remaining rows
for r in range(1, rows):
dp[0] += grid[r][0] # first column: can only come from above
for c in range(1, cols):
dp[c] = min(dp[c], dp[c - 1]) + grid[r][c]
return dp[cols - 1]

Key Edge Cases

  • Single row or single column grid: path is forced, just sum all cells
  • 1x1 grid: answer is the single cell value
  • Grid with negative values: min-path logic still works, but "unique paths" counting is unaffected by values
  • Obstacles (cells marked as blocked): set dp to infinity (min-path) or 0 (counting)

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
42
Repository forks
7
7-day trend score
19

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/sequenzia/agent-alchemy/dp-patterns/badge)](https://verified-skill.com/skills/sequenzia/agent-alchemy/dp-patterns)

Something wrong with this skill or its page? Tell us.