Dynamic Programming Problem Classification

refer: https://www.cnblogs.com/Renyi-Fan/p/7421109.html
树形DP:0-1背包
线性DP:LCS, LIS
区间DP: 某段数组或者矩阵的最大子字符串和或者积
数位DP:
状态压缩DP:旅行商

但是看这些分类 发现都是一些竞赛题目,所以我们不从这个下手。

如果是这样,那还不如划分为 一维DP,二维DP, 三维DP
refer: https://blog.csdn.net/qqxx6661/article/details/79951989

一维DP变换多端 需要有清晰思路
longest valid parentheses: we define dp[i] as the longest valid parenthese ends in i, where i must is ‘)’ otherwise the solution may not contains charAt(i). and we also have other subcases which is pretty hard.
maximum sum of subarray: dp[i] indicates the maxsum which contains i, so we have dp[i] = dp[i-1]+nums[i], and finally, we have to get the largest number among dp.(also, this problem can be done using bianry seach problems)
decode ways: easy dp, dp[i] is the number of ways we have if the String has only i characters.
unique BST: this is actually a pretty dp, dp[i] represents the number if unqiue bst if we have only i element in given array. dp[i] = sum of (dp[j] * dp[i-j-1]) where j = 0 to i-1.
triangle minimum sum of path: this problem solution seems a bit of confusion. we know that the length of the path needs to be the height of this tree, so that is the length of dp[]. and instead of starting from root, we starting from the bottom. and dp[i] will keep changing the whole time(from start to end).
Palindrome Partition 1/2/3: given a string, partition such that all of the subtrings are palindrome. the first problem requires us to get all the partition results, so backtracking. and the second question is to find the minimum cut that makes the string palindrome, and the third problem is change the minimum number of chars in given string, which makes give k cuts, each substring is palindrome. k is given.
Word Break 1/2: word break1, dp[i] indicates if string only have length i, we have what we want or not. it’s quite easy. for word break2: classic follow up: return the results, backtracking.
Best time to buy and sell stock1/2/3: problem1: dp[i] indicates that max gap if the whole array has only i items. problems2: now we can do as many transant as we want. use greedy.
problems3: now we only allows two transcantions: this can be done in dp, and there is a very detailed solution is leetcode discussion section.

some follow ups:
decode ways2: now we have another possible char in string, “*” which represents any numbers from 1 to 9. now how many ways can we have? This is still a dp problem, just that we have more subcases to consider. dp[i] only have relations with dp[i-1] or dp[i-2].
unique bst2: this is not a dp, this problem requires me to print all the unqiue trees, use backtracking I guess.

2D DP:
Longest palindrome substring: this problem can be done in brute force for sure, but using dp will be smarter, dp[i][j] indicates boolean of substring(i,j) is palindrome, and it has relations to do with dp[i+1][j-1], and we will use a max to track the max length.
interleaving string: given three string, judge if the first two can compose to the third without change relative order.
Package problem: used to dig deep on such problems.
unique path1/2: classic dp
minimum path sum: classic dp
edit distance: class dp
distinct subsequence(how many ways can be delete some char at s, to make s to another string t?): dp[i][j] indicates s的前i-1个字符里面含有多少t.substring(0,j)的子串。and we only have two subcases: s.charAt(i-1)==t.charAt(j-1) or not. due to we need the whole s and the whole t, so the return results should be dp[i][j]

follow ups of 2D DP:
LC516 longest palindrome subsequence: with the same idea of LC5, longest palindrome substring. dp[i][j] indicates the longest palindrome length in substring[i,j]. because we need to get dp[0][len-1], so i loop has to from len-1 to 0, and j has to be from i+1 to len-1 each time. and each time we have two subcases: charAt(i)==charAt(j) or not. and pay special attention to initialize the dp matrix.
LC940 distinct subsequence: given a string s, count the number of distinct subsequence of s. pay attention: we want the number of unique subsequences. and of course, we can get all subsequence and use set to remove duplicates. but we use dp, so we define dp[i] as the number of unique subsequence of S if S has only i chars.
and this problem gives me a thought: dp ususually refers to bottom up, and memo refers to top down

3D DP: Scramble String

posted @ 2020-04-30 05:12  EvanMeetTheWorld  阅读(23)  评论(0)    收藏  举报