本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

動的計画法 ドキュメント
動的計画法
ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
ディスカッション

動的計画法 ガイド · 2/6

How It Works

この章は現在、英語でのみ提供しています。

Most DP solutions are designed with the same five questions. This chapter applies them to two classic problems, minimum coin change and longest common subsequence (LCS), and traces the tables by hand so you can see the answers being built.

A five-step recipe

  1. State: what parameters identify a subproblem? Pick the smallest set that still determines the answer.
  2. Recurrence: how is a state's answer computed from smaller states? This usually means "try every choice and keep the best".
  3. Base cases: which states are trivial?
  4. Order: in which order must states be computed so that dependencies are ready? (Memoization finds the order automatically.)
  5. Answer: which state holds the final result, and do you also need to reconstruct the choices?

Example 1: minimum coin change

Given coin denominations [1, 3, 4] (unlimited supply of each) and an amount of 6, what is the fewest number of coins that adds up to exactly 6?

A greedy approach takes the largest coin first: 4, then 1, then 1, which is three coins. But 3 + 3 uses only two. Greedy fails, so we use DP.

  • State: dp[a] = fewest coins that sum to amount a.
  • Recurrence: dp[a] = 1 + min(dp[a - c]) over every coin c with c <= a.
  • Base case: dp[0] = 0. Unreachable amounts stay at infinity.
  • Order: increasing a, because dp[a] depends only on smaller amounts.
  • Answer: dp[6].

Filling the table from left to right:

aCandidates (coin: 1 + dp[a - coin])dp[a]Last coin
0base case0-
11: 1 + 011
21: 1 + 121
31: 1 + 2, 3: 1 + 013
41: 1 + 1, 3: 1 + 1, 4: 1 + 014
51: 1 + 1, 3: 1 + 2, 4: 1 + 121
61: 1 + 2, 3: 1 + 1, 4: 1 + 223

The answer is dp[6] = 2. Following the "Last coin" column backwards gives the coins themselves: from 6 take a 3 and move to 3, take a 3 and move to 0.

この章について質問がありますか?
前の章Concepts次の章Implementation
import math def min_coins(coins: list[int], amount: int) -> tuple[int, list[int]]: dp = [0] + [math.inf] * amount last = [0] * (amount + 1) for a in range(1, amount + 1): for c in coins: if c <= a and dp[a - c] + 1 < dp[a]: dp[a] = dp[a - c] + 1 last[a] = c if dp[amount] == math.inf: return -1, [] used, a = [], amount while a > 0: used.append(last[a]) a -= last[a] return dp[amount], used print(min_coins([1, 3, 4], 6)) # (2, [3, 3]) print(min_coins([5, 10], 3)) # (-1, [])

The same recurrence written top-down reads almost like the definition. The cache makes sure each amount is solved only once.

import math
from functools import cache


def min_coins(coins: tuple[int, ...], amount: int) -> int:
    @cache
    def best(a: int) -> float:
        if a == 0:
            return 0
        return min((1 + best(a - c) for c in coins if c <= a), default=math.inf)

    result = best(amount)
    return -1 if result == math.inf else int(result)


print(min_coins((1, 3, 4), 6))  # 2

Example 2: longest common subsequence

A subsequence keeps characters in order but may skip some. For a = "ABCBA" and b = "BCAB", what is the longest string that is a subsequence of both?

  • State: L[i][j] = LCS length of the first i characters of a and the first j characters of b.
  • Recurrence: if a[i-1] == b[j-1], then L[i][j] = L[i-1][j-1] + 1; otherwise L[i][j] = max(L[i-1][j], L[i][j-1]).
  • Base cases: L[0][j] = L[i][0] = 0 (an empty prefix shares nothing).
  • Order: row by row, left to right. Each cell needs the cell above, to the left, and diagonally up-left.
  • Answer: L[5][4].
""BCAB
""00000
A00011
B01112
C01222
B01223
A01233

The bottom-right cell says the LCS has length 3. To recover one, start there and walk back: on a match, take the character and move diagonally; otherwise move toward the larger neighbor. Ties can lead to different but equally long answers, such as "BCB" and "BCA".

def lcs_table(a: str, b: str) -> list[list[int]]:
    L = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
    for i in range(1, len(a) + 1):
        for j in range(1, len(b) + 1):
            if a[i - 1] == b[j - 1]:
                L[i][j] = L[i - 1][j - 1] + 1
            else:
                L[i][j] = max(L[i - 1][j], L[i][j - 1])
    return L


for row in lcs_table("ABCBA", "BCAB"):
    print(row)
# last row printed: [0, 1, 2, 3, 3]

Summary

  • Design a DP by answering five questions: state, recurrence, base cases, order, answer.
  • Tracing a small table by hand is the fastest way to check a recurrence.
  • Store the choice made at each state (or walk the table backwards) to reconstruct the actual solution, not just its value.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。