Skip to content

Commit

Permalink
Merge 6287942 into 9bc3d1a
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Sep 8, 2018
2 parents 9bc3d1a + 6287942 commit 5e9addb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ problems from
* [Day 15](https://github.com/vaskoz/dailycodingproblem-go/issues/33)
* [Day 16](https://github.com/vaskoz/dailycodingproblem-go/issues/35)
* [Day 17](https://github.com/vaskoz/dailycodingproblem-go/issues/37)
* [Day 18](https://github.com/vaskoz/dailycodingproblem-go/issues/39)
35 changes: 35 additions & 0 deletions day18/problem.go
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]])
}
56 changes: 56 additions & 0 deletions day18/problem_test.go
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)
}
}
}

0 comments on commit 5e9addb

Please sign in to comment.