Skip to content

Commit

Permalink
Merge pull request #753 from vaskoz/day373
Browse files Browse the repository at this point in the history
Day373
  • Loading branch information
vaskoz committed Nov 27, 2019
2 parents 3a16197 + d7f5d50 commit 2fc7372
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,5 @@ problems from
* Day 370 - Never received an e-mail for this day.
* [Day 371](https://github.com/vaskoz/dailycodingproblem-go/issues/750)
* [Day 372](https://github.com/vaskoz/dailycodingproblem-go/issues/748)
* [Day 373](https://github.com/vaskoz/dailycodingproblem-go/issues/752)

57 changes: 57 additions & 0 deletions day373/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package day373

import "sort"

// LongestConsecutiveSequenceBrute runs in O(N log N)
// due to sorting the input.
func LongestConsecutiveSequenceBrute(nums []int) int {
sort.Ints(nums)

longest := 0
current := 1

for i := 1; i < len(nums); i++ {
if nums[i] == nums[i-1]+1 {
current++
} else {
if current > longest {
longest = current
}
current = 1
}
}

if current > longest {
longest = current
}

return longest
}

// LongestConsecutiveSequenceLinear runs in O(N).
func LongestConsecutiveSequenceLinear(nums []int) int {
set := make(map[int]struct{}, len(nums))

for _, num := range nums {
set[num] = struct{}{}
}

longest := 0

for _, num := range nums {
if _, found := set[num-1]; !found {
found = true
count := 0

for ; found; count++ {
_, found = set[num+count+1]
}

if count > longest {
longest = count
}
}
}

return longest
}
57 changes: 57 additions & 0 deletions day373/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package day373

import "testing"

// nolint
var testcases = []struct {
input []int
lcs int
}{
{[]int{5, 2, 99, 3, 4, 1, 100}, 5},
{[]int{100, 4, 200, 1, 3, 2}, 4},
{[]int{100, 4, 101, 103, 1, 102, 3, 104, 2, 105}, 6},
}

func TestLongestConsecutiveSequenceBrute(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
copied := append([]int{}, tc.input...)

if result := LongestConsecutiveSequenceBrute(copied); result != tc.lcs {
t.Errorf("Expected %v, got %v", tc.lcs, result)
}
}
}

func BenchmarkLongestConsecutiveSequenceBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
copied := append([]int{}, tc.input...)

LongestConsecutiveSequenceBrute(copied)
}
}
}

func TestLongestConsecutiveSequenceLinear(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
copied := append([]int{}, tc.input...)

if result := LongestConsecutiveSequenceLinear(copied); result != tc.lcs {
t.Errorf("Expected %v, got %v", tc.lcs, result)
}
}
}

func BenchmarkLongestConsecutiveSequenceLinear(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
copied := append([]int{}, tc.input...)

LongestConsecutiveSequenceLinear(copied)
}
}
}

0 comments on commit 2fc7372

Please sign in to comment.