From 66f8d8489267c94e960480a63ac21b03f0e4c13c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 12 Aug 2024 21:43:51 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB1004.=20Max=20Consecutive=20O?= =?UTF-8?q?nes=20III=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/max-consecutive-ones-iii.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 cs/max-consecutive-ones-iii.cs diff --git a/cs/max-consecutive-ones-iii.cs b/cs/max-consecutive-ones-iii.cs new file mode 100644 index 0000000..5effc2a --- /dev/null +++ b/cs/max-consecutive-ones-iii.cs @@ -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; + } +}