Closed KevinACoder closed 5 years ago
//1004. Max Consecutive Ones III
func longestOnes(ones []int, k int) (ans int) {
var start, end, zeros int
for ; end < len(ones); end++ {
if ones[end] == 0 {zeros++}
//move 'start' util zeros are equal or less than k
for zeros > k {
//remove the head element, if it is zeros, reduce
// num of zero in sliding window
if ones[start] == 0 {zeros--}
start++
}
ans = Max(ans, end - start + 1)
}
return
}