Skip to content

Commit e8486d8

Browse files
committed
Time: 5 ms (8.25%) | Memory: 56.8 MB (43.46%) - LeetSync
1 parent 161bf5f commit e8486d8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int longestSubarray(int[] nums) {
3+
int l = 0;
4+
int r = 0;
5+
int result = 0;
6+
int ones = 0;
7+
int zeros = 0;
8+
9+
for(; r < nums.length; r++) {
10+
ones += nums[r] == 1?1:0;
11+
zeros += nums[r] == 0?1:0;
12+
13+
while(zeros > 1) {
14+
ones -= nums[l] == 1?1:0;
15+
zeros -= nums[l] == 0?1:0;
16+
l++;
17+
}
18+
19+
result = Math.max(result, r - l +1);
20+
21+
}
22+
23+
return result - 1;
24+
}
25+
}

0 commit comments

Comments
 (0)