Given an integer n
, return the least number of perfect square numbers that sum to n
.
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1
, 4
, 9
, and 16
are perfect squares while 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.
Constraints:
1 <= n <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a number, and you want to find the fewest perfect squares that add up to it. The brute force method is like trying every possible combination of perfect squares to see if they sum up to your number.
Here's how the algorithm would work step-by-step:
def perfect_squares_brute_force(number):
# Generate perfect squares less than or equal to the input number
perfect_squares = [square**2 for square in range(1, int(number**0.5) + 1)]
if number in perfect_squares:
return 1
# Iterate through possible number of perfect squares
for num_squares in range(2, number + 1):
# Try combinations of perfect squares
combinations = find_combinations(number, num_squares, perfect_squares)
if combinations:
return num_squares
return number
def find_combinations(target_number, num_squares, perfect_squares):
if num_squares == 0:
return target_number == 0
if target_number < 0:
return False
# Check all possible combinations with recursion
for perfect_square in perfect_squares:
if find_combinations(target_number - perfect_square, num_squares - 1, perfect_squares):
return True
return False
def num_squares(number):
# Handle edge case for non-positive numbers
if number <= 0:
return 0
# Use brute force method to find fewest perfect squares
return perfect_squares_brute_force(number)
The goal is to find the smallest number of perfect squares that add up to a given number. Instead of trying every possible combination, we use a method that remembers the best solutions for smaller numbers to build up to the solution for the bigger number.
Here's how the algorithm would work step-by-step:
def perfect_squares(number):
number_of_squares = [0] * (number + 1)
for current_number in range(1, number + 1):
min_squares_needed = current_number
# Iterate through all perfect squares <= current number
square_root = 1
while square_root * square_root <= current_number:
square = square_root * square_root
# Find number of squares needed using a perfect square
remaining_number = current_number - square
squares_needed = number_of_squares[remaining_number] + 1
# Update minimum if a better solution is found
min_squares_needed = min(min_squares_needed, squares_needed)
square_root += 1
# Store the minimum number of perfect squares
number_of_squares[current_number] = min_squares_needed
#The last element of the list contains the result
return number_of_squares[number]
Case | How to Handle |
---|---|
Input n is 0 | Return 0 because no perfect squares are needed to sum to 0. |
Input n is 1 | Return 1 since 1 is a perfect square itself. |
Input n is a perfect square (e.g., 4, 9, 16) | Return 1 immediately because n itself forms a perfect square that sums to n |
Large value of n that could cause integer overflow issues during intermediate calculations (specifically when constructing the list of perfect squares or during BFS) | Use long data type for intermediate calculations or apply appropriate bounds checking to prevent integer overflow. |
Input n requiring many perfect squares to sum to it (worst-case scenario for performance) | Consider the time complexity of the chosen algorithm and optimize for large n, such as using dynamic programming or a BFS-based approach with pruning. |
Input n where the optimal solution involves many small perfect squares (e.g., n=7) | The solution should explore all possible combinations including multiple instances of smaller perfect squares like 1. |
Negative Input | Throw an IllegalArgumentException or return -1 as the problem statement specified positive integers only. |
Legendre's three-square theorem states that a natural number n can be represented as the sum of three squares of integers if and only if n is not of the form n = 4^a(8b + 7) for nonnegative integers a and b; numbers of this form would require 4 squares. | Applying Legendre's Three Square Theorem can potentially provide a faster solution by reducing the search space when a 4 square solution is required. |