提示:本网站现在为维护调试状态

Dynamic Programming: 5 Universal Templates

Choyeon
Choyeon
Sep 16, 2026 1 min read 296 Views
330 words 1,571 chars Updated Nov 8, 2026
Dynamic Programming: 5 Universal Templates
Table of contents

Dynamic Programming: 5 Universal Problem-Solving Templates

DP is the highest-signal topic in algorithm interviews. Core idea: split large problem into overlapping subproblems, store intermediate results to avoid recomputation. Master 5 steps + 5 templates to solve 90% of DP problems.

Universal Five Steps + Knapsack Family

Five steps: (1) Define what dp[i][j] means. (2) Derive transitions by considering last binary choice. (3) Init base cases. (4) Determine correct for-loop order. (5) Apply rolling-array space optimization. 0/1 Knapsack iterates capacity backwards (one item each); Unbounded iterates forwards.

from functools import lru_cache
from typing import List
import bisect

def t1_01_knapsack(cap: int, ws: List[int], vs: List[int]) -> int:
    n = len(ws); dp = [0] * (cap + 1)
    for i in range(n):
        for c in range(cap, ws[i] - 1, -1):
            dp[c] = max(dp[c], dp[c - ws[i]] + vs[i])
    return dp[cap]

def t2_unbounded_knapsack(cap: int, ws: List[int], vs: List[int]) -> int:
    dp = [0] * (cap + 1)
    for c in range(1, cap + 1):
        for i, w in enumerate(ws):
            if w <= c: dp[c] = max(dp[c], dp[c - w] + vs[i])
    return dp[cap]

def t3_lcs(a: str, b: str) -> int:
    m, n = len(a), len(b); prev = [0] * (n + 1)
    for i in range(1, m + 1):
        cur = [0] * (n + 1)
        for j in range(1, n + 1):
            cur[j] = prev[j - 1] + 1 if a[i - 1] == b[j - 1] else max(prev[j], cur[j - 1])
        prev = cur
    return prev[n]

def t4_lis(nums: List[int]) -> int:
    tails = []
    for x in nums:
        i = bisect.bisect_left(tails, x)
        if i == len(tails): tails.append(x)
        else: tails[i] = x
    return len(tails)

def t5_interval_palindrome_cuts(s: str) -> int:
    n = len(s)
    is_pal = [[False] * n for _ in range(n)]
    for i in range(n - 1, -1, -1):
        for j in range(i, n):
            is_pal[i][j] = s[i] == s[j] and (j - i < 3 or is_pal[i + 1][j - 1])
    cut = [float('inf')] * n
    for j in range(n):
        if is_pal[0][j]: cut[j] = 0
        else:
            for i in range(1, j + 1):
                if is_pal[i][j]: cut[j] = min(cut[j], cut[i - 1] + 1)
    return int(cut[n - 1])

if __name__ == "__main__":
    print(t1_01_knapsack(10, [2, 3, 5, 7], [3, 4, 6, 10]))
    print(t3_lcs("abcde", "aceb"), t4_lis([10, 9, 2, 5, 3, 7, 101, 18]))
    print(t5_interval_palindrome_cuts("aabcbdadcb"))

Subsequence / Interval / Bitmask / Tree DP

For two-string subsequence, dp[i][j] encodes first i chars vs first j. Interval DP enumerates by interval length (palindromes/games/stones). Bitmask tracks visited set (TSP). Tree DFS returns two states per child (picked / not-picked).

DP Type State Keyword Traversal Order Example Problem
0/1 Knapsack dp[c] = max value cap c Capacity reverse LC416 Partition Equal Subset
Unbounded KS dp[c] = min coins to reach c Capacity forward LC322 Coin Change
LIS/LCS dp[i] = LIS ending at i Nested i<j loops LC300 / LC1143
Interval DP dp[i][j] = best on [i,j] Length increasing LC132 Palindrome Partition II
Bitmask DP dp[mask] = best for visited set Mask increasing LC847 Shortest Path Visiting All
Tree DP dfs(node) -> (take, no-take) Post-order walk LC337 House Robber III

Best Practices

If transitions feel mysterious, start with brute recursion + memoization and draw the recursion tree. Once you see the shape of overlapping subproblems, convert to bottom-up iterative. Pass samples first; optimize space only when logic is rock solid.

X微博Telegram

About the author

Poll

你最希望 Rosetta 优先加入哪项功能?

社区投票(演示数据,可后台删除)

Comments (2)

0 / 3000
算法小菜鸟
算法小菜鸟Sep 6, 2026

作为一个刚入行的后端新人,这篇文章帮我理清了好多概念,谢谢博主持续输出高质量内容!

DevOps小能手
DevOps小能手devops-lab.cnSep 6, 2026

太及时了,正好项目里要上 Redis 集群,照着来一遍省了好多事,谢谢!

Related posts

Python Concurrency: asyncio vs ThreadPool vs ProcessPool

Python Concurrency: asyncio vs ThreadPool vs ProcessPool

Deep Python concurrency comparison: ThreadPool for IO-bound, ProcessPool bypassing GIL for CPU-bound, asyncio coroutines for ultra IO. 4-dimension decision tree with perf and complexity.

1 min read
RAG in Practice: Embedding + Vector DB + ReRank

RAG in Practice: Embedding + Vector DB + ReRank

Build production-grade RAG from scratch: doc chunking (size + overlap windows), embedding model selection, vector DB retrieval filters, Reranker refining Top-K, plus citation grounding and prompt compression. Complete runnable code included.

1 min read
Alembic Deep Dive: Migrations, Autogenerate, and Branch Merging

Alembic Deep Dive: Migrations, Autogenerate, and Branch Merging

Comprehensive Alembic (official SQLAlchemy migration tool) guide: env.py setup, autogenerate detecting model changes (fields, indexes, constraints), batch scripts, downgrade rollback strategies, plus standard SOP for multi-developer migration branch conflicts and divergent head merging, with production deploy checklist.

1 min read
Quick Start with FastAPI - Building High-Performance REST APIs

Quick Start with FastAPI - Building High-Performance REST APIs

FastAPI stands out among modern Python web frameworks with auto-generated docs based on type hints, async support, and Node.js-level performance. This hands-on guide builds a complete CRUD API from scratch covering route definition, request validation, dependency injection, and database integration—master FastAPI essentials in 30 minutes.

This post requires a password 4 min read
Advanced Nuxt 3 - Deep Dive into SSR and SSG

Advanced Nuxt 3 - Deep Dive into SSR and SSG

Nuxt 3 is built on Vue 3 and Vite, introducing the new Nitro rendering engine. This article deeply analyzes the working principles, use cases, and performance differences of three core modes: Server-Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR). With code demos, learn to configure rendering strategies, optimize first-screen load time, and implement caching policies.

1 min read
Go Concurrency: Goroutines, Channels & sync.WaitGroup

Go Concurrency: Goroutines, Channels & sync.WaitGroup

Complete Go native concurrency guide: lightweight goroutines, CSP-style channels, sync.WaitGroup, Context cancellation, worker pool patterns, and goroutine leak prevention.

1 min read