Skip to content

Commit a48fd45

Browse files
refactor 1004
1 parent 0c767f0 commit a48fd45

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ _If you like this project, please leave me a star._ ★
211211
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | |Easy|
212212
|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1008.java) | |Medium| Recursion
213213
|1005|[Maximize Sum Of Array After K Negations](https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1005.java) | [:tv:](https://youtu.be/spiwTAuz1_4) |Easy|
214+
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1004.java) | |Medium| Two Pointers, Sliding Window
214215
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | |Medium|
215216
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | |Easy|
216217
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | |Easy|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1004 {
4+
public static class Solution1 {
5+
public int longestOnes(int[] A, int k) {
6+
int result = 0;
7+
int i = 0;
8+
for (int j = 0; j < A.length; j++) {
9+
if (A[j] == 0) {
10+
k--;
11+
}
12+
while (k < 0) {
13+
if (A[i] == 0) {
14+
k++;
15+
}
16+
i++;
17+
}
18+
result = Math.max(result, j - i + 1);
19+
}
20+
return result;
21+
}
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1004;
4+
import com.fishercoder.solutions._1128;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.TestCase.assertEquals;
9+
10+
public class _1004Test {
11+
private static _1004.Solution1 solution1;
12+
private static int[] A;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1004.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
A = new int[]{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
22+
assertEquals(6, solution1.longestOnes(A, 2));
23+
}
24+
}

0 commit comments

Comments
 (0)