-
Notifications
You must be signed in to change notification settings - Fork 15
/
countlongestsequence.go
57 lines (50 loc) · 986 Bytes
/
countlongestsequence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Given an unsorted array, count the longest occuring sequence in the array. In the example below, the count is 4 => 1,2,3,4
This algorithm builds a graph then counts the longest branch, it runs in O(N) time
*/
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(buildGraph([]int{100, 4, 200, 1, 3, 2}))
}
func buildGraph(values []int) int {
m := make(map[int]*Node)
for _, v := range values {
m[v] = &Node{value: v}
}
g := Graph{}
for k := range m {
if node, ok := m[k+1]; ok {
m[k].child = node
}
if _, ok := m[k-1]; !ok {
g.parents = append(g.parents, m[k])
}
}
return g.findLongestSequence()
}
type Graph struct {
parents []*Node
}
type Node struct {
value int
child *Node
}
func (g *Graph) findLongestSequence() int {
max := math.MinInt64
for _, p := range g.parents {
counter := 1
parent := p
for parent.child != nil {
counter++
parent = parent.child
}
if counter > max {
max = counter
}
}
return max
}