From 0e3547231961c98209db4b6f0afce09a6650d507 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 22 Dec 2024 11:48:29 +0200 Subject: [PATCH 01/13] Added tasks 3392-3397 --- README.md | 2 +- .../Solution.java | 19 +++ .../readme.md | 32 ++++ .../Solution.java | 41 +++++ .../readme.md | 57 +++++++ .../Solution.java | 48 ++++++ .../readme.md | 57 +++++++ .../Solution.java | 142 ++++++++++++++++++ .../readme.md | 50 ++++++ .../Solution.java | 34 +++++ .../readme.md | 50 ++++++ .../Solution.java | 23 +++ .../readme.md | 37 +++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 35 +++++ .../SolutionTest.java | 48 ++++++ .../SolutionTest.java | 29 ++++ .../SolutionTest.java | 25 +++ .../SolutionTest.java | 18 +++ 19 files changed, 764 insertions(+), 1 deletion(-) create mode 100644 src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.java create mode 100644 src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md create mode 100644 src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java create mode 100644 src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md create mode 100644 src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java create mode 100644 src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md create mode 100644 src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md create mode 100644 src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.java create mode 100644 src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md create mode 100644 src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.java create mode 100644 src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/readme.md create mode 100644 src/test/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.java diff --git a/README.md b/README.md index a29369742..1d8215730 100644 --- a/README.md +++ b/README.md @@ -706,7 +706,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.39' | | | | | | |-|-|-|-|-|- -| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 12 | 26.01 +| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 7 | 99.33 | 1779 |[Find Nearest Point That Has the Same X or Y Coordinate](src/main/java/g1701_1800/s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate/Solution.java)| Easy | Array | 1 | 100.00 #### Day 4 Loop diff --git a/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.java b/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.java new file mode 100644 index 000000000..2d52fc44e --- /dev/null +++ b/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.java @@ -0,0 +1,19 @@ +package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition; + +// #Easy #2024_12_22_Time_1_ms_(100.00%)_Space_45.5_MB_(100.00%) + +public class Solution { + public int countSubarrays(int[] nums) { + int window = 3; + int cnt = 0; + for (int i = 0; i <= nums.length - window; i++) { + float first = nums[i]; + float second = nums[i + 1]; + float third = nums[i + 2]; + if (second / 2 == first + third) { + cnt++; + } + } + return cnt; + } +} diff --git a/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md b/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md new file mode 100644 index 000000000..b502289ee --- /dev/null +++ b/src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md @@ -0,0 +1,32 @@ +3392\. Count Subarrays of Length Three With a Condition + +Easy + +Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals _exactly_ half of the second number. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [1,2,1,4,1] + +**Output:** 1 + +**Explanation:** + +Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number. + +**Example 2:** + +**Input:** nums = [1,1,1] + +**Output:** 0 + +**Explanation:** + +`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number. + +**Constraints:** + +* `3 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java new file mode 100644 index 000000000..b3949ce4c --- /dev/null +++ b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java @@ -0,0 +1,41 @@ +package g3301_3400.s3393_count_paths_with_the_given_xor_value; + +// #Medium #2024_12_22_Time_83_ms_(100.00%)_Space_57_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + private final int mod = (int) (1e9 + 7); + private int m = -1; + private int n = -1; + private int[][][] dp; + + public int countPathsWithXorValue(int[][] grid, int k) { + m = grid.length; + n = grid[0].length; + dp = new int[m][n][16]; + for (int[][] a : dp) { + for (int[] b : a) { + Arrays.fill(b, -1); + } + } + return dfs(grid, 0, k, 0, 0); + } + + private int dfs(int[][] grid, int xorVal, int k, int i, int j) { + if (i < 0 || j < 0 || j >= n || i >= m) { + return 0; + } + xorVal ^= grid[i][j]; + if (dp[i][j][xorVal] != -1) { + return dp[i][j][xorVal]; + } + if (i == m - 1 && j == n - 1 && xorVal == k) { + return 1; + } + int down = dfs(grid, xorVal, k, i + 1, j); + int right = dfs(grid, xorVal, k, i, j + 1); + dp[i][j][xorVal] = (down + right) % mod; + return dp[i][j][xorVal]; + } +} diff --git a/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md new file mode 100644 index 000000000..121a32ba4 --- /dev/null +++ b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md @@ -0,0 +1,57 @@ +3393\. Count Paths With the Given XOR Value + +Medium + +You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`. + +Your task is to calculate the number of paths you can take from the top-left cell `(0, 0)` to the bottom-right cell `(m - 1, n - 1)` satisfying the following **constraints**: + +* You can either move to the right or down. Formally, from the cell `(i, j)` you may move to the cell `(i, j + 1)` or to the cell `(i + 1, j)` if the target cell _exists_. +* The `XOR` of all the numbers on the path must be **equal** to `k`. + +Return the total number of such paths. + +Since the answer can be very large, return the result **modulo** 109 + 7. + +**Example 1:** + +**Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11 + +**Output:** 3 + +**Explanation:** + +The 3 paths are: + +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)` +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)` +* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)` + +**Example 2:** + +**Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2 + +**Output:** 5 + +**Explanation:** + +The 5 paths are: + +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)` +* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)` +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)` +* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)` +* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)` + +**Example 3:** + +**Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10 + +**Output:** 0 + +**Constraints:** + +* `1 <= m == grid.length <= 300` +* `1 <= n == grid[r].length <= 300` +* `0 <= grid[r][c] < 16` +* `0 <= k < 16` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java new file mode 100644 index 000000000..c71d45079 --- /dev/null +++ b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java @@ -0,0 +1,48 @@ +package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections; + +// #Medium #2024_12_22_Time_136_ms_(100.00%)_Space_128.7_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + public boolean checkValidCuts(int n, int[][] rectangles) { + int m = rectangles.length; + int[][] xAxis = new int[m][2]; + int[][] yAxis = new int[m][2]; + int ind = 0; + for (int[] axis : rectangles) { + int startX = axis[0]; + int startY = axis[1]; + int endX = axis[2]; + int endY = axis[3]; + xAxis[ind] = new int[] {startX, endX}; + yAxis[ind] = new int[] {startY, endY}; + ind++; + } + Arrays.sort(xAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); + Arrays.sort(yAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); + int verticalCuts = findSections(xAxis); + if (verticalCuts > 2) { + return true; + } + int horizontalCuts = findSections(yAxis); + return horizontalCuts > 2; + } + + private int findSections(int[][] axis) { + int end = axis[0][1]; + int sections = 1; + for (int i = 1; i < axis.length; i++) { + if (end > axis[i][0]) { + end = Math.max(end, axis[i][1]); + } else { + sections++; + end = axis[i][1]; + } + if (sections > 2) { + return sections; + } + } + return sections; + } +} diff --git a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md new file mode 100644 index 000000000..3c707e831 --- /dev/null +++ b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md @@ -0,0 +1,57 @@ +3394\. Check if Grid can be Cut into Sections + +Medium + +You are given an integer `n` representing the dimensions of an `n x n` grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates `rectangles`, where `rectangles[i]` is in the form [startx, starty, endx, endy], representing a rectangle on the grid. Each rectangle is defined as follows: + +* (startx, starty): The bottom-left corner of the rectangle. +* (endx, endy): The top-right corner of the rectangle. + +**Note** that the rectangles do not overlap. Your task is to determine if it is possible to make **either two horizontal or two vertical cuts** on the grid such that: + +* Each of the three resulting sections formed by the cuts contains **at least** one rectangle. +* Every rectangle belongs to **exactly** one section. + +Return `true` if such cuts can be made; otherwise, return `false`. + +**Example 1:** + +**Input:** n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png) + +The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true. + +**Example 2:** + +**Input:** n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png) + +We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true. + +**Example 3:** + +**Input:** n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]] + +**Output:** false + +**Explanation:** + +We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false. + +**Constraints:** + +* 3 <= n <= 109 +* 3 <= rectangles.length <= 105 +* `0 <= rectangles[i][0] < rectangles[i][2] <= n` +* `0 <= rectangles[i][1] < rectangles[i][3] <= n` +* No two rectangles overlap. \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java new file mode 100644 index 000000000..c5eb2649b --- /dev/null +++ b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java @@ -0,0 +1,142 @@ +package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i; + +// #Hard #2024_12_22_Time_1115_ms_(100.00%)_Space_45.2_MB_(100.00%) + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Solution { + + private static final int MOD = 1000000007; + + public int subsequencesWithMiddleMode(int[] a) { + int n = a.length; + + // Create a dictionary to store indices of each number + Map> dict = new HashMap<>(); + for (int i = 0; i < n; i++) { + dict.computeIfAbsent(a[i], k -> new ArrayList<>()).add(i); + } + + long ans = 0L; + + // Iterate over each unique number and its indices + for (Map.Entry> entry : dict.entrySet()) { + List b = entry.getValue(); + int m = b.size(); + + for (int k = 0; k < m; k++) { + int i = b.get(k); + int r = m - 1 - k; + int u = i - k; + int v = (n - 1 - i) - r; + // Case 2: Frequency of occurrence is 2 times + ans = (ans + convert(k, 1) * convert(u, 1) % MOD * convert(v, 2) % MOD) % MOD; + ans = (ans + convert(r, 1) * convert(u, 2) % MOD * convert(v, 1) % MOD) % MOD; + // Case 3: Frequency of occurrence is 3 times + ans = (ans + convert(k, 2) * convert(v, 2) % MOD) % MOD; + ans = (ans + convert(r, 2) * convert(u, 2) % MOD) % MOD; + ans = + (ans + + convert(k, 1) + * convert(r, 1) + % MOD + * convert(u, 1) + % MOD + * convert(v, 1) + % MOD) + % MOD; + + // Case 4: Frequency of occurrence is 4 times + ans = (ans + convert(k, 2) * convert(r, 1) % MOD * convert(v, 1) % MOD) % MOD; + ans = (ans + convert(k, 1) * convert(r, 2) % MOD * convert(u, 1) % MOD) % MOD; + + // Case 5: Frequency of occurrence is 5 times + ans = (ans + convert(k, 2) * convert(r, 2) % MOD) % MOD; + } + } + long dif = 0; + // Principle of inclusion-exclusion + for (Map.Entry> midEntry : dict.entrySet()) { + List b = midEntry.getValue(); + int m = b.size(); + for (Map.Entry> tmpEntry : dict.entrySet()) { + if (!midEntry.getKey().equals(tmpEntry.getKey())) { + List c = tmpEntry.getValue(); + int size = c.size(); + for (int k = 0, j = 0; k < m; k++) { + int i = b.get(k); + int r = m - 1 - k; + int u = i - k; + int v = (n - 1 - i) - r; + while (j < size && c.get(j) < i) { + j++; + } + int x = j; + int y = size - x; + dif = + (dif + + convert(k, 1) + * convert(x, 1) + % MOD + * convert(y, 1) + % MOD + * convert(v - y, 1) + % MOD) + % MOD; + dif = + (dif + + convert(k, 1) + * convert(y, 2) + % MOD + * convert(u - x, 1) + % MOD) + % MOD; + dif = + (dif + convert(k, 1) * convert(x, 1) % MOD * convert(y, 2) % MOD) + % MOD; + + dif = + (dif + + convert(r, 1) + * convert(x, 1) + % MOD + * convert(y, 1) + % MOD + * convert(u - x, 1) + % MOD) + % MOD; + dif = + (dif + + convert(r, 1) + * convert(x, 2) + % MOD + * convert(v - y, 1) + % MOD) + % MOD; + dif = + (dif + convert(r, 1) * convert(x, 2) % MOD * convert(y, 1) % MOD) + % MOD; + } + } + } + } + return (int) ((ans - dif + MOD) % MOD); + } + + private long convert(int n, int k) { + if (k > n) { + return 0; + } + if (k == 0 || k == n) { + return 1; + } + long res = 1; + for (int i = 0; i < k; i++) { + res = res * (n - i) / (i + 1); + } + return res % MOD; + } +} diff --git a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md new file mode 100644 index 000000000..1c3261b05 --- /dev/null +++ b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md @@ -0,0 +1,50 @@ +3395\. Subsequences with a Unique Middle Mode I + +Hard + +Given an integer array `nums`, find the number of subsequences of size 5 of `nums` with a **unique middle mode**. + +Since the answer may be very large, return it **modulo** 109 + 7. + +A **mode** of a sequence of numbers is defined as the element that appears the **maximum** number of times in the sequence. + +A sequence of numbers contains a **unique mode** if it has only one mode. + +A sequence of numbers `seq` of size 5 contains a **unique middle mode** if the _middle element_ (`seq[2]`) is a **unique mode**. + +A **subsequence** is a **non-empty** array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. + +**Example 1:** + +**Input:** nums = [1,1,1,1,1,1] + +**Output:** 6 + +**Explanation:** + +`[1, 1, 1, 1, 1]` is the only subsequence of size 5 that can be formed, and it has a unique middle mode of 1. This subsequence can be formed in 6 different ways, so the output is 6. + +**Example 2:** + +**Input:** nums = [1,2,2,3,3,4] + +**Output:** 4 + +**Explanation:** + +`[1, 2, 2, 3, 4]` and `[1, 2, 3, 3, 4]` each have a unique middle mode because the number at index 2 has the greatest frequency in the subsequence. `[1, 2, 2, 3, 3]` does not have a unique middle mode because 2 and 3 appear twice. + +**Example 3:** + +**Input:** nums = [0,1,2,3,4,5,6,7,8] + +**Output:** 0 + +**Explanation:** + +There is no subsequence of length 5 with a unique middle mode. + +**Constraints:** + +* `5 <= nums.length <= 1000` +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.java b/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.java new file mode 100644 index 000000000..d6ae812ea --- /dev/null +++ b/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.java @@ -0,0 +1,34 @@ +package g3301_3400.s3396_minimum_number_of_operations_to_make_elements_in_array_distinct; + +// #Easy #2024_12_22_Time_4_ms_(100.00%)_Space_45_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minimumOperations(int[] nums) { + Map map = new HashMap<>(); + int dupct = 0; + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + if (map.get(num) == 2) { + dupct++; + } + } + int n = nums.length; + int i = 0; + int op = 0; + while (dupct > 0) { + op++; + int limit = Math.min(n, i + 3); + for (; i < limit; i++) { + int val = map.get(nums[i]); + if (val == 2) { + dupct--; + } + map.put(nums[i], val - 1); + } + } + return op; + } +} diff --git a/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md b/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md new file mode 100644 index 000000000..ae8e9e0a8 --- /dev/null +++ b/src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md @@ -0,0 +1,50 @@ +3396\. Minimum Number of Operations to Make Elements in Array Distinct + +Easy + +You are given an integer array `nums`. You need to ensure that the elements in the array are **distinct**. To achieve this, you can perform the following operation any number of times: + +* Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements. + +**Note** that an empty array is considered to have distinct elements. Return the **minimum** number of operations needed to make the elements in the array distinct. + +**Example 1:** + +**Input:** nums = [1,2,3,4,2,3,3,5,7] + +**Output:** 2 + +**Explanation:** + +* In the first operation, the first 3 elements are removed, resulting in the array `[4, 2, 3, 3, 5, 7]`. +* In the second operation, the next 3 elements are removed, resulting in the array `[3, 5, 7]`, which has distinct elements. + +Therefore, the answer is 2. + +**Example 2:** + +**Input:** nums = [4,5,6,4,4] + +**Output:** 2 + +**Explanation:** + +* In the first operation, the first 3 elements are removed, resulting in the array `[4, 4]`. +* In the second operation, all remaining elements are removed, resulting in an empty array. + +Therefore, the answer is 2. + +**Example 3:** + +**Input:** nums = [6,7,8,9] + +**Output:** 0 + +**Explanation:** + +The array already contains distinct elements. Therefore, the answer is 0. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.java b/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.java new file mode 100644 index 000000000..8a5dbd801 --- /dev/null +++ b/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.java @@ -0,0 +1,23 @@ +package g3301_3400.s3397_maximum_number_of_distinct_elements_after_operations; + +// #Medium #2024_12_22_Time_19_ms_(100.00%)_Space_57.8_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + public int maxDistinctElements(int[] nums, int k) { + Arrays.sort(nums); + int next = nums[0] - k + 1; + int n = nums.length; + int ans = 1; + for (int i = 1; i < n; i++) { + if (nums[i] + k < next) { + continue; + } + next = Math.max(next, nums[i] - k); + ans++; + next++; + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/readme.md b/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/readme.md new file mode 100644 index 000000000..d8bc43c01 --- /dev/null +++ b/src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/readme.md @@ -0,0 +1,37 @@ +3397\. Maximum Number of Distinct Elements After Operations + +Medium + +You are given an integer array `nums` and an integer `k`. + +You are allowed to perform the following **operation** on each element of the array **at most** _once_: + +* Add an integer in the range `[-k, k]` to the element. + +Return the **maximum** possible number of **distinct** elements in `nums` after performing the **operations**. + +**Example 1:** + +**Input:** nums = [1,2,2,3,3,4], k = 2 + +**Output:** 6 + +**Explanation:** + +`nums` changes to `[-1, 0, 1, 2, 3, 4]` after performing operations on the first four elements. + +**Example 2:** + +**Input:** nums = [4,4,4,4], k = 1 + +**Output:** 3 + +**Explanation:** + +By adding -1 to `nums[0]` and 1 to `nums[1]`, `nums` changes to `[3, 5, 4, 4]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/test/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.java b/src/test/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.java new file mode 100644 index 000000000..f7791afa8 --- /dev/null +++ b/src/test/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition; + +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, 2, 1, 4, 1}), equalTo(1)); + } + + @Test + void countSubarrays2() { + assertThat(new Solution().countSubarrays(new int[] {1, 1, 1}), equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.java b/src/test/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.java new file mode 100644 index 000000000..f0c253f16 --- /dev/null +++ b/src/test/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.java @@ -0,0 +1,35 @@ +package g3301_3400.s3393_count_paths_with_the_given_xor_value; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countPathsWithXorValue() { + assertThat( + new Solution() + .countPathsWithXorValue( + new int[][] {{2, 1, 5}, {7, 10, 0}, {12, 6, 4}}, 11), + equalTo(3)); + } + + @Test + void countPathsWithXorValue2() { + assertThat( + new Solution() + .countPathsWithXorValue( + new int[][] {{1, 3, 3, 3}, {0, 3, 3, 2}, {3, 0, 1, 1}}, 2), + equalTo(5)); + } + + @Test + void countPathsWithXorValue3() { + assertThat( + new Solution() + .countPathsWithXorValue( + new int[][] {{1, 1, 1, 2}, {3, 0, 3, 2}, {3, 0, 2, 2}}, 10), + equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.java b/src/test/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.java new file mode 100644 index 000000000..2c6a795c8 --- /dev/null +++ b/src/test/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.java @@ -0,0 +1,48 @@ +package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void checkValidCuts() { + assertThat( + new Solution() + .checkValidCuts( + 5, + new int[][] { + {1, 0, 5, 2}, {0, 2, 2, 4}, {3, 2, 5, 3}, {0, 4, 4, 5} + }), + equalTo(true)); + } + + @Test + void checkValidCuts2() { + assertThat( + new Solution() + .checkValidCuts( + 4, + new int[][] { + {0, 0, 1, 1}, {2, 0, 3, 4}, {0, 2, 2, 3}, {3, 0, 4, 3} + }), + equalTo(true)); + } + + @Test + void checkValidCuts3() { + assertThat( + new Solution() + .checkValidCuts( + 4, + new int[][] { + {0, 2, 2, 4}, + {1, 0, 3, 2}, + {2, 2, 3, 4}, + {3, 0, 4, 2}, + {3, 2, 4, 4} + }), + equalTo(false)); + } +} diff --git a/src/test/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.java b/src/test/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.java new file mode 100644 index 000000000..76fb3f9c8 --- /dev/null +++ b/src/test/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.java @@ -0,0 +1,29 @@ +package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void subsequencesWithMiddleMode() { + assertThat( + new Solution().subsequencesWithMiddleMode(new int[] {1, 1, 1, 1, 1, 1}), + equalTo(6)); + } + + @Test + void subsequencesWithMiddleMode2() { + assertThat( + new Solution().subsequencesWithMiddleMode(new int[] {1, 2, 2, 3, 3, 4}), + equalTo(4)); + } + + @Test + void subsequencesWithMiddleMode3() { + assertThat( + new Solution().subsequencesWithMiddleMode(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8}), + equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.java b/src/test/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.java new file mode 100644 index 000000000..a6ef00be7 --- /dev/null +++ b/src/test/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.java @@ -0,0 +1,25 @@ +package g3301_3400.s3396_minimum_number_of_operations_to_make_elements_in_array_distinct; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumOperations() { + assertThat( + new Solution().minimumOperations(new int[] {1, 2, 3, 4, 2, 3, 3, 5, 7}), + equalTo(2)); + } + + @Test + void minimumOperations2() { + assertThat(new Solution().minimumOperations(new int[] {4, 5, 6, 4, 4}), equalTo(2)); + } + + @Test + void minimumOperations3() { + assertThat(new Solution().minimumOperations(new int[] {6, 7, 8, 9}), equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.java b/src/test/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.java new file mode 100644 index 000000000..6f171a5c9 --- /dev/null +++ b/src/test/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3397_maximum_number_of_distinct_elements_after_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxDistinctElements() { + assertThat(new Solution().maxDistinctElements(new int[] {1, 2, 2, 3, 3, 4}, 2), equalTo(6)); + } + + @Test + void maxDistinctElements2() { + assertThat(new Solution().maxDistinctElements(new int[] {4, 4, 4, 4}, 1), equalTo(3)); + } +} From d8453171561b9624d212616e2c91edfef992cf04 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 22 Dec 2024 11:51:10 +0200 Subject: [PATCH 02/13] Improved format --- .../Solution.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java index c5eb2649b..0ee5bf405 100644 --- a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java +++ b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java @@ -8,25 +8,20 @@ import java.util.Map; public class Solution { - private static final int MOD = 1000000007; public int subsequencesWithMiddleMode(int[] a) { int n = a.length; - // Create a dictionary to store indices of each number Map> dict = new HashMap<>(); for (int i = 0; i < n; i++) { dict.computeIfAbsent(a[i], k -> new ArrayList<>()).add(i); } - long ans = 0L; - // Iterate over each unique number and its indices for (Map.Entry> entry : dict.entrySet()) { List b = entry.getValue(); int m = b.size(); - for (int k = 0; k < m; k++) { int i = b.get(k); int r = m - 1 - k; From b85cfd7d7b40e898fb2fe87b35208c79a819e7c1 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 22 Dec 2024 11:56:08 +0200 Subject: [PATCH 03/13] Fixed sonar --- .../s3393_count_paths_with_the_given_xor_value/Solution.java | 4 ++-- .../Solution.java | 1 + .../Solution.java | 5 ++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java index b3949ce4c..c6082f7a8 100644 --- a/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java +++ b/src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.java @@ -5,7 +5,7 @@ import java.util.Arrays; public class Solution { - private final int mod = (int) (1e9 + 7); + private static final int MOD = (int) (1e9 + 7); private int m = -1; private int n = -1; private int[][][] dp; @@ -35,7 +35,7 @@ private int dfs(int[][] grid, int xorVal, int k, int i, int j) { } int down = dfs(grid, xorVal, k, i + 1, j); int right = dfs(grid, xorVal, k, i, j + 1); - dp[i][j][xorVal] = (down + right) % mod; + dp[i][j][xorVal] = (down + right) % MOD; return dp[i][j][xorVal]; } } diff --git a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java index c71d45079..446a1be95 100644 --- a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java +++ b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java @@ -4,6 +4,7 @@ import java.util.Arrays; +@SuppressWarnings("unused") public class Solution { public boolean checkValidCuts(int n, int[][] rectangles) { int m = rectangles.length; diff --git a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java index 0ee5bf405..921dc0436 100644 --- a/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java +++ b/src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.java @@ -61,7 +61,9 @@ public int subsequencesWithMiddleMode(int[] a) { if (!midEntry.getKey().equals(tmpEntry.getKey())) { List c = tmpEntry.getValue(); int size = c.size(); - for (int k = 0, j = 0; k < m; k++) { + int k = 0; + int j = 0; + while (k < m) { int i = b.get(k); int r = m - 1 - k; int u = i - k; @@ -114,6 +116,7 @@ public int subsequencesWithMiddleMode(int[] a) { dif = (dif + convert(r, 1) * convert(x, 2) % MOD * convert(y, 1) % MOD) % MOD; + k++; } } } From 7fe89f19a76cfac85c75cc3ec4d4bf880e701eeb Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 22 Dec 2024 14:24:25 +0200 Subject: [PATCH 04/13] Fixed sonar --- .../s3394_check_if_grid_can_be_cut_into_sections/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java index 446a1be95..00af92a54 100644 --- a/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java +++ b/src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.java @@ -4,7 +4,7 @@ import java.util.Arrays; -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "java:S1172"}) public class Solution { public boolean checkValidCuts(int n, int[][] rectangles) { int m = rectangles.length; From 7d2f2c92b191f40d19865899f057cff9370a3b8e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 24 Dec 2024 05:46:08 +0200 Subject: [PATCH 05/13] Added tasks 3398, 3399 --- .../Solution.java | 67 ++++++++++++++ .../readme.md | 45 ++++++++++ .../Solution.java | 87 +++++++++++++++++++ .../readme.md | 45 ++++++++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ 6 files changed, 290 insertions(+) create mode 100644 src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md create mode 100644 src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java create mode 100644 src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md create mode 100644 src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java diff --git a/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.java b/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.java new file mode 100644 index 000000000..bc2ed7b73 --- /dev/null +++ b/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.java @@ -0,0 +1,67 @@ +package g3301_3400.s3398_smallest_substring_with_identical_characters_i; + +// #Hard #2024_12_24_Time_1_ms_(100.00%)_Space_42.9_MB_(39.83%) + +public class Solution { + public int minLength(String s, int ops) { + char[] arr2 = s.toCharArray(); + int q = '0'; + int w = '1'; + int p1 = ops; + int p2 = ops; + for (int i = 0; i < s.length(); i++) { + if (arr2[i] != q) { + p1--; + } + if (arr2[i] != w) { + p2--; + } + if (q == '0') { + q = '1'; + } else { + q = '0'; + } + if (w == '0') { + w = '1'; + } else { + w = '0'; + } + } + if (p1 >= 0 || p2 >= 0) { + return 1; + } + int low = 2; + int high = s.length(); + int ans = 0; + int n = s.length(); + while (low <= high) { + int mid = (low + high) / 2; + char[] arr = s.toCharArray(); + int p = ops; + int c = 1; + for (int i = 1; i < n; i++) { + if (arr[i] == arr[i - 1]) { + c++; + } else { + c = 1; + } + if (c > mid) { + if (arr[i - 1] == '0') { + arr[i - 1] = '1'; + } else { + arr[i - 1] = '0'; + } + p--; + c = 0; + } + } + if (p < 0) { + low = mid + 1; + } else { + ans = mid; + high = mid - 1; + } + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md b/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md new file mode 100644 index 000000000..eb273a6bd --- /dev/null +++ b/src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md @@ -0,0 +1,45 @@ +3398\. Smallest Substring With Identical Characters I + +Hard + +You are given a binary string `s` of length `n` and an integer `numOps`. + +You are allowed to perform the following operation on `s` **at most** `numOps` times: + +* Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. + +You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. + +Return the **minimum** length after the operations. + +**Example 1:** + +**Input:** s = "000001", numOps = 1 + +**Output:** 2 + +**Explanation:** + +By changing `s[2]` to `'1'`, `s` becomes `"001001"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. + +**Example 2:** + +**Input:** s = "0000", numOps = 2 + +**Output:** 1 + +**Explanation:** + +By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `"1010"`. + +**Example 3:** + +**Input:** s = "0101", numOps = 0 + +**Output:** 1 + +**Constraints:** + +* `1 <= n == s.length <= 1000` +* `s` consists only of `'0'` and `'1'`. +* `0 <= numOps <= n` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java new file mode 100644 index 000000000..2d0be824c --- /dev/null +++ b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java @@ -0,0 +1,87 @@ +package g3301_3400.s3399_smallest_substring_with_identical_characters_ii; + +// #Hard #2024_12_24_Time_11_ms_(100.00%)_Space_45.7_MB_(54.55%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public int minLength(String s, int numOps) { + int l = s.length(); + int lingyi = 0; + int yiling = 0; + List pq = new ArrayList<>(); + char thisone = s.charAt(0); + int chang = 1; + if (thisone == '0') { + yiling++; + } else { + lingyi++; + } + for (int i = 1; i < l; i++) { + char cur = s.charAt(i); + if (cur == thisone) { + chang++; + } else { + if (chang >= 2) { + pq.add(chang); + } + chang = 1; + thisone = cur; + } + if (i % 2 == 0) { + if (cur == '0') { + yiling++; + } else { + lingyi++; + } + } else { + if (cur == '0') { + lingyi++; + } else { + yiling++; + } + } + } + if (numOps >= lingyi || numOps >= yiling) { + return 1; + } + if (chang >= 2) { + pq.add(chang); + } + int one = -1; + int two = -1; + for (int cur : pq) { + if (cur > one) { + two = one; + one = cur; + } else if (cur > two) { + two = cur; + } + } + if (two == -1) { + return one / (numOps + 1) > 1 ? one / (numOps + 1) : 2; + } + if (numOps == 0) { + return one; + } + if (numOps == 1) { + return (one / 2 > two) ? (one / 2 == 1 ? 2 : one / 2) : two; + } + int left = 2; + int right = l / (numOps + 1); + while (left < right) { + int mid = left + (right - left) / 2; + int sum = 0; + for (Integer integer : pq) { + sum += integer / (mid + 1); + } + if (sum <= numOps) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } +} diff --git a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md new file mode 100644 index 000000000..424c9c840 --- /dev/null +++ b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md @@ -0,0 +1,45 @@ +3399\. Smallest Substring With Identical Characters II + +Hard + +You are given a binary string `s` of length `n` and an integer `numOps`. + +You are allowed to perform the following operation on `s` **at most** `numOps` times: + +* Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. + +You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. + +Return the **minimum** length after the operations. + +**Example 1:** + +**Input:** s = "000001", numOps = 1 + +**Output:** 2 + +**Explanation:** + +By changing `s[2]` to `'1'`, `s` becomes `"001001"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. + +**Example 2:** + +**Input:** s = "0000", numOps = 2 + +**Output:** 1 + +**Explanation:** + +By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `"1010"`. + +**Example 3:** + +**Input:** s = "0101", numOps = 0 + +**Output:** 1 + +**Constraints:** + +* 1 <= n == s.length <= 105 +* `s` consists only of `'0'` and `'1'`. +* `0 <= numOps <= n` \ No newline at end of file diff --git a/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java new file mode 100644 index 000000000..d97538604 --- /dev/null +++ b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3398_smallest_substring_with_identical_characters_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minLength() { + assertThat(new Solution().minLength("000001", 1), equalTo(1)); + } + + @Test + void minLength2() { + assertThat(new Solution().minLength("0000", 2), equalTo(1)); + } + + @Test + void minLength3() { + assertThat(new Solution().minLength("0101", 0), equalTo(1)); + } +} diff --git a/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java new file mode 100644 index 000000000..020bda8f3 --- /dev/null +++ b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3399_smallest_substring_with_identical_characters_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minLength() { + assertThat(new Solution().minLength("000001", 1), equalTo(2)); + } + + @Test + void minLength2() { + assertThat(new Solution().minLength("0000", 2), equalTo(1)); + } + + @Test + void minLength3() { + assertThat(new Solution().minLength("0101", 0), equalTo(1)); + } +} From 02579a15533ac6f02893c59316ceb5af90c22890 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 24 Dec 2024 05:50:00 +0200 Subject: [PATCH 06/13] fixed test --- .../SolutionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java index d97538604..34e83fbcc 100644 --- a/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java +++ b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java @@ -8,7 +8,7 @@ class SolutionTest { @Test void minLength() { - assertThat(new Solution().minLength("000001", 1), equalTo(1)); + assertThat(new Solution().minLength("000001", 1), equalTo(2)); } @Test From 90a0323f1a3900e13711e60688524429290f93e5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 24 Dec 2024 10:19:56 +0200 Subject: [PATCH 07/13] Improved task 3399 --- .../Solution.java | 116 ++++++++---------- 1 file changed, 51 insertions(+), 65 deletions(-) diff --git a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java index 2d0be824c..548573378 100644 --- a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java +++ b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java @@ -1,87 +1,73 @@ package g3301_3400.s3399_smallest_substring_with_identical_characters_ii; -// #Hard #2024_12_24_Time_11_ms_(100.00%)_Space_45.7_MB_(54.55%) +// #Hard #2024_12_24_Time_15_ms_(99.39%)_Space_45.9_MB_(43.03%) import java.util.ArrayList; import java.util.List; public class Solution { public int minLength(String s, int numOps) { - int l = s.length(); - int lingyi = 0; - int yiling = 0; - List pq = new ArrayList<>(); - char thisone = s.charAt(0); - int chang = 1; - if (thisone == '0') { - yiling++; - } else { - lingyi++; - } - for (int i = 1; i < l; i++) { - char cur = s.charAt(i); - if (cur == thisone) { - chang++; - } else { - if (chang >= 2) { - pq.add(chang); - } - chang = 1; - thisone = cur; + byte[] b = s.getBytes(); + int flips1 = 0; + int flips2 = 0; + for (int i = 0; i < b.length; i++) { + byte e1 = (byte) ((i % 2 == 0) ? '0' : '1'); + byte e2 = (byte) ((i % 2 == 0) ? '1' : '0'); + if (b[i] != e1) { + flips1++; } - if (i % 2 == 0) { - if (cur == '0') { - yiling++; - } else { - lingyi++; - } - } else { - if (cur == '0') { - lingyi++; - } else { - yiling++; - } + if (b[i] != e2) { + flips2++; } } - if (numOps >= lingyi || numOps >= yiling) { + int flips = Math.min(flips1, flips2); + if (flips <= numOps) { return 1; } - if (chang >= 2) { - pq.add(chang); - } - int one = -1; - int two = -1; - for (int cur : pq) { - if (cur > one) { - two = one; - one = cur; - } else if (cur > two) { - two = cur; + List seg = new ArrayList<>(); + int count = 1; + int max = 1; + for (int i = 1; i < b.length; i++) { + if (b[i] != b[i - 1]) { + if (count != 1) { + seg.add(count); + max = Math.max(max, count); + } + count = 1; + } else { + count++; } } - if (two == -1) { - return one / (numOps + 1) > 1 ? one / (numOps + 1) : 2; - } - if (numOps == 0) { - return one; + if (count != 1) { + seg.add(count); + max = Math.max(max, count); } - if (numOps == 1) { - return (one / 2 > two) ? (one / 2 == 1 ? 2 : one / 2) : two; + int l = 2; + int r = max; + int res = max; + while (l <= r) { + int m = l + (r - l) / 2; + if (check(m, seg, numOps)) { + r = m - 1; + res = m; + } else { + l = m + 1; + } } - int left = 2; - int right = l / (numOps + 1); - while (left < right) { - int mid = left + (right - left) / 2; - int sum = 0; - for (Integer integer : pq) { - sum += integer / (mid + 1); + return res; + } + + private boolean check(int sz, List seg, int ops) { + for (int i : seg) { + if (i <= sz) { + continue; } - if (sum <= numOps) { - right = mid; - } else { - left = mid + 1; + int x = i / (sz + 1); + ops -= x; + if (ops < 0) { + return false; } } - return left; + return true; } } From 28664701f55e8ef7df517d3d2ca378569a4dff8a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 24 Dec 2024 10:25:27 +0200 Subject: [PATCH 08/13] Added test --- .../Solution.java | 3 --- .../SolutionTest.java | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java index 548573378..4ca5c483a 100644 --- a/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java +++ b/src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.java @@ -59,9 +59,6 @@ public int minLength(String s, int numOps) { private boolean check(int sz, List seg, int ops) { for (int i : seg) { - if (i <= sz) { - continue; - } int x = i / (sz + 1); ops -= x; if (ops < 0) { diff --git a/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java index 020bda8f3..098d8b10d 100644 --- a/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java +++ b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java @@ -20,4 +20,9 @@ void minLength2() { void minLength3() { assertThat(new Solution().minLength("0101", 0), equalTo(1)); } + + @Test + void minLength4() { + assertThat(new Solution().minLength("000", 0), equalTo(3)); + } } From 6199a5991591ecbf02b554397c0a49354d8fb46c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 24 Dec 2024 10:27:36 +0200 Subject: [PATCH 09/13] Added test --- .../SolutionTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java index 098d8b10d..4dd01877f 100644 --- a/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java +++ b/src/test/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.java @@ -25,4 +25,9 @@ void minLength3() { void minLength4() { assertThat(new Solution().minLength("000", 0), equalTo(3)); } + + @Test + void minLength5() { + assertThat(new Solution().minLength("000001", 1), equalTo(2)); + } } From 8546ff78924e9777fb60638383cbbd03284b060c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 29 Dec 2024 09:40:19 +0200 Subject: [PATCH 10/13] Added tasks 3402-3405 --- .../Solution.java | 18 ++++++ .../readme.md | 43 ++++++++++++++ .../Solution.java | 25 ++++++++ .../readme.md | 45 +++++++++++++++ .../Solution.java | 23 ++++++++ .../readme.md | 57 +++++++++++++++++++ .../Solution.java | 40 +++++++++++++ .../readme.md | 50 ++++++++++++++++ .../SolutionTest.java | 22 +++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 21 +++++++ .../SolutionTest.java | 23 ++++++++ 12 files changed, 385 insertions(+) create mode 100644 src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.java create mode 100644 src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/readme.md create mode 100644 src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.java create mode 100644 src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md create mode 100644 src/main/java/g3401_3500/s3404_count_special_subsequences/Solution.java create mode 100644 src/main/java/g3401_3500/s3404_count_special_subsequences/readme.md create mode 100644 src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java create mode 100644 src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md create mode 100644 src/test/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3404_count_special_subsequences/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.java diff --git a/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.java b/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.java new file mode 100644 index 000000000..52ecf87a8 --- /dev/null +++ b/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.java @@ -0,0 +1,18 @@ +package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing; + +// #Easy #2024_12_29_Time_1_(100.00%)_Space_44.99_(100.00%) + +public class Solution { + public int minimumOperations(int[][] grid) { + int ans = 0; + for (int c = 0; c < grid[0].length; ++c) { + for (int r = 1; r < grid.length; ++r) { + if (grid[r][c] <= grid[r - 1][c]) { + ans += grid[r - 1][c] + 1 - grid[r][c]; + grid[r][c] = grid[r - 1][c] + 1; + } + } + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/readme.md b/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/readme.md new file mode 100644 index 000000000..76bdfe5dd --- /dev/null +++ b/src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/readme.md @@ -0,0 +1,43 @@ +3402\. Minimum Operations to Make Columns Strictly Increasing + +Easy + +You are given a `m x n` matrix `grid` consisting of **non-negative** integers. + +In one operation, you can increment the value of any `grid[i][j]` by 1. + +Return the **minimum** number of operations needed to make all columns of `grid` **strictly increasing**. + +**Example 1:** + +**Input:** grid = [[3,2],[1,3],[3,4],[0,1]] + +**Output:** 15 + +**Explanation:** + +* To make the 0th column strictly increasing, we can apply 3 operations on `grid[1][0]`, 2 operations on `grid[2][0]`, and 6 operations on `grid[3][0]`. +* To make the 1st column strictly increasing, we can apply 4 operations on `grid[3][1]`. + +![](https://assets.leetcode.com/uploads/2024/11/10/firstexample.png) + +**Example 2:** + +**Input:** grid = [[3,2,1],[2,1,0],[1,2,3]] + +**Output:** 12 + +**Explanation:** + +* To make the 0th column strictly increasing, we can apply 2 operations on `grid[1][0]`, and 4 operations on `grid[2][0]`. +* To make the 1st column strictly increasing, we can apply 2 operations on `grid[1][1]`, and 2 operations on `grid[2][1]`. +* To make the 2nd column strictly increasing, we can apply 2 operations on `grid[1][2]`. + +![](https://assets.leetcode.com/uploads/2024/11/10/secondexample.png) + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= m, n <= 50` +* `0 <= grid[i][j] < 2500` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.java b/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.java new file mode 100644 index 000000000..39f7d7e32 --- /dev/null +++ b/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.java @@ -0,0 +1,25 @@ +package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i; + +// #Medium #2024_12_29_Time_5_(100.00%)_Space_45.20_(100.00%) + +public class Solution { + public String answerString(String word, int numFriends) { + if (numFriends == 1) { + return word; + } + int n = word.length(); + int maxlen = n - numFriends + 1; + char maxchar = word.charAt(0); + String res = ""; + for (int i = 0; i < n; i++) { + if (word.charAt(i) >= maxchar) { + String curr = word.substring(i, Math.min(i + maxlen, n)); + if (curr.compareTo(res) > 0) { + res = curr; + } + maxchar = word.charAt(i); + } + } + return res; + } +} diff --git a/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md b/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md new file mode 100644 index 000000000..75a49bd2f --- /dev/null +++ b/src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md @@ -0,0 +1,45 @@ +3403\. Find the Lexicographically Largest String From the Box I + +Medium + +You are given a string `word`, and an integer `numFriends`. + +Alice is organizing a game for her `numFriends` friends. There are multiple rounds in the game, where in each round: + +* `word` is split into `numFriends` **non-empty** strings, such that no previous round has had the **exact** same split. +* All the split words are put into a box. + +Find the **lexicographically largest** string from the box after all the rounds are finished. + +A string `a` is **lexicographically smaller** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. + If the first `min(a.length, b.length)` characters do not differ, then the shorter string is the lexicographically smaller one. + +**Example 1:** + +**Input:** word = "dbca", numFriends = 2 + +**Output:** "dbc" + +**Explanation:** + +All possible splits are: + +* `"d"` and `"bca"`. +* `"db"` and `"ca"`. +* `"dbc"` and `"a"`. + +**Example 2:** + +**Input:** word = "gggg", numFriends = 4 + +**Output:** "g" + +**Explanation:** + +The only possible split is: `"g"`, `"g"`, `"g"`, and `"g"`. + +**Constraints:** + +* 1 <= word.length <= 5 * 103 +* `word` consists only of lowercase English letters. +* `1 <= numFriends <= word.length` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3404_count_special_subsequences/Solution.java b/src/main/java/g3401_3500/s3404_count_special_subsequences/Solution.java new file mode 100644 index 000000000..02d3621f0 --- /dev/null +++ b/src/main/java/g3401_3500/s3404_count_special_subsequences/Solution.java @@ -0,0 +1,23 @@ +package g3401_3500.s3404_count_special_subsequences; + +// #Medium #2024_12_29_Time_331_(100.00%)_Space_55.49_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public long numberOfSubsequences(int[] nums) { + Map freq = new HashMap<>(); + long ans = 0; + for (int r = 4; r < nums.length; ++r) { + for (int p = 0, q = r - 2; p < q - 1; ++p) { + Double key = (double) nums[p] / nums[q]; + freq.put(key, freq.getOrDefault(key, 0) + 1); + } + for (int s = r + 2; s < nums.length; ++s) { + ans += freq.getOrDefault((double) nums[s] / nums[r], 0); + } + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3404_count_special_subsequences/readme.md b/src/main/java/g3401_3500/s3404_count_special_subsequences/readme.md new file mode 100644 index 000000000..7096c5d22 --- /dev/null +++ b/src/main/java/g3401_3500/s3404_count_special_subsequences/readme.md @@ -0,0 +1,57 @@ +3404\. Count Special Subsequences + +Medium + +You are given an array `nums` consisting of positive integers. + +A **special subsequence** is defined as a subsequence of length 4, represented by indices `(p, q, r, s)`, where `p < q < r < s`. This subsequence **must** satisfy the following conditions: + +* `nums[p] * nums[r] == nums[q] * nums[s]` +* There must be _at least_ **one** element between each pair of indices. In other words, `q - p > 1`, `r - q > 1` and `s - r > 1`. + +A subsequence is a sequence derived from the array by deleting zero or more elements without changing the order of the remaining elements. + +Return the _number_ of different **special** **subsequences** in `nums`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,6,1] + +**Output:** 1 + +**Explanation:** + +There is one special subsequence in `nums`. + +* `(p, q, r, s) = (0, 2, 4, 6)`: + * This corresponds to elements `(1, 3, 3, 1)`. + * `nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3` + * `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3` + +**Example 2:** + +**Input:** nums = [3,4,3,4,3,4,3,4] + +**Output:** 3 + +**Explanation:** + +There are three special subsequences in `nums`. + +* `(p, q, r, s) = (0, 2, 4, 6)`: + * This corresponds to elements `(3, 3, 3, 3)`. + * `nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9` + * `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9` +* `(p, q, r, s) = (1, 3, 5, 7)`: + * This corresponds to elements `(4, 4, 4, 4)`. + * `nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16` + * `nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16` +* `(p, q, r, s) = (0, 2, 5, 7)`: + * This corresponds to elements `(3, 3, 4, 4)`. + * `nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12` + * `nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12` + +**Constraints:** + +* `7 <= nums.length <= 1000` +* `1 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java new file mode 100644 index 000000000..d4ba6f443 --- /dev/null +++ b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java @@ -0,0 +1,40 @@ +package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements; + +// #Hard #2024_12_29_Time_57_(100.00%)_Space_44.55_(100.00%) + +public class Solution { + private final int mod = (int) (1e9 + 7); + + public int countGoodArrays(int n, int m, int k) { + long[] f = new long[n + 1]; + f[0] = 1; + f[1] = 1; + for (int i = 2; i < f.length; i++) { + f[i] = (f[i - 1] * i % mod); + } + long ans = comb(n - 1, k, f); + ans = ans * m % mod; + ans = ans * ex(m - 1, n - k - 1) % mod; + return (int) ans; + } + + private long ex(long b, long e) { + long ans = 1; + while (e > 0) { + if (e % 2 == 1) { + ans = (ans * b) % mod; + } + b = (b * b) % mod; + e = e >> 1; + } + return ans; + } + + private long comb(int n, int r, long[] f) { + return f[n] * (ff(f[r])) % mod * ff(f[n - r]) % mod; + } + + private long ff(long x) { + return ex(x, mod - 2); + } +} diff --git a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md new file mode 100644 index 000000000..8aebabcb3 --- /dev/null +++ b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md @@ -0,0 +1,50 @@ +3405\. Count the Number of Arrays with K Matching Adjacent Elements + +Hard + +You are given three integers `n`, `m`, `k`. A **good array** `arr` of size `n` is defined as follows: + +* Each element in `arr` is in the **inclusive** range `[1, m]`. +* _Exactly_ `k` indices `i` (where `1 <= i < n`) satisfy the condition `arr[i - 1] == arr[i]`. + +Return the number of **good arrays** that can be formed. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 3, m = 2, k = 1 + +**Output:** 4 + +**Explanation:** + +* There are 4 good arrays. They are `[1, 1, 2]`, `[1, 2, 2]`, `[2, 1, 1]` and `[2, 2, 1]`. +* Hence, the answer is 4. + +**Example 2:** + +**Input:** n = 4, m = 2, k = 2 + +**Output:** 6 + +**Explanation:** + +* The good arrays are `[1, 1, 1, 2]`, `[1, 1, 2, 2]`, `[1, 2, 2, 2]`, `[2, 1, 1, 1]`, `[2, 2, 1, 1]` and `[2, 2, 2, 1]`. +* Hence, the answer is 6. + +**Example 3:** + +**Input:** n = 5, m = 2, k = 0 + +**Output:** 2 + +**Explanation:** + +* The good arrays are `[1, 2, 1, 2, 1]` and `[2, 1, 2, 1, 2]`. Hence, the answer is 2. + +**Constraints:** + +* 1 <= n <= 105 +* 1 <= m <= 105 +* `0 <= k <= n - 1` \ No newline at end of file diff --git a/src/test/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.java b/src/test/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.java new file mode 100644 index 000000000..ee34484d8 --- /dev/null +++ b/src/test/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.java @@ -0,0 +1,22 @@ +package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumOperations() { + assertThat( + new Solution().minimumOperations(new int[][] {{3, 2}, {1, 3}, {3, 4}, {0, 1}}), + equalTo(15)); + } + + @Test + void minimumOperations2() { + assertThat( + new Solution().minimumOperations(new int[][] {{3, 2, 1}, {2, 1, 0}, {1, 2, 3}}), + equalTo(12)); + } +} diff --git a/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java b/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java new file mode 100644 index 000000000..b31a5ddfe --- /dev/null +++ b/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void answerString() { + assertThat(new Solution().answerString("dbca", 2), equalTo("dbc")); + } + + @Test + void answerString2() { + assertThat(new Solution().answerString("gggg", 4), equalTo("g")); + } +} diff --git a/src/test/java/g3401_3500/s3404_count_special_subsequences/SolutionTest.java b/src/test/java/g3401_3500/s3404_count_special_subsequences/SolutionTest.java new file mode 100644 index 000000000..fb62c30b6 --- /dev/null +++ b/src/test/java/g3401_3500/s3404_count_special_subsequences/SolutionTest.java @@ -0,0 +1,21 @@ +package g3401_3500.s3404_count_special_subsequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfSubsequences() { + assertThat( + new Solution().numberOfSubsequences(new int[] {1, 2, 3, 4, 3, 6, 1}), equalTo(1L)); + } + + @Test + void numberOfSubsequences2() { + assertThat( + new Solution().numberOfSubsequences(new int[] {3, 4, 3, 4, 3, 4, 3, 4}), + equalTo(3L)); + } +} diff --git a/src/test/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.java b/src/test/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.java new file mode 100644 index 000000000..cdd24f62a --- /dev/null +++ b/src/test/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.java @@ -0,0 +1,23 @@ +package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countGoodArrays() { + assertThat(new Solution().countGoodArrays(3, 2, 1), equalTo(4)); + } + + @Test + void countGoodArrays2() { + assertThat(new Solution().countGoodArrays(4, 2, 2), equalTo(6)); + } + + @Test + void countGoodArrays3() { + assertThat(new Solution().countGoodArrays(5, 2, 0), equalTo(2)); + } +} From 46e718c7258fac5c9113624dff813a139a411b35 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 29 Dec 2024 09:44:59 +0200 Subject: [PATCH 11/13] Fixed sonar --- .../Solution.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java index d4ba6f443..c803b7747 100644 --- a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java +++ b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java @@ -3,18 +3,18 @@ // #Hard #2024_12_29_Time_57_(100.00%)_Space_44.55_(100.00%) public class Solution { - private final int mod = (int) (1e9 + 7); + private final static int MOD = (int) (1e9 + 7); public int countGoodArrays(int n, int m, int k) { long[] f = new long[n + 1]; f[0] = 1; f[1] = 1; for (int i = 2; i < f.length; i++) { - f[i] = (f[i - 1] * i % mod); + f[i] = (f[i - 1] * i % MOD); } long ans = comb(n - 1, k, f); - ans = ans * m % mod; - ans = ans * ex(m - 1, n - k - 1) % mod; + ans = ans * m % MOD; + ans = ans * ex(m - 1L, n - k - 1L) % MOD; return (int) ans; } @@ -22,19 +22,19 @@ private long ex(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) { - ans = (ans * b) % mod; + ans = (ans * b) % MOD; } - b = (b * b) % mod; + b = (b * b) % MOD; e = e >> 1; } return ans; } private long comb(int n, int r, long[] f) { - return f[n] * (ff(f[r])) % mod * ff(f[n - r]) % mod; + return f[n] * (ff(f[r])) % MOD * ff(f[n - r]) % MOD; } private long ff(long x) { - return ex(x, mod - 2); + return ex(x, MOD - 2L); } } From 817d1b5e7620132d9070d6868ed388e698af39d9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 29 Dec 2024 09:46:55 +0200 Subject: [PATCH 12/13] Fixed format --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java index c803b7747..b7796cda8 100644 --- a/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java +++ b/src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.java @@ -3,7 +3,7 @@ // #Hard #2024_12_29_Time_57_(100.00%)_Space_44.55_(100.00%) public class Solution { - private final static int MOD = (int) (1e9 + 7); + private static final int MOD = (int) (1e9 + 7); public int countGoodArrays(int n, int m, int k) { long[] f = new long[n + 1]; From 3f88690adab7e4c396ee51a9cdce784415959f48 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 29 Dec 2024 09:56:50 +0200 Subject: [PATCH 13/13] Added tests --- .../SolutionTest.java | 5 +++++ .../SolutionTest.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java index 34e83fbcc..f9677980d 100644 --- a/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java +++ b/src/test/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.java @@ -20,4 +20,9 @@ void minLength2() { void minLength3() { assertThat(new Solution().minLength("0101", 0), equalTo(1)); } + + @Test + void minLength4() { + assertThat(new Solution().minLength("000", 2), equalTo(1)); + } } diff --git a/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java b/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java index b31a5ddfe..b72b80e36 100644 --- a/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java +++ b/src/test/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.java @@ -15,4 +15,9 @@ void answerString() { void answerString2() { assertThat(new Solution().answerString("gggg", 4), equalTo("g")); } + + @Test + void answerString3() { + assertThat(new Solution().answerString("a", 1), equalTo("a")); + } }