-
Notifications
You must be signed in to change notification settings - Fork 32
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 #938 from vaskoz/day463
Day463
- Loading branch information
Showing
3 changed files
with
73 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,38 @@ | ||
package day463 | ||
|
||
// MinimizeQux returns the smallest Qux slice | ||
// possible. | ||
// Runs in O(N*K) where K is the number of elements that can removed. | ||
// Uses O(N) extra space to avoid modifying the input. | ||
func MinimizeQux(input []rune) []rune { | ||
lastLen := 0 | ||
copied := make([]rune, len(input)) | ||
copy(copied, input) | ||
|
||
for len(copied) != lastLen { | ||
lastLen = len(copied) | ||
copied = minimizeQux(copied) | ||
} | ||
|
||
return copied | ||
} | ||
|
||
func minimizeQux(input []rune) []rune { | ||
min := input | ||
|
||
for i := 1; i < len(input); i++ { | ||
if input[i] != input[i-1] { | ||
missing := 'R' + 'G' + 'B' - input[i] - input[i-1] | ||
end := append([]rune{missing}, input[i+1:]...) | ||
candidate := append([]rune{}, input[:i-1]...) | ||
candidate = append(candidate, end...) | ||
candidate = minimizeQux(candidate) | ||
|
||
if len(candidate) < len(min) { | ||
min = candidate | ||
} | ||
} | ||
} | ||
|
||
return min | ||
} |
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,34 @@ | ||
package day463 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input []rune | ||
expected []rune | ||
}{ | ||
{[]rune{'R', 'G', 'B', 'G', 'B'}, []rune{'R'}}, | ||
{[]rune{'G', 'B', 'G', 'B'}, []rune{'B', 'B'}}, | ||
{[]rune{'B', 'B', 'B', 'R', 'G', 'G', 'G'}, []rune{'G', 'G'}}, | ||
} | ||
|
||
func TestMinimizeQux(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if result := MinimizeQux(tc.input); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v, got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMinimizeQux(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MinimizeQux(tc.input) | ||
} | ||
} | ||
} |