Skip to content

Commit d48c431

Browse files
authored
Added tasks 2024, 2025, 2027.
1 parent 795fa8f commit d48c431

File tree

9 files changed

+370
-0
lines changed

9 files changed

+370
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g2001_2100.s2024_maximize_the_confusion_of_an_exam;
2+
3+
// #Medium #String #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2022_05_25_Time_21_ms_(44.78%)_Space_48.4_MB_(29.41%)
5+
6+
public class Solution {
7+
public int maxConsecutiveAnswers(String answerKey, int k) {
8+
int max;
9+
int right = 0;
10+
int originalK = k;
11+
while (k > 0 && right < answerKey.length()) {
12+
if (answerKey.charAt(right) == 'T') {
13+
k--;
14+
}
15+
right++;
16+
}
17+
max = right;
18+
int left = 0;
19+
while (right < answerKey.length() && left < answerKey.length()) {
20+
if (answerKey.charAt(right) == 'F') {
21+
right++;
22+
max = Math.max(max, right - left);
23+
} else {
24+
while (left < right && answerKey.charAt(left) == 'F') {
25+
left++;
26+
}
27+
left++;
28+
right++;
29+
}
30+
}
31+
right = 0;
32+
k = originalK;
33+
while (k > 0 && right < answerKey.length()) {
34+
if (answerKey.charAt(right) == 'F') {
35+
k--;
36+
}
37+
right++;
38+
}
39+
max = Math.max(max, right);
40+
left = 0;
41+
while (right < answerKey.length() && left < answerKey.length()) {
42+
if (answerKey.charAt(right) == 'T') {
43+
right++;
44+
max = Math.max(max, right - left);
45+
} else {
46+
while (left < right && answerKey.charAt(left) == 'T') {
47+
left++;
48+
}
49+
left++;
50+
right++;
51+
}
52+
}
53+
return max;
54+
}
55+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2024\. Maximize the Confusion of an Exam
2+
3+
Medium
4+
5+
A teacher is writing a test with `n` true/false questions, with `'T'` denoting true and `'F'` denoting false. He wants to confuse the students by **maximizing** the number of **consecutive** questions with the **same** answer (multiple trues or multiple falses in a row).
6+
7+
You are given a string `answerKey`, where `answerKey[i]` is the original answer to the <code>i<sup>th</sup></code> question. In addition, you are given an integer `k`, the maximum number of times you may perform the following operation:
8+
9+
* Change the answer key for any question to `'T'` or `'F'` (i.e., set `answerKey[i]` to `'T'` or `'F'`).
10+
11+
Return _the **maximum** number of consecutive_ `'T'`s or `'F'`s _in the answer key after performing the operation at most_ `k` _times_.
12+
13+
**Example 1:**
14+
15+
**Input:** answerKey = "TTFF", k = 2
16+
17+
**Output:** 4
18+
19+
**Explanation:** We can replace both the 'F's with 'T's to make answerKey = "TTTT".
20+
21+
There are four consecutive 'T's.
22+
23+
**Example 2:**
24+
25+
**Input:** answerKey = "TFFT", k = 1
26+
27+
**Output:** 3
28+
29+
**Explanation:** We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
30+
31+
Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
32+
33+
In both cases, there are three consecutive 'F's.
34+
35+
**Example 3:**
36+
37+
**Input:** answerKey = "TTFTTFTT", k = 1
38+
39+
**Output:** 5
40+
41+
**Explanation:** We can replace the first 'F' to make answerKey = "TTTTTFTT"
42+
43+
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT".
44+
45+
In both cases, there are five consecutive 'T's.
46+
47+
**Constraints:**
48+
49+
* `n == answerKey.length`
50+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
51+
* `answerKey[i]` is either `'T'` or `'F'`
52+
* `1 <= k <= n`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g2001_2100.s2025_maximum_number_of_ways_to_partition_an_array;
2+
3+
// #Hard #Array #Hash_Table #Prefix_Sum #Counting #Enumeration
4+
// #2022_05_25_Time_172_ms_(100.00%)_Space_75.5_MB_(65.10%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class Solution {
12+
public int waysToPartition(int[] nums, int k) {
13+
int n = nums.length;
14+
long[] ps = new long[n];
15+
ps[0] = nums[0];
16+
for (int i = 1; i < n; i++) {
17+
ps[i] = ps[i - 1] + nums[i];
18+
}
19+
Map<Long, ArrayList<Integer>> partDiffs = new HashMap<>();
20+
int maxWays = 0;
21+
for (int i = 1; i < n; i++) {
22+
long partL = ps[i - 1];
23+
long partR = ps[n - 1] - partL;
24+
long partDiff = partR - partL;
25+
if (partDiff == 0) {
26+
maxWays++;
27+
}
28+
ArrayList<Integer> idxSet =
29+
partDiffs.computeIfAbsent(partDiff, k1 -> new ArrayList<>());
30+
idxSet.add(i);
31+
}
32+
for (int j = 0; j < n; j++) {
33+
int ways = 0;
34+
long newDiff = (long) k - nums[j];
35+
ArrayList<Integer> leftList = partDiffs.get(newDiff);
36+
if (leftList != null) {
37+
int i = upperBound(leftList, j);
38+
ways += leftList.size() - i;
39+
}
40+
ArrayList<Integer> rightList = partDiffs.get(-newDiff);
41+
if (rightList != null) {
42+
int i = upperBound(rightList, j);
43+
ways += i;
44+
}
45+
maxWays = Math.max(ways, maxWays);
46+
}
47+
return maxWays;
48+
}
49+
50+
public static int upperBound(List<Integer> arr, int val) {
51+
int ans = -1;
52+
int n = arr.size();
53+
int l = 0;
54+
int r = n;
55+
while (l <= r) {
56+
int mid = l + (r - l) / 2;
57+
if (mid == n) {
58+
return n;
59+
}
60+
if (arr.get(mid) > val) {
61+
ans = mid;
62+
r = mid - 1;
63+
} else {
64+
l = mid + 1;
65+
}
66+
}
67+
return ans;
68+
}
69+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2025\. Maximum Number of Ways to Partition an Array
2+
3+
Hard
4+
5+
You are given a **0-indexed** integer array `nums` of length `n`. The number of ways to **partition** `nums` is the number of `pivot` indices that satisfy both conditions:
6+
7+
* `1 <= pivot < n`
8+
* `nums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]`
9+
10+
You are also given an integer `k`. You can choose to change the value of **one** element of `nums` to `k`, or to leave the array **unchanged**.
11+
12+
Return _the **maximum** possible number of ways to **partition**_ `nums` _to satisfy both conditions after changing **at most** one element_.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,-1,2], k = 3
17+
18+
**Output:** 1
19+
20+
**Explanation:** One optimal approach is to change nums[0] to k. The array becomes [**3**,-1,2].
21+
22+
There is one way to partition the array:
23+
24+
- For pivot = 2, we have the partition [3,-1 | 2]: 3 + -1 == 2.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [0,0,0], k = 1
29+
30+
**Output:** 2
31+
32+
**Explanation:** The optimal approach is to leave the array unchanged.
33+
34+
There are two ways to partition the array:
35+
36+
- For pivot = 1, we have the partition [0 | 0,0]: 0 == 0 + 0.
37+
38+
- For pivot = 2, we have the partition [0,0 | 0]: 0 + 0 == 0.
39+
40+
**Example 3:**
41+
42+
**Input:** nums = [22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k = -33
43+
44+
**Output:** 4
45+
46+
**Explanation:** One optimal approach is to change nums[2] to k. The array becomes [22,4,**\-33**,-20,-15,15,-16,7,19,-10,0,-13,-14].
47+
48+
There are four ways to partition the array.
49+
50+
**Constraints:**
51+
52+
* `n == nums.length`
53+
* <code>2 <= n <= 10<sup>5</sup></code>
54+
* <code>-10<sup>5</sup> <= k, nums[i] <= 10<sup>5</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2001_2100.s2027_minimum_moves_to_convert_string;
2+
3+
// #Easy #String #Greedy #2022_05_25_Time_4_ms_(5.74%)_Space_42.1_MB_(37.50%)
4+
5+
public class Solution {
6+
public int minimumMoves(String s) {
7+
int moves = 0;
8+
int i = 0;
9+
while (i < s.length()) {
10+
if (s.charAt(i) == 'O') {
11+
i++;
12+
continue;
13+
}
14+
String candidate = i + 3 <= s.length() ? s.substring(i, i + 3) : s.substring(i);
15+
if (candidate.contains("X")) {
16+
moves++;
17+
i += 3;
18+
} else {
19+
i++;
20+
}
21+
}
22+
return moves;
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2027\. Minimum Moves to Convert String
2+
3+
Easy
4+
5+
You are given a string `s` consisting of `n` characters which are either `'X'` or `'O'`.
6+
7+
A **move** is defined as selecting **three** **consecutive characters** of `s` and converting them to `'O'`. Note that if a move is applied to the character `'O'`, it will stay the **same**.
8+
9+
Return _the **minimum** number of moves required so that all the characters of_ `s` _are converted to_ `'O'`.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "XXX"
14+
15+
**Output:** 1
16+
17+
**Explanation:** XXX -> OOO We select all the 3 characters and convert them in one move.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "XXOX"
22+
23+
**Output:** 2
24+
25+
**Explanation:** XXOX -> OOOX -> OOOO
26+
27+
We select the first 3 characters in the first move, and convert them to `'O'`.
28+
29+
Then we select the last 3 characters and convert them so that the final string contains all `'O'`s.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "OOOO"
34+
35+
**Output:** 0
36+
37+
**Explanation:** There are no `'X's` in `s` to convert.
38+
39+
**Constraints:**
40+
41+
* `3 <= s.length <= 1000`
42+
* `s[i]` is either `'X'` or `'O'`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2024_maximize_the_confusion_of_an_exam;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxConsecutiveAnswers() {
11+
assertThat(new Solution().maxConsecutiveAnswers("TTFF", 2), equalTo(4));
12+
}
13+
14+
@Test
15+
void maxConsecutiveAnswers2() {
16+
assertThat(new Solution().maxConsecutiveAnswers("TTFF", 1), equalTo(3));
17+
}
18+
19+
@Test
20+
void maxConsecutiveAnswers3() {
21+
assertThat(new Solution().maxConsecutiveAnswers("TTFTTFTT", 1), equalTo(5));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2001_2100.s2025_maximum_number_of_ways_to_partition_an_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void waysToPartition() {
11+
assertThat(new Solution().waysToPartition(new int[] {2, -1, 2}, 3), equalTo(1));
12+
}
13+
14+
@Test
15+
void waysToPartition2() {
16+
assertThat(new Solution().waysToPartition(new int[] {0, 0, 0}, 1), equalTo(2));
17+
}
18+
19+
@Test
20+
void waysToPartition3() {
21+
assertThat(
22+
new Solution()
23+
.waysToPartition(
24+
new int[] {22, 4, -25, -20, -15, 15, -16, 7, 19, -10, 0, -13, -14},
25+
-33),
26+
equalTo(4));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2027_minimum_moves_to_convert_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumMoves() {
11+
assertThat(new Solution().minimumMoves("XXX"), equalTo(1));
12+
}
13+
14+
@Test
15+
void minimumMoves2() {
16+
assertThat(new Solution().minimumMoves("XXOX"), equalTo(2));
17+
}
18+
19+
@Test
20+
void minimumMoves3() {
21+
assertThat(new Solution().minimumMoves("OOOO"), equalTo(0));
22+
}
23+
}

0 commit comments

Comments
 (0)