Skip to content

Commit 1ab0ff1

Browse files
authored
Update Max Consecutive Ones.java
1 parent 4eb2a5e commit 1ab0ff1

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

Easy/Max Consecutive Ones.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
class Solution {
2-
public int findMaxConsecutiveOnes(int[] nums) {
3-
int i = 0;
4-
int maxOnes = Integer.MIN_VALUE;
5-
int temp = 0;
6-
while (i < nums.length) {
7-
if (nums[i] == 1) {
8-
temp++;
9-
}
10-
else {
11-
maxOnes = Math.max(maxOnes, temp);
12-
temp = 0;
13-
}
14-
15-
i++;
2+
public int findMaxConsecutiveOnes(int[] nums) {
3+
int maxCount = 0;
4+
for (int i = 0; i < nums.length; ) {
5+
if (nums[i] == 1) {
6+
int currIdx = i;
7+
while (i < nums.length && nums[i] == 1) {
8+
i++;
169
}
17-
18-
return Math.max(maxOnes, temp);
10+
maxCount = Math.max(maxCount, i - currIdx);
11+
}
12+
i++;
1913
}
14+
return maxCount;
15+
}
2016
}

0 commit comments

Comments
 (0)