Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45037ce

Browse files
Update 0279.完全平方数.md
1 parent 87e7b9d commit 45037ce

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎problems/0279.完全平方数.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ class Solution:
231231

232232
return dp[n]
233233

234+
```
235+
先遍历背包, 再遍历物品
236+
```python
237+
class Solution:
238+
def numSquares(self, n: int) -> int:
239+
dp = [float('inf')] * (n + 1)
240+
dp[0] = 0
241+
242+
for i in range(1, int(n ** 0.5) + 1): # 遍历物品
243+
for j in range(i * i, n + 1): # 遍历背包
244+
# 更新凑成数字 j 所需的最少完全平方数数量
245+
dp[j] = min(dp[j - i * i] + 1, dp[j])
246+
247+
return dp[n]
248+
249+
234250
```
235251
其他版本
236252
```python

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /