Movatterモバイル変換


[0]ホーム

URL:


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

Commit77af493

Browse files
Update 0279-perfect-squares.java
1 parentedcdfa7 commit77af493

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎java/0279-perfect-squares.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
classSolution {
2+
publicintnumSquares(intn) {
3+
int[]dp =newint[n+1];
4+
Arrays.fill(dp,n);
5+
dp[0] =0;
6+
7+
for(inttarget =0;target <n +1;target++){
8+
for(ints =0;s <target;s++){
9+
intsquare =s*s;
10+
if(target -square <0)
11+
break;
12+
dp[target] =Math.min(dp[target],1 +dp[target -square]);
13+
}
14+
}
15+
returndp[n];
16+
}
17+
}
18+
19+
/*---------------------------------------------------------------------------------------------------------------------------------------------------------*/
20+
121
//This is a BFS based approach.
222
//We can also do this problem similar to coin change but the Time and space complexity will remain same (Just an extra queue in this one stil linear space).
323

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp