-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubarrayAfterDeletingOne.java
More file actions
36 lines (28 loc) · 1005 Bytes
/
Copy pathLongestSubarrayAfterDeletingOne.java
File metadata and controls
36 lines (28 loc) · 1005 Bytes
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
package LeetcodePractice;
public class LongestSubarrayAfterDeletingOne {
public int longestSubarray(int[] nums) {
int left = 0, right = 0;
int zeroCount = 0;
int maxLength = 0;
while (right < nums.length) {
if (nums[right] == 0) {
zeroCount++;
}
while (zeroCount > 1) {
if (nums[left] == 0) {
zeroCount--;
}
left++;
}
maxLength = Math.max(maxLength, right - left);
right++;
}
return maxLength;
}
public static void main(String[] args) {
LongestSubarrayAfterDeletingOne sol = new LongestSubarrayAfterDeletingOne();
System.out.println(sol.longestSubarray(new int[]{1, 1, 0, 1}));
System.out.println(sol.longestSubarray(new int[]{0, 1, 1, 1, 0, 1, 1, 0, 1}));
System.out.println(sol.longestSubarray(new int[]{1, 1, 1}));
}
}