Skip to content

Commit 623c431

Browse files
fix 487
1 parent ebe3226 commit 623c431

File tree

3 files changed

+140
-27
lines changed

3 files changed

+140
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ _If you like this project, please leave me a star._ ★
787787
|491|[Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_491.java) | |Medium| Backtracking, DFS
788788
|490|[The Maze](https://leetcode.com/problems/the-maze/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_490.java) | |Medium| BFS
789789
|488|[Zuma Game](https://leetcode.com/problems/zuma-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_488.java) | |Hard | DFS, Backtracking
790-
|487|[Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_487.java) | [:tv:](https://youtu.be/nKhteIRZ2Ok) |Medium| Array
790+
|487|[Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_487.java) | [:tv:](https://youtu.be/nKhteIRZ2Ok) |Medium| Array, Sliding Window
791791
|486|[Predict the Winner](https://leetcode.com/problems/predict-the-winner/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_486.java) | | Medium | DP
792792
|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)|[Java](../master/src/main/java/com/fishercoder/solutions/_485.java) [Javascript](../master/javascript/_485.js)| [:tv:](https://youtu.be/nKhteIRZ2Ok)|Easy| Array
793793
|484|[Find Permutation](https://leetcode.com/problems/find-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_484.java) | |Medium | Array, String, Greedy

src/main/java/com/fishercoder/solutions/_487.java

+25-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22

33
public class _487 {
44

5-
public static int findMaxConsecutiveOnes(int[] nums) {
6-
int maxOnes = 0;
7-
for (int i = 0; i < nums.length; i++) {
8-
int newOnes = 0;
9-
while (i < nums.length && nums[i] == 1) {
10-
newOnes++;
11-
i++;
5+
public static class Solution1 {
6+
/**
7+
* I implemented this on my own after a quick read from https://leetcode.com/problems/max-consecutive-ones-ii/solution/
8+
*/
9+
public static int findMaxConsecutiveOnes(int[] nums) {
10+
int left = 0;
11+
int right = 0;
12+
int zeroes = 0;
13+
int ans = 0;
14+
while (right < nums.length) {
15+
if (nums[right] == 0) {
16+
zeroes++;
17+
}
18+
if (zeroes <= 1) {
19+
ans = Math.max(ans, right - left + 1);
20+
} else {
21+
while (left < nums.length && zeroes > 1) {
22+
if (nums[left] == 0) {
23+
zeroes--;
24+
}
25+
left++;
26+
}
27+
}
28+
right++;
1229
}
13-
maxOnes = Math.max(maxOnes, newOnes);
30+
return ans;
1431
}
15-
return maxOnes;
1632
}
17-
1833
}

0 commit comments

Comments
 (0)