【动态规划】入门

746. 使用最小花费爬楼梯 - 力扣(LeetCode)

 DFS

class Solution {
public:
    int dfs(vector cost,int x)
    {
        if(x==0||x==1) return 0;
        return min(dfs(cost,x-1)+cost[x-1],dfs(cost,x-2)+cost[x-2]);
    }
    int minCostClimbingStairs(vector& cost) 
    {
        int res=dfs(cost,cost.size());
        return res;
    }
};

记忆化搜索

const int N=1010;
class Solution {
public:
    int mem[N];
    int dfs(vector cost,int x)
    {
        if(mem[x]) return mem[x];
        if(x==0||x==1) return 0;
        return mem[x]=min(dfs(cost,x-1)+cost[x-1],dfs(cost,x-2)+cost[x-2]);
    }
    int minCostClimbingStairs(vector& cost) 
    {
        int res=dfs(cost,cost.size());
        return res;
    }
};

DP

const int N=1010;
class Solution {
public:
    int f[N];
    int minCostClimbingStairs(vector& cost) 
    {
        int n=cost.size();
        int f[N];
        f[0]=f[1]=0;
        for(int i=2;i<=n;i++)
        {
            f[i]=min(f[i-1]+cost[i-1],f[i-2]+cost[i-2]);
        }
        return f[n];
    }
};

300. 最长递增子序列 - 力扣(LeetCode)

记忆化搜索

const int N=3010;
class Solution {
public:
    int mem[N];
    int dfs(vector&nums,int x)
    {   
        if(mem[x]) return mem[x];
        int res=0;
        for(int i=0;i& nums) {
        int n=nums.size();
        int res=-0x3f3f3f3f;
        for(int i=0;i

 DP

const int N=3010;
class Solution {
public:
    int lengthOfLIS(vector& nums) {
        int n=nums.size();
        int res=-0x3f3f3f3f;
        int f[N];
        for(int i=0;i

 322. 零钱兑换 - 力扣(LeetCode)

记忆化搜索

const int N=10010;
class Solution {
public:
    int mem[N];
    int dfs(vector& coins,int x)
    {
        int n=coins.size();
        if(mem[x]) return mem[x];
        if(x==0) return 0;
        if(x<0) return -1;
        
        int res=0x3f3f3f3f;
        for(int i=0;i=coins[i]) res=min(res,dfs(coins,x-coins[i])+1);
        }
        return mem[x]=res;
    }
    int coinChange(vector& coins, int amount) {
        int n=coins.size();
        int ans=dfs(coins,amount);

        return ans==0x3f3f3f3f?-1:ans;
    }
};

DP

const int N=10010;
class Solution {
public:
    int f[N];
    int coinChange(vector& coins, int amount) {
        int n=coins.size();
        memset(f,0x3f,sizeof f);
        f[0]=0;
        for(int i=1;i<=amount;i++)
        {
            for(int j=0;j=coins[j]) f[i]=min(f[i],f[i-coins[j]]+1);
            }
        }
        return f[amount]>1e9?-1:f[amount];
    }
};

剑指 Offer 42. 连续子数组的最大和 - 力扣(LeetCode)

在线处理

class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        int maxn=-0x3f3f3f3f;
        int thissum=0,maxsum=0;
        for(int i=0;imaxsum) maxsum=thissum;
            if(thissum<0) thissum=0;
        }
        if(maxsum==0) maxsum=maxn;

        return maxsum;
    }
};

 DP

const int N=1e5+10;
class Solution {
public:
    int f[N]; 
    int maxSubArray(vector& nums) 
    {
        memset(f,-0x3f3f3f3f,sizeof f);
        int  n=nums.size();
        f[0]=nums[0];
        int maxn=f[0];
        for(int i=1;i

343. 整数拆分 - 力扣(LeetCode)

DP

const int N=100;
class Solution {
public:
    int f[N]={0};
    int integerBreak(int n) {
        f[0]=f[1]=1;
        for(int i=1;i<=n;i++)//容量
        {
            for(int j=0;j

 

你可能感兴趣的:(动态规划,算法)