返回 Skill 列表
extension
分类: 开发与工程无需 API Key

dynamic-programming

通过定义状态、转换、基本情况和优化策略(记忆化/表格化)来设计动态规划解决方案。当最优子结构和重叠子问题使得暴力或贪心方法不够用时使用;不要用于持久化模式或部署拓扑决策。

person作者: jakexiaohubgithub

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

  1. Define subproblem state and objective function.
  2. Derive transitions and base cases.
  3. Choose memoization or tabulation strategy.
  4. Optimize memory if full table is unnecessary.
  5. 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.