feat: longest consecutive sequence
This commit is contained in:
86
pkg/longest_consecutive_sequence/main.go
Normal file
86
pkg/longest_consecutive_sequence/main.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
32
pkg/longest_consecutive_sequence/main_test.go
Normal file
32
pkg/longest_consecutive_sequence/main_test.go
Normal file
@@ -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}))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user