-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_1493_LongestSubarrayof1'sAfterDeletingOneElement
More file actions
51 lines (45 loc) · 1.37 KB
/
LC_1493_LongestSubarrayof1'sAfterDeletingOneElement
File metadata and controls
51 lines (45 loc) · 1.37 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
class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
int maxCount = 0;
int zeroes = 0;
for(int i = 0; i < n; i++){
if(nums[i] == 0){
zeroes++;
int leftcount = 0;
int rightcount = 0;
int temp = i-1;
while(temp >= 0 && nums[temp] == 1){
leftcount++;
temp--;
}
temp = i+1;
while(temp < n && nums[temp] == 1){
rightcount++;
temp++;
}
maxCount = Math.max(maxCount, leftcount+rightcount);
}
}
// System.out.println(maxCount);
return (maxCount == 0)? ((zeroes == n)? 0 : n-1) : maxCount;
}
}
--------------------------------------------------------------------------------------------------
class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
int maxCount = 0;
int zeroes = 0;
int st = 0;
for(int i = 0; i < n; i++){
zeroes += (nums[i] == 0)? 1 : 0;
while(zeroes > 1){
zeroes -= (nums[st] == 0)? 1 : 0;
st++;
}
maxCount = Math.max(maxCount, i-st);
}
return maxCount;
}
}