Update 0279.完全平方数.md

This commit is contained in:
fwqaaq
2023-05-30 18:30:14 +08:00
committed by GitHub
parent 83a87ab5ad
commit a2ff242303

View File

@ -369,6 +369,27 @@ impl Solution {
}
```
```rust
// 先遍历物品
impl Solution {
pub fn num_squares(n: i32) -> i32 {
let (n, mut goods) = (n as usize, 1);
let mut dp = vec![i32::MAX; n + 1];
dp[0] = 0;
loop {
if goods * goods > n {
break;
}
for j in goods * goods..=n {
dp[j] = dp[j].min(dp[j - goods * goods] + 1);
}
goods += 1;
}
dp[n]
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">