Skip to content

Commit 77af493

Browse files
Update 0279-perfect-squares.java
1 parent edcdfa7 commit 77af493

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+
class Solution {
2+
public int numSquares(int n) {
3+
int[] dp = new int[n+1];
4+
Arrays.fill(dp, n);
5+
dp[0] = 0;
6+
7+
for(int target = 0; target < n + 1; target++){
8+
for(int s = 0; s < target; s++){
9+
int square = s*s;
10+
if(target - square < 0)
11+
break;
12+
dp[target] = Math.min(dp[target], 1 + dp[target - square]);
13+
}
14+
}
15+
return dp[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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy