diff --git a/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/Solution.java b/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/Solution.java new file mode 100644 index 000000000..4e0d65b50 --- /dev/null +++ b/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/Solution.java @@ -0,0 +1,29 @@ +package g3101_3200.s3127_make_a_square_with_the_same_color; + +// #Easy #Array #Matrix #Enumeration #2024_05_02_Time_0_ms_(100.00%)_Space_41.7_MB_(64.59%) + +public class Solution { + public boolean canMakeSquare(char[][] grid) { + int n = grid.length; + int m = grid[0].length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < m - 1; j++) { + int countBlack = 0; + int countWhite = 0; + for (int k = i; k <= i + 1; k++) { + for (int l = j; l <= j + 1; l++) { + if (grid[k][l] == 'W') { + countWhite++; + } else { + countBlack++; + } + } + } + if (countBlack >= 3 || countWhite >= 3) { + return true; + } + } + } + return false; + } +} diff --git a/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md b/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md new file mode 100644 index 000000000..9aebee2d0 --- /dev/null +++ b/src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md @@ -0,0 +1,45 @@ +3127\. Make a Square with the Same Color + +Easy + +You are given a 2D matrix `grid` of size `3 x 3` consisting only of characters `'B'` and `'W'`. Character `'W'` represents the white color, and character `'B'` represents the black color. + +Your task is to change the color of **at most one** cell so that the matrix has a `2 x 2` square where all cells are of the same color. + +Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`. + +**Example 1:** + +**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","B"]] + +**Output:** true + +**Explanation:** + +It can be done by changing the color of the `grid[0][2]`. + +**Example 2:** + +**Input:** grid = [["B","W","B"],["W","B","W"],["B","W","B"]] + +**Output:** false + +**Explanation:** + +It cannot be done by changing at most one cell. + +**Example 3:** + +**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","W"]] + +**Output:** true + +**Explanation:** + +The `grid` already contains a `2 x 2` square of the same color. + +**Constraints:** + +* `grid.length == 3` +* `grid[i].length == 3` +* `grid[i][j]` is either `'W'` or `'B'`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3128_right_triangles/Solution.java b/src/main/java/g3101_3200/s3128_right_triangles/Solution.java new file mode 100644 index 000000000..07e3922d3 --- /dev/null +++ b/src/main/java/g3101_3200/s3128_right_triangles/Solution.java @@ -0,0 +1,26 @@ +package g3101_3200.s3128_right_triangles; + +// #Medium #Array #Hash_Table #Math #Counting #Combinatorics +// #2024_05_02_Time_6_ms_(100.00%)_Space_145.9_MB_(90.67%) + +public class Solution { + public long numberOfRightTriangles(int[][] grid) { + int n = grid.length; + int m = grid[0].length; + int[] columns = new int[n]; + int[] rows = new int[m]; + long sum = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + columns[i] += grid[i][j]; + rows[j] += grid[i][j]; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + sum += (long) grid[i][j] * (rows[j] - 1) * (columns[i] - 1); + } + } + return sum; + } +} diff --git a/src/main/java/g3101_3200/s3128_right_triangles/readme.md b/src/main/java/g3101_3200/s3128_right_triangles/readme.md new file mode 100644 index 000000000..dc4ed51df --- /dev/null +++ b/src/main/java/g3101_3200/s3128_right_triangles/readme.md @@ -0,0 +1,77 @@ +3128\. Right Triangles + +Medium + +You are given a 2D boolean matrix `grid`. + +Return an integer that is the number of **right triangles** that can be made with the 3 elements of `grid` such that **all** of them have a value of 1. + +**Note:** + +* A collection of 3 elements of `grid` is a **right triangle** if one of its elements is in the **same row** with another element and in the **same column** with the third element. The 3 elements do not have to be next to each other. + +**Example 1:** + +0 **1** 0 + +0 **1 1** + +0 1 0 + +0 1 0 + +0 **1 1** + +0 **1** 0 + +**Input:** grid = [[0,1,0],[0,1,1],[0,1,0]] + +**Output:** 2 + +**Explanation:** + +There are two right triangles. + +**Example 2:** + +1 0 0 0 + +0 1 0 1 + +1 0 0 0 + +**Input:** grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]] + +**Output:** 0 + +**Explanation:** + +There are no right triangles. + +**Example 3:** + +**1** 0 **1** + +**1** 0 0 + +1 0 0 + +**1** 0 **1** + +1 0 0 + +**1** 0 0 + +**Input:** grid = [[1,0,1],[1,0,0],[1,0,0]] + +**Output: **2 + +**Explanation:** + +There are two right triangles. + +**Constraints:** + +* `1 <= grid.length <= 1000` +* `1 <= grid[i].length <= 1000` +* `0 <= grid[i][j] <= 1` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/Solution.java b/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/Solution.java new file mode 100644 index 000000000..bab9ebd5a --- /dev/null +++ b/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/Solution.java @@ -0,0 +1,53 @@ +package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i; + +// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_44.1_MB_(98.38%) + +public class Solution { + private static final int MODULUS = (int) 1e9 + 7; + + private int add(int x, int y) { + return (x + y) % MODULUS; + } + + private int subtract(int x, int y) { + return (x + MODULUS - y) % MODULUS; + } + + private int multiply(int x, int y) { + return (int) ((long) x * y % MODULUS); + } + + public int numberOfStableArrays(int zero, int one, int limit) { + if (limit == 1) { + return Math.max(2 - Math.abs(zero - one), 0); + } + int max = Math.max(zero, one); + int min = Math.min(zero, one); + int[][] lcn = new int[max + 1][max + 1]; + int[] row0 = lcn[0]; + int[] row1; + int[] row2; + row0[0] = 1; + for (int s = 1, sLim = s - limit; s <= max; s++, sLim++) { + row2 = sLim > 0 ? lcn[sLim - 1] : new int[] {}; + row1 = row0; + row0 = lcn[s]; + int c; + for (c = (s - 1) / limit + 1; c <= sLim; c++) { + row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1]); + } + for (; c <= s; c++) { + row0[c] = add(row1[c], row1[c - 1]); + } + } + row1 = lcn[min]; + int result = 0; + int s0 = add(min < max ? row0[min + 1] : 0, row0[min]); + for (int c = min; c > 0; c--) { + int s1 = s0; + s0 = add(row0[c], row0[c - 1]); + result = add(result, multiply(row1[c], add(s0, s1))); + } + return result; + } +} diff --git a/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md b/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md new file mode 100644 index 000000000..4ad502708 --- /dev/null +++ b/src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md @@ -0,0 +1,51 @@ +3129\. Find All Possible Stable Binary Arrays I + +Medium + +You are given 3 positive integers `zero`, `one`, and `limit`. + +A binary array `arr` is called **stable** if: + +* The number of occurrences of 0 in `arr` is **exactly** `zero`. +* The number of occurrences of 1 in `arr` is **exactly** `one`. +* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1. + +Return the _total_ number of **stable** binary arrays. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** zero = 1, one = 1, limit = 2 + +**Output:** 2 + +**Explanation:** + +The two possible stable binary arrays are `[1,0]` and `[0,1]`, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2. + +**Example 2:** + +**Input:** zero = 1, one = 2, limit = 1 + +**Output:** 1 + +**Explanation:** + +The only possible stable binary array is `[1,0,1]`. + +Note that the binary arrays `[1,1,0]` and `[0,1,1]` have subarrays of length 2 with identical elements, hence, they are not stable. + +**Example 3:** + +**Input:** zero = 3, one = 3, limit = 2 + +**Output:** 14 + +**Explanation:** + +All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. + +**Constraints:** + +* `1 <= zero, one, limit <= 200` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/Solution.java b/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/Solution.java new file mode 100644 index 000000000..7d06b519e --- /dev/null +++ b/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/Solution.java @@ -0,0 +1,85 @@ +package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii; + +// #Hard #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_40.6_MB_(100.00%) + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + private static final int N = 1000; + private long[] factorial; + private long[] reverse; + + public int numberOfStableArrays(int zero, int one, int limit) { + if (factorial == null) { + factorial = new long[N + 1]; + reverse = new long[N + 1]; + factorial[0] = 1; + reverse[0] = 1; + long x = 1; + for (int i = 1; i <= N; ++i) { + x = (x * i) % MOD; + factorial[i] = (int) x; + reverse[i] = getInverse(x, MOD); + } + } + long ans = 0; + long[] s = new long[one + 1]; + int n = Math.min(zero, one) + 1; + for (int groups0 = (zero + limit - 1) / limit; groups0 <= Math.min(zero, n); ++groups0) { + long s0 = calc(groups0, zero, limit); + for (int groups1 = Math.max(groups0 - 1, (one + limit - 1) / limit); + groups1 <= Math.min(groups0 + 1, one); + ++groups1) { + long s1; + if (s[groups1] != 0) { + s1 = s[groups1]; + } else { + s1 = s[groups1] = calc(groups1, one, limit); + } + ans = (ans + s0 * s1 * (groups1 == groups0 ? 2 : 1)) % MOD; + } + } + return (int) ((ans + MOD) % MOD); + } + + long calc(int groups, int x, int limit) { + long s = 0; + int sign = 1; + for (int k = 0; k * limit <= x - groups && k <= groups; k++) { + s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD; + sign *= -1; + } + return s; + } + + public long comb(int n, int k) { + return (factorial[n] * reverse[k] % MOD) * reverse[n - k] % MOD; + } + + public long getInverse(long n, long mod) { + long p = mod; + long x = 1; + long y = 0; + while (p > 0) { + long quotient = n / p; + long remainder = n % p; + long tempY = x - quotient * y; + x = y; + y = tempY; + n = p; + p = remainder; + } + return ((x % mod) + mod) % mod; + } + + public long quickPower(long base, long power, long p) { + long result = 1; + while (power > 0) { + if ((power & 1) == 1) { + result = result * base % p; + } + power >>= 1; + base = base * base % p; + } + return result; + } +} diff --git a/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md b/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md new file mode 100644 index 000000000..735cc4370 --- /dev/null +++ b/src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md @@ -0,0 +1,49 @@ +3130\. Find All Possible Stable Binary Arrays II + +Hard + +You are given 3 positive integers `zero`, `one`, and `limit`. + +A binary array `arr` is called **stable** if: + +* The number of occurrences of 0 in `arr` is **exactly** `zero`. +* The number of occurrences of 1 in `arr` is **exactly** `one`. +* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1. + +Return the _total_ number of **stable** binary arrays. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** zero = 1, one = 1, limit = 2 + +**Output:** 2 + +**Explanation:** + +The two possible stable binary arrays are `[1,0]` and `[0,1]`. + +**Example 2:** + +**Input:** zero = 1, one = 2, limit = 1 + +**Output:** 1 + +**Explanation:** + +The only possible stable binary array is `[1,0,1]`. + +**Example 3:** + +**Input:** zero = 3, one = 3, limit = 2 + +**Output:** 14 + +**Explanation:** + +All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. + +**Constraints:** + +* `1 <= zero, one, limit <= 1000` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/Solution.java b/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/Solution.java new file mode 100644 index 000000000..9dc3fb980 --- /dev/null +++ b/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/Solution.java @@ -0,0 +1,18 @@ +package g3101_3200.s3131_find_the_integer_added_to_array_i; + +// #Easy #Array #2024_05_02_Time_0_ms_(100.00%)_Space_43_MB_(75.29%) + +public class Solution { + public int addedInteger(int[] nums1, int[] nums2) { + int n1 = nums1.length; + int s1 = 0; + int s2 = 0; + for (int i : nums1) { + s1 += i; + } + for (int i : nums2) { + s2 += i; + } + return (s2 - s1) / n1; + } +} diff --git a/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md b/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md new file mode 100644 index 000000000..7e3d5937f --- /dev/null +++ b/src/main/java/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md @@ -0,0 +1,47 @@ +3131\. Find the Integer Added to Array I + +Easy + +You are given two arrays of equal length, `nums1` and `nums2`. + +Each element in `nums1` has been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. + +As a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies. + +Return the integer `x`. + +**Example 1:** + +**Input:** nums1 = [2,6,4], nums2 = [9,7,5] + +**Output:** 3 + +**Explanation:** + +The integer added to each element of `nums1` is 3. + +**Example 2:** + +**Input:** nums1 = [10], nums2 = [5] + +**Output:** \-5 + +**Explanation:** + +The integer added to each element of `nums1` is -5. + +**Example 3:** + +**Input:** nums1 = [1,1,1,1], nums2 = [1,1,1,1] + +**Output:** 0 + +**Explanation:** + +The integer added to each element of `nums1` is 0. + +**Constraints:** + +* `1 <= nums1.length == nums2.length <= 100` +* `0 <= nums1[i], nums2[i] <= 1000` +* The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by adding `x` to each element of `nums1`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/Solution.java b/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/Solution.java new file mode 100644 index 000000000..38311bbae --- /dev/null +++ b/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/Solution.java @@ -0,0 +1,35 @@ +package g3101_3200.s3132_find_the_integer_added_to_array_ii; + +// #Medium #Array #Sorting #Two_Pointers #Enumeration +// #2024_05_02_Time_2_ms_(100.00%)_Space_42.3_MB_(96.46%) + +import java.util.Arrays; + +public class Solution { + public int minimumAddedInteger(int[] nums1, int[] nums2) { + Arrays.sort(nums1); + Arrays.sort(nums2); + if (checkOk(nums1, nums2, 2)) { + return nums2[0] - nums1[2]; + } else if (checkOk(nums1, nums2, 1)) { + return nums2[0] - nums1[1]; + } else { + return nums2[0] - nums1[0]; + } + } + + private boolean checkOk(int[] nums1, int[] nums2, int start) { + int i = 0; + int diff = nums2[i] - nums1[start]; + while (i < nums2.length) { + if (start - i > 2) { + return false; + } + if (nums2[i] == nums1[start] + diff) { + i++; + } + start++; + } + return i == nums2.length; + } +} diff --git a/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md b/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md new file mode 100644 index 000000000..1235d3d30 --- /dev/null +++ b/src/main/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md @@ -0,0 +1,38 @@ +3132\. Find the Integer Added to Array II + +Medium + +You are given two integer arrays `nums1` and `nums2`. + +From `nums1` two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. + +As a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies. + +Return the **minimum** possible integer `x` that achieves this equivalence. + +**Example 1:** + +**Input:** nums1 = [4,20,16,12,8], nums2 = [14,18,10] + +**Output:** \-2 + +**Explanation:** + +After removing elements at indices `[0,4]` and adding -2, `nums1` becomes `[18,14,10]`. + +**Example 2:** + +**Input:** nums1 = [3,5,5,3], nums2 = [7,7] + +**Output:** 2 + +**Explanation:** + +After removing elements at indices `[0,3]` and adding 2, `nums1` becomes `[7,7]`. + +**Constraints:** + +* `3 <= nums1.length <= 200` +* `nums2.length == nums1.length - 2` +* `0 <= nums1[i], nums2[i] <= 1000` +* The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by removing two elements and adding `x` to each element of `nums1`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3133_minimum_array_end/Solution.java b/src/main/java/g3101_3200/s3133_minimum_array_end/Solution.java new file mode 100644 index 000000000..789ec45cf --- /dev/null +++ b/src/main/java/g3101_3200/s3133_minimum_array_end/Solution.java @@ -0,0 +1,30 @@ +package g3101_3200.s3133_minimum_array_end; + +// #Medium #Bit_Manipulation #2024_05_02_Time_1_ms_(92.38%)_Space_40.8_MB_(58.58%) + +public class Solution { + public long minEnd(int n, int x) { + n = n - 1; + int[] xb = new int[64]; + int[] nb = new int[64]; + for (int i = 0; i < 32; i++) { + xb[i] = (x >> i) & 1; + nb[i] = (n >> i) & 1; + } + int i = 0; + int j = 0; + while (i < 64) { + if (xb[i] != 1) { + xb[i] = nb[j++]; + } + i++; + } + long ans = 0; + long p = 1; + for (i = 0; i < 64; i++) { + ans += (xb[i]) * p; + p *= 2; + } + return ans; + } +} diff --git a/src/main/java/g3101_3200/s3133_minimum_array_end/readme.md b/src/main/java/g3101_3200/s3133_minimum_array_end/readme.md new file mode 100644 index 000000000..84519a5f4 --- /dev/null +++ b/src/main/java/g3101_3200/s3133_minimum_array_end/readme.md @@ -0,0 +1,31 @@ +3133\. Minimum Array End + +Medium + +You are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`. + +Return the **minimum** possible value of `nums[n - 1]`. + +**Example 1:** + +**Input:** n = 3, x = 4 + +**Output:** 6 + +**Explanation:** + +`nums` can be `[4,5,6]` and its last element is 6. + +**Example 2:** + +**Input:** n = 2, x = 7 + +**Output:** 15 + +**Explanation:** + +`nums` can be `[7,15]` and its last element is 15. + +**Constraints:** + +* 1 <= n, x <= 108 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/Solution.java b/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/Solution.java new file mode 100644 index 000000000..7061ed30a --- /dev/null +++ b/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/Solution.java @@ -0,0 +1,52 @@ +package g3101_3200.s3134_find_the_median_of_the_uniqueness_array; + +// #Hard #Array #Hash_Table #Binary_Search #Sliding_Window +// #2024_05_02_Time_47_ms_(100.00%)_Space_56.8_MB_(91.38%) + +public class Solution { + public int medianOfUniquenessArray(int[] nums) { + int max = 0; + for (int x : nums) { + max = Math.max(max, x); + } + int n = nums.length; + long k = ((long) n * (n + 1) / 2 + 1) / 2; + int left = 0; + int right = n / 2; + while (left <= right) { + int mid = left + right >> 1; + if (check(nums, max, mid, k)) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return left; + } + + private boolean check(int[] nums, int max, int target, long k) { + long count = 0; + int distinct = 0; + int n = nums.length; + int left = 0; + int right = 0; + int[] freq = new int[max + 1]; + while (right < n) { + int x = nums[right++]; + if (++freq[x] == 1) { + distinct++; + } + while (distinct > target) { + x = nums[left++]; + if (--freq[x] == 0) { + distinct--; + } + } + count += right - left; + if (count >= k) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md b/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md new file mode 100644 index 000000000..14f3d22f3 --- /dev/null +++ b/src/main/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md @@ -0,0 +1,46 @@ +3134\. Find the Median of the Uniqueness Array + +Hard + +You are given an integer array `nums`. The **uniqueness array** of `nums` is the sorted array that contains the number of distinct elements of all the subarrays of `nums`. In other words, it is a sorted array consisting of `distinct(nums[i..j])`, for all `0 <= i <= j < nums.length`. + +Here, `distinct(nums[i..j])` denotes the number of distinct elements in the subarray that starts at index `i` and ends at index `j`. + +Return the **median** of the **uniqueness array** of `nums`. + +**Note** that the **median** of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the **smaller** of the two values is taken. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 1 + +**Explanation:** + +The uniqueness array of `nums` is `[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]` which is equal to `[1, 1, 1, 2, 2, 3]`. The uniqueness array has a median of 1. Therefore, the answer is 1. + +**Example 2:** + +**Input:** nums = [3,4,3,4,5] + +**Output:** 2 + +**Explanation:** + +The uniqueness array of `nums` is `[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. + +**Example 3:** + +**Input:** nums = [4,3,5,4] + +**Output:** 2 + +**Explanation:** + +The uniqueness array of `nums` is `[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3127_make_a_square_with_the_same_color/SolutionTest.java b/src/test/java/g3101_3200/s3127_make_a_square_with_the_same_color/SolutionTest.java new file mode 100644 index 000000000..7f881e591 --- /dev/null +++ b/src/test/java/g3101_3200/s3127_make_a_square_with_the_same_color/SolutionTest.java @@ -0,0 +1,35 @@ +package g3101_3200.s3127_make_a_square_with_the_same_color; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void canMakeSquare() { + assertThat( + new Solution() + .canMakeSquare( + new char[][] {{'B', 'W', 'B'}, {'B', 'W', 'W'}, {'B', 'W', 'B'}}), + equalTo(true)); + } + + @Test + void canMakeSquare2() { + assertThat( + new Solution() + .canMakeSquare( + new char[][] {{'B', 'W', 'B'}, {'W', 'B', 'W'}, {'B', 'W', 'B'}}), + equalTo(false)); + } + + @Test + void canMakeSquare3() { + assertThat( + new Solution() + .canMakeSquare( + new char[][] {{'B', 'W', 'B'}, {'B', 'W', 'W'}, {'B', 'W', 'W'}}), + equalTo(true)); + } +} diff --git a/src/test/java/g3101_3200/s3128_right_triangles/SolutionTest.java b/src/test/java/g3101_3200/s3128_right_triangles/SolutionTest.java new file mode 100644 index 000000000..2d59357c4 --- /dev/null +++ b/src/test/java/g3101_3200/s3128_right_triangles/SolutionTest.java @@ -0,0 +1,33 @@ +package g3101_3200.s3128_right_triangles; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfRightTriangles() { + assertThat( + new Solution() + .numberOfRightTriangles(new int[][] {{0, 1, 0}, {0, 1, 1}, {0, 1, 0}}), + equalTo(2L)); + } + + @Test + void numberOfRightTriangles2() { + assertThat( + new Solution() + .numberOfRightTriangles( + new int[][] {{1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}), + equalTo(0L)); + } + + @Test + void numberOfRightTriangles3() { + assertThat( + new Solution() + .numberOfRightTriangles(new int[][] {{1, 0, 1}, {1, 0, 0}, {1, 0, 0}}), + equalTo(2L)); + } +} diff --git a/src/test/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/SolutionTest.java b/src/test/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/SolutionTest.java new file mode 100644 index 000000000..6a64444b9 --- /dev/null +++ b/src/test/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfStableArrays() { + assertThat(new Solution().numberOfStableArrays(1, 1, 2), equalTo(2)); + } + + @Test + void numberOfStableArrays2() { + assertThat(new Solution().numberOfStableArrays(1, 2, 1), equalTo(1)); + } + + @Test + void numberOfStableArrays3() { + assertThat(new Solution().numberOfStableArrays(3, 3, 2), equalTo(14)); + } +} diff --git a/src/test/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/SolutionTest.java b/src/test/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/SolutionTest.java new file mode 100644 index 000000000..93bf9aaf1 --- /dev/null +++ b/src/test/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfStableArrays() { + assertThat(new Solution().numberOfStableArrays(1, 1, 2), equalTo(2)); + } + + @Test + void numberOfStableArrays2() { + assertThat(new Solution().numberOfStableArrays(1, 2, 1), equalTo(1)); + } + + @Test + void numberOfStableArrays3() { + assertThat(new Solution().numberOfStableArrays(3, 3, 2), equalTo(14)); + } +} diff --git a/src/test/java/g3101_3200/s3131_find_the_integer_added_to_array_i/SolutionTest.java b/src/test/java/g3101_3200/s3131_find_the_integer_added_to_array_i/SolutionTest.java new file mode 100644 index 000000000..fac6ef606 --- /dev/null +++ b/src/test/java/g3101_3200/s3131_find_the_integer_added_to_array_i/SolutionTest.java @@ -0,0 +1,26 @@ +package g3101_3200.s3131_find_the_integer_added_to_array_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void addedInteger() { + assertThat( + new Solution().addedInteger(new int[] {2, 6, 4}, new int[] {9, 7, 5}), equalTo(3)); + } + + @Test + void addedInteger2() { + assertThat(new Solution().addedInteger(new int[] {10}, new int[] {5}), equalTo(-5)); + } + + @Test + void addedInteger3() { + assertThat( + new Solution().addedInteger(new int[] {1, 1, 1, 1}, new int[] {1, 1, 1, 1}), + equalTo(0)); + } +} diff --git a/src/test/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/SolutionTest.java b/src/test/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/SolutionTest.java new file mode 100644 index 000000000..f42fb537d --- /dev/null +++ b/src/test/java/g3101_3200/s3132_find_the_integer_added_to_array_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3132_find_the_integer_added_to_array_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumAddedInteger() { + assertThat( + new Solution() + .minimumAddedInteger(new int[] {4, 20, 16, 12, 8}, new int[] {14, 18, 10}), + equalTo(-2)); + } + + @Test + void minimumAddedInteger2() { + assertThat( + new Solution().minimumAddedInteger(new int[] {3, 5, 5, 3}, new int[] {7, 7}), + equalTo(2)); + } +} diff --git a/src/test/java/g3101_3200/s3133_minimum_array_end/SolutionTest.java b/src/test/java/g3101_3200/s3133_minimum_array_end/SolutionTest.java new file mode 100644 index 000000000..87984cb83 --- /dev/null +++ b/src/test/java/g3101_3200/s3133_minimum_array_end/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3133_minimum_array_end; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minEnd() { + assertThat(new Solution().minEnd(3, 4), equalTo(6L)); + } + + @Test + void minEnd2() { + assertThat(new Solution().minEnd(2, 7), equalTo(15L)); + } +} diff --git a/src/test/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/SolutionTest.java b/src/test/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/SolutionTest.java new file mode 100644 index 000000000..ec761e960 --- /dev/null +++ b/src/test/java/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3134_find_the_median_of_the_uniqueness_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void medianOfUniquenessArray() { + assertThat(new Solution().medianOfUniquenessArray(new int[] {1, 2, 3}), equalTo(1)); + } + + @Test + void medianOfUniquenessArray2() { + assertThat(new Solution().medianOfUniquenessArray(new int[] {3, 4, 3, 4, 5}), equalTo(2)); + } + + @Test + void medianOfUniquenessArray3() { + assertThat(new Solution().medianOfUniquenessArray(new int[] {4, 3, 5, 4}), equalTo(2)); + } +}