-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1493_longestSubarray.cpp
More file actions
65 lines (49 loc) · 1.93 KB
/
1493_longestSubarray.cpp
File metadata and controls
65 lines (49 loc) · 1.93 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public:
int longestSubarray(vector<int>& nums) {
// Initialize two pointers and variables to track window size and deleted zeros
int ptrL = 0, ptrR = 0, maxWindow = 0, maxDeleted = 0;
// Iterate through the array using the right pointer
for(int ptrR = 0; ptrR < nums.size(); ++ptrR){
// If the current element is 0, increment the count of deleted zeros
if(nums[ptrR] == 0){
maxDeleted++;
}
// If more than one zero is in the current window, move the left pointer to shrink the window
while(maxDeleted > 1){
if(nums[ptrL] == 0){
maxDeleted--; // Reduce the count of deleted zeros as we move past a zero
}
ptrL++; // Move the left pointer to the right
}
// Calculate the maximum window length (excluding one zero) by updating maxWindow
maxWindow = max(maxWindow, ptrR - ptrL);
}
// Return the maximum window length found
return maxWindow;
}
};
/*
Explanation with Picture:
Consider the array: [1, 1, 0, 1, 1, 0, 1, 1, 1]
We use two pointers, ptrL and ptrR, to represent the sliding window:
Initial state:
ptrL -> 0
ptrR -> 0
[1, 1, 0, 1, 1, 0, 1, 1, 1]
As ptrR moves to the right, it expands the window, and we keep track of zeros encountered:
Step 1:
ptrR moves to index 2 (nums[2] == 0)
Count of zeros (maxDeleted) = 1
Step 2:
ptrR moves to index 5 (nums[5] == 0)
Count of zeros (maxDeleted) = 2
Since maxDeleted > 1, ptrL will move right to reduce the window size:
ptrL -> 3
Updated state:
ptrL at index 3
ptrR at index 5
Continue expanding the window until ptrR reaches the end, while ensuring only one zero is deleted in the current window. The goal is to find the largest possible window length (excluding one zero).
Final result:
The longest subarray with at most one zero deleted is of length 5.
*/