{
    "slug": "dynamic_programming",
    "term": "Dynamic Programming",
    "category": "algorithms",
    "difficulty": "advanced",
    "short": "An optimisation technique that solves problems by breaking them into overlapping subproblems, storing results to avoid redundant computation.",
    "long": "Dynamic programming applies when a problem has optimal substructure (optimal solution built from optimal subproblems) and overlapping subproblems (same subproblem solved multiple times). Two approaches: top-down memoisation (recursive with a cache) and bottom-up tabulation (iterative, fill a table). Classic examples: Fibonacci, longest common subsequence, knapsack problem. In web development, DP appears in query result caching, diff algorithms, and compiler optimisations.",
    "aliases": [
        "DP",
        "memoisation",
        "tabulation"
    ],
    "tags": [
        "algorithms",
        "optimisation",
        "computer-science"
    ],
    "misconception": "Dynamic programming always requires a multidimensional array — many DP problems only need O(n) or O(1) space by keeping only the last few rows.",
    "why_it_matters": "Recognising that an exponential naive solution has overlapping subproblems lets you transform it to polynomial time — the difference between unusable and instant at scale.",
    "common_mistakes": [
        "Naive recursion without memoisation for Fibonacci or similar — O(2ⁿ) instead of O(n).",
        "Allocating a full 2D DP table when only the previous row is needed — wastes O(n²) memory.",
        "Not identifying the recurrence relation before coding — DP without the relation is just guessing.",
        "Confusing DP with greedy algorithms — greedy makes locally optimal choices, DP explores all subproblems."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "recursion_patterns",
        "big_o_notation",
        "memoization",
        "time_complexity"
    ],
    "prerequisites": [
        "recursion_patterns",
        "memoization",
        "big_o_notation"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Dynamic_programming"
    ],
    "bad_code": "// Naive Fibonacci — O(2ⁿ) — recalculates same values exponentially:\nfunction fib(int $n): int {\n    if ($n <= 1) return $n;\n    return fib($n - 1) + fib($n - 2); // fib(40) = ~1 billion calls\n}",
    "good_code": "// Memoised — O(n) time, O(n) space:\nfunction fib(int $n, array &$memo = []): int {\n    if ($n <= 1) return $n;\n    return $memo[$n] ??= fib($n - 1, $memo) + fib($n - 2, $memo);\n}\n\n// Bottom-up tabulation — O(n) time, O(1) space:\nfunction fibTab(int $n): int {\n    [$a, $b] = [0, 1];\n    for ($i = 2; $i <= $n; $i++) [$a, $b] = [$b, $a + $b];\n    return $b;\n}",
    "quick_fix": "If a recursive solution recomputes the same subproblems, add memoization (top-down) or switch to tabulation (bottom-up) — store results in an array keyed by input",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/dynamic_programming",
        "html_url": "https://codeclaritylab.com/glossary/dynamic_programming",
        "json_url": "https://codeclaritylab.com/glossary/dynamic_programming.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Dynamic Programming](https://codeclaritylab.com/glossary/dynamic_programming) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/dynamic_programming"
            }
        }
    }
}