Skip to content

Commit 203b8ca

Browse files
authored
Added tasks 2379, 2380.
1 parent 1744708 commit 203b8ca

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

README.md

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

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 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
1852+
| 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
18511853
| 2376 |[Count Special Integers](src/main/java/g2301_2400/s2376_count_special_integers/Solution.java)| Hard | Dynamic_Programming, Math | 0 | 100.00
18521854
| 2375 |[Construct Smallest Number From DI String](src/main/java/g2301_2400/s2375_construct_smallest_number_from_di_string/Solution.java)| Medium | String, Greedy, Stack, Backtracking | 0 | 100.00
18531855
| 2374 |[Node With Highest Edge Score](src/main/java/g2301_2400/s2374_node_with_highest_edge_score/Solution.java)| Medium | Hash_Table, Graph | 4 | 97.68
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2301_2400.s2379_minimum_recolors_to_get_k_consecutive_black_blocks;
2+
3+
// #Easy #String #Sliding_Window #2022_08_23_Time_1_ms_(80.00%)_Space_42.2_MB_(20.00%)
4+
5+
public class Solution {
6+
public int minimumRecolors(String blocks, int k) {
7+
int n = blocks.length();
8+
int ans;
9+
int i;
10+
int cur = 0;
11+
for (i = 0; i < k; i++) {
12+
if (blocks.charAt(i) == 'W') {
13+
cur++;
14+
}
15+
}
16+
ans = cur;
17+
for (i = k; i < n; i++) {
18+
if (blocks.charAt(i) == 'W') {
19+
cur++;
20+
}
21+
if (blocks.charAt(i - k) == 'W') {
22+
cur--;
23+
}
24+
ans = Math.min(ans, cur);
25+
}
26+
return ans;
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2379\. Minimum Recolors to Get K Consecutive Black Blocks
2+
3+
Easy
4+
5+
You are given a **0-indexed** string `blocks` of length `n`, where `blocks[i]` is either `'W'` or `'B'`, representing the color of the <code>i<sup>th</sup></code> block. The characters `'W'` and `'B'` denote the colors white and black, respectively.
6+
7+
You are also given an integer `k`, which is the desired number of **consecutive** black blocks.
8+
9+
In one operation, you can **recolor** a white block such that it becomes a black block.
10+
11+
Return _the **minimum** number of operations needed such that there is at least **one** occurrence of_ `k` _consecutive black blocks._
12+
13+
**Example 1:**
14+
15+
**Input:** blocks = "WBBWWBBWBW", k = 7
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks so that blocks = "BBBBBBBWBW".
22+
23+
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
24+
25+
Therefore, we return 3.
26+
27+
**Example 2:**
28+
29+
**Input:** blocks = "WBWBBBW", k = 2
30+
31+
**Output:** 0
32+
33+
**Explanation:**
34+
35+
No changes need to be made, since 2 consecutive black blocks already exist.
36+
37+
Therefore, we return 0.
38+
39+
**Constraints:**
40+
41+
* `n == blocks.length`
42+
* `1 <= n <= 100`
43+
* `blocks[i]` is either `'W'` or `'B'`.
44+
* `1 <= k <= n`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g2301_2400.s2380_time_needed_to_rearrange_a_binary_string;
2+
3+
// #Medium #String #Dynamic_Programming #Simulation
4+
// #2022_08_23_Time_3_ms_(70.00%)_Space_41.8_MB_(70.00%)
5+
6+
public class Solution {
7+
public int secondsToRemoveOccurrences(String s) {
8+
int lastOne = -1;
9+
int result = 0;
10+
int prevResult = 0;
11+
int curResult = 0;
12+
int countOne = 0;
13+
int countZero = 0;
14+
int diff;
15+
int pTarget;
16+
int pWait;
17+
int cTarget;
18+
for (int i = 0; i < s.length(); ++i) {
19+
if (s.charAt(i) == '0') {
20+
++countZero;
21+
continue;
22+
}
23+
++countOne;
24+
diff = i - lastOne - 1;
25+
prevResult = curResult;
26+
cTarget = countOne - 1;
27+
pTarget = cTarget - 1;
28+
pWait = prevResult - (lastOne - pTarget);
29+
if (diff > pWait) {
30+
curResult = countZero;
31+
} else {
32+
curResult = countZero == 0 ? 0 : pWait - diff + 1 + countZero;
33+
}
34+
result = curResult;
35+
lastOne = i;
36+
}
37+
return result;
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2380\. Time Needed to Rearrange a Binary String
2+
3+
Medium
4+
5+
You are given a binary string `s`. In one second, **all** occurrences of `"01"` are **simultaneously** replaced with `"10"`. This process **repeats** until no occurrences of `"01"` exist.
6+
7+
Return _the number of seconds needed to complete this process._
8+
9+
**Example 1:**
10+
11+
**Input:** s = "0110101"
12+
13+
**Output:** 4
14+
15+
**Explanation:**
16+
17+
After one second, s becomes "1011010".
18+
19+
After another second, s becomes "1101100".
20+
21+
After the third second, s becomes "1110100".
22+
23+
After the fourth second, s becomes "1111000".
24+
25+
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete, so we return 4.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "11100"
30+
31+
**Output:** 0
32+
33+
**Explanation:** No occurrence of "01" exists in s, and the processes needed 0 seconds to complete, so we return 0.
34+
35+
**Constraints:**
36+
37+
* `1 <= s.length <= 1000`
38+
* `s[i]` is either `'0'` or `'1'`.
39+
40+
**Follow up:**
41+
42+
Can you solve this problem in O(n) time complexity?
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2379_minimum_recolors_to_get_k_consecutive_black_blocks;
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 minimumRecolors() {
11+
assertThat(new Solution().minimumRecolors("WBBWWBBWBW", 7), equalTo(3));
12+
}
13+
14+
@Test
15+
void minimumRecolors2() {
16+
assertThat(new Solution().minimumRecolors("WBWBBBW", 2), equalTo(0));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2380_time_needed_to_rearrange_a_binary_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 secondsToRemoveOccurrences() {
11+
assertThat(new Solution().secondsToRemoveOccurrences("0110101"), equalTo(4));
12+
}
13+
14+
@Test
15+
void secondsToRemoveOccurrences2() {
16+
assertThat(new Solution().secondsToRemoveOccurrences("11100"), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)