给定正整数 n,找到若干个完全平方数(比如1, 4, 9, 16, ...
)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
输入: n = 12 输出: 3 解释: 12 = 4 + 4 + 4
输入: n = 13 输出: 2 解释: 13 = 4 + 9
use std::collections::HashSet;
impl Solution {
pub fn num_squares(n: i32) -> i32 {
let pt_sq: Vec<i32> = (1..=n).map(|x| x * x).filter(|&x| x <= n).collect();
let mut set = HashSet::new();
let mut v = vec![n];
let mut cnt = 1;
while !v.is_empty() {
for _ in 0..v.len() {
let num = v.remove(0);
for i in &pt_sq {
if num - i == 0 {
return cnt;
} else if num - i > 0 && !set.contains(&(num - i)) {
set.insert(num - i);
v.push(num - i);
} else if num - i < 0 {
break;
}
}
}
cnt += 1;
}
0
}
}
impl Solution {
pub fn num_squares(n: i32) -> i32 {
let n = n as usize;
let mut v = vec![0; n];
let mut i = 1;
while i * i <= n {
v[i * i - 1] = 1;
i += 1;
}
for i in 2..=n {
if v[i - 1] == 0 {
v[i - 1] = i as i32;
let mut j = 1;
while j * j < i {
v[i - 1] = v[i - 1].min(v[i - j * j - 1] + 1);
j += 1;
}
}
}
v[n - 1]
}
}