mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-22 17:02:21 +08:00
30 lines
447 B
Go
30 lines
447 B
Go
package leetcode
|
|
|
|
func longestOnes(A []int, K int) int {
|
|
res, left, right := 0, 0, 0
|
|
for left < len(A) {
|
|
if right < len(A) && ((A[right] == 0 && K > 0) || A[right] == 1) {
|
|
if A[right] == 0 {
|
|
K--
|
|
}
|
|
right++
|
|
} else {
|
|
if K == 0 || (right == len(A) && K > 0) {
|
|
res = max(res, right-left)
|
|
}
|
|
if A[left] == 0 {
|
|
K++
|
|
}
|
|
left++
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func max(a int, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|