mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «1004. Max Consecutive Ones III»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
0d1683fb1d
commit
66f8d84892
1 changed files with 22 additions and 0 deletions
22
cs/max-consecutive-ones-iii.cs
Normal file
22
cs/max-consecutive-ones-iii.cs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
public class Solution {
|
||||||
|
public int LongestOnes(int[] nums, int k) {
|
||||||
|
int maxLength = 0;
|
||||||
|
|
||||||
|
int zeros = 0;
|
||||||
|
for (int i = 0, j = 0; j < nums.Length; ++j) {
|
||||||
|
if (nums[j] == 0) {
|
||||||
|
++zeros;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; zeros > k; ++i) {
|
||||||
|
if (nums[i] == 0) {
|
||||||
|
--zeros;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
maxLength = Math.Max(maxLength, j - i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxLength;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue