【Code Caprice】Day45

1. Complete backpack to solve the problem of climbing stairs

70. Climb the stairs

1. The abstract problem of climbing stairs into a backpack is: there are 1 and 2 items, and the backpack has n capacity, and different arrangement results are calculated. Because you can choose between 1 grid and 2 grids arbitrarily when taking the stairs, this is the reason for the complete backpack; and each time you walk the stairs is in order, so the problem it involves is an arrangement problem.

2. Then we know that it is a complete knapsack arrangement problem. We only need to traverse the knapsack first and then traverse the objects to find all the results. The idea of ​​this question is still: to go to position j, you must go to position j-1 and go to The sum of the j-2 position methods

class Solution {
public:
    int climbStairs(int n) {
        vector<int> dp(n+1,0);
        dp[0]=1;
        for(int j=0;j<=n;j++)
        {
            for(int i=1;i<=2;i++)
            {
                if(j>=i)
                    dp[j]+=dp[j-i];
            }
        }
        return dp[n];
    }
};

2. Change

322. Exchange of change

1. The meaning of the dp array: dp[j] refers to the number of the minimum number that can meet the backpack capacity at the j position.

2. The condition of the dp array: for the i object, dp[j]=dp[j-coins[i]]+1, then since we need to find the least number of methods, then the condition is dp[j]=min( dp[j],dp[j-coins[i]]+1);

3. Initialization: It is known from the condition that dp[0]=0. In addition, since we are looking for the minimum value, the other positions of the array except 0 cannot be initialized to 0; because if it is 0, dp[j]=min(dp [j], dp[j-coins[i]]+1), no matter how you update the initialized value, it will affect the generation of the condition, then we initialize other positions to amount+1, so that it will not interfere with the conditions of other positions up.

4. This question considers the number of results. There is no need to consider the problem of combination and arrangement of the complete knapsack mentioned before, because no matter the combination or arrangement, the minimum number is the same result. But the combination can optimize part of the efficiency, because they don't need to consider more situations than permutations.

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int>dp(amount+1,amount+1);
        dp[0]=0;
        for(int i=0;i<coins.size();i++)
        {
            for(int j=coins[i];j<=amount;j++)
            {
                dp[j]=min(dp[j],dp[j-coins[i]]+1);
            }
        }
        if(dp[amount]==amount+1)
            return -1;
        return dp[amount];
    }
};

3. Perfect square numbers

279. Perfect Squares

1. The meaning of the dp array in this question is basically the same as that of the previous question: dp[j] refers to the number of the minimum number that can meet the backpack capacity under position j

2. The condition of the dp array: first we traverse i, but the condition requires a complete square number, so the object is i*i. For the i object, dp[j]=dp[ji*i]+1, So since what needs to be found is the least number of methods, then the condition is dp[j]=min(dp[j],dp[ji*i]+1)

3. When considering dp[0], there is no number to represent, so dp[0]=0; in addition, since we are looking for the minimum value, the other positions of the array except 0 cannot be initialized to 0, so since 1 is exactly It is also a perfect square number, the most conservative result is also the result of all 1, and there are n natural types. Then we initialize the dp array in other positions to n

4. How to write the traversal: firstly, for the traversal of the object i, since we cannot be sure whether n is a perfect square number, it is natural to include n when considering the range, so i<=n; when traversing the backpack, the initial value of j The value should be i*2, because we just learned that i*i is actually an object.

class Solution {
public:
    int numSquares(int n) {
        vector<int>dp(n+1,n);
        dp[0]=0;
        for(int i=0;i<=n;i++)
        {
            for(int j=i*i;j<=n;j++)
            {
                dp[j]=min(dp[j],dp[j-i*i]+1);
            }
        }
        return dp[n];
    }
};

day46

4. Word splitting

139. Word Splitting

1. The meaning of the dp array: dp[i] is the position i, whether there are any characters that meet the conditions to form a string from position 0 to i. true if there is one. Otherwise false

2. First of all, it is not as long as the local string matching the backpack can be found to be successful. The combination of strings has a sequence, so when considering the traversal order, you should use the method of traversing the backpack first and then traversing the objects. Sort to find results

3. The conditions for a single character string: the current position is j, and the previous position is i. Since it is judged from front to back, assuming that the value of position i has been judged to be true, it means that position i meets the requirements and the corresponding value can be found, then we only need to judge whether it is at the position of the string [i,j] we are looking for There are string combinations that also exist, then we can get that dp[j] is also true

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool>dp(s.size()+1,false);
        dp[0]=true;
        for(int j=0;j<=s.size();j++)
        {
            for(int i=0;i<j;i++)
            {
                string word = s.substr(i,j-i);
                if(wordSet.find(word)!=wordSet.end()&&dp[i])
                    dp[j]=true;
            }
        }
        return dp[s.size()];
    }
};

Guess you like

Origin blog.csdn.net/m0_63488627/article/details/131000704