跳到正文
产品桌面工具TASTECAD图纸查看TASTEFILES文件管理TASTEFTP文件传输TASTESSH远程工作空间TASTEZIP压缩与解压开发工具TASTECODERust 集成开发环境TASTESTUDIO集成开发与测试环境TASTEETL数据整合(ETL)服务器TASTEREMOTE远程桌面服务器TASTEGEO空间信息服务器TASTEMAIL邮件工作空间开始使用下载文档购买全部产品
市场
资讯
技术资讯今日开发资讯开发文档编程语言RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express应用与桌面框架TauriFlutterReact NativeElectron算法算法基础核心算法进阶算法实用算法
社区项目开发日志提示词AI 工具问题作品展示社区指南
合作技术传授网站构建桌面应用构建发布与支付流水线销售合作伙伴联系我们
服务远程桌面安装包定制
TASTEDEV
产品

桌面工具

TASTECAD复杂图纸,轻松打开TASTEFILES只要需要的文件,最快送达TASTEFTP让文件往来,干净利落TASTESSH你与服务器之间,连接更牢靠TASTEZIP压得更快,解得更稳

开发工具

TASTECODE在一个窗口中完成 Rust 应用的编辑、构建、调试和测试TASTESTUDIO从编辑到远程测试,再到经批准的 AI 修复TASTEETL边看数值,边搬数据

服务器

TASTEREMOTE在自己的服务器上运行的远程桌面TASTEGEO用 Rust 重新构建兼容 GeoServer 的空间信息服务器TASTEMAIL把邮件的主导权拿回自己手中

开始使用

下载所有产品的最新版本文档各产品使用方法购买订阅与永久许可全部产品一览全部产品
全部产品
市场资讯
技术

资讯

今日开发资讯语言、框架、GitHub 与 AI 官方发布开发文档语言、框架与算法指南

编程语言

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

框架

Web 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular
服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express
应用与桌面框架TauriFlutterReact NativeElectron

算法

算法基础复杂度分析 · 排序 · 查找 · 基础数据结构 · 树与堆核心算法图遍历 · 最短路径与最小生成树 · 动态规划 · 贪心算法 · 分治法 · 回溯法进阶算法字符串算法 · 数学与数论 · 位运算 · 线段树 · 并查集实用算法编程面试解题策略 · LRU 缓存 · 限流 · 一致性哈希
查看全部开发文档
社区
项目发现开发者与作品开发日志决策与试错提示词可复用的指令AI 工具问题Claude、Codex、Gemini 的问题与变通办法作品展示已完成的成果社区指南我们如何公开构建
合作
技术传授用 Rust 和 egui 打造原生应用网站构建10 种语言、账户、文档与下载桌面应用构建Rust 和 egui,不用 WebView发布与支付流水线签名、MSI、deb、rpm、AppImage、哈希销售合作伙伴推荐即得 30%联系我们开发、合作与投资咨询
服务
远程桌面用浏览器连接你的电脑安装包定制只装需要的,设置预先放好
购买
ZH
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
登录
登录
TASTEDEV

从小工具到更好的开发。

support@tastedev.net支持 TASTEDEV
产品产品线购买文档下载市场客户支持
社区项目开发日志提示词AI 工具问题作品展示
政策隐私政策服务条款订阅条款退款政策社区规范
账户登录注册
访客
今天
012345678901234567890123456789012345678901234567890123456789
昨天
012345678901234567890123456789012345678901234567890123456789
累计浏览
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
公司名称
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
讨论

动态规划 指南 · 5/6

Practice Problems

本章目前仅提供英文版。

These five problems were written for this guide. Try each one before reading the approach: write down the state, the recurrence, the base cases and the order first, then code it. All solutions run with Python 3.9 or later.

Problem 1: stairs with broken steps

A staircase has n steps. You start on step 0 and may climb 1, 2 or 3 steps at a time, but some steps are broken and cannot be stepped on. How many different ways are there to reach step n? Return the count modulo 1_000_000_007.

Example: with n = 4 and step 2 broken, the valid routes are 0-1-4, 0-3-4 and 0-1-3-4, so the answer is 3. Routes such as 0-2-4 or 0-1-2-3-4 are invalid because they touch step 2.

Approach: ways[i] = number of ways to stand on step i. Broken steps have 0 ways; otherwise ways[i] = ways[i-1] + ways[i-2] + ways[i-3]. Base case ways[0] = 1.

MOD = 1_000_000_007


def count_climbs(n: int, broken: set[int]) -> int:
    ways = [0] * (n + 1)
    ways[0] = 1
    for i in range(1, n + 1):
        if i in broken:
            continue
        ways[i] = sum(ways[i - s] for s in (1, 2, 3) if i - s >= 0) % MOD
    return ways[n]


print(count_climbs(4, {2}))  # 3
print(count_climbs(30, set()))  # 53798080

Problem 2: cheapest route across a grid

A warehouse floor is a grid of tolls. A robot starts at the top-left cell and must reach the bottom-right cell, moving only right or down, paying the toll of every cell it enters (including the first). Find the minimum total toll.

Approach: cost[r][c] = cheapest total to arrive at cell (r, c). Each cell is reached from above or from the left, so cost[r][c] = grid[r][c] + min(above, left). A single row suffices because each row only needs the row above.

def cheapest_route(grid: list[list[int]]) -> int:
    cols = len(grid[0])
    row = [0] * cols
    for r, line in enumerate(grid):
        for c, toll in enumerate(line):
            if r == 0 and c == 0:
                row[c] = toll
            elif r == 0:
                row[c] = row[c - 1] + toll
            elif c == 0:
                row[c] = row[c] + toll
            else:
                row[c] = min(row[c], row[c - 1]) + toll
    return row[-1]


print(cheapest_route([[1, 3, 1], [1, 5, 1], [4, 2, 1]]))  # 7

Problem 3: typo distance

A search box wants to suggest the closest product name to what a user typed. Define the distance between two words as the fewest single-character insertions, deletions or substitutions needed to turn one into the other (Levenshtein distance). Compute it.

Approach: d[i][j] = distance between the first i characters of s and the first of . If the characters match, ; otherwise it is 1 plus the minimum of delete (), insert () and substitute (). Base cases: , .

对本章有疑问?
上一章Complexity下一章Real-World Use
j
t
d[i][j] = d[i-1][j-1]
d[i-1][j]
d[i][j-1]
d[i-1][j-1]
d[i][0] = i
d[0][j] = j
def edit_distance(s: str, t: str) -> int:
    prev = list(range(len(t) + 1))
    for i, a in enumerate(s, start=1):
        cur = [i] + [0] * len(t)
        for j, b in enumerate(t, start=1):
            if a == b:
                cur[j] = prev[j - 1]
            else:
                cur[j] = 1 + min(prev[j], cur[j - 1], prev[j - 1])
        prev = cur
    return prev[-1]


print(edit_distance("keybaord", "keyboard"))  # 2
print(edit_distance("kitten", "sitting"))    # 3

Problem 4: fair split of a prize pool

A team won several prizes with integer values. Can they be divided into two groups with exactly equal total value?

Approach: if the total is odd, the answer is no. Otherwise ask whether some subset sums to total // 2. That is a 0/1 knapsack with booleans: can[s] is true when some subset of the items seen so far sums to s. Iterate sums from high to low so each prize is used once.

def can_split_evenly(prizes: list[int]) -> bool:
    total = sum(prizes)
    if total % 2:
        return False
    target = total // 2
    can = [True] + [False] * target
    for p in prizes:
        for s in range(target, p - 1, -1):
            can[s] = can[s] or can[s - p]
    return can[target]


print(can_split_evenly([3, 1, 5, 9, 4]))  # False (total 22, no subset sums to 11)
print(can_split_evenly([3, 1, 5, 9, 2]))  # True (9 + 1 = 3 + 5 + 2)

Problem 5: longest warming streak

Given daily temperatures, find the longest sequence of days (not necessarily consecutive) on which each day was strictly warmer than the previous chosen day, and return one such sequence.

Approach: the O(n^2) LIS is easy to reconstruct. length[i] is the longest increasing subsequence ending at day i, and parent[i] remembers the previous day in that subsequence.

def warming_streak(temps: list[int]) -> list[int]:
    if not temps:
        return []
    n = len(temps)
    length = [1] * n
    parent = [-1] * n
    for i in range(n):
        for j in range(i):
            if temps[j] < temps[i] and length[j] + 1 > length[i]:
                length[i] = length[j] + 1
                parent[i] = j
    i = max(range(n), key=length.__getitem__)
    streak = []
    while i != -1:
        streak.append(temps[i])
        i = parent[i]
    return streak[::-1]


print(warming_streak([12, 9, 14, 10, 15, 11, 18, 13]))  # [12, 14, 15, 18]

For long inputs, combine the bisect version from the complexity chapter with an index array to reconstruct in O(n log n).

Summary

  • Counting problems add over choices; optimization problems take a min or max.
  • Grid and string problems use two indices, but often need only one row of memory.
  • Many "is it possible" questions reduce to a boolean knapsack.
  • Store a parent pointer whenever you must output the solution, not just its size.

0 条评论

热门评论最新优先

登录 · 登录后即可发表评论。