Dynamic Programming
归纳总结
优化问题,通常都会让你找最小,最大,最少,最多。。。
可以拆分成小问题,小问题 => 大问题。即,当前的运算只基于前面的结果,而和之后的结果没有关系。
某些时候,可以以要求解的参数为index,如要求解最小步数,那么可以以步数为index
通常情况下,对于每一个点,有“选择“和“不选择“两种状态,相当于两条不同的合流,取哪一条,怎么取,要看题意。
e.g.1 House Robber
e.g.2 House Robber II
e.g.3 House Robber III
经典题目
这道题跟 maximum subarray sum 的区别在于,乘法可以负负得正,也就是说当前最小的值也有可能变成最大的值。所以我们需要同时记录当前最大和最小的数。
这里的当前,means ending here (including the current element)
dp[i][j]: the minimum of health needed at the current cell to reach the bottom-right cell.
300 Longest Increasing Subsequence 873 Length of Longest Fibonacci Subsequence
718 Maximum Length of Repeated Subarray
871 Minimum Number of Refueling Stops
我们以步数为index,构建dp array。dp[i] 代表停 i 次可以走的最远距离。 那么,每次到一个新的站点时,有停或不停两种选择,当然前提是要能到达这个站点。 所以,dp[j + 1] = max(dp[j + 1], dp[j] + stations[i][1]), if dp[j] >= stations[i][0]
680 Valid Palindrome II Variant: Given a string and a num k of how many chars you can delete, return true if you can make the string palindrome
516 Longest Palindromic Subsequence
Last updated