Skip to content

Commit 53cdf2d

Browse files
add 2024
1 parent 37d4e34 commit 53cdf2d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _If you like this project, please leave me a star._ ★
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|2028|[Find Missing Observations](https://leetcode.com/problems/find-missing-observations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2028.java) ||Medium||
1212
|2027|[Minimum Moves to Convert String](https://leetcode.com/problems/minimum-moves-to-convert-string/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2027.java) ||Easy||
13+
|2024|[Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2024.java) ||Medium||
1314
|2023|[Number of Pairs of Strings With Concatenation Equal to Target](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2023.java) ||Medium||
1415
|2022|[Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2022.java) ||Easy||
1516
|2018|[Check if Word Can Be Placed In Crossword](https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2018.java) ||Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2024 {
4+
public static class Solution1 {
5+
public int maxConsecutiveAnswers(String answerKey, int k) {
6+
int max;
7+
//change T to F and count number of Fs
8+
int right = 0;
9+
int originalK = k;
10+
while (k > 0 && right < answerKey.length()) {
11+
if (answerKey.charAt(right) == 'T') {
12+
k--;
13+
}
14+
right++;
15+
}
16+
max = right;
17+
int left = 0;
18+
while (right < answerKey.length() && left < answerKey.length()) {
19+
if (answerKey.charAt(right) == 'F') {
20+
right++;
21+
max = Math.max(max, right - left);
22+
} else {
23+
while (left < right && answerKey.charAt(left) == 'F') {
24+
left++;
25+
}
26+
left++;
27+
right++;
28+
}
29+
}
30+
31+
//change F to T
32+
right = 0;
33+
k = originalK;
34+
while (k > 0 && right < answerKey.length()) {
35+
if (answerKey.charAt(right) == 'F') {
36+
k--;
37+
}
38+
right++;
39+
}
40+
max = Math.max(max, right);
41+
left = 0;
42+
while (right < answerKey.length() && left < answerKey.length()) {
43+
if (answerKey.charAt(right) == 'T') {
44+
right++;
45+
max = Math.max(max, right - left);
46+
} else {
47+
while (left < right && answerKey.charAt(left) == 'T') {
48+
left++;
49+
}
50+
left++;
51+
right++;
52+
}
53+
}
54+
return max;
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2024;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2024Test {
10+
private static _2024.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2024.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.maxConsecutiveAnswers("TTFF", 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.maxConsecutiveAnswers("TFFT", 1));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(5, solution1.maxConsecutiveAnswers("TTFTTFTT", 1));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(1, solution1.maxConsecutiveAnswers("F", 1));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(8, solution1.maxConsecutiveAnswers("FFFTTFTTFT", 3));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)