Fullstar

Archives

  • December 2025
  • August 2024
  • July 2024
  • February 2024
  • November 2023
  • August 2023
  • July 2023
  • January 2023
  • November 2022
  • October 2022
  • September 2022
  • February 2022
  • January 2022
  • September 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020

Categories

  • Code
  • Lens
  • Life
0
Fullstar
  • Code

DP问题专项

  • November 24, 2022
  • Brandon
Total
0
Shares
0
0
0

一. General Idea

DP ≈ recursion + memorization + guessing
memorize(remember) & reuse solutions to subproblems that help solve the problem.
time = #subproblems * time/subproblem, treating recursive call as Θ(1).

Bottom-up DP algorithm: topological sort of subproblem dependency DAG
often can save space

Guessing: try all guesses and take the best one

Shortest paths:
guess: 既然不知道哪条路到最后的节点最短那就全试一遍,然后选最短的路(无法解决环路问题)

为解决环路问题,引入了k

二. 5 “easy” steps to DP:

  1. define subproblems
    #subproblems
  2. guess
    #choices for guess
  3. relate subproblem solutions
    time/subproblem (similar to #choices for guess)
  4. recurse & memorize or build DP table bottom-up
    check subproblem recurrence is acyclic, i.e. has topological order
    total time
  5. solve original problem
    extra time

三. Subproblems for strings/sequences

  1. suffixes x[i:] ∀i
  2. prefixes x[:i] ∀i
  3. substrings x[i:j] ∀i≤j
    once find I need to use both 1 and 2, then mostly I need to use 3

四. Kinds of guessing

1 in 2(&3): guessing which subproblem to use to solve the bigger subproblem

2 in 1: add more subproblems to guess/remember more

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Related Topics
  • leetcode
Brandon

Previous Article
  • Code

Emacs配置python环境

  • November 7, 2022
  • Brandon
View Post
Next Article
  • Code

Emacs for Java

  • January 16, 2023
  • Brandon
View Post
You May Also Like
View Post
  • Code

WordPress 后台任务利器:使用 BGRunner 构建可靠的异步处理

  • Brandon
  • December 14, 2025
View Post
  • Code

WordPress image offload

  • Brandon
  • December 14, 2025
View Post
  • Code

ComfyUI应用手册

  • Brandon
  • December 6, 2025
View Post
  • Code

Leetcode Java常用代码

  • Brandon
  • February 17, 2024
View Post
  • Code

Golang入门

  • Brandon
  • February 4, 2024
View Post
  • Code

Setting Up and Maintaining a Ubuntu Environment for My Home Server

  • Brandon
  • November 24, 2023
View Post
  • Code

Swift Learning Log

  • Brandon
  • August 31, 2023
View Post
  • Code

English Learning – Food Related

  • Brandon
  • August 31, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Fullstar

Input your search keywords and press Enter.