动态规划
首先理解动态规划五部曲:
--1.确定dp数组以及下标的含义
--2.确定递推公式
--3.dp数组如何初始化
--4.确定遍历顺序
--5.举例推导dp数组
其中动态规划可以分为基础题目、背包问题、打家劫舍问题、股票问题以及子序列问题。大佬们在刷题的时候遇到相似类型的背景的时候就可以快速定位出这是一道动态规划的题目,然后想一想dp数组表示什么,递推公式是啥,该怎么初始化,然后最后验证一下边界条件即可。
动态规划基础
-1.斐波那契数
https://leetcode-cn.com/problems/fibonacci-number/
-2.爬楼梯
https://mp.weixin.qq.com/s/Ohop0jApSII9xxOMiFhGIw
这个题目就是斐波那契的变式题目,其递推的思路就是当我站在第i阶楼梯上的时候,其状态是由第i-1阶和第i-2阶的状态共同决定的,据此就可以写出相应的递推公式出来。
-3.使用最小花费爬楼梯
https://leetcode.cn/problems/min-cost-climbing-stairs/
-4.不同路径
https://leetcode-cn.com/problems/unique-paths/
-5.不同路径 + 障碍类型
https://leetcode-cn.com/problems/unique-paths-ii/
-6.整数拆分
https://leetcode.cn/problems/integer-break/
-7.不同的二叉搜索树
https://leetcode.cn/problems/unique-binary-search-trees/
背包问题
-1.01背包背景知识
https://mp.weixin.qq.com/s/FwIiPPmR18_AJO5eiidT6w
-2.01背包有关滚动数组
https://mp.weixin.qq.com/s/M4uHxNVKRKm5HPjkNZBnFA
有关滚动数组的相关知识,其主要是看到递推公式只和上一行有关,那么直接把上一行数组的内容复制下来,然后直接dp就可以了。这样做的话就是节省空间。
-3.分割等和子集
https://leetcode.cn/problems/partition-equal-subset-sum/
使用01背包的思想,求一半,然后背包容量就是一半的数值,最后判断dp[target] 和 target之间的关系即可。
-4.最后一块石头的重量
https://leetcode.cn/problems/last-stone-weight-ii/
-5.目标和
https://leetcode.cn/problems/target-sum/
-6.一和零
https://leetcode.cn/problems/ones-and-zeroes/
-7.完全背包
https://mp.weixin.qq.com/s/akwyxlJ4TLvKcw26KB9uJw
-8.零钱兑换
https://leetcode.cn/problems/coin-change-ii/
-9.组合总和
https://leetcode.cn/problems/combination-sum-iv/
-10.爬楼梯
https://leetcode.cn/problems/climbing-stairs/
-11.零钱兑换
https://leetcode.cn/problems/coin-change/
-12.完全平方数
https://leetcode.cn/problems/perfect-squares/
-13.单词拆分
https://leetcode.cn/problems/word-break/
-14.多重背包
https://mp.weixin.qq.com/s/b-UUUmbvG7URWyCjQkiuuQ
这个问题就是物品有m件,那么把这m件单独拆开,变成01背包问题即可。
-15.背包问题总结
https://mp.weixin.qq.com/s/ZOehl3U1mDiyOQjFG1wNJA
背包问题涉及到递推公式、两层for的遍历顺序、初值的选取等内容,想要回顾的话首选看这篇。
打家劫舍问题
总结:数组上连续元素二选一,成环之后连续元素二选一,在树上连续元素二选一
-1.打家劫舍
https://leetcode.cn/problems/house-robber/
-2.打家劫舍2
https://leetcode.cn/problems/house-robber-ii/
-3.打家劫舍3
https://leetcode.cn/problems/house-robber-iii/
股票系列问题
-1.买卖股票的最佳时机
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
-2.买卖股票的最佳时机II
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
-3.买卖股票的最佳时机III
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/
-4.买卖股票的最佳时机IV
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/
-5.本周讲了些啥(系列六)
https://mp.weixin.qq.com/s/GVu-6eF0iNkpVDKRXTPOTA
-6.最佳买卖股票时机含冷冻期
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
-7.本周讲了这些(系列七)
https://mp.weixin.qq.com/s/vdzDlrEvhXWRzblTnOnzKg
-8.买卖股票的最佳时机含手续费
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
-9.买卖股票--总结
https://mp.weixin.qq.com/s/sC5XyEtDQWkonKnbCvZhDw
子序列系列问题
-1.最长递增子序列
https://leetcode.cn/problems/longest-increasing-subsequence/
-2.最长连续递增序列
https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
-3.最长重复子数组
https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
-4.最长公共子序列
https://leetcode.cn/problems/longest-common-subsequence/
-5.不相交的线
https://leetcode.cn/problems/uncrossed-lines/
-6.最大子数组和
https://leetcode.cn/problems/maximum-subarray/
-7.判断子序列
https://leetcode.cn/problems/is-subsequence/
-8.不同的子序列
https://leetcode.cn/problems/distinct-subsequences/
-9.两个字符串的删除操作
https://leetcode.cn/problems/delete-operation-for-two-strings/
-10.编辑距离
https://leetcode.cn/problems/edit-distance/
-11.为了绝杀编辑距离
https://mp.weixin.qq.com/s/kbs4kCUzg8gPFttF9H3Yyw
-12.回文子串
https://leetcode.cn/problems/palindromic-substrings/
-13.最长回文子序列
https://leetcode.cn/problems/longest-palindromic-subsequence/