给你一个整数 n
,返回 和为 n
的完全平方数的最少数量 。
完全平方数 是一个整数,其值等于另一个整数的平方;换句话说,其值等于一个整数自乘的积。例如,1
、4
、9
和 16
都是完全平方数,而 3
和 11
不是。
示例 1:
输入:n =12
输出:3 解释:12 = 4 + 4 + 4
示例 2:
输入:n =13
输出:2 解释:13 = 4 + 9
提示:
1 <= n <= 104
方法一:动态规划(完全背包)
我们定义
我们可以枚举使用的最后一个数字的数量
其中
不妨令
将二式代入一式,我们可以得到以下状态转移方程:
最后答案即为
时间复杂度
注意到
相似题目:
class Solution:
def numSquares(self, n: int) -> int:
m = int(sqrt(n))
f = [[inf] * (n + 1) for _ in range(m + 1)]
f[0][0] = 0
for i in range(1, m + 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if j >= i * i:
f[i][j] = min(f[i][j], f[i][j - i * i] + 1)
return f[m][n]
class Solution:
def numSquares(self, n: int) -> int:
m = int(sqrt(n))
f = [0] + [inf] * n
for i in range(1, m + 1):
for j in range(i * i, n + 1):
f[j] = min(f[j], f[j - i * i] + 1)
return f[n]
class Solution {
public int numSquares(int n) {
int m = (int) Math.sqrt(n);
int[][] f = new int[m + 1][n + 1];
for (var g : f) {
Arrays.fill(g, 1 << 30);
}
f[0][0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i * i) {
f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
}
}
}
return f[m][n];
}
}
class Solution {
public int numSquares(int n) {
int m = (int) Math.sqrt(n);
int[] f = new int[n + 1];
Arrays.fill(f, 1 << 30);
f[0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = i * i; j <= n; ++j) {
f[j] = Math.min(f[j], f[j - i * i] + 1);
}
}
return f[n];
}
}
class Solution {
public:
int numSquares(int n) {
int m = sqrt(n);
int f[m + 1][n + 1];
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i * i) {
f[i][j] = min(f[i][j], f[i][j - i * i] + 1);
}
}
}
return f[m][n];
}
};
class Solution {
public:
int numSquares(int n) {
int m = sqrt(n);
int f[n + 1];
memset(f, 0x3f, sizeof(f));
f[0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = i * i; j <= n; ++j) {
f[j] = min(f[j], f[j - i * i] + 1);
}
}
return f[n];
}
};
func numSquares(n int) int {
m := int(math.Sqrt(float64(n)))
f := make([][]int, m+1)
const inf = 1 << 30
for i := range f {
f[i] = make([]int, n+1)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = 0
for i := 1; i <= m; i++ {
for j := 0; j <= n; j++ {
f[i][j] = f[i-1][j]
if j >= i*i {
f[i][j] = min(f[i][j], f[i][j-i*i]+1)
}
}
}
return f[m][n]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func numSquares(n int) int {
m := int(math.Sqrt(float64(n)))
f := make([]int, n+1)
for i := range f {
f[i] = 1 << 30
}
f[0] = 0
for i := 1; i <= m; i++ {
for j := i * i; j <= n; j++ {
f[j] = min(f[j], f[j-i*i]+1)
}
}
return f[n]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
function numSquares(n: number): number {
const m = Math.floor(Math.sqrt(n));
const f: number[][] = Array(m + 1)
.fill(0)
.map(() => Array(n + 1).fill(1 << 30));
f[0][0] = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i * i) {
f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
}
}
}
return f[m][n];
}
function numSquares(n: number): number {
const m = Math.floor(Math.sqrt(n));
const f: number[] = Array(n + 1).fill(1 << 30);
f[0] = 0;
for (let i = 1; i <= m; ++i) {
for (let j = i * i; j <= n; ++j) {
f[j] = Math.min(f[j], f[j - i * i] + 1);
}
}
return f[n];
}