-
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.
- 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 day374 | ||
|
||
import ( | ||
"errors" | ||
"sort" | ||
) | ||
|
||
var ( | ||
errNoSuchIndex = errors.New("no index is a fixed point") | ||
errInputNotSorted = errors.New("input is not sorted and must be") | ||
) | ||
|
||
// LowestFixedPoint returns the lowest fixed point index. | ||
// If no such index exists, it returns an error. | ||
// Also returns an error if input is not sorted. | ||
// Runs in O(log N) worst case time thanks to binary search. | ||
func LowestFixedPoint(sorted []int) (int, error) { | ||
if !sort.IntsAreSorted(sorted) { | ||
return 0, errInputNotSorted | ||
} | ||
|
||
for lo, hi := 0, len(sorted); lo <= hi; { | ||
switch mid := (lo + hi) / 2; { | ||
case sorted[mid] == mid: | ||
if hi-lo == 0 { | ||
return mid, nil | ||
} | ||
|
||
hi = mid | ||
case sorted[mid] < mid: | ||
lo = mid + 1 | ||
default: | ||
hi = mid - 1 | ||
} | ||
} | ||
|
||
return 0, errNoSuchIndex | ||
} |
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 day374 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input []int | ||
expected int | ||
expectedErr error | ||
}{ | ||
{[]int{-5, -3, 2, 3}, 2, nil}, | ||
{[]int{2, 3, -3}, 0, errInputNotSorted}, | ||
{[]int{-5, -3, 5, 13}, 0, errNoSuchIndex}, | ||
{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 0, nil}, | ||
{[]int{-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, 1, 15}, 15, nil}, | ||
} | ||
|
||
func TestLowestFixedPoint(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if res, err := LowestFixedPoint(tc.input); res != tc.expected || err != tc.expectedErr { | ||
t.Errorf("Expected (%v,%v), got (%v,%v)", tc.expected, tc.expectedErr, res, err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLowestFixedPoint(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
LowestFixedPoint(tc.input) // nolint | ||
} | ||
} | ||
} |