From 9a0d13d73412277faedf19a086294e8bb17e53ae Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 9 Jul 2024 19:00:12 +0300 Subject: [PATCH 1/4] Added tasks 3206-3213 --- .../s3206_alternating_groups_i/Solution.java | 22 +++++++ .../s3206_alternating_groups_i/readme.md | 43 +++++++++++++ .../Solution.java | 23 +++++++ .../readme.md | 52 ++++++++++++++++ .../s3208_alternating_groups_ii/Solution.java | 38 ++++++++++++ .../s3208_alternating_groups_ii/readme.md | 58 +++++++++++++++++ .../Solution.java | 26 ++++++++ .../readme.md | 40 ++++++++++++ .../Solution.java | 13 ++++ .../s3210_find_the_encrypted_string/readme.md | 38 ++++++++++++ .../Solution.java | 37 +++++++++++ .../readme.md | 33 ++++++++++ .../Solution.java | 24 +++++++ .../readme.md | 44 +++++++++++++ .../Solution.java | 62 +++++++++++++++++++ .../readme.md | 48 ++++++++++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 26 ++++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 21 +++++++ .../SolutionTest.java | 29 +++++++++ .../SolutionTest.java | 28 +++++++++ 24 files changed, 777 insertions(+) create mode 100644 src/main/java/g3201_3300/s3206_alternating_groups_i/Solution.java create mode 100644 src/main/java/g3201_3300/s3206_alternating_groups_i/readme.md create mode 100644 src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java create mode 100644 src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/readme.md create mode 100644 src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java create mode 100644 src/main/java/g3201_3300/s3208_alternating_groups_ii/readme.md create mode 100644 src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/Solution.java create mode 100644 src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/readme.md create mode 100644 src/main/java/g3201_3300/s3210_find_the_encrypted_string/Solution.java create mode 100644 src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md create mode 100644 src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/Solution.java create mode 100644 src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/readme.md create mode 100644 src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java create mode 100644 src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/readme.md create mode 100644 src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/Solution.java create mode 100644 src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/readme.md create mode 100644 src/test/java/g3201_3300/s3206_alternating_groups_i/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3207_maximum_points_after_enemy_battles/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3208_alternating_groups_ii/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3210_find_the_encrypted_string/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/SolutionTest.java create mode 100644 src/test/java/g3201_3300/s3213_construct_string_with_minimum_cost/SolutionTest.java diff --git a/src/main/java/g3201_3300/s3206_alternating_groups_i/Solution.java b/src/main/java/g3201_3300/s3206_alternating_groups_i/Solution.java new file mode 100644 index 000000000..a76eb3404 --- /dev/null +++ b/src/main/java/g3201_3300/s3206_alternating_groups_i/Solution.java @@ -0,0 +1,22 @@ +package g3201_3300.s3206_alternating_groups_i; + +// #Easy #Array #Sliding_Window #2024_07_09_Time_1_ms_(97.24%)_Space_42.8_MB_(90.31%) + +public class Solution { + public int numberOfAlternatingGroups(int[] colors) { + int n = colors.length; + int count = 0; + if (colors[n - 1] != colors[0] && colors[0] != colors[1]) { + count++; + } + if (colors[n - 1] != colors[0] && colors[n - 1] != colors[n - 2]) { + count++; + } + for (int i = 1; i < n - 1; i++) { + if (colors[i] != colors[i - 1] && colors[i] != colors[i + 1]) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g3201_3300/s3206_alternating_groups_i/readme.md b/src/main/java/g3201_3300/s3206_alternating_groups_i/readme.md new file mode 100644 index 000000000..03dec61d4 --- /dev/null +++ b/src/main/java/g3201_3300/s3206_alternating_groups_i/readme.md @@ -0,0 +1,43 @@ +3206\. Alternating Groups I + +Easy + +There is a circle of red and blue tiles. You are given an array of integers `colors`. The color of tile `i` is represented by `colors[i]`: + +* `colors[i] == 0` means that tile `i` is **red**. +* `colors[i] == 1` means that tile `i` is **blue**. + +Every 3 contiguous tiles in the circle with **alternating** colors (the middle tile has a different color from its **left** and **right** tiles) is called an **alternating** group. + +Return the number of **alternating** groups. + +**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. + +**Example 1:** + +**Input:** colors = [1,1,1] + +**Output:** 0 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-53-171.png) + +**Example 2:** + +**Input:** colors = [0,1,0,0,1] + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-47-491.png) + +Alternating groups: + +**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-50-441.png)**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-48-211.png)**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-49-351.png)** + +**Constraints:** + +* `3 <= colors.length <= 100` +* `0 <= colors[i] <= 1` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java new file mode 100644 index 000000000..5f685f0ac --- /dev/null +++ b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java @@ -0,0 +1,23 @@ +package g3201_3300.s3207_maximum_points_after_enemy_battles; + +// #Medium #Array #Greedy #2024_07_09_Time_1_ms_(100.00%)_Space_55.5_MB_(99.34%) + +public class Solution { + public long maximumPoints(int[] enemyEnergies, int currentEnergy) { + int n = enemyEnergies.length; + int min = enemyEnergies[0]; + for (int i = 1; i < n; i++) { + min = Math.min(min, enemyEnergies[i]); + } + if (currentEnergy == 0 || currentEnergy < min) { + return 0; + } + long points = 0; + long sum = (long) currentEnergy; + for (int i = n - 1; i >= 0; i--) { + sum += enemyEnergies[i]; + } + sum -= min; + return sum / min; + } +} diff --git a/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/readme.md b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/readme.md new file mode 100644 index 000000000..694783a8a --- /dev/null +++ b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/readme.md @@ -0,0 +1,52 @@ +3207\. Maximum Points After Enemy Battles + +Medium + +You are given an integer array `enemyEnergies` denoting the energy values of various enemies. + +You are also given an integer `currentEnergy` denoting the amount of energy you have initially. + +You start with 0 points, and all the enemies are unmarked initially. + +You can perform **either** of the following operations **zero** or multiple times to gain points: + +* Choose an **unmarked** enemy, `i`, such that `currentEnergy >= enemyEnergies[i]`. By choosing this option: + * You gain 1 point. + * Your energy is reduced by the enemy's energy, i.e. `currentEnergy = currentEnergy - enemyEnergies[i]`. +* If you have **at least** 1 point, you can choose an **unmarked** enemy, `i`. By choosing this option: + * Your energy increases by the enemy's energy, i.e. `currentEnergy = currentEnergy + enemyEnergies[i]`. + * The enemy `i` is **marked**. + +Return an integer denoting the **maximum** points you can get in the end by optimally performing operations. + +**Example 1:** + +**Input:** enemyEnergies = [3,2,2], currentEnergy = 2 + +**Output:** 3 + +**Explanation:** + +The following operations can be performed to get 3 points, which is the maximum: + +* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 1`, and `currentEnergy = 0`. +* Second operation on enemy 0: `currentEnergy` increases by 3, and enemy 0 is marked. So, `points = 1`, `currentEnergy = 3`, and marked enemies = `[0]`. +* First operation on enemy 2: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 2`, `currentEnergy = 1`, and marked enemies = `[0]`. +* Second operation on enemy 2: `currentEnergy` increases by 2, and enemy 2 is marked. So, `points = 2`, `currentEnergy = 3`, and marked enemies = `[0, 2]`. +* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 3`, `currentEnergy = 1`, and marked enemies = `[0, 2]`. + +**Example 2:** + +**Input:** enemyEnergies = [2], currentEnergy = 10 + +**Output:** 5 + +**Explanation:** + +Performing the first operation 5 times on enemy 0 results in the maximum number of points. + +**Constraints:** + +* 1 <= enemyEnergies.length <= 105 +* 1 <= enemyEnergies[i] <= 109 +* 0 <= currentEnergy <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java b/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java new file mode 100644 index 000000000..3d735bdf8 --- /dev/null +++ b/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java @@ -0,0 +1,38 @@ +package g3201_3300.s3208_alternating_groups_ii; + +// #Medium #Array #Sliding_Window #2024_07_09_Time_2_ms_(99.02%)_Space_63.3_MB_(22.96%) + +public class Solution { + public int numberOfAlternatingGroups(int[] colors, int k) { + int i = 0, len = 0, total = 0; + while (i < colors.length - 1) { + int j = i + 1; + if (colors[j] != colors[i]) { + len = 2; + j++; + while (j < colors.length && colors[j] != colors[j - 1]) { + j++; + len++; + } + if (j == colors.length) break; + total += Math.max(0, (len - k + 1)); + } + i = j; + len = 0; + } + if (colors[0] != colors[colors.length - 1]) { + // if(len == colors.length) { + // return Math.max(0, colors.length); + // } + len = len == 0 ? 2 : len + 1; + int j = 1; + while (j < colors.length && colors[j] != colors[j - 1]) { + j++; + len++; + } + if (j >= k) len -= (j - k + 1); + } + total += Math.max(0, (len - k + 1)); + return total; + } +} diff --git a/src/main/java/g3201_3300/s3208_alternating_groups_ii/readme.md b/src/main/java/g3201_3300/s3208_alternating_groups_ii/readme.md new file mode 100644 index 000000000..73b6d5591 --- /dev/null +++ b/src/main/java/g3201_3300/s3208_alternating_groups_ii/readme.md @@ -0,0 +1,58 @@ +3208\. Alternating Groups II + +Medium + +There is a circle of red and blue tiles. You are given an array of integers `colors` and an integer `k`. The color of tile `i` is represented by `colors[i]`: + +* `colors[i] == 0` means that tile `i` is **red**. +* `colors[i] == 1` means that tile `i` is **blue**. + +An **alternating** group is every `k` contiguous tiles in the circle with **alternating** colors (each tile in the group except the first and last one has a different color from its **left** and **right** tiles). + +Return the number of **alternating** groups. + +**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. + +**Example 1:** + +**Input:** colors = [0,1,0,1,0], k = 3 + +**Output:** 3 + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png)** + +Alternating groups: + +![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png)![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png)![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png) + +**Example 2:** + +**Input:** colors = [0,1,0,0,1,0,1], k = 6 + +**Output:** 2 + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png)** + +Alternating groups: + +![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png)![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png) + +**Example 3:** + +**Input:** colors = [1,1,0,1], k = 4 + +**Output:** 0 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png) + +**Constraints:** + +* 3 <= colors.length <= 105 +* `0 <= colors[i] <= 1` +* `3 <= k <= colors.length` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/Solution.java b/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/Solution.java new file mode 100644 index 000000000..278140a0d --- /dev/null +++ b/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/Solution.java @@ -0,0 +1,26 @@ +package g3201_3300.s3209_number_of_subarrays_with_and_value_of_k; + +// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree +// #2024_07_09_Time_7_ms_(100.00%)_Space_62.9_MB_(11.74%) + +public class Solution { + public long countSubarrays(int[] nums, int k) { + long ans = 0; + int left = 0; + int right = 0; + for (int i = 0; i < nums.length; i++) { + int x = nums[i]; + for (int j = i - 1; j >= 0 && (nums[j] & x) != nums[j]; j--) { + nums[j] &= x; + } + while (left <= i && nums[left] < k) { + left++; + } + while (right <= i && nums[right] <= k) { + right++; + } + ans += right - left; + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/readme.md b/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/readme.md new file mode 100644 index 000000000..3cf6d05f4 --- /dev/null +++ b/src/main/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/readme.md @@ -0,0 +1,40 @@ +3209\. Number of Subarrays With AND Value of K + +Hard + +Given an array of integers `nums` and an integer `k`, return the number of subarrays of `nums` where the bitwise `AND` of the elements of the subarray equals `k`. + +**Example 1:** + +**Input:** nums = [1,1,1], k = 1 + +**Output:** 6 + +**Explanation:** + +All subarrays contain only 1's. + +**Example 2:** + +**Input:** nums = [1,1,2], k = 1 + +**Output:** 3 + +**Explanation:** + +Subarrays having an `AND` value of 1 are: [**1**,1,2], [1,**1**,2], [**1,1**,2]. + +**Example 3:** + +**Input:** nums = [1,2,3], k = 2 + +**Output:** 2 + +**Explanation:** + +Subarrays having an `AND` value of 2 are: [1,**2**,3], [1,**2,3**]. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i], k <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3210_find_the_encrypted_string/Solution.java b/src/main/java/g3201_3300/s3210_find_the_encrypted_string/Solution.java new file mode 100644 index 000000000..92abd65b9 --- /dev/null +++ b/src/main/java/g3201_3300/s3210_find_the_encrypted_string/Solution.java @@ -0,0 +1,13 @@ +package g3201_3300.s3210_find_the_encrypted_string; + +// #Easy #String #2024_07_09_Time_1_ms_(100.00%)_Space_42.8_MB_(34.96%) + +public class Solution { + public String getEncryptedString(String s, int k) { + int n = s.length(); + k = k % n; + StringBuilder str = new StringBuilder(s.substring(k, n)); + str.append(s.substring(0, k)); + return str.toString(); + } +} diff --git a/src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md b/src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md new file mode 100644 index 000000000..08db6266f --- /dev/null +++ b/src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md @@ -0,0 +1,38 @@ +3210\. Find the Encrypted String + +Easy + +You are given a string `s` and an integer `k`. Encrypt the string using the following algorithm: + +* For each character `c` in `s`, replace `c` with the kth character after `c` in the string (in a cyclic manner). + +Return the _encrypted string_. + +**Example 1:** + +**Input:** s = "dart", k = 3 + +**Output:** "tdar" + +**Explanation:** + +* For `i = 0`, the 3rd character after `'d'` is `'t'`. +* For `i = 1`, the 3rd character after `'a'` is `'d'`. +* For `i = 2`, the 3rd character after `'r'` is `'a'`. +* For `i = 3`, the 3rd character after `'t'` is `'r'`. + +**Example 2:** + +**Input:** s = "aaa", k = 1 + +**Output:** "aaa" + +**Explanation:** + +As all the characters are the same, the encrypted string will also be the same. + +**Constraints:** + +* `1 <= s.length <= 100` +* 1 <= k <= 104 +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/Solution.java b/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/Solution.java new file mode 100644 index 000000000..6abb1b6a3 --- /dev/null +++ b/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/Solution.java @@ -0,0 +1,37 @@ +package g3201_3300.s3211_generate_binary_strings_without_adjacent_zeros; + +// #Medium #String #Bit_Manipulation #Recursion #2024_07_09_Time_1_ms_(100.00%)_Space_46_MB_(27.65%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public List validStrings(int n) { + List strings = new ArrayList<>(); + dfs(n, new StringBuilder(), strings); + return strings; + } + + private void dfs(int n, StringBuilder build, List strings) { + if (build.length() == n) { + strings.add(build.toString()); + return; + } + // need to add a one + if (!build.isEmpty() && build.charAt(build.length() - 1) == '0') { + build.append('1'); + dfs(n, build, strings); + // undo for backtracking + build.setLength(build.length() - 1); + return; + } + // choose to append a one + build.append('1'); + dfs(n, build, strings); + build.setLength(build.length() - 1); + // choose to append a zero + build.append('0'); + dfs(n, build, strings); + build.setLength(build.length() - 1); + } +} diff --git a/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/readme.md b/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/readme.md new file mode 100644 index 000000000..87c61912f --- /dev/null +++ b/src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/readme.md @@ -0,0 +1,33 @@ +3211\. Generate Binary Strings Without Adjacent Zeros + +Medium + +You are given a positive integer `n`. + +A binary string `x` is **valid** if all substrings of `x` of length 2 contain **at least** one `"1"`. + +Return all **valid** strings with length `n`**,** in _any_ order. + +**Example 1:** + +**Input:** n = 3 + +**Output:** ["010","011","101","110","111"] + +**Explanation:** + +The valid strings of length 3 are: `"010"`, `"011"`, `"101"`, `"110"`, and `"111"`. + +**Example 2:** + +**Input:** n = 1 + +**Output:** ["0","1"] + +**Explanation:** + +The valid strings of length 1 are: `"0"` and `"1"`. + +**Constraints:** + +* `1 <= n <= 18` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java new file mode 100644 index 000000000..f09f09131 --- /dev/null +++ b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java @@ -0,0 +1,24 @@ +package g3201_3300.s3212_count_submatrices_with_equal_frequency_of_x_and_y; + +// #Medium #Array #Matrix #Prefix_Sum #2024_07_09_Time_15_ms_(100.00%)_Space_117_MB_(99.13%) + +public class Solution { + public int numberOfSubmatrices(char[][] grid) { + int m = grid.length, n = grid[0].length, ans = 0; + int[][] row = new int[n][2]; + for (int i = 0; i < m; i++) { + int[] count = new int[2]; + for (int j = 0; j < n; j++) { + if (grid[i][j] != '.') { + count[grid[i][j] - 'X']++; + } + row[j][0] += count[0]; + row[j][1] += count[1]; + if (row[j][0] > 0 && row[j][0] == row[j][1]) { + ans++; + } + } + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/readme.md b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/readme.md new file mode 100644 index 000000000..5f0787647 --- /dev/null +++ b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/readme.md @@ -0,0 +1,44 @@ +3212\. Count Submatrices With Equal Frequency of X and Y + +Medium + +Given a 2D character matrix `grid`, where `grid[i][j]` is either `'X'`, `'Y'`, or `'.'`, return the number of submatrices that contains: + +* `grid[0][0]` +* an **equal** frequency of `'X'` and `'Y'`. +* **at least** one `'X'`. + +**Example 1:** + +**Input:** grid = [["X","Y","."],["Y",".","."]] + +**Output:** 3 + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/07/examplems.png)** + +**Example 2:** + +**Input:** grid = [["X","X"],["X","Y"]] + +**Output:** 0 + +**Explanation:** + +No submatrix has an equal frequency of `'X'` and `'Y'`. + +**Example 3:** + +**Input:** grid = [[".","."],[".","."]] + +**Output:** 0 + +**Explanation:** + +No submatrix has at least one `'X'`. + +**Constraints:** + +* `1 <= grid.length, grid[i].length <= 1000` +* `grid[i][j]` is either `'X'`, `'Y'`, or `'.'`. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/Solution.java b/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/Solution.java new file mode 100644 index 000000000..033d705ca --- /dev/null +++ b/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/Solution.java @@ -0,0 +1,62 @@ +package g3201_3300.s3213_construct_string_with_minimum_cost; + +// #Hard #Array #String #Dynamic_Programming #Suffix_Array +// #2024_07_09_Time_182_ms_(100.00%)_Space_61.4_MB_(72.97%) + +import java.util.Arrays; + +public class Solution { + private static int invalid = Integer.MAX_VALUE; + + private static class Node { + int cost = -1; + Node[] chd = new Node[26]; + } + + private Node rt; + + public int minimumCost(String target, String[] words, int[] costs) { + rt = new Node(); + int m = words.length; + int n = target.length(); + for (int i = 0; i < m; ++i) { + if (words[i].length() <= n) { + insert(words[i], costs[i]); + } + } + int[] dp = new int[n + 1]; + Arrays.fill(dp, invalid); + dp[0] = 0; + for (int i = 0; i < n; ++i) { + if (dp[i] == invalid) { + continue; + } + int nowC = dp[i]; + Node now = rt; + for (int j = i; now != null && j < n; ++j) { + int ch = target.charAt(j) - 'a'; + now = now.chd[ch]; + if (now != null && now.cost != -1) { + dp[j + 1] = Math.min(dp[j + 1], nowC + now.cost); + } + } + } + + return dp[n] == invalid ? -1 : dp[n]; + } + + private void insert(String wd, int cst) { + int len = wd.length(); + Node now = rt; + for (int i = 0; i < len; ++i) { + int ch = wd.charAt(i) - 'a'; + if (now.chd[ch] == null) { + now.chd[ch] = new Node(); + } + now = now.chd[ch]; + } + if (now.cost == -1 || now.cost > cst) { + now.cost = cst; + } + } +} diff --git a/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/readme.md b/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/readme.md new file mode 100644 index 000000000..a78d9bf21 --- /dev/null +++ b/src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost/readme.md @@ -0,0 +1,48 @@ +3213\. Construct String with Minimum Cost + +Hard + +You are given a string `target`, an array of strings `words`, and an integer array `costs`, both arrays of the same length. + +Imagine an empty string `s`. + +You can perform the following operation any number of times (including **zero**): + +* Choose an index `i` in the range `[0, words.length - 1]`. +* Append `words[i]` to `s`. +* The cost of operation is `costs[i]`. + +Return the **minimum** cost to make `s` equal to `target`. If it's not possible, return `-1`. + +**Example 1:** + +**Input:** target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5] + +**Output:** 7 + +**Explanation:** + +The minimum cost can be achieved by performing the following operations: + +* Select index 1 and append `"abc"` to `s` at a cost of 1, resulting in `s = "abc"`. +* Select index 2 and append `"d"` to `s` at a cost of 1, resulting in `s = "abcd"`. +* Select index 4 and append `"ef"` to `s` at a cost of 5, resulting in `s = "abcdef"`. + +**Example 2:** + +**Input:** target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100] + +**Output:** \-1 + +**Explanation:** + +It is impossible to make `s` equal to `target`, so we return -1. + +**Constraints:** + +* 1 <= target.length <= 5 * 104 +* 1 <= words.length == costs.length <= 5 * 104 +* `1 <= words[i].length <= target.length` +* The total sum of `words[i].length` is less than or equal to 5 * 104. +* `target` and `words[i]` consist only of lowercase English letters. +* 1 <= costs[i] <= 104 \ No newline at end of file diff --git a/src/test/java/g3201_3300/s3206_alternating_groups_i/SolutionTest.java b/src/test/java/g3201_3300/s3206_alternating_groups_i/SolutionTest.java new file mode 100644 index 000000000..cc29f6c80 --- /dev/null +++ b/src/test/java/g3201_3300/s3206_alternating_groups_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3206_alternating_groups_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfAlternatingGroups() { + assertThat(new Solution().numberOfAlternatingGroups(new int[] {1, 1, 1}), equalTo(0)); + } + + @Test + void numberOfAlternatingGroups2() { + assertThat(new Solution().numberOfAlternatingGroups(new int[] {0, 1, 0, 0, 1}), equalTo(3)); + } +} diff --git a/src/test/java/g3201_3300/s3207_maximum_points_after_enemy_battles/SolutionTest.java b/src/test/java/g3201_3300/s3207_maximum_points_after_enemy_battles/SolutionTest.java new file mode 100644 index 000000000..285e54fa8 --- /dev/null +++ b/src/test/java/g3201_3300/s3207_maximum_points_after_enemy_battles/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3207_maximum_points_after_enemy_battles; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumPoints() { + assertThat(new Solution().maximumPoints(new int[] {3, 2, 2}, 2), equalTo(3L)); + } + + @Test + void maximumPoints2() { + assertThat(new Solution().maximumPoints(new int[] {2}, 10), equalTo(5L)); + } +} diff --git a/src/test/java/g3201_3300/s3208_alternating_groups_ii/SolutionTest.java b/src/test/java/g3201_3300/s3208_alternating_groups_ii/SolutionTest.java new file mode 100644 index 000000000..c45decdcb --- /dev/null +++ b/src/test/java/g3201_3300/s3208_alternating_groups_ii/SolutionTest.java @@ -0,0 +1,26 @@ +package g3201_3300.s3208_alternating_groups_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfAlternatingGroups() { + assertThat( + new Solution().numberOfAlternatingGroups(new int[] {0, 1, 0, 1, 0}, 3), equalTo(3)); + } + + @Test + void numberOfAlternatingGroups2() { + assertThat( + new Solution().numberOfAlternatingGroups(new int[] {0, 1, 0, 0, 1, 0, 1}, 6), + equalTo(2)); + } + + @Test + void numberOfAlternatingGroups3() { + assertThat(new Solution().numberOfAlternatingGroups(new int[] {1, 1, 0, 1}, 4), equalTo(0)); + } +} diff --git a/src/test/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/SolutionTest.java b/src/test/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/SolutionTest.java new file mode 100644 index 000000000..571222be8 --- /dev/null +++ b/src/test/java/g3201_3300/s3209_number_of_subarrays_with_and_value_of_k/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3209_number_of_subarrays_with_and_value_of_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countSubarrays() { + assertThat(new Solution().countSubarrays(new int[] {1, 1, 2}, 1), equalTo(3L)); + } + + @Test + void countSubarrays2() { + assertThat(new Solution().countSubarrays(new int[] {1, 2, 3}, 2), equalTo(2L)); + } +} diff --git a/src/test/java/g3201_3300/s3210_find_the_encrypted_string/SolutionTest.java b/src/test/java/g3201_3300/s3210_find_the_encrypted_string/SolutionTest.java new file mode 100644 index 000000000..03d0b70eb --- /dev/null +++ b/src/test/java/g3201_3300/s3210_find_the_encrypted_string/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3210_find_the_encrypted_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void getEncryptedString() { + assertThat(new Solution().getEncryptedString("dart", 3), equalTo("tdar")); + } + + @Test + void getEncryptedString2() { + assertThat(new Solution().getEncryptedString("aaa", 1), equalTo("aaa")); + } +} diff --git a/src/test/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/SolutionTest.java b/src/test/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/SolutionTest.java new file mode 100644 index 000000000..aadd0e358 --- /dev/null +++ b/src/test/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros/SolutionTest.java @@ -0,0 +1,21 @@ +package g3201_3300.s3211_generate_binary_strings_without_adjacent_zeros; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validStrings() { + assertThat( + new Solution().validStrings(3), + equalTo(List.of("111", "110", "101", "011", "010"))); + } + + @Test + void validStrings2() { + assertThat(new Solution().validStrings(1), equalTo(List.of("1", "0"))); + } +} diff --git a/src/test/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/SolutionTest.java b/src/test/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/SolutionTest.java new file mode 100644 index 000000000..bb74c7e68 --- /dev/null +++ b/src/test/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/SolutionTest.java @@ -0,0 +1,29 @@ +package g3201_3300.s3212_count_submatrices_with_equal_frequency_of_x_and_y; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfSubmatrices() { + assertThat( + new Solution().numberOfSubmatrices(new char[][] {{'X', 'Y', '.'}, {'Y', '.', '.'}}), + equalTo(3)); + } + + @Test + void numberOfSubmatrices2() { + assertThat( + new Solution().numberOfSubmatrices(new char[][] {{'X', 'X'}, {'X', 'Y'}}), + equalTo(0)); + } + + @Test + void numberOfSubmatrices3() { + assertThat( + new Solution().numberOfSubmatrices(new char[][] {{'.', '.'}, {'.', '.'}}), + equalTo(0)); + } +} diff --git a/src/test/java/g3201_3300/s3213_construct_string_with_minimum_cost/SolutionTest.java b/src/test/java/g3201_3300/s3213_construct_string_with_minimum_cost/SolutionTest.java new file mode 100644 index 000000000..57b0225ba --- /dev/null +++ b/src/test/java/g3201_3300/s3213_construct_string_with_minimum_cost/SolutionTest.java @@ -0,0 +1,28 @@ +package g3201_3300.s3213_construct_string_with_minimum_cost; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumCost() { + assertThat( + new Solution() + .minimumCost( + "abcdef", + new String[] {"abdef", "abc", "d", "def", "ef"}, + new int[] {100, 1, 1, 10, 5}), + equalTo(7)); + } + + @Test + void minimumCost2() { + assertThat( + new Solution() + .minimumCost( + "aaaa", new String[] {"z", "zz", "zzz"}, new int[] {1, 10, 100}), + equalTo(-1)); + } +} From a5d09470f2fb19404b21192c43cb8890ab516f3d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 9 Jul 2024 19:04:11 +0300 Subject: [PATCH 2/4] Fixed style --- .../s3208_alternating_groups_ii/Solution.java | 12 +++++++++--- .../Solution.java | 10 ++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java b/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java index 3d735bdf8..36fd609e5 100644 --- a/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java +++ b/src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java @@ -4,7 +4,9 @@ public class Solution { public int numberOfAlternatingGroups(int[] colors, int k) { - int i = 0, len = 0, total = 0; + int i = 0; + int len = 0; + int total = 0; while (i < colors.length - 1) { int j = i + 1; if (colors[j] != colors[i]) { @@ -14,7 +16,9 @@ public int numberOfAlternatingGroups(int[] colors, int k) { j++; len++; } - if (j == colors.length) break; + if (j == colors.length) { + break; + } total += Math.max(0, (len - k + 1)); } i = j; @@ -30,7 +34,9 @@ public int numberOfAlternatingGroups(int[] colors, int k) { j++; len++; } - if (j >= k) len -= (j - k + 1); + if (j >= k) { + len -= (j - k + 1); + } } total += Math.max(0, (len - k + 1)); return total; diff --git a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java index f09f09131..de42277f1 100644 --- a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java +++ b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java @@ -4,13 +4,15 @@ public class Solution { public int numberOfSubmatrices(char[][] grid) { - int m = grid.length, n = grid[0].length, ans = 0; + int m = grid.length; + int n = grid[0].length; + int ans = 0; int[][] row = new int[n][2]; - for (int i = 0; i < m; i++) { + for (char[] chars : grid) { int[] count = new int[2]; for (int j = 0; j < n; j++) { - if (grid[i][j] != '.') { - count[grid[i][j] - 'X']++; + if (chars[j] != '.') { + count[chars[j] - 'X']++; } row[j][0] += count[0]; row[j][1] += count[1]; From f03d6403ee9adedc069b086d1e61108e14dc6799 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 9 Jul 2024 19:06:18 +0300 Subject: [PATCH 3/4] Fixed sonar --- .../s3207_maximum_points_after_enemy_battles/Solution.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java index 5f685f0ac..2033b68b9 100644 --- a/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java +++ b/src/main/java/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.java @@ -12,8 +12,7 @@ public long maximumPoints(int[] enemyEnergies, int currentEnergy) { if (currentEnergy == 0 || currentEnergy < min) { return 0; } - long points = 0; - long sum = (long) currentEnergy; + long sum = currentEnergy; for (int i = n - 1; i >= 0; i--) { sum += enemyEnergies[i]; } From fcf3121b2fe3009bf49863c387193cce85ee0173 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 9 Jul 2024 19:10:21 +0300 Subject: [PATCH 4/4] Fixed sonar --- .../Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java index de42277f1..00de7e34c 100644 --- a/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java +++ b/src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y/Solution.java @@ -4,7 +4,6 @@ public class Solution { public int numberOfSubmatrices(char[][] grid) { - int m = grid.length; int n = grid[0].length; int ans = 0; int[][] row = new int[n][2];