Skip to content

Instantly share code, notes, and snippets.

@theCoderCat
Created April 22, 2020 14:56
Show Gist options
  • Save theCoderCat/d7b27cb313e14088a60dbdefee2e7f6f to your computer and use it in GitHub Desktop.
Save theCoderCat/d7b27cb313e14088a60dbdefee2e7f6f to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
fmt.Println(scan([]int{0, 1, 2, 1, 2, 1, 2, 1, 2, 1, 9}))
}
func scan(A []int) int {
n := len(A)
if n<=1 {
return n
}
pos := 0
maxLen := 0
for pos < n {
black := pos
white := pos+1
for white+2<n && A[black]==A[black+2] && A[white]==A[white+2] {
black+=2
white+=2
}
if black+2<n && A[black]==A[black+2] {
black+=2
}
end := max(black, white)
maxLen = max(maxLen, end-pos+1)
pos = end
}
return maxLen
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func check(e error) {
if e != nil {
panic(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment