1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/cs/longest-subarray-of-1s-after-deleting-one-element.cs
2024-08-18 19:54:44 +02:00

20 lines
456 B
C#

public class Solution {
public int BitToInt(int num) => num == 0 ? 1 : 0;
public int LongestSubarray(int[] nums) {
var longest = 0;
var zeros = 0;
for (int l = 0, r = 0; r < nums.Length; ++r) {
zeros += BitToInt(nums[r]);
for (; zeros > 1; ++l) {
zeros -= BitToInt(nums[l]);
}
longest = Math.Max(longest, r - l);
}
return longest;
}
}