-
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 #308 from vaskoz/day144
Day144
- Loading branch information
Showing
3 changed files
with
64 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,23 @@ | ||
package day144 | ||
|
||
// ClosestLargerNumberIndexBrute looks for the closest number larger | ||
// than the target and returns the index of that number. | ||
// If no number if found, then -1 is returned. | ||
// Runs in O(N) time. | ||
func ClosestLargerNumberIndexBrute(nums []int, targetIndex int) int { | ||
if targetIndex < 0 || targetIndex >= len(nums) { | ||
panic("index out of bounds") | ||
} | ||
val := nums[targetIndex] | ||
left, right := targetIndex-1, targetIndex+1 | ||
for left >= 0 || right < len(nums) { | ||
if left >= 0 && nums[left] > val { | ||
return left | ||
} else if right < len(nums) && nums[right] > val { | ||
return right | ||
} | ||
left-- | ||
right++ | ||
} | ||
return -1 | ||
} |
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,40 @@ | ||
package day144 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
nums []int | ||
targetIndex int | ||
expected int | ||
}{ | ||
{[]int{4, 1, 3, 5, 6}, 0, 3}, | ||
{[]int{4, 1, 3, 5, 6}, 4, -1}, | ||
{[]int{4, 1, 3, 5, 6}, 1, 0}, | ||
} | ||
|
||
func TestClosestLargerNumberIndexBrute(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := ClosestLargerNumberIndexBrute(tc.nums, tc.targetIndex); result != tc.expected { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func TestClosestLargerNumberIndexBruteBoundsCheck(t *testing.T) { | ||
t.Parallel() | ||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Errorf("It should panic with an index out of bounds") | ||
} | ||
}() | ||
ClosestLargerNumberIndexBrute([]int{1, 2}, 3) | ||
} | ||
|
||
func BenchmarkClosestLargerNumberIndexBrute(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ClosestLargerNumberIndexBrute(tc.nums, tc.targetIndex) | ||
} | ||
} | ||
} |