Skip to content

Commit

Permalink
Merge 058d618 into 4a8876a
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Apr 3, 2019
2 parents 4a8876a + 058d618 commit ad41526
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,4 @@ problems from
* [Day 221](https://github.com/vaskoz/dailycodingproblem-go/issues/454)
* [Day 222](https://github.com/vaskoz/dailycodingproblem-go/issues/458)
* [Day 223](https://github.com/vaskoz/dailycodingproblem-go/issues/460)
* [Day 224](https://github.com/vaskoz/dailycodingproblem-go/issues/462)
13 changes: 13 additions & 0 deletions day224/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package day224

// SmallestPositiveIntNotInSubsetSum returns the
// smallest positive integer that is not the sum of a subset of the array,
// given a sorted array.
// Runs in O(N) time and O(1) space.
func SmallestPositiveIntNotInSubsetSum(nums []int) int {
result := 1
for i := 0; i < len(nums) && nums[i] <= result; i++ {
result += nums[i]
}
return result
}
31 changes: 31 additions & 0 deletions day224/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package day224

import "testing"

var testcases = []struct {
nums []int
smallest int
}{
{[]int{1, 2, 3, 10}, 7},
{[]int{1, 3, 4, 5}, 2},
{[]int{1, 2, 6, 10, 11, 15}, 4},
{[]int{1, 1, 1, 1}, 5},
{[]int{1, 1, 3, 4}, 10},
}

func TestSmallestPositiveIntNotInSubsetSum(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := SmallestPositiveIntNotInSubsetSum(tc.nums); result != tc.smallest {
t.Errorf("For %v, expected %v, got %v", tc.nums, tc.smallest, result)
}
}
}

func BenchmarkSmallestPositiveIntNotInSubsetSum(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
SmallestPositiveIntNotInSubsetSum(tc.nums)
}
}
}

0 comments on commit ad41526

Please sign in to comment.