Skip to content

Commit 090c577

Browse files
authored
Merge pull request neetcode-gh#1634 from tahsintunan/279
Create 279. Perfect Squares
2 parents bce3127 + f980ffb commit 090c577

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

go/0279-perfect-squares.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var MX = 10005
2+
3+
func numSquares(n int) int {
4+
dp := make([]int, n+1)
5+
for i := range dp {
6+
dp[i] = MX
7+
}
8+
9+
dp[0] = 0
10+
for i := 1; i <= n; i++ {
11+
mn := MX
12+
j := 1
13+
for i-j*j >= 0 {
14+
mn = min(mn, dp[i-j*j]+1)
15+
j++
16+
}
17+
dp[i] = mn
18+
}
19+
return dp[n]
20+
}
21+
22+
func min(a, b int) int {
23+
if a < b {
24+
return a
25+
}
26+
return b
27+
}

0 commit comments

Comments
 (0)