mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Update 0279.完全平方数.md
This commit is contained in:
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user