Dynamic programming

http://blog.csdn.net/linhuanmars/article/details/38468361

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/

坐标型 20%

f[i] 第i个

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

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

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

序列型 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/ https://leetcode.com/problems/paint-house/description/ https://leetcode.com/problems/paint-house-ii/description/ https://leetcode.com/problems/maximum-vacation-days/description/ https://leetcode.com/problems/house-robber/ https://leetcode.com/problems/house-robber-ii/description/ https://leetcode.com/problems/climbing-stairs/ https://leetcode.com/problems/maximum-subarray/description/

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

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.

buy1 = Math.max(buy1, -prices[i]);
sell1 = Math.max(sell1, buy1 + prices[i]);
buy2 = Math.max(buy2, sell1 - prices[i]);
sell2 = Math.max(sell2, buy2 + prices[i]);

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/

双序列型

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

划分型 20%

https://leetcode.com/problems/decode-ways/ https://leetcode.com/problems/decode-ways-ii https://leetcode.com/problems/house-robber-iii

区间型 15%

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

背包型 10% coin change

https://blog.csdn.net/wzy_1988/article/details/12260343

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

from 1 to k

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

subproblem

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

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/ https://leetcode.com/problems/unique-binary-search-trees/description/

MiniMax problem (3D DP)

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

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

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

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

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

1. Number Tower,数塔

2. Fibonacci Numbers,斐波那契数列

  1. Fibonacci numbers,斐波那契数列问题

  2. House thief,偷房子问题

3. Memory Search,记忆化搜索

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

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

  2. Subset Sum,子集和问题

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

  4. Other,其它

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

  1. Coin Change,换硬币问题

6. Counting DP,计数 DP

7. Probability DP,概率 DP

8. Tree DP,树状 DP

9. Optimal Solution,最优解问题

  1. Paint House,粉刷房子问题

  2. Minimum Number of Operations,最少操作次数问题

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,最长回文子字符串

  3. Count Palindromic Subsequences/Substrings,回文子序列/子字符串的个数

  4. Palindromic Partitioning,回文分割

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