Files
obsidian/АиСД/Задачи/LeetCode/Max Consecutive Ones III.md
T
roma-dxunvrs f57762fd3b Init
2026-09-03 21:40:46 +03:00

23 lines
738 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 | [[Скользящее окно]]
## Идея
Скользящее окно с счетчиком нулей, дополнение к [[Max Consecutive Ones II]], только теперь условие 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;
}
}
```