-
Notifications
You must be signed in to change notification settings - Fork 2
/
longestSubarray.ts
31 lines (25 loc) · 925 Bytes
/
longestSubarray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
type LongestSubarray = (nums: number[]) => number;
/**
* Accepted
*/
export const longestSubarray: LongestSubarray = (nums) => {
let left = 0;
let zeroCount = 0;
let maxLength = 0;
// Iterate through the array using the right pointer
for (let right = 0; right < nums.length; right++) {
// If the current element is 0, increment the zero count
if (nums[right] === 0) zeroCount += 1;
// If there are more than one zero in the current window, adjust the window
while (zeroCount > 1) {
// If the element at the left pointer is 0, decrement the zero count
if (nums[left] === 0) zeroCount -= 1;
// Move the left pointer to the right to reduce the window size
left += 1;
}
// Update the maximum length of the window containing at most one 0
maxLength = Math.max(maxLength, right - left);
}
// Return the maximum length of the subarray
return maxLength;
};