Dynamic Programming
Overview
Use this skill to design correct and efficient DP solutions with explicit state modeling and complexity reasoning.
Scope Boundaries
- Use this skill when the task matches the trigger condition described in
description. - Do not use this skill when the primary task falls outside this skill's domain.
Inputs To Gather
- Problem objective and correctness constraints.
- State variables needed to represent subproblems.
- Transition rules and dependency ordering.
- Input size limits and memory constraints.
Deliverables
- DP formulation (state, transition, base cases).
- Complexity analysis (time/space) and optimization options.
- Chosen implementation strategy (top-down/bottom-up).
- Edge-case and correctness verification plan.
Quick Example
- Problem: minimum cost path.
- State:
dp[i][j]= min cost to reach cell(i,j). - Transition:
dp[i][j] = cost[i][j] + min(dp[i-1][j], dp[i][j-1]). - Base: first row/column initialization.
Quality Standard
- State definition is complete and non-redundant.
- Transition uses only valid predecessor states.
- Base cases cover minimal subproblems correctly.
- Complexity fits constraints or includes optimization plan.
Workflow
- Define subproblem state and objective function.
- Derive transitions and base cases.
- Choose memoization or tabulation strategy.
- Optimize memory if full table is unnecessary.
- Validate against edge cases and known examples.
Failure Conditions
- Stop when state does not capture required decision context.
- Stop when transition introduces cyclic/invalid dependencies.
- Escalate when complexity exceeds target constraints without viable optimization.