# Question

Formatted question description: https://leetcode.ca/all/279.html

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

# Algorithm

Create a one-dimensional dp array of length n+1, initialize the first value to 0, and initialize the remaining values to INT_MAX

i loops from 0 to n, j loops from 1 to the position of i+jj <= n, and then updates the value of dp[i+jj] each time, dynamically updating the dp array, where dp[i] means positive The integer i can be composed of multiple perfect squares.

Note that the wording here, i must start from 0, j must start from 1. Because our original intention is to use dp[i] to update dp[i + j * j], if i=0, j=1 , Then dp[i] and dp[i + j * j] are equal.

# Code

• import java.util.Arrays;

public class Perfect_Squares {

public static void main(String[] args) {
Perfect_Squares out = new Perfect_Squares();
Solution s = out.new Solution();

System.out.println(s.numSquares(43));
}

class Solution_lessLoop {
public int numSquares(int n) {

if (n <= 0) {
return 0;
}

// count[i] means, for number i, minimum square count
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);

dp[0] = 0;
for (int i = 0; i <= n; ++i) {
for (int j = 1; i + j * j <= n; ++j) {
dp[i + j * j] = Math.min(dp[i + j * j], dp[i] + 1);
}
}

return dp[n];
}
}

class Solution {
public int numSquares(int n) {

if (n <= 0) {
return 0;
}

// count[i] means, for number i, minimum square count
int[] count = new int[n + 1];
Arrays.fill(count, Integer.MAX_VALUE);
count[0] = 0;

// fill in sqaures first
int maxPossibleSqaureRoot = (int)Math.sqrt(n * 1.0);
for (int i = 1; i <= maxPossibleSqaureRoot; i++) {
count[(int)Math.pow(i, 2)] = 1;
}

for (int i = 1; i <= n; i++) {

int start = 0;
int end = i / 2;

for (int j = start; j <= end; j++) {
count[i] = Math.min(count[i], count[j] + count[i - j]);
}
}

return count[n];
}
}
}

############

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

• // OJ: https://leetcode.com/problems/perfect-squares/
// Time: O(NS) where S is the count of square numbers less than n.
// Space: O(N)
class Solution {
public:
int numSquares(int n) {
vector<int> v(n + 1, INT_MAX);
for (int i = 1; i * i <= n; ++i) v[i * i] = 1;
for (int i = 1; i <= n; ++i) {
if (v[i] == 1) continue;
for (int j = 1; j * j < i; ++j) {
v[i] = min(v[i], 1 + v[i - j * j]);
}
}
return v[n];
}
};

• class Solution:
def numSquares(self, n: int) -> int:
dp = [0] * (n + 1)
for i in range(1, n + 1):
j, mi = 1, inf
while j * j <= i:
mi = min(mi, dp[i - j * j])
j += 1
dp[i] = mi + 1
return dp[-1]

############

class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
squares = []
j = 1
while j * j <= n:
squares.append(j * j)
j += 1
level = 0
queue = [n]
visited = [False] * (n + 1)
while queue:
level += 1
temp = []
for q in queue:
for factor in squares:
if q - factor == 0:
return level
if q - factor < 0:
break
if visited[q - factor]:
continue
temp.append(q - factor)
visited[q - factor] = True
queue = temp
return level


• func numSquares(n int) int {
dp := make([]int, n+1)
for i := 1; i <= n; i++ {
mi := 100000
for j := 1; j*j <= i; j++ {
mi = min(mi, dp[i-j*j])
}
dp[i] = mi + 1
}
return dp[n]
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

• function numSquares(n: number): number {
let dp = new Array(n + 1).fill(0);
for (let i = 1; i <= n; ++i) {
let min = Infinity;
for (let j = 1; j * j <= i; ++j) {
min = Math.min(min, dp[i - j * j]);
}
dp[i] = min + 1;
}
return dp.pop();
}