Dynamic programming

http://blog.csdn.net/linhuanmars/article/details/38468361arrow-up-right

DP:
1.状态:即定义,中间的状态
 2.方程:即从前一种状态推出现在的状态.
 3.初始化:极限小的状态,即为起点.
 4.答案:终点

 递归:
 1. 定义(状态)
    • 接受什么参数
    • 做了什么事
    • 返回什么值
 2. 拆解(方程)
    • 如何将参数变小
 3. 出口(初始化)
    • 什么时候可以直接 return
坐标型  20%
序列型  20%
划分型  20%
区间型  15%
背包型  10%
拓扑型  5%
博弈型  5%
综合型  5%

state machine

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/arrow-up-right

坐标型 20%

f[i] 第i个

https://leetcode.com/problems/word-break/description/arrow-up-right https://leetcode.com/problems/concatenated-words/description/arrow-up-right https://leetcode.com/problems/unique-paths/arrow-up-right

Divide & conquer, solve with 2 pass: order and reversed order https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/arrow-up-right https://leetcode.com/problems/bomb-enemyarrow-up-right https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/description/arrow-up-right

LIS problem https://leetcode.com/problems/longest-increasing-subsequencearrow-up-right https://leetcode.com/problems/russian-doll-envelopes/description/arrow-up-right https://leetcode.com/problems/increasing-triplet-subsequencearrow-up-right https://leetcode.com/problems/maximum-length-of-pair-chainarrow-up-right https://leetcode.com/problems/number-of-longest-increasing-subsequencearrow-up-right https://leetcode.com/problems/increasing-subsequences/description/arrow-up-right https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/arrow-up-right

序列型 20%

f[i] 前i个

  • can be O(1) variable or O(n)/1D variable

these problems are conditional, discuss different situation:

https://leetcode.com/problems/paint-fence/description/arrow-up-right https://leetcode.com/problems/paint-house/description/arrow-up-right https://leetcode.com/problems/paint-house-ii/description/arrow-up-right https://leetcode.com/problems/maximum-vacation-days/description/arrow-up-right https://leetcode.com/problems/house-robber/arrow-up-right https://leetcode.com/problems/house-robber-ii/description/arrow-up-right https://leetcode.com/problems/climbing-stairs/arrow-up-right https://leetcode.com/problems/maximum-subarray/description/arrow-up-right

局部最优和全局最优 https://leetcode.com/problems/maximum-product-subarray/description/arrow-up-right https://leetcode.com/problems/maximum-subarrayarrow-up-right

Stocks: the whole series key point: buy and sell must cannot happen together, because if price go down, it's buy point, selling here get 0 profit, meaningless. if price go up, it's sell point, won't buy. buy2 must happen before sell1, because if not, buy2 will be negative, won't update.

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iiarrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iiiarrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/arrow-up-right

双序列型

https://leetcode.com/problems/edit-distance/arrow-up-right https://leetcode.com/problems/regular-expression-matching/arrow-up-right https://leetcode.com/problems/wildcard-matching/description/arrow-up-right https://leetcode.com/problems/distinct-subsequences/description/arrow-up-right https://leetcode.com/problems/minimum-window-subsequence/description/arrow-up-right https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-stringsarrow-up-right https://leetcode.com/problems/delete-operation-for-two-stringsarrow-up-right

划分型 20%

https://leetcode.com/problems/decode-ways/arrow-up-right https://leetcode.com/problems/decode-ways-iiarrow-up-right https://leetcode.com/problems/house-robber-iiiarrow-up-right

区间型 15%

https://leetcode.com/problems/longest-palindromic-substring/description/arrow-up-right

背包型 10% coin change

https://blog.csdn.net/wzy_1988/article/details/12260343arrow-up-right

https://leetcode.com/problems/partition-equal-subset-sum/description/arrow-up-right https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/arrow-up-right https://leetcode.com/problems/combination-sum-iv/description/arrow-up-right https://leetcode.com/problems/coin-change/description/arrow-up-right https://leetcode.com/problems/target-sum/description/arrow-up-right https://leetcode.com/problems/ones-and-zeroes/description/arrow-up-right https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/description/arrow-up-right https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/description/arrow-up-right https://leetcode.com/problems/house-robber-iiarrow-up-right

from 1 to k

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/arrow-up-right https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/arrow-up-right https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/description/arrow-up-right

subproblem

these problems are not DP, but there ideas are similar: solving subproblem. https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/arrow-up-right https://leetcode.com/problems/count-of-range-sum/description/arrow-up-right https://leetcode.com/problems/reverse-pairs/description/arrow-up-right

dp + divide&conquer

it's not only one subproblem, instead, many subproblems need to be compared or processed. https://leetcode.com/problems/encode-string-with-shortest-length/description/arrow-up-right https://leetcode.com/problems/unique-binary-search-trees/description/arrow-up-right

MiniMax problem (3D DP)

most game problem https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/arrow-up-right https://leetcode.com/problems/can-i-win/description/arrow-up-right

3 for-loop -> DFS + Memo https://leetcode.com/problems/split-array-largest-sum/description/arrow-up-right https://leetcode.com/problems/burst-balloons/arrow-up-right

https://leetcode.com/problems/largest-sum-of-averages/description/arrow-up-right https://leetcode.com/problems/knight-probability-in-chessboard/description/arrow-up-right

https://leetcode.com/problems/encode-string-with-shortest-length/description/arrow-up-right

https://leetcode.com/discuss/general-discussion/592146/dynamic-programming-summary/513130arrow-up-right

1. Number Tower,数塔

2. Fibonacci Numbers,斐波那契数列

3. Memory Search,记忆化搜索

4. 0/1 Knapsack, 0/1 背包

  1. Equal Subset Sum Partition,相等子集划分问题

  2. Subset Sum,子集和问题

  3. Minimum Subset Sum Difference,子集和的最小差问题

5. Unbounded Knapsack,无限(完全)背包

6. Counting DP,计数 DP

7. Probability DP,概率 DP

8. Tree DP,树状 DP

9. Optimal Solution,最优解问题

10. Can I Win? 博弈 DP

11. Interval DP,区间 DP

12. Subsequence/Substring,子序列/子字符串

12.1. Longest Subsequence/Substring,最长子序列/子字符串

  1. Longest Common Substring,最长公共子串

  2. Shortest Common Super-sequence,最短公共超级子序列

12.2. Palindromic Subsequence/Substring,回文子序列/子字符串

  1. Longest Palindromic Subsequence,最长回文子序列

  2. Longest Palindromic Substring,最长回文子字符串

13. String,字符串上的动态规划

13.1. String Transform,字符串变换

13.2. Regular Expression Matching,正则表达式匹配

14. Multi-start state,多起始状态

15. DP Optimizition,DP 优化

15. Bitmask DP,状态压缩 DP

16. 轮廓线动态规划

Last updated