[LeetCode] 0279. Perfect Squares perfect square

topic

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

Given a positive integer n, to find the perfect square number (such as 1, 4, 9, 16, ...) so that their sum 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.

A Solution

Dynamic programming, which is slower solution, to keep the pit, now mainly doing BFS title.

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

Solution two

The shortest path, BFS

Reference: https://blog.csdn.net/qq_17550379/article/details/80875782

Ideas: find the shortest path Well, certainly used the breadth-first search, continue dismantling digital queue, the answer is returned directly after obtaining the shortest path. Specific approach is very simple, the following are specific BFS ideas:

The push queue n, and then taken out dismantling (-1, -4, -9 ...), if not equal to 0 after subtracting, by remaining values ​​will push queue, waiting for a second round of search, if the reduction after the fall is equal to 0 directly find the answer, you can return to step + 1, do not need to search anymore (remember to have visited Aung, because it is a cycle diagram).

class Solution {
public:
    int numSquares(int n) {
        queue<pair<int, int>> q;
        // pair.first 代表将n拆解后的数字,pair.second代表所走的步数
        // 刚开始推入未拆解的n,步数为0
        q.push(make_pair(n, 0));

        bool visited[n + 1];
        memset(visited, 0, sizeof(visited));
        visited[n] = true;

        // 开始广度优先搜索
        while (!q.empty()) {
            // 取队头,进行拆解
            auto pair = q.front();
            q.pop();
            
            int i = 1;
            int next_num = pair.first - i * i;
            
            // 在推入队列前先看看能不能解答
            while (next_num >= 0) {
                if (next_num == 0) {
                    return pair.second + 1;
                }
                // 还有余数没扣完,就将可以的下一步都推入队列
                if (!visited[next_num]) {
                    q.push(make_pair(next_num, pair.second + 1));
                    visited[next_num] = true;
                }
                // 计算下一步
                i++;
                next_num = pair.first - i * i;
            }
        }
        return 0;
    }
};

operation result:

Runtime: 16 ms, faster than 86.01% of C++ online submissions for Perfect Squares.
Memory Usage: 11.6 MB, less than 24.62% of C++ online submissions for Perfect Squares.

Solution three

Lagrange four square theorem: any positive integer can be expressed as not to exceed the sum of squares of four integers. Inference: number satisfying n (where four integer) number four and the square theorem, must satisfy n = 4 ^ a (8b + 7).

Do have good computer mathematics, this theorem tells us that the answer to this question is nothing more than just 1, 2, and if the above formula is satisfied, then the answer is 4, if you can not be one or two perfect squares composition, then returns 3.

(Really violent, ah, to escape ......

class Solution {
public:
    int numSquares(int n) {
        // Lagrange四平方定理:任何一个正整数都可以表示成不超过四个整数的平方之和。
        // 推论:满足四数平方和定理的数n(四个整数的情况),必定满足 n=4^a(8b+7)。
        while (n % 4 == 0)
            n /= 4;

        if (n % 8 == 7)
            return 4;

        int a = 0;
        while (a * a <= n) {
            int b = (int)sqrt(n - a * a);
            if (a * a + b * b == n) {
                return (a ==0 || b == 0) ? 1 : 2;
            }
            a++;
        }
        return 3;
    }
};

operation result:

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Perfect Squares.
Memory Usage: 8.4 MB, less than 90.15% of C++ online submissions for Perfect Squares.

Guess you like

Origin www.cnblogs.com/bingmang/p/11408326.html