动态规划
- 爬楼梯
回溯法
#include
class Solution {
public:
int climbStairs(int n) {
if (n == 1 || n == 2){ //递归出口,剩下两节返回两种方法
return n;
}
return climbStairs(n-1) + climbStairs(n-2); //递归树分两枝
}
};
int main(){
Solution solve;
printf("%d\n", solve.climbStairs(3));
return 0;
}
动态规划
第i阶的方式数量 = 第i-1阶方式数量 + 第i-2阶方式方式数量
#include
#include
class Solution {
public:
int climbStairs(int n) {
std::vector dp(n + 3, 0);
dp[1] = 1; //边界条件
dp[2] = 2;
for (int i = 3; i <= n; i++){ //写出递推公式
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
};
int main(){
Solution solve;
printf("%d\n", solve.climbStairs(3));
return 0;
}
盗取财宝,不能盗相邻房间
dp[i] = max(dp[i-1],dp[i-2]+nums[i])
#include
#include
class Solution {
public:
int rob(std::vector& nums) {
if (nums.size() == 0){ //边界条件
return 0;
}
if (nums.size() == 1){
return nums[0];
}
std::vector dp(nums.size(), 0); //递推公式
dp[0] = nums[0];
dp[1] = std::max(nums[0], nums[1]);
for (int i = 2; i < nums.size(); i++){
dp[i] = std::max(dp[i-1], dp[i-2] + nums[i]);
}
return dp[nums.size() - 1];
}
};
int main(){
Solution solve;
std::vector nums;
nums.push_back(5);
nums.push_back(2);
nums.push_back(6);
nums.push_back(3);
nums.push_back(1);
nums.push_back(7);
printf("%d\n", solve.rob(nums));
return 0;
}
最大加和序列
若dp[i-1]>0,dp[i]=dp[i-1]+nums[i]
否则dp[i]=nums[i]
#include
#include
class Solution {
public:
int maxSubArray(std::vector& nums) {
std::vector dp(nums.size(), 0);
dp[0] = nums[0]; //边界条件
int max_res = dp[0];
for (int i = 1; i < nums.size(); i++){
dp[i] = std::max(dp[i-1] + nums[i], nums[i]); //递推公式
if (max_res < dp[i]){ //找到dp[i]中最大的
max_res = dp[i];
}
}
return max_res;
}
};
int main(){
Solution solve;
std::vector nums;
nums.push_back(-2);
nums.push_back(1);
nums.push_back(-3);
nums.push_back(4);
nums.push_back(-1);
nums.push_back(2);
nums.push_back(1);
nums.push_back(-5);
nums.push_back(4);
printf("%d\n", solve.maxSubArray(nums));
return 0;
}
最小数值钞票
当i-coins[j] >= 0且dp[i-coins[j]!=-1]时: 表示可以减去相应的钞票值,并且减去后对应的数组元素有值
j = 0,1,2,3,4; coins[j] = 1,2,5,7,10
dp[i] = getmin(dp[i-coins[j]]) + 1 dp[i]就是减去一张钞票结果的最小值加一
#include
#include
class Solution {
public:
int coinChange(std::vector& coins, int amount) {
std::vector dp;
for (int i = 0; i <= amount; i++){
dp.push_back(-1);
}
dp[0] = 0; //边界条件
for (int i = 1; i <= amount; i++){
for (int j = 0; j < coins.size(); j++){
if (i - coins[j] >= 0 && dp[i - coins[j]] != -1){ //可以减去相应的钞票值,并且减去后对应的数组元素有值
if (dp[i] == -1 || dp[i] > dp[i - coins[j]] + 1){ //dp[i]为-1或者dp[i]大于当前计算的结果
dp[i] = dp[i - coins[j]] + 1; //更新dp[i]
}
}
}
}
return dp[amount];
}
};
int main(){
Solution solve;
std::vector coins;
coins.push_back(1);
coins.push_back(2);
coins.push_back(5);
coins.push_back(7);
coins.push_back(10);
for (int i = 1; i<= 14; i++){
printf("dp[%d] = %d\n", i, solve.coinChange(coins, i));
}
return 0;
}
三角形,最小路径
dp[i][j]=min(dp[i+1][j],dp[i+1][j+1])+triangle[i][j]
#include
#include
class Solution {
public:
int minimumTotal(std::vector >& triangle){
if (triangle.size() == 0){
return 0;
}
std::vector > dp; //构造dp和三角形相同
for (int i = 0; i < triangle.size(); i++){
dp.push_back(std::vector());
for (int j = 0; j < triangle.size(); j++){
dp[i].push_back(0);
}
}
for (int i = 0; i < dp.size(); i++){ //边界条件,最后一行就是triangle的值
dp[dp.size()-1][i] = triangle[dp.size()-1][i];
}
for (int i = dp.size() - 2; i >= 0; i--){ //遍历列和行
for (int j = 0; j < dp[i].size(); j++)
dp[i][j] = std::min(dp[i+1][j], dp[i+1][j+1]) //递推公式
+ triangle[i][j];
}
return dp[0][0];
}
};
int main(){
std::vector > triangle;
int test[][10] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}};
for (int i = 0; i < 4; i++){
triangle.push_back(std::vector());
for (int j = 0; j < i + 1; j++){
triangle[i].push_back(test[i][j]);
}
}
Solution solve;
printf("%d\n", solve.minimumTotal(triangle));
return 0;
}
最长上升子序列
若nums[i]>nums[j],说明nums[i]可放置在nums[j]的后面,组成最长上升子序列 第二种,用栈解决 最小路径之和 最长公共子序列 股票问题
若dp[i]#include
#include
#include
dp[i][j] 表示子串A[0...i](数组长度为n)和子串B[0...j](数组长度为m)的最长公共子序列
当A[i] == B[j]时,dp[i][j] = dp[i-1][j-1] + 1;
否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
最优解为dp[n-1][m-1];
https://www.cnblogs.com/fengziwei/p/7827959.html
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String str1 = scanner.nextLine().toLowerCase();
String str2 = scanner.nextLine().toLowerCase();
System.out.println(findLCS(str1, str1.length(), str2, str2.length()));
}
}
public static int findLCS(String A, int n, String B, int m) {
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1];
}
}
}
return dp[n][m];
}
}
状态转移方程:
分为第i天不卖出和卖出
dp[k][i] = max(dp[k][i-1], dp[k-1][j]+price[i] - price[j], j属于[0,i-1])