Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Feb 8, 2024
1 parent f8f156f commit 66704cf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions leetcode/279.PerfectSquares/perfectSquares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://leetcode.com/problems/perfect-squares

package main

import (
"math"
)

func numSquares(n int) int {
f := make([]int, n+1)

for i := 1; i <= n; i++ {
minCount := math.MaxInt32
for j := 1; j*j <= i; j++ {
minCount = min(minCount, f[i-j*j]+1)
}
f[i] = minCount
}

return f[n]
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func main() {
// Example usage:
n := 12
result := numSquares(n)
println(result) // Output: 2 (12 = 4 + 4 + 4)
}

0 comments on commit 66704cf

Please sign in to comment.