Created
April 22, 2020 14:56
-
-
Save theCoderCat/d7b27cb313e14088a60dbdefee2e7f6f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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