-
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.
- Loading branch information
Showing
3 changed files
with
144 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,75 @@ | ||
package day155 | ||
|
||
import ( | ||
"errors" | ||
"sort" | ||
) | ||
|
||
var errNoMajority = errors.New("no majority exists") | ||
|
||
// ErrNoMajority returns the same error returned when no majority exists. | ||
func ErrNoMajority() error { | ||
return errNoMajority | ||
} | ||
|
||
// MajorityElementMap returns the number that occurs over half the time. | ||
// Runs in O(N) time and O(unique values) in space. | ||
func MajorityElementMap(nums []int) (int, error) { | ||
counts := make(map[int]int) | ||
for _, v := range nums { | ||
counts[v]++ | ||
} | ||
threshold := len(nums) / 2 | ||
for k, v := range counts { | ||
if v >= threshold { | ||
return k, nil | ||
} | ||
} | ||
return 0, errNoMajority | ||
} | ||
|
||
// MajorityElementSort returns the number that occurs over half the time. | ||
// Runs in O(N log N) time and O(1) in space. | ||
func MajorityElementSort(nums []int) (int, error) { | ||
if len(nums) < 1 { | ||
return 0, errNoMajority | ||
} | ||
nums = append([]int{}, nums...) // don't mutate the input. | ||
sort.Ints(nums) | ||
var maxVal, maxCount int | ||
val := nums[0] | ||
count := 1 | ||
for i := 1; i < len(nums); i++ { | ||
if nums[i] == val { | ||
count++ | ||
} else { | ||
if count > maxCount { | ||
maxCount = count | ||
maxVal = val | ||
} | ||
val = nums[i] | ||
count = 1 | ||
} | ||
} | ||
if count > maxCount { | ||
return val, nil | ||
} | ||
return maxVal, nil | ||
} | ||
|
||
// MajorityBoyerMoore runs in O(N) time and O(1) space. | ||
// Limitation: only works if there is definitely a majority value. | ||
func MajorityBoyerMoore(nums []int) (int, error) { | ||
var m, i int | ||
for _, x := range nums { | ||
if i == 0 { | ||
m = x | ||
i = 1 | ||
} else if m == x { | ||
i++ | ||
} else { | ||
i-- | ||
} | ||
} | ||
return m, nil | ||
} |
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,68 @@ | ||
package day155 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
nums []int | ||
majority int | ||
err error | ||
}{ | ||
{[]int{1, 2, 1, 1, 3, 1, 4, 0}, 1, nil}, | ||
{[]int{}, 0, ErrNoMajority()}, | ||
{[]int{4, 1, 2, 4, 1, 4, 1, 3, 4, 0, 4}, 4, nil}, | ||
{[]int{1, 2, 4, 1, 4, 1, 4, 3, 4, 0, 4}, 4, nil}, | ||
} | ||
|
||
func TestMajorityElementMap(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result, err := MajorityElementMap(tc.nums); err != tc.err || result != tc.majority { | ||
t.Errorf("Expected (%v,%v) got (%v,%v)", tc.majority, tc.err, result, err) | ||
} | ||
} | ||
} | ||
|
||
func TestMajorityElementSort(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result, err := MajorityElementSort(tc.nums); err != tc.err || result != tc.majority { | ||
t.Errorf("Expected (%v,%v) got (%v,%v)", tc.majority, tc.err, result, err) | ||
} | ||
} | ||
} | ||
|
||
func TestMajorityBoyerMoore(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if tc.err != nil { | ||
continue | ||
} | ||
if result, err := MajorityBoyerMoore(tc.nums); err != tc.err || result != tc.majority { | ||
t.Errorf("Expected (%v,%v) got (%v,%v)", tc.majority, tc.err, result, err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMajorityElementMap(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MajorityElementMap(tc.nums) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMajorityElementSort(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MajorityElementSort(tc.nums) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMajorityBoyerMoore(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MajorityBoyerMoore(tc.nums) | ||
} | ||
} | ||
} |