-
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
92 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,35 @@ | ||
package day18 | ||
|
||
// MaxSubarraysBrute uses no extra memory, but runs in O(k*N) | ||
func MaxSubarraysBrute(nums []int, k int) []int { | ||
var result []int | ||
for i := 0; i < len(nums)-k+1; i++ { | ||
max := nums[i] | ||
for j := i + 1; j < i+k; j++ { | ||
if nums[j] > max { | ||
max = nums[j] | ||
} | ||
} | ||
result = append(result, max) | ||
} | ||
return result | ||
} | ||
|
||
// MaxSubarraysOnePass runs through the input only once. | ||
func MaxSubarraysOnePass(nums []int, k int) []int { | ||
var result []int | ||
var sublist []int // O(k) extra space | ||
for i := 0; i < len(nums); i++ { | ||
if i >= k { | ||
result = append(result, nums[sublist[0]]) | ||
} | ||
for len(sublist) > 0 && sublist[0] <= i-k { | ||
sublist = sublist[1:] | ||
} | ||
for len(sublist) > 0 && nums[i] >= nums[sublist[len(sublist)-1]] { | ||
sublist = sublist[:len(sublist)-1] | ||
} | ||
sublist = append(sublist, i) | ||
} | ||
return append(result, nums[sublist[0]]) | ||
} |
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,56 @@ | ||
package day18 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
input []int | ||
k int | ||
expected []int | ||
}{ | ||
{[]int{10, 5, 2, 7, 8, 7}, 3, []int{10, 7, 8, 8}}, | ||
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, | ||
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, | ||
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, | ||
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}, | ||
25, | ||
[]int{25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, | ||
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, | ||
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}}, | ||
} | ||
|
||
func TestMaxSubarraysBrute(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := MaxSubarraysBrute(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v bot got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMaxSubarraysBrute(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MaxSubarraysBrute(tc.input, tc.k) | ||
} | ||
} | ||
} | ||
|
||
func TestMaxSubarraysOnePass(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := MaxSubarraysOnePass(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v bot got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMaxSubarraysOnePass(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MaxSubarraysOnePass(tc.input, tc.k) | ||
} | ||
} | ||
} |