Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b75cbbd

Browse files
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)

AltStyle によって変換されたページ (->オリジナル) /