Illustrates leetcode279 - perfect square

Each question comes with dynamic diagram provides java, python answer two languages, and strive to provide leetcode optimal solution.

description:

Given a positive integer  n- , found several perfect square (for example  1, 4, 9, 16, ...) so that their sum is equal to  n- . At least you need to make a perfect square of the number and composition.

Example 1:

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

Example 2:

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

Ideas:

    This question is the official classification of dynamic programming [], so we use the method to solve dynamic programming, dynamic programming the most important thing is to find its state transition equation (that is, find out the relationship between the state).

  In addition to the state transition equation, we can also use the state transition table methods to solve problems, but the state transition table only solution is relatively low dimension problems, such as the famous 0-1 knapsack problem, decisions that affect the transfer of only two states, the goods into the backpack, do not put items into a backpack. So it is easy to draw a two-dimensional state transition table, but as of today we have to solve this problem, if n = 12, then the state transition to influence decisions at least there are three, take 1, take four, take 9, the human brain is hard to imagine the state transition tables multidimensional, so here we use the method to solve the state transition equation.

State transition equation is derived:

Minimum number function f (n) for the sake of perfect square composed of n (which is the title), the f (12) = 3; f (13) = 2.

We denoted f (n) = m. n can be split into n = d + k * k this form.

Such as 12 = 8 + 4 + 2 * 3 * 2.13 = 3, because both the 12 or 13 is composed of perfect square, so it must be converted into such a form.

f (n) = f (d) + f (k * k), because k * k is a perfect square, so f (k * k) = 1

I.e., f (n) = f (d) + 1, and = d + k * k can be obtained from the n, d = n - k * k, it can be turned into the equation:

f(n) = f(n-k*k) + 1,(k*k < n)。

This leads to the state transition equation: dp [i] = min (dp [ij * j] +1, dp [i]), (j * j <= i)

The minimum and this causes dp [i] is taken dp [ij * j] +1 may be more than one value, the minimum value of these values.

GIF:

FIG example is f (5) = 2

5 = 4 + 1 

achieve:

java:

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

result:

python3:

class Solution:
    def numSquares(self, n: int) -> int:
        dp = [i for i in range(n + 1)]
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                if i - j * j >= 0:
                    dp[i] = min(dp[i], dp[i - j * j] + 1)
                else:
                    break
        return dp[n]

结果:

期待您的关注、推荐、收藏,同时也期待您的纠错和批评,想看leetcode的其他题,可以在博客下方留言,每周都会更新。

Guess you like

Origin www.cnblogs.com/nedulee/p/12147626.html