
Problem Statement
In this problem, you are tasked with determining the minimum count of perfect square numbers which, when summed, equate to a given integer n
. A perfect square is defined as an integer that can be expressed as the product of some integer with itself. Examples of perfect squares include the numbers 1
, 4
, 9
, 16
, and so forth, while examples of non-perfect squares are 3
and 11
.
The challenge lies in efficiently determining the smallest combination of these perfect squares that sums up to the integer n
.
Examples
Example 1
Input:
n = 12
Output:
3
Explanation:
12 = 4 + 4 + 4.
Example 2
Input:
n = 13
Output:
2
Explanation:
13 = 4 + 9.
Constraints
1 <= n <= 104
Approach and Intuition
The task can be tackled by understanding and applying the principles of dynamic programming. The main idea is to construct solutions to smaller subproblems and use these solutions in a bottom-up manner to solve the larger problem at hand.
- Initialize an array
dp
of sizen + 1
with a default value of a large number (likefloat('inf')
) that signifies an undefined or infinite distance. This array will hold the minimum number of perfect squares needed for each integer from0
ton
. Setdp[0] = 0
because zero perfect squares sum up to zero. - Loop through each number
i
from1
ton
. For each number, iterate through each perfect squarej
starting from1
untilj^2 <= i
.- For each perfect square
j
, compute the potential new count asdp[i - j^2] + 1
- this represents the total number of perfect squares used if we includej^2
in the sum that makes upi
. - If this new calculated value is smaller than the currently stored
dp[i]
, updatedp[i]
with the new smaller value. This ensures that the minimum count is always stored.
- For each perfect square
- By the end of this process,
dp[n]
will contain the minimum number of perfect square numbers required to sum up ton
.
This dynamic programming approach ensures that all possibilities are considered without repetitive calculations, making the solution efficient and scalable within the problem's constraints.
Solutions
- Java
class Solution {
protected boolean checkPerfectSquare(int num) {
int sqrt = (int) Math.sqrt(num);
return num == sqrt * sqrt;
}
public int countSquares(int number) {
while (number % 4 == 0)
number /= 4;
if (number % 8 == 7)
return 4;
if (this.checkPerfectSquare(number))
return 1;
for (int i = 1; i * i <= number; ++i) {
if (this.checkPerfectSquare(number - i * i))
return 2;
}
return 3;
}
}
The given Java solution determines the minimum number of perfect square numbers which sum up to a given integer n
. This solution is based on Lagrange's Four Square theorem, which states that every natural number can be represented as the sum of four or fewer square numbers.
The
checkPerfectSquare
method checks if a given number is a perfect square. It calculates the square root of the number and then checks if the square of this root reverts to the original number, ensuring that the number is a perfect square.The
countSquares
method first reduces the given number by continuously dividing it by 4 if it's divisible by 4. This maintains the number's ability to be expressed as the sum of squares while simplifying the problem.If the number modulo 8 equals 7, by the properties derived from Lagrange's theorem, it always needs four squares for representation. Thus, it immediately returns 4.
The method then checks if the reduced number itself is a perfect square using
checkPerfectSquare
. If true, it returns 1, indicating the number alone is a perfect square.If not a single perfect square, it recursively checks every square less than the given number to see if the difference between the number and any of these squares is itself a perfect square. If so, it returns 2, indicating the number can be represented as a sum of two perfect squares.
If all the above conditions fail, the default return is 3, meaning the number can be represented using three squares.
This solution leverages mathematical properties to efficiently reduce the number of operations needed to determine the minimum squares required. By understanding and accounting for the special cases early (divisibility by 4 and remainder of 7 when divided by 8), the method quickly narrows down potential solutions, ensuring a more optimal performance.
No comments yet.