Skip to content

Commit 9eb162c

Browse files
authored
Added tasks 2381, 2382, 2383.
1 parent f205d01 commit 9eb162c

File tree

10 files changed

+362
-0
lines changed

10 files changed

+362
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ implementation 'com.github.javadev:leetcode-in-java:1.12'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2383 |[Minimum Hours of Training to Win a Competition](src/main/java/g2301_2400/s2383_minimum_hours_of_training_to_win_a_competition/Solution.java)| Easy | Array, Greedy | 0 | 100.00
1852+
| 2382 |[Maximum Segment Sum After Removals](src/main/java/g2301_2400/s2382_maximum_segment_sum_after_removals/Solution.java)| Hard | Array, Prefix_Sum, Union_Find, Ordered_Set | 28 | 100.00
1853+
| 2381 |[Shifting Letters II](src/main/java/g2301_2400/s2381_shifting_letters_ii/Solution.java)| Medium | Array, String, Prefix_Sum | 10 | 75.00
18511854
| 2380 |[Time Needed to Rearrange a Binary String](src/main/java/g2301_2400/s2380_time_needed_to_rearrange_a_binary_string/Solution.java)| Medium | String, Dynamic_Programming, Simulation | 3 | 70.00
18521855
| 2379 |[Minimum Recolors to Get K Consecutive Black Blocks](src/main/java/g2301_2400/s2379_minimum_recolors_to_get_k_consecutive_black_blocks/Solution.java)| Easy | String, Sliding_Window | 1 | 80.00
18531856
| 2376 |[Count Special Integers](src/main/java/g2301_2400/s2376_count_special_integers/Solution.java)| Hard | Dynamic_Programming, Math | 0 | 100.00
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2301_2400.s2381_shifting_letters_ii;
2+
3+
// #Medium #Array #String #Prefix_Sum #2022_08_25_Time_10_ms_(75.00%)_Space_82.7_MB_(75.00%)
4+
5+
public class Solution {
6+
public String shiftingLetters(String s, int[][] shifts) {
7+
int[] diff = new int[s.length() + 1];
8+
int l;
9+
int r;
10+
for (int[] shift : shifts) {
11+
l = shift[0];
12+
r = shift[1] + 1;
13+
diff[l] += 26;
14+
diff[r] += 26;
15+
if (shift[2] == 0) {
16+
diff[l]--;
17+
diff[r]++;
18+
} else {
19+
diff[l]++;
20+
diff[r]--;
21+
}
22+
diff[l] %= 26;
23+
diff[r] %= 26;
24+
}
25+
StringBuilder sb = new StringBuilder();
26+
int current = 0;
27+
int val;
28+
for (int i = 0; i < s.length(); ++i) {
29+
current += diff[i];
30+
val = s.charAt(i) - 'a';
31+
val += current;
32+
val %= 26;
33+
sb.append((char) ('a' + val));
34+
}
35+
return sb.toString();
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2381\. Shifting Letters II
2+
3+
Medium
4+
5+
You are given a string `s` of lowercase English letters and a 2D integer array `shifts` where <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code>. For every `i`, **shift** the characters in `s` from the index <code>start<sub>i</sub></code> to the index <code>end<sub>i</sub></code> (**inclusive**) forward if <code>direction<sub>i</sub> = 1</code>, or shift the characters backward if <code>direction<sub>i</sub> = 0</code>.
6+
7+
Shifting a character **forward** means replacing it with the **next** letter in the alphabet (wrapping around so that `'z'` becomes `'a'`). Similarly, shifting a character **backward** means replacing it with the **previous** letter in the alphabet (wrapping around so that `'a'` becomes `'z'`).
8+
9+
Return _the final string after all such shifts to_ `s` _are applied_.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
14+
15+
**Output:** "ace"
16+
17+
**Explanation:** Firstly, shift the characters from index 0 to index 1 backward. Now s = "zac".
18+
19+
Secondly, shift the characters from index 1 to index 2 forward. Now s = "zbd".
20+
21+
Finally, shift the characters from index 0 to index 2 forward. Now s = "ace".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "dztz", shifts = [[0,0,0],[1,1,1]]
26+
27+
**Output:** "catz"
28+
29+
**Explanation:** Firstly, shift the characters from index 0 to index 0 backward. Now s = "cztz".
30+
31+
Finally, shift the characters from index 1 to index 1 forward. Now s = "catz".
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length, shifts.length <= 5 * 10<sup>4</sup></code>
36+
* `shifts[i].length == 3`
37+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> < s.length</code>
38+
* <code>0 <= direction<sub>i</sub> <= 1</code>
39+
* `s` consists of lowercase English letters.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g2301_2400.s2382_maximum_segment_sum_after_removals;
2+
3+
// #Hard #Array #Prefix_Sum #Union_Find #Ordered_Set
4+
// #2022_08_25_Time_28_ms_(100.00%)_Space_139.3_MB_(42.86%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private static class UF {
10+
int[] root;
11+
long[] sum;
12+
13+
public UF(int n) {
14+
this.root = new int[n];
15+
Arrays.fill(this.root, -1);
16+
this.sum = new long[n];
17+
}
18+
19+
public void insert(int x, int value) {
20+
if (root[x] != -1 || sum[x] != 0) {
21+
return;
22+
}
23+
this.root[x] = x;
24+
this.sum[x] = value;
25+
}
26+
27+
public int find(int x) {
28+
while (root[x] != x) {
29+
int fa = root[x];
30+
int ga = root[fa];
31+
root[x] = ga;
32+
x = fa;
33+
}
34+
return x;
35+
}
36+
37+
public void union(int x, int y) {
38+
int rx = find(x);
39+
int ry = find(y);
40+
if (x == y) {
41+
return;
42+
}
43+
root[rx] = ry;
44+
sum[ry] += sum[rx];
45+
}
46+
47+
public boolean has(int x) {
48+
return root[x] != -1 || sum[x] != 0;
49+
}
50+
}
51+
52+
public long[] maximumSegmentSum(int[] nums, int[] removeQueries) {
53+
int n = removeQueries.length;
54+
long[] ret = new long[n];
55+
long max = 0L;
56+
UF uf = new UF(n);
57+
for (int i = n - 1; i >= 0; i--) {
58+
int u = removeQueries[i];
59+
uf.insert(u, nums[u]);
60+
for (int v = u - 1; v <= u + 1; v += 2) {
61+
if (v >= 0 && v < n && uf.has(v)) {
62+
uf.union(v, u);
63+
}
64+
}
65+
ret[i] = max;
66+
int ru = uf.find(u);
67+
max = Math.max(max, uf.sum[ru]);
68+
}
69+
return ret;
70+
}
71+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2382\. Maximum Segment Sum After Removals
2+
3+
Hard
4+
5+
You are given two **0-indexed** integer arrays `nums` and `removeQueries`, both of length `n`. For the <code>i<sup>th</sup></code> query, the element in `nums` at the index `removeQueries[i]` is removed, splitting `nums` into different segments.
6+
7+
A **segment** is a contiguous sequence of **positive** integers in `nums`. A **segment sum** is the sum of every element in a segment.
8+
9+
Return _an integer array_ `answer`_, of length_ `n`_, where_ `answer[i]` _is the **maximum** segment sum after applying the_ <code>i<sup>th</sup></code> _removal._
10+
11+
**Note:** The same index will **not** be removed more than once.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]
16+
17+
**Output:** [14,7,2,2,0]
18+
19+
**Explanation:** Using 0 to indicate a removed element, the answer is as follows:
20+
21+
Query 1: Remove the 0th element, nums becomes [0,2,5,6,1] and the maximum segment sum is 14 for segment [2,5,6,1].
22+
23+
Query 2: Remove the 3rd element, nums becomes [0,2,5,0,1] and the maximum segment sum is 7 for segment [2,5].
24+
25+
Query 3: Remove the 2nd element, nums becomes [0,2,0,0,1] and the maximum segment sum is 2 for segment [2].
26+
27+
Query 4: Remove the 4th element, nums becomes [0,2,0,0,0] and the maximum segment sum is 2 for segment [2].
28+
29+
Query 5: Remove the 1st element, nums becomes [0,0,0,0,0] and the maximum segment sum is 0, since there are no segments.
30+
31+
Finally, we return [14,7,2,2,0].
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [3,2,11,1], removeQueries = [3,2,1,0]
36+
37+
**Output:** [16,5,3,0]
38+
39+
**Explanation:** Using 0 to indicate a removed element, the answer is as follows:
40+
41+
Query 1: Remove the 3rd element, nums becomes [3,2,11,0] and the maximum segment sum is 16 for segment [3,2,11].
42+
43+
Query 2: Remove the 2nd element, nums becomes [3,2,0,0] and the maximum segment sum is 5 for segment [3,2].
44+
45+
Query 3: Remove the 1st element, nums becomes [3,0,0,0] and the maximum segment sum is 3 for segment [3].
46+
47+
Query 4: Remove the 0th element, nums becomes [0,0,0,0] and the maximum segment sum is 0, since there are no segments.
48+
49+
Finally, we return [16,5,3,0].
50+
51+
**Constraints:**
52+
53+
* `n == nums.length == removeQueries.length`
54+
* <code>1 <= n <= 10<sup>5</sup></code>
55+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
56+
* `0 <= removeQueries[i] < n`
57+
* All the values of `removeQueries` are **unique**.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2301_2400.s2383_minimum_hours_of_training_to_win_a_competition;
2+
3+
// #Easy #Array #Greedy #2022_08_25_Time_0_ms_(100.00%)_Space_42.4_MB_(42.86%)
4+
5+
public class Solution {
6+
public int minNumberOfHours(
7+
int initialEnergy, int initialExperience, int[] energy, int[] experience) {
8+
int totalEnergy = 0;
9+
for (int e : energy) {
10+
totalEnergy += e;
11+
}
12+
int result = Math.max(0, totalEnergy - initialEnergy + 1);
13+
int currentExp = initialExperience;
14+
int exp;
15+
for (int i = 0; i < experience.length - 1; ++i) {
16+
exp = experience[i];
17+
if (currentExp <= exp) {
18+
result += exp - currentExp + 1;
19+
currentExp = exp + 1;
20+
}
21+
currentExp += exp;
22+
}
23+
int last = experience[experience.length - 1];
24+
if (currentExp <= last) {
25+
result += last - currentExp + 1;
26+
}
27+
return result;
28+
}
29+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2383\. Minimum Hours of Training to Win a Competition
2+
3+
Easy
4+
5+
You are entering a competition, and are given two **positive** integers `initialEnergy` and `initialExperience` denoting your initial energy and initial experience respectively.
6+
7+
You are also given two **0-indexed** integer arrays `energy` and `experience`, both of length `n`.
8+
9+
You will face `n` opponents **in order**. The energy and experience of the <code>i<sup>th</sup></code> opponent is denoted by `energy[i]` and `experience[i]` respectively. When you face an opponent, you need to have both **strictly** greater experience and energy to defeat them and move to the next opponent if available.
10+
11+
Defeating the <code>i<sup>th</sup></code> opponent **increases** your experience by `experience[i]`, but **decreases** your energy by `energy[i]`.
12+
13+
Before starting the competition, you can train for some number of hours. After each hour of training, you can **either** choose to increase your initial experience by one, or increase your initial energy by one.
14+
15+
Return _the **minimum** number of training hours required to defeat all_ `n` _opponents_.
16+
17+
**Example 1:**
18+
19+
**Input:** initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
20+
21+
**Output:** 8
22+
23+
**Explanation:** You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
24+
25+
You face the opponents in the following order:
26+
27+
- You have more energy and experience than the 0<sup>th</sup> opponent so you win.
28+
29+
Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.
30+
31+
- You have more energy and experience than the 1<sup>st</sup> opponent so you win.
32+
33+
Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.
34+
35+
- You have more energy and experience than the 2<sup>nd</sup> opponent so you win.
36+
37+
Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.
38+
39+
- You have more energy and experience than the 3<sup>rd</sup> opponent so you win.
40+
41+
Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.
42+
43+
You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
44+
45+
It can be proven that no smaller answer exists.
46+
47+
**Example 2:**
48+
49+
**Input:** initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
50+
51+
**Output:** 0
52+
53+
**Explanation:** You do not need any additional energy or experience to win the competition, so we return 0.
54+
55+
**Constraints:**
56+
57+
* `n == energy.length == experience.length`
58+
* `1 <= n <= 100`
59+
* `1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2381_shifting_letters_ii;
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 shiftingLetters() {
11+
assertThat(
12+
new Solution()
13+
.shiftingLetters("abc", new int[][] {{0, 1, 0}, {1, 2, 1}, {0, 2, 1}}),
14+
equalTo("ace"));
15+
}
16+
17+
@Test
18+
void shiftingLetters2() {
19+
assertThat(
20+
new Solution().shiftingLetters("dztz", new int[][] {{0, 0, 0}, {1, 1, 1}}),
21+
equalTo("catz"));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2382_maximum_segment_sum_after_removals;
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 maximumSegmentSum() {
11+
assertThat(
12+
new Solution()
13+
.maximumSegmentSum(new int[] {1, 2, 5, 6, 1}, new int[] {0, 3, 2, 4, 1}),
14+
equalTo(new long[] {14, 7, 2, 2, 0}));
15+
}
16+
17+
@Test
18+
void maximumSegmentSum2() {
19+
assertThat(
20+
new Solution().maximumSegmentSum(new int[] {3, 2, 11, 1}, new int[] {3, 2, 1, 0}),
21+
equalTo(new long[] {16, 5, 3, 0}));
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2301_2400.s2383_minimum_hours_of_training_to_win_a_competition;
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 minNumberOfHours() {
11+
assertThat(
12+
new Solution()
13+
.minNumberOfHours(5, 3, new int[] {1, 4, 3, 2}, new int[] {2, 6, 3, 1}),
14+
equalTo(8));
15+
}
16+
17+
@Test
18+
void minNumberOfHours2() {
19+
assertThat(new Solution().minNumberOfHours(2, 4, new int[] {1}, new int[] {3}), equalTo(0));
20+
}
21+
}

0 commit comments

Comments
 (0)