File tree Expand file tree Collapse file tree 3 files changed +140
-27
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 3 files changed +140
-27
lines changed Original file line number Diff line number Diff line change @@ -787,7 +787,7 @@ _If you like this project, please leave me a star._ ★
787
787
|491|[ Increasing Subsequences] ( https://leetcode.com/problems/increasing-subsequences/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_491.java ) | |Medium| Backtracking, DFS
788
788
|490|[ The Maze] ( https://leetcode.com/problems/the-maze/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_490.java ) | |Medium| BFS
789
789
|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
791
791
|486|[ Predict the Winner] ( https://leetcode.com/problems/predict-the-winner/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_486.java ) | | Medium | DP
792
792
|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
793
793
|484|[ Find Permutation] ( https://leetcode.com/problems/find-permutation/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_484.java ) | |Medium | Array, String, Greedy
Original file line number Diff line number Diff line change 2
2
3
3
public class _487 {
4
4
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 ++;
12
29
}
13
- maxOnes = Math . max ( maxOnes , newOnes ) ;
30
+ return ans ;
14
31
}
15
- return maxOnes ;
16
32
}
17
-
18
33
}
You can’t perform that action at this time.
0 commit comments