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:
parent
0dbb8d49e9
commit
e8d15a5bfa
1 changed files with 20 additions and 0 deletions
20
cs/longest-subarray-of-1s-after-deleting-one-element.cs
Normal file
20
cs/longest-subarray-of-1s-after-deleting-one-element.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue