1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

cs: add «1004. Max Consecutive Ones III»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-12 21:43:51 +02:00
parent 0d1683fb1d
commit 66f8d84892
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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;
}
}