leetCode 279. Complete square number dynamic programming + complete backpack

Given an integer  n , return   the smallest number of perfect squares 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.

Example 1:

Input: n = 12
 Output: 3 
 Explanation: 12 = 4 + 4 + 4

Example 2:

Input: n = 13
 Output: 2
 Explanation: 13 = 4 + 9

>>Ideas and analysis

You can look at this question like this: A perfect square number is regarded as an item (can be used in unlimited pieces), and the positive integer n is a backpack. How many items are there at least to complete this backpack? Then you can use dynamic programming: 322. Complete backpack idea of ​​change exchange to solve this problem~

>>Five Steps of Dynamic Rules

1. Determine the meaning of dp array (dp table) and subscripts

        dp[j]: The minimum number of perfect square numbers whose sum is j is dp[j]

2. Determine the recursion formula

  • dp[j] can be derived from dp[i - i * i] , dp[j - i * i] + 1 can be made into dp[j] , at this time we can choose the smallest dp[j]
  • Recursion formula : dp[j] = min(dp[j - i * i] + 1,dp[j]);

3.dp array initialization

(1)dp[0] = 0;

  • It makes no sense, because in the problem description, find several perfect square numbers (such as 1, 4, 9, 16,...). The problem description does not say that you should start from 0. dp[0] = 0 is purely for recursion. formula

(2) dp[j] with non-zero subscript must be initially the maximum value

  • Because it can be seen from the recursive formula dp[j] = min(dp[j - i * i] + 1,dp[j]); that the smallest dp[j] must be selected every time, in order to avoid dp[j ] may be overwritten by the initial value during recursion. It is necessary to initialize the non-0 subscript dp[j] to the maximum value.

4. Determine the traversal order

  • Method 1: Traverse the items first and then the backpack (find the number of combinations)
  • Method 2: Traverse the backpack first and then the items (find the number of permutations)

What is sought in this question is the minimum number of perfect square numbers of money  , regardless of the order. So either method 1 or method 2 is fine! ! 

5. Derivation of dp array with examples

We have entered n as 5 as an example. Using the traversal sequence of method 1, that is, traversing items first and then traversing backpacks, the dp status is as follows:

(1) Use method 1 to traverse: 

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for (int i = 1; i * i <= n; i++) { // 遍历物品
            for (int j = i * i; j <= n; j++) { // 遍历背包
                dp[j] = min(dp[j - i * i] + 1, dp[j]);
            }
        }
        return dp[n];
    }
};
  • Time complexity: O(n * √n)
  • Space complexity: O(n)

(2) Use method 2 to traverse:

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1,INT_MAX);
        dp[0] = 0;  

        for(int j=0;j<=n;j++) { // 背包
            for(int i=1;i*i <= n;i++) { // 物品
                if(j>=i*i) 
                    dp[j] = min(dp[j],dp[j - i*i] + 1);
            }
        }
        return dp[n];
    }
};
  • Time complexity: O(n * √n)
  • Space complexity: O(n)

Reference and recommended articles and videos: 

Code Caprice (programmercarl.com) The complete backpack of dynamic programming, changing the soup without changing the medicine! | LeetCode: 279.Perfect Square Number_bilibili_bilibili

Video screenshot from Code Caprice:

 

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/133388041