Files
obsidian/АиСД/Задачи/LeetCode/Max Consecutive Ones III.md
T
roma-dxunvrs 0e6e583e6d
deploy / Pull and Restart (push) Successful in 17s
refactor: wiki links -> md links
2026-09-22 08:52:23 +03:00

23 lines
819 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Leetcode #1004 | #Medium | [Скользящее окно](../../Методы/Скользящее%20окно.md)
## Идея
Скользящее окно с счетчиком нулей, дополнение к [Max Consecutive Ones II](Max%20Consecutive%20Ones%20II.md), только теперь условие zeroCount>k а не >1
## Big-O
- Время ```O(N)```
- Память ```O(1)```
## Код
```Java
class Solution {
public int longestOnes(int[] nums, int k) {
int l = 0, zeroCount = 0, res = 0;
for (int r = 0; r < nums.length; r++) {
if (nums[r] == 0) zeroCount++;
while (zeroCount > k) {
if (nums[l] == 0) zeroCount--;
l++;
}
res = Math.max(res, r - l + 1);
}
return res;
}
}
```