27 lines
446 B
Go
27 lines
446 B
Go
// Package q1004 implements a solution for https://leetcode.com/problems/max-consecutive-ones-iii/
|
|
package q1004
|
|
|
|
func longestOnes(nums []int, k int) int {
|
|
flipped := 0
|
|
curLen, maxLen := 0, 0
|
|
tail := 0
|
|
|
|
for i := range nums {
|
|
curLen++
|
|
if nums[i] == 0 {
|
|
flipped++
|
|
|
|
for flipped > k {
|
|
if nums[tail] == 0 {
|
|
flipped--
|
|
}
|
|
tail++
|
|
curLen--
|
|
}
|
|
}
|
|
maxLen = max(maxLen, curLen)
|
|
}
|
|
return maxLen
|
|
}
|
|
|
|
var _ = longestOnes
|