-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from vaskoz/day156
Day156
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package day156 | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
// MinSquared returns the smallest number | ||
// of squared integers which sum to n. | ||
// Runs in exponential time. | ||
func MinSquared(n int) int { | ||
return minSquared(n, 0, n) | ||
} | ||
|
||
func minSquared(n, level, minSoFar int) int { | ||
if n <= 3 { | ||
return n | ||
} | ||
end := int(math.Sqrt(float64(n))) + 1 | ||
for i := end; i > 0; i-- { | ||
if x := i * i; x <= n { | ||
if y := 1 + minSquared(n-x, level+1, minSoFar); y < minSoFar { | ||
minSoFar = y | ||
} | ||
} | ||
} | ||
return minSoFar | ||
} | ||
|
||
// MinSquaredDP returns the smallest number | ||
// of squared integers which sum to n. | ||
// Runs in O(N^2) time and O(N) space. | ||
func MinSquaredDP(n int) int { | ||
table := make([]int, 0, n+1) | ||
table = append(table, 0, 1, 2, 3) | ||
for i := 4; i <= n+1; i++ { | ||
table = append(table, i) | ||
for x := 1; x < i+1; x++ { | ||
if v := x * x; v <= i && table[i-v]+1 < table[i] { | ||
table[i] = table[i-v] + 1 | ||
} | ||
} | ||
} | ||
return table[n] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package day156 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
n, expected int | ||
}{ | ||
{13, 2}, | ||
{27, 3}, | ||
} | ||
|
||
func TestMinSquared(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := MinSquared(tc.n); result != tc.expected { | ||
t.Errorf("For N=%d Expected %v got %v", tc.n, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMinSquared(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MinSquared(tc.n) | ||
} | ||
} | ||
} | ||
|
||
func TestMinSquaredDP(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := MinSquaredDP(tc.n); result != tc.expected { | ||
t.Errorf("For N=%d Expected %v got %v", tc.n, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMinSquaredDP(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MinSquaredDP(tc.n) | ||
} | ||
} | ||
} |