LeetCode常见题型——动态规划

1. 算法思想

动态规划(Dynamic Programming, DP)主要用来查找有很多子问题的最优解。它将问题重新组合成子问题,为避免多次解决这些子问题,他们的结果都逐渐被计算并保存,从简单的问题开始,直到整个问题被解决。因此,DP保存递归时的结果,不会在解决同样问题时花费时间。

动态规划只能用于有最优子结构的的问题,即局部最优解能决定全局最优解。其关键在于找到转移方程。此外,可以对动态规划进行空间压缩,起到节省空间消耗的效果。

在一些情况下,动态规划可以看作是有状态记录的优先搜索。动态规划是从从下而上的,即先解决子问题,再解决父问题;而带有状态记录的优先搜索是自上而下的,即从父问题搜索到子问题,已搜索的子问题需进行状态记录,防止重复计算。如果题目要获得最终状态,则使用动态搜索比较方便;如果题目需要输出所有的路径,那么使用带有状态记录的优先搜索会比较方便。

2. 常见题型

(1)基本一维动态规划

LeetCode-70. Climbing Stairs [C++][Java]_贫道绝缘子的博客-CSDN博客You are climbing a staircase. It takesnsteps to reach the top. Each time you can either climb1or2steps. In how many distinct ways can you climb to the top?https://blog.csdn.net/qq_15711195/article/details/123342137LeetCode-198. House Robber [C++][Java]_贫道绝缘子的博客-CSDN博客You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected andit will automaticahttps://blog.csdn.net/qq_15711195/article/details/123365151LeetCode-413. Arithmetic Slices [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integer arraynums, returnthe number of arithmeticsubarraysofnums. Asubarrayis a contiguous subsequence of the array.https://blog.csdn.net/qq_15711195/article/details/123365490

LeetCode-213. House Robber II [C++][Java]_贫道绝缘子的博客-CSDN博客You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place arearranged in a circle.That means the first house is the neighbor of the last one.https://blog.csdn.net/qq_15711195/article/details/123461995?spm=1001.2014.3001.5502LeetCode-53. Maximum Subarray [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integer arraynums, find the contiguous subarray (containing at least one number) which has the largest sum and returnits sum. Asubarrayis acontiguouspart of an array.https://blog.csdn.net/qq_15711195/article/details/123464648?spm=1001.2014.3001.5502LeetCode-343. Integer Break [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integern, break it into the sum ofkpositive integers, wherek >= 2, and maximize the product of those integers. Returnthe maximum product you can get.https://blog.csdn.net/qq_15711195/article/details/123470177

(2)基本二维动态规划

LeetCode-64. Minimum Path Sum [C++][Java]_贫道绝缘子的博客-CSDN博客Given am x ngridfilled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note:You can only move either down or right at any point in time.https://blog.csdn.net/qq_15711195/article/details/123413646LeetCode-542. 01 Matrix [C++][Java]_贫道绝缘子的博客-CSDN博客Given anm x nbinary matrixmat, returnthe distance of the nearest0for each cell. The distance between two adjacent cells is1.https://blog.csdn.net/qq_15711195/article/details/123414199LeetCode-221. Maximal Square [C++][Java]_贫道绝缘子的博客-CSDN博客Given anm x nbinarymatrixfilled with0's and1's,find the largest square containing only1'sand return its area.https://blog.csdn.net/qq_15711195/article/details/123435053

(3)分割类型

LeetCode-279. Perfect Squares [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integern, returnthe least number of perfect square numbers that sum ton. Aperfect squareis an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,1,4,9, and16are perfect shttps://blog.csdn.net/qq_15711195/article/details/123435319LeetCode-91. Decode Ways [C++][Java]_贫道绝缘子的博客-CSDN博客Given a stringscontaining only digits, returnthenumberof ways todecodeit. The test cases are generated so that the answer fits in a32-bitinteger.https://blog.csdn.net/qq_15711195/article/details/123436454LeetCode-139. Word Break [C++][Java]_贫道绝缘子的博客-CSDN博客Given a stringsand a dictionary of stringswordDict, returntrueifscan be segmented into a space-separated sequence of one or more dictionary words. Notethat the same word in the dictionary may be reused multiple times in the segmentation.https://blog.csdn.net/qq_15711195/article/details/123438402

(4)求解子序列

LeetCode-300. Longest Increasing Subsequence [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integer arraynums, return the length of the longest strictly increasing subsequence. Asubsequenceis a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements.https://blog.csdn.net/qq_15711195/article/details/123439005LeetCode-1143. Longest Common Subsequence [C++][Java]_贫道绝缘子的博客-CSDN博客Given two stringstext1andtext2, returnthe length of their longestcommon subsequence.If there is nocommon subsequence, return0.https://blog.csdn.net/qq_15711195/article/details/123439733

LeetCode-583. Delete Operation for Two Strings [C++][Java]_贫道绝缘子的博客-CSDN博客Given two stringsword1andword2, returnthe minimum number ofstepsrequired to makeword1andword2the same. In onestep, you can delete exactly one character in either string.https://blog.csdn.net/qq_15711195/article/details/123470190LeetCode-646. Maximum Length of Pair Chain [C++][Java]_贫道绝缘子的博客-CSDN博客A pairp2 = [c, d]followsa pairp1 = [a, b]ifb < c. Achainof pairs can be formed in this fashion. Returnthe length longest chain which can be formed.https://blog.csdn.net/qq_15711195/article/details/123490799LeetCode-376. Wiggle Subsequence [C++][Java]_贫道绝缘子的博客-CSDN博客Awiggle sequenceis a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative.https://blog.csdn.net/qq_15711195/article/details/123491144

(5)背包问题

LeetCode常见题型——背包问题_贫道绝缘子的博客-CSDN博客背包问题:有N 个物品和容量为W 的背包,每个物品都有自己的体积w 和价值v,求拿哪些物品可以使得背包所装下物品的总价值最大。https://blog.csdn.net/qq_15711195/article/details/123455338

(6)字符串编辑

LeetCode-72. Edit Distance [C++][Java]_贫道绝缘子的博客-CSDN博客Given two stringsword1andword2, returnthe minimum number of operations required to convertword1toword2.https://blog.csdn.net/qq_15711195/article/details/123456153?spm=1001.2014.3001.5502LeetCode-650. 2 Keys Keyboard [C++][Java]_贫道绝缘子的博客-CSDN博客Given an integern, returnthe minimum number of operations to get the character'A'exactlyntimes on the screen.https://blog.csdn.net/qq_15711195/article/details/123458698LeetCode-10. Regular Expression Matching [C++][Java]_贫道绝缘子的博客-CSDN博客Given an input stringsand a patternp, implement regular expression matching with support for'.'and'*'https://blog.csdn.net/qq_15711195/article/details/123459521

(7)股票交易

LeetCode-121. Best Time to Buy and Sell Stock [C++][Java]_贫道绝缘子的博客-CSDN博客You want to maximize your profit by choosing asingle dayto buy one stock and choosing adifferent day in the futureto sell that stock. Returnthe maximum profit you can achieve from this transaction. If you cannot achieve any profit, return0.https://blog.csdn.net/qq_15711195/article/details/122993511LeetCode-122. Best Time to Buy and Sell Stock II [C++][Java]_贫道绝缘子的博客-CSDN博客On each day, you may decide to buy and/or sell the stock. You can only holdat most oneshare of the stock at any time. However, you can buy it then immediately sell it on thesame day. Find and returnthemaximumprofit you can achieve.https://blog.csdn.net/qq_15711195/article/details/122993359?spm=1001.2014.3001.5502LeetCode-123. Best Time to Buy and Sell Stock III [C++][Java]_贫道绝缘子的博客-CSDN博客Find the maximum profit you can achieve. You may completeat most two transactions. Note:You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).https://blog.csdn.net/qq_15711195/article/details/122993610?spm=1001.2014.3001.5502LeetCode-188. Best Time to Buy and Sell Stock IV [C++][Java]_贫道绝缘子的博客-CSDN博客You are given an integer arraypriceswhereprices[i]is the price of a given stock on theithday, and an integerk. Find the maximum profit you can achieve. You may complete at mostktransactions.https://blog.csdn.net/qq_15711195/article/details/123460764LeetCode-309. Best Time to Buy and Sell Stock with Cooldown [C++][Java]_贫道绝缘子的博客-CSDN博客Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: After you sell your stock, you cannot buy stock on the next dayhttps://blog.csdn.net/qq_15711195/article/details/123460942

LeetCode-714. Best Time to Buy and Sell Stock with Transaction Fee [C++][Java]_贫道绝缘子的博客-CSDN博客You are given an arraypriceswhereprices[i]is the price of a given stock on theithday, and an integerfeerepresenting a transaction fee. Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to payhttps://blog.csdn.net/qq_15711195/article/details/123491550

你可能感兴趣的:(LeetCode刷题怪,动态规划,leetcode,算法)