From acbf3a25f0df16f67ef0e5238a2c35c2f7dba756 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Wed, 3 Apr 2019 11:02:35 -0600 Subject: [PATCH 1/2] day 224: super easy, sorted array, smallest positive int not in subset sum --- day224/problem.go | 13 +++++++++++++ day224/problem_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 day224/problem.go create mode 100644 day224/problem_test.go diff --git a/day224/problem.go b/day224/problem.go new file mode 100644 index 0000000..1dae348 --- /dev/null +++ b/day224/problem.go @@ -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 +} diff --git a/day224/problem_test.go b/day224/problem_test.go new file mode 100644 index 0000000..a8e6924 --- /dev/null +++ b/day224/problem_test.go @@ -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) + } + } +} From 058d618326420996b18386dd5ed116938c6a9538 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Wed, 3 Apr 2019 11:03:05 -0600 Subject: [PATCH 2/2] add day 224 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5a4dc8b..7f50028 100644 --- a/README.md +++ b/README.md @@ -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)