-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from vaskoz/day373
Day373
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |