algorithms

A catalog of data structures and problems with approaches to solve them

View the Project on GitHub gitgik/algorithms

Minumum squares that sum to n

Given a positive integer n, find the smallest number of squared integers which sum to n.

For example, given n = 13, return 2 since 13 = 32 + 22 = 9 + 4.

Given n = 27, return 3 since 27 = 32 + 32 + 32 = 9 + 9 + 9.

Naive Approach

We can use a recursive approach to naively solve this problem:

from math import inf

def num_squares(n):
    if n == 0:
        return 0
    
    min_num_squares = inf
    i = 1
    while n - i * i >= 0:
        min_num_squares = min(min_num_squares, num_squares(n - i * i) + 1)
        i += 1
        
    return min_num_squares
num_squares(27)
3

DP Approach

Since this takes exponential time, we can speed it up by using a cache with dynamic programming.

from math import inf

def min_num_squares(n):
    cache = [inf for i in range(n + 1)]
    cache[0] = 0
    
    for i in range(n + 1):
        j = 1
        while j * j <= i:
            cache[i] = min(cache[i], cache[i - j * j] + 1)
            j += 1
    return cache[n]
min_num_squares(13)
2

This will run on O(n^2) time and O(n) space