From 20629eab228225748504b49a197d5f17dbfdfaa6 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 7 Jan 2026 18:54:16 -0500 Subject: [PATCH] feat: longest consecutive sequence --- pkg/longest_consecutive_sequence/main.go | 86 +++++++++++++++++++ pkg/longest_consecutive_sequence/main_test.go | 32 +++++++ 2 files changed, 118 insertions(+) create mode 100644 pkg/longest_consecutive_sequence/main.go create mode 100644 pkg/longest_consecutive_sequence/main_test.go diff --git a/pkg/longest_consecutive_sequence/main.go b/pkg/longest_consecutive_sequence/main.go new file mode 100644 index 0000000..22cc4df --- /dev/null +++ b/pkg/longest_consecutive_sequence/main.go @@ -0,0 +1,86 @@ +package longest_consecutive_sequence + +// type Node struct { +// // The location of the parent node. +// Parent *Node + +// // The number of descendent nodes, including itself. +// Children int +// } + +// func FindAncestor(n *Node) *Node { +// ancestor := n + +// for ancestor.Parent != nil { +// ancestor = ancestor.Parent +// } + +// return ancestor +// } + +// func LongestConsecutive(nums []int) int { +// nodes := map[int]*Node{} + +// for _, num := range nums { +// if _, exists := nodes[num]; exists { +// continue +// } + +// node := &Node{Parent: nil, Children: 1} + +// if lower, lower_exists := nodes[num-1]; lower_exists { +// ancestor := FindAncestor(lower) +// ancestor.Parent = node +// node.Children += ancestor.Children +// } + +// if upper, lower_exists := nodes[num+1]; lower_exists { +// ancestor := FindAncestor(upper) +// ancestor.Parent = node +// node.Children += ancestor.Children +// } + +// nodes[num] = node +// } + +// maximum := 0 + +// for _, node := range nodes { +// maximum = max(maximum, node.Children) +// } + +// return maximum +// } + +func LongestConsecutive(nums []int) int { + items := map[int]bool{} + + for _, num := range nums { + items[num] = true + } + + maximum := 0 + + for item, unused := range items { + if !unused { + continue + } + + items[item] = false + sequence := 1 + + for i := item - 1; items[i]; i-- { + items[i] = false + sequence++ + } + + for i := item + 1; items[i]; i++ { + items[i] = false + sequence++ + } + + maximum = max(maximum, sequence) + } + + return maximum +} diff --git a/pkg/longest_consecutive_sequence/main_test.go b/pkg/longest_consecutive_sequence/main_test.go new file mode 100644 index 0000000..99c5d91 --- /dev/null +++ b/pkg/longest_consecutive_sequence/main_test.go @@ -0,0 +1,32 @@ +package longest_consecutive_sequence_test + +import ( + "testing" + + "git.maximhutz.com/practice/pkg/longest_consecutive_sequence" + "github.com/stretchr/testify/assert" +) + +func Test1(t *testing.T) { + assert.Equal(t, 4, longest_consecutive_sequence.LongestConsecutive([]int{100, 4, 200, 1, 3, 2})) +} + +func Test2(t *testing.T) { + assert.Equal(t, 9, longest_consecutive_sequence.LongestConsecutive([]int{0, 3, 7, 2, 5, 8, 4, 6, 0, 1})) +} + +func Test3(t *testing.T) { + assert.Equal(t, 3, longest_consecutive_sequence.LongestConsecutive([]int{1, 0, 1, 2})) +} + +func Test4(t *testing.T) { + assert.Equal(t, 0, longest_consecutive_sequence.LongestConsecutive([]int{})) +} + +func Test5(t *testing.T) { + assert.Equal(t, 7, longest_consecutive_sequence.LongestConsecutive([]int{1, 3, 5, 7, 2, 6, 4})) +} + +func Test6(t *testing.T) { + assert.Equal(t, 7, longest_consecutive_sequence.LongestConsecutive([]int{1, 3, 5, 7, 2, 6, 4})) +}