[Dynamic Programming Algorithm Exercise] day16


1. Complete backpack

1. Introduction to the topic

DP42 [Template] Complete Backpack
This question comes from Niuke.com. You can click on the link above to enter the question page for practice.
Insert image description here
Insert image description here
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

#include <iostream>
using namespace std;
#include<vector>
int main() {
    
    
    int n, V;
    cin>>n;
    cin>>V;
    vector<int> v(n, 0);//存放物品到的体积
    vector<int> w(n, 0);//存放物品的价值
    for(int i = 0;i < n; ++i)
    {
    
    
        cin>>v[i];
        cin>>w[i];
    }
    //问题1
    vector<int> dp1(V + 1, 0);//dp[j]表示体积为j的背包至多能装多大价值的物品
    for(int i = 0;i < n; ++i)
    {
    
    
        for(int j = v[i];j <= V; ++j)
        {
    
    
            dp1[j] = max(dp1[j - v[i]] + w[i], dp1[j]);
        }
    }
    cout<<dp1[V]<<endl;
    //问题2
    vector<int> dp2(V + 1, -1);//dp[j]表示体积为j的背包至多能装多大价值的物品
    dp2[0] = 0;//初始化体积为0时的dp值
    for(int i = 0;i < n; ++i)
    {
    
    
        for(int j = v[i];j <= V; ++j)
        {
    
    
            if(dp2[j - v[i]] != -1)
            {
    
    
                dp2[j] = max(dp2[j - v[i]] + w[i], dp2[j]);
            }
        }
    }
    cout<<(dp2[V] == -1 ? 0 : dp2[V])<<endl;
    return 0;
}

4. Running results

Insert image description here

2. 322. Change exchange

1. Introduction to the topic

322. Change Exchange
You are given an integer array coins, representing coins of different denominations; and an integer amount, representing the total amount.
Calculate and return the minimum number of coins required to make up the total amount. If no coin combination can complete the total amount, -1 is returned.
You can think of the number of each type of coin as infinite.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int coinChange(vector<int>& coins, int amount) {
    
    
        vector<int> dp(amount + 1, INT_MAX);//dp[j]表示要凑成总金额为j所需要的硬币数
        dp[0] = 0;//总金额为0的话,所需硬币数为0
        for(int i = 0;i < coins.size(); ++i)
        {
    
    
            for(int j = coins[i];j <= amount; ++j)
            {
    
    
                if(dp[j - coins[i]] != INT_MAX)//如果等于INT_MAX说明该位置不能被合成,不能使用该位置
                {
    
    
                    dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
                }
            }
        }
        return (dp[amount] == INT_MAX ? -1 : dp[amount]);
    }
};

4. Running results

Insert image description here

3. 518. Change Exchange II

1. Introduction to the topic

518. Change Exchange II
gives you an integer array coins to represent coins of different denominations, and an integer amount to represent the total amount.
Please calculate and return the number of coin combinations that can make up the total amount. If no coin combination can add up to the total amount, 0 is returned.
Suppose there are an infinite number of coins of each denomination.
The question data ensures that the result conforms to a 32-bit signed integer.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int change(int amount, vector<int>& coins) {
    
    
        vector<int> dp(amount + 1, 0);//dp[j]表示凑成总金额为j的硬币组合数
        //初始化
        dp[0] = 1;//凑成0的硬币组合只有一种,就是一个硬币也没有;其他位置都初始化为0(为了避免影响结果)
        for(int i = 0;i < coins.size(); ++i)
        {
    
    
            for(int j = coins[i];j <= amount; ++j)
            {
    
    
                if(dp[j - coins[i]] != 0)//如果等于-1,说明该位置无法凑出,所以不能使用该位置
                dp[j] += dp[j - coins[i]];//要求组合数,因此要用+
            }
        }
        return (dp[amount] == 0 ? 0 : dp[amount]);
    }
};

4. Running results

Insert image description here

4. 279. Perfect square numbers

1. Introduction to the topic

279. Perfect Square Numbers
Given an integer n, return the minimum number of perfect square numbers whose sum is n.
A perfect square number is an integer whose value is equal to the square of another integer; in other words, its value is equal to the product of an integer multiplied by itself. For example, 1, 4, 9, and 16 are all perfect square numbers, but 3 and 11 are not.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int numSquares(int n) {
    
    
        vector<int> dp(n + 1, INT_MAX);//dp[j]表示和为j的完全平方数的最少数量
        dp[0] = 0;
        //dp[0]初始化为0,和为0的完全平方数的数量为0
        //其他位置全部初始化为INT_MAX,避免影响min的结果
        for(int i = 1;i <= n; ++i)
        {
    
    
            for(int j = i * i;j <= n; ++j)
            {
    
    
                if(dp[j - i * i] != INT_MAX)//如果相等,说明该位置不能被组成,因此该位置不能被使用
                dp[j] = min(dp[j - i * i] + 1, dp[j]);
            }
        }
        return dp[n];
    }
};

4. Running results

Insert image description here


Summarize

Today is the 16th day of algorithm practice.
Be broad-minded and make appointments, accumulate thick and thin , and keep working hard.
Source: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131471379