Skip to content

Commit b75cbbd

Browse files
authored
1004. Max Consecutive Ones III
1 parent 3ca0383 commit b75cbbd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1004. Max Consecutive Ones III.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
fun longestOnes(nums: IntArray, k: Int): Int {
3+
var maxNumConsecutiveOne = 0
4+
var windowStart = 0
5+
var zerosInWindow = 0
6+
for (windowEnd in nums.indices) {
7+
if (nums[windowEnd] == 0) {
8+
zerosInWindow++
9+
}
10+
while (zerosInWindow > k) {
11+
if (nums[windowStart] == 0) {
12+
zerosInWindow--
13+
}
14+
windowStart++
15+
}
16+
maxNumConsecutiveOne = maxOf(maxNumConsecutiveOne, windowEnd - windowStart + 1)
17+
}
18+
return maxNumConsecutiveOne
19+
}
20+
}

0 commit comments

Comments
 (0)