cs: add «1493. Longest Subarray of 1's After Deleting One Element»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-18 19:54:44 +02:00
parent 0dbb8d49e9
commit e8d15a5bfa
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,20 @@
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;
}
}