Skip to content

Commit bc4663f

Browse files
authored
Create Perfect Square
1 parent 22f916a commit bc4663f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Perfect Square

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int numSquares(int n) {
3+
int[] dp = new int[n+1];
4+
if(n<=3){
5+
return n;
6+
}
7+
dp[0] = 0;
8+
dp[1] = 1;
9+
dp[2] = 2;
10+
dp[3] = 3;
11+
for(int i = 4; i<=n; i++){
12+
dp[i] = i;
13+
for(int j = 1; j <= Math.sqrt(i); j++){
14+
dp[i] = Math.min(dp[i], 1+dp[i-j*j]);
15+
}
16+
}
17+
return dp[n];
18+
}
19+
}

0 commit comments

Comments
 (0)