Skip to content

Commit

Permalink
Merge pull request #216 from vaskoz/day102fix
Browse files Browse the repository at this point in the history
Day102fix
  • Loading branch information
vaskoz committed Dec 2, 2018
2 parents db2b481 + 545bcdb commit b8737c9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
31 changes: 29 additions & 2 deletions day102/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ func ContiguousSumBrute(nums []int, k int) []int {
return result
}

// ContiguousSumFaster uses a window to return the
// ContiguousSumNonNegative uses a window to return the
// contiguous subset that sums to K.
// Can only tolerate non-negative values, panics otherwise.
// Runtime is O(N) and O(1) space.
func ContiguousSumFaster(nums []int, k int) []int {
func ContiguousSumNonNegative(nums []int, k int) []int {
var result []int
var sum, begin int
for end := 0; end < len(nums); end++ {
if nums[end] < 0 {
panic("negative values aren't permitted")
}
sum += nums[end]
if sum == k {
result = nums[begin : end+1]
Expand All @@ -37,3 +41,26 @@ func ContiguousSumFaster(nums []int, k int) []int {
}
return result
}

// ContiguousSumNegatives returns the
// contiguous subset that sums to K.
// This implementation tolerates negative values.
// Runtime is O(N) and O(N) space.
func ContiguousSumNegatives(nums []int, k int) []int {
var result []int
var sum int
m := make(map[int]int)
for i := range nums {
sum += nums[i]
if sum-k == 0 {
result = nums[0 : i+1]
break
}
if start, found := m[sum-k]; found {
result = nums[start+1 : i+1]
break
}
m[sum] = i
}
return result
}
77 changes: 68 additions & 9 deletions day102/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

var testcases = []struct {
var nonNegativeTestcases = []struct {
input []int
k int
expected []int
Expand All @@ -18,9 +18,27 @@ var testcases = []struct {
49, []int{4, 5, 6, 7, 8, 9, 10}},
}

var negativeTestcases = []struct {
input []int
k int
expected []int
}{
{[]int{-1, 2, 3, 4, 5}, 1, []int{-1, 2}},
{[]int{1, -2, 3, 4, 5}, 5, []int{-2, 3, 4}},
{[]int{1, 2, -3, 4, 5}, 6, []int{-3, 4, 5}},
{[]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},
-49, []int{-24, -25}},
}

func TestContiguousSumBrute(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
for _, tc := range nonNegativeTestcases {
if result := ContiguousSumBrute(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
for _, tc := range negativeTestcases {
if result := ContiguousSumBrute(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
Expand All @@ -29,25 +47,66 @@ func TestContiguousSumBrute(t *testing.T) {

func BenchmarkContiguousSumBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
for _, tc := range nonNegativeTestcases {
ContiguousSumBrute(tc.input, tc.k)
}
for _, tc := range negativeTestcases {
ContiguousSumBrute(tc.input, tc.k)
}
}
}

func TestContiguousSumFaster(t *testing.T) {
func TestContiguousSumNonNegative(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := ContiguousSumFaster(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
for _, tc := range nonNegativeTestcases {
if result := ContiguousSumNonNegative(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkContiguousSumFaster(b *testing.B) {
func TestContiguousSumNonNegativePanic(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected a panic from the negative input value")
}
}()
ContiguousSumNonNegative([]int{1, -2, 3}, 10)
}

func BenchmarkContiguousSumNonNegative(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
ContiguousSumFaster(tc.input, tc.k)
for _, tc := range nonNegativeTestcases {
ContiguousSumNonNegative(tc.input, tc.k)
}
for _, tc := range nonNegativeTestcases {
ContiguousSumNonNegative(tc.input, tc.k)
}
}
}

func TestContiguousSumNegatives(t *testing.T) {
t.Parallel()
for _, tc := range nonNegativeTestcases {
if result := ContiguousSumNegatives(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
for _, tc := range negativeTestcases {
if result := ContiguousSumNegatives(tc.input, tc.k); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkContiguousSumNegatives(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range nonNegativeTestcases {
ContiguousSumNegatives(tc.input, tc.k)
}
for _, tc := range negativeTestcases {
ContiguousSumNegatives(tc.input, tc.k)
}
}
}

0 comments on commit b8737c9

Please sign in to comment.