diff --git a/220-contains-duplicate-3.go b/220-contains-duplicate-3.go new file mode 100644 index 0000000..a471a09 --- /dev/null +++ b/220-contains-duplicate-3.go @@ -0,0 +1,22 @@ +package main + +import "math" + +func ContainsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + if k < 1 { + return false + } + + for wSz := 1; wSz <= k; wSz++ { + ws := 0 + for we := ws + wSz; we < len(nums); we++ { + if int(math.Abs(float64(nums[ws]-nums[we]))) <= t { + return true + } + ws++ + } + + } + + return false +} diff --git a/220-contains-duplicate-3_test.go b/220-contains-duplicate-3_test.go new file mode 100644 index 0000000..54eb96b --- /dev/null +++ b/220-contains-duplicate-3_test.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestContainsNearbyAlmostDuplicate(t *testing.T) { + assert.False(t, ContainsNearbyAlmostDuplicate([]int{1, 5, 9, 1, 5, 9}, 2, 3)) + assert.True(t, ContainsNearbyAlmostDuplicate([]int{1, 2, 3, 1}, 3, 0)) + assert.True(t, ContainsNearbyAlmostDuplicate([]int{1, 0, 1, 1}, 1, 2)) +}