From 67029d4b017c1d78547815f0e807b2b728996c9c Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 28 Jul 2026 08:27:45 +0700 Subject: [PATCH 01/22] Added tasks 3853-3875 --- .../Solution.java | 29 ++++ .../s3853_merge_close_characters/readme.md | 57 +++++++ .../Solution.java | 45 ++++++ .../readme.md | 60 +++++++ .../Solution.java | 38 +++++ .../readme.md | 48 ++++++ .../s3856_trim_trailing_vowels/Solution.java | 21 +++ .../s3856_trim_trailing_vowels/readme.md | 44 ++++++ .../Solution.java | 10 ++ .../readme.md | 123 +++++++++++++++ .../Solution.java | 28 ++++ .../readme.md | 48 ++++++ .../Solution.java | 80 ++++++++++ .../readme.md | 84 ++++++++++ .../s3861_minimum_capacity_box/Solution.java | 18 +++ .../s3861_minimum_capacity_box/readme.md | 47 ++++++ .../Solution.java | 25 +++ .../readme.md | 62 ++++++++ .../Solution.java | 53 +++++++ .../readme.md | 46 ++++++ .../Solution.java | 34 ++++ .../readme.md | 59 +++++++ .../Solution.java | 20 +++ .../s3866_first_unique_even_element/readme.md | 34 ++++ .../Solution.java | 41 +++++ .../readme.md | 127 +++++++++++++++ .../Solution.java | 29 ++++ .../readme.md | 57 +++++++ .../Solution.java | 148 ++++++++++++++++++ .../readme.md | 65 ++++++++ .../s3870_count_commas_in_range/Solution.java | 10 ++ .../s3870_count_commas_in_range/readme.md | 36 +++++ .../Solution.java | 25 +++ .../s3871_count_commas_in_range_ii/readme.md | 36 +++++ .../Solution.java | 44 ++++++ .../readme.md | 38 +++++ .../Solution.java | 80 ++++++++++ .../readme.md | 65 ++++++++ .../Solution.java | 10 ++ .../readme.md | 44 ++++++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 19 +++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ .../SolutionTest.java | 17 ++ 60 files changed, 2311 insertions(+) create mode 100644 src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java create mode 100644 src/main/java/g3801_3900/s3853_merge_close_characters/readme.md create mode 100644 src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java create mode 100644 src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/readme.md create mode 100644 src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java create mode 100644 src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/readme.md create mode 100644 src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java create mode 100644 src/main/java/g3801_3900/s3856_trim_trailing_vowels/readme.md create mode 100644 src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/Solution.java create mode 100644 src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md create mode 100644 src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/Solution.java create mode 100644 src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/readme.md create mode 100644 src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java create mode 100644 src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md create mode 100644 src/main/java/g3801_3900/s3861_minimum_capacity_box/Solution.java create mode 100644 src/main/java/g3801_3900/s3861_minimum_capacity_box/readme.md create mode 100644 src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/Solution.java create mode 100644 src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/readme.md create mode 100644 src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java create mode 100644 src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/readme.md create mode 100644 src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/Solution.java create mode 100644 src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/readme.md create mode 100644 src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java create mode 100644 src/main/java/g3801_3900/s3866_first_unique_even_element/readme.md create mode 100644 src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java create mode 100644 src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md create mode 100644 src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/Solution.java create mode 100644 src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/readme.md create mode 100644 src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java create mode 100644 src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/readme.md create mode 100644 src/main/java/g3801_3900/s3870_count_commas_in_range/Solution.java create mode 100644 src/main/java/g3801_3900/s3870_count_commas_in_range/readme.md create mode 100644 src/main/java/g3801_3900/s3871_count_commas_in_range_ii/Solution.java create mode 100644 src/main/java/g3801_3900/s3871_count_commas_in_range_ii/readme.md create mode 100644 src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/Solution.java create mode 100644 src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/readme.md create mode 100644 src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java create mode 100644 src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/readme.md create mode 100644 src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java create mode 100644 src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/readme.md create mode 100644 src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java diff --git a/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java b/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java new file mode 100644 index 000000000..234a3f7c7 --- /dev/null +++ b/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java @@ -0,0 +1,29 @@ +package g3801_3900.s3853_merge_close_characters; + +// #Medium #String #Hash_Table #Senior #Biweekly_Contest_177 +// #2026_07_27_Time_2_ms_(91.01%)_Space_44.66_MB_(52.91%) + +public class Solution { + public String mergeCharacters(String s, int k) { + StringBuilder result = new StringBuilder(); + result.ensureCapacity(s.length()); + int[] cnt = new int[26]; + for (int t = 0; t < s.length(); t++) { + char c = s.charAt(t); + int idx = c - 'a'; + if (cnt[idx] > 0) { + continue; + } + + result.append(c); + cnt[idx]++; + + if (result.length() > k) { + char drop = result.charAt(result.length() - k - 1); + cnt[drop - 'a']--; + } + } + + return result.toString(); + } +} diff --git a/src/main/java/g3801_3900/s3853_merge_close_characters/readme.md b/src/main/java/g3801_3900/s3853_merge_close_characters/readme.md new file mode 100644 index 000000000..da447b622 --- /dev/null +++ b/src/main/java/g3801_3900/s3853_merge_close_characters/readme.md @@ -0,0 +1,57 @@ +3853\. Merge Close Characters + +Medium + +You are given a string `s` consisting of lowercase English letters and an integer `k`. + +Two **equal** characters in the **current** string `s` are considered **close** if the distance between their indices is **at most** `k`. + +When two characters are **close**, the right one merges into the left. Merges happen **one at a time**, and after each merge, the string updates until no more merges are possible. + +Return the resulting string after performing all possible merges. + +**Note**: If multiple merges are possible, always merge the pair with the **smallest left** index. If multiple pairs share the smallest left index, choose the pair with the **smallest right** index. + +**Example 1:** + +**Input:** s = "abca", k = 3 + +**Output:** "abc" + +**Explanation:** + +* Characters `'a'` at indices `i = 0` and `i = 3` are close as `3 - 0 = 3 <= k`. +* Merge them into the left `'a'` and `s = "abc"`. +* No other equal characters are close, so no further merges occur. + +**Example 2:** + +**Input:** s = "aabca", k = 2 + +**Output:** "abca" + +**Explanation:** + +* Characters `'a'` at indices `i = 0` and `i = 1` are close as `1 - 0 = 1 <= k`. +* Merge them into the left `'a'` and `s = "abca"`. +* Now the remaining `'a'` characters at indices `i = 0` and `i = 3` are not close as `k < 3`, so no further merges occur. + +**Example 3:** + +**Input:** s = "yybyzybz", k = 2 + +**Output:** "ybzybz" + +**Explanation:** + +* Characters `'y'` at indices `i = 0` and `i = 1` are close as `1 - 0 = 1 <= k`. +* Merge them into the left `'y'` and `s = "ybyzybz"`. +* Now the characters `'y'` at indices `i = 0` and `i = 2` are close as `2 - 0 = 2 <= k`. +* Merge them into the left `'y'` and `s = "ybzybz"`. +* No other equal characters are close, so no further merges occur. + +**Constraints:** + +* `1 <= s.length <= 100` +* `1 <= k <= s.length` +* `s` consists of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java new file mode 100644 index 000000000..b0e0c6783 --- /dev/null +++ b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java @@ -0,0 +1,45 @@ +package g3801_3900.s3854_minimum_operations_to_make_array_parity_alternating; + +// #Medium #Array #Greedy #Staff #Biweekly_Contest_177 +// #2026_07_27_Time_9_ms_(100.00%)_Space_146.36_MB_(88.24%) + +public class Solution { + public int[] makeParityAlternating(int[] nums) { + if (nums.length == 1) return new int[] {0, 0}; + int zero = 0; + int one = 0; + for (int i = 0; i < nums.length; i++) { + if ((nums[i] & 1) != (i & 1)) { + zero++; + } + if ((nums[i] & 1) != ((i + 1) & 1)) { + one++; + } + } + int[] ans = new int[2]; + ans[0] = Math.min(one, zero); + + if (one == zero) { + ans[1] = Math.min(fun(nums, 0), fun(nums, 1)); + } else if (one > zero) { + ans[1] = fun(nums, 0); + } else ans[1] = fun(nums, 1); + return ans; + } + + private int fun(int[] nums, int parity) { + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + + for (int i = 0; i < nums.length; i++) { + if ((nums[i] & 1) == ((i + parity) & 1)) { + max = Math.max(max, nums[i]); + min = Math.min(min, nums[i]); + } else { + max = Math.max(max, nums[i] - 1); + min = Math.min(min, nums[i] + 1); + } + } + return Math.max(max - min, 1); + } +} diff --git a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/readme.md b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/readme.md new file mode 100644 index 000000000..bdccb7f98 --- /dev/null +++ b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/readme.md @@ -0,0 +1,60 @@ +3854\. Minimum Operations to Make Array Parity Alternating + +Medium + +You are given an integer array `nums`. + +An array is called **parity alternating** if for every index `i` where `0 <= i < n - 1`, `nums[i]` and `nums[i + 1]` have different parity (one is even and the other is odd). + +In one operation, you may choose any index `i` and either increase `nums[i]` by 1 or decrease `nums[i]` by 1. + +Return an integer array `answer` of length 2 where: + +* `answer[0]` is the **minimum** number of operations required to make the array parity alternating. +* `answer[1]` is the **minimum** possible value of `max(nums) - min(nums)` taken over all arrays that are parity alternating and can be obtained by performing **exactly** `answer[0]` operations. + +An array of length 1 is considered parity alternating. + +**Example 1:** + +**Input:** nums = [-2,-3,1,4] + +**Output:** [2,6] + +**Explanation:** + +Applying the following operations: + +* Increase `nums[2]` by 1, resulting in `nums = [-2, -3, 2, 4]`. +* Decrease `nums[3]` by 1, resulting in `nums = [-2, -3, 2, 3]`. + +The resulting array is parity alternating, and the value of `max(nums) - min(nums) = 3 - (-3) = 6` is the minimum possible among all parity alternating arrays obtainable using exactly 2 operations. + +**Example 2:** + +**Input:** nums = [0,2,-2] + +**Output:** [1,3] + +**Explanation:** + +Applying the following operation: + +* Decrease `nums[1]` by 1, resulting in `nums = [0, 1, -2]`. + +The resulting array is parity alternating, and the value of `max(nums) - min(nums) = 1 - (-2) = 3` is the minimum possible among all parity alternating arrays obtainable using exactly 1 operation. + +**Example 3:** + +**Input:** nums = [7] + +**Output:** [0,0] + +**Explanation:** + +No operations are required. The array is already parity alternating, and the value of `max(nums) - min(nums) = 7 - 7 = 0`, which is the minimum possible. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java new file mode 100644 index 000000000..2f4e52906 --- /dev/null +++ b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java @@ -0,0 +1,38 @@ +package g3801_3900.s3855_sum_of_k_digit_numbers_in_a_range; + +// #Hard #Math #Divide_and_Conquer #Number_Theory #Combinatorics #Senior_Staff #Biweekly_Contest_177 +// #2026_07_27_Time_2_ms_(90.59%)_Space_43.08_MB_(36.47%) + +public class Solution { + private static final int MOD = 1000000007; + + public int sumOfNumbers(int l, int r, int k) { + long count = r - (long) l + 1; + long sumRange = (l + r) * count / 2; + + long t1 = sumRange % MOD; + long t2 = power(count, (long) k - 1); + + long repunit = (power(10, k) - 1 + MOD) % MOD; + long inv9 = power(9, (long) MOD - 2); + long t3 = (repunit * inv9) % MOD; + + long ans = (t1 * t2) % MOD; + ans = (ans * t3) % MOD; + + return (int) ans; + } + + private long power(long base, long exp) { + long res = 1; + base %= MOD; + while (exp > 0) { + if (exp % 2 == 1) { + res = (res * base) % MOD; + } + base = (base * base) % MOD; + exp /= 2; + } + return res; + } +} diff --git a/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/readme.md b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/readme.md new file mode 100644 index 000000000..05a999ca4 --- /dev/null +++ b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/readme.md @@ -0,0 +1,48 @@ +3855\. Sum of K-Digit Numbers in a Range + +Hard + +You are given three integers `l`, `r`, and `k`. + +Consider all possible integers consisting of **exactly** `k` digits, where each digit is chosen independently from the integer range `[l, r]` (inclusive). If 0 is included in the range, leading zeros are allowed. + +Return an integer representing the **sum of all such numbers.** Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** l = 1, r = 2, k = 2 + +**Output:** 66 + +**Explanation:** + +* All numbers formed using `k = 2` digits in the range `[1, 2]` are `11, 12, 21, 22`. +* The total sum is `11 + 12 + 21 + 22 = 66`. + +**Example 2:** + +**Input:** l = 0, r = 1, k = 3 + +**Output:** 444 + +**Explanation:** + +* All numbers formed using `k = 3` digits in the range `[0, 1]` are `000, 001, 010, 011, 100, 101, 110, 111`. +* These numbers without leading zeros are `0, 1, 10, 11, 100, 101, 110, 111`. +* The total sum is 444. + +**Example 3:** + +**Input:** l = 5, r = 5, k = 10 + +**Output:** 555555520 + +**Explanation:** + +* 5555555555 is the only valid number consisting of `k = 10` digits in the range `[5, 5]`. +* The total sum is 5555555555 % (109 + 7) = 555555520. + +**Constraints:** + +* `0 <= l <= r <= 9` +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java new file mode 100644 index 000000000..e509e0a09 --- /dev/null +++ b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java @@ -0,0 +1,21 @@ +package g3801_3900.s3856_trim_trailing_vowels; + +// #Easy #String #Mid_Level #Weekly_Contest_491 +// #2026_07_27_Time_1_ms_(99.24%)_Space_44.18_MB_(85.98%) + +public class Solution { + public String trimTrailingVowels(String s) { + int i = s.length() - 1; + + while (i >= 0 + && (s.charAt(i) == 'a' + || s.charAt(i) == 'e' + || s.charAt(i) == 'i' + || s.charAt(i) == 'o' + || s.charAt(i) == 'u')) { + i--; + } + + return s.substring(0, i + 1); + } +} diff --git a/src/main/java/g3801_3900/s3856_trim_trailing_vowels/readme.md b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/readme.md new file mode 100644 index 000000000..e738b74f4 --- /dev/null +++ b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/readme.md @@ -0,0 +1,44 @@ +3856\. Trim Trailing Vowels + +Easy + +You are given a string `s` that consists of lowercase English letters. + +Return the string obtained by removing **all** trailing **vowels** from `s`. + +The **vowels** consist of the characters `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. + +**Example 1:** + +**Input:** s = "idea" + +**Output:** "id" + +**Explanation:** + +Removing "id**ea**", we obtain the string `"id"`. + +**Example 2:** + +**Input:** s = "day" + +**Output:** "day" + +**Explanation:** + +There are no trailing vowels in the string `"day"`. + +**Example 3:** + +**Input:** s = "aeiou" + +**Output:** "" + +**Explanation:** + +Removing "**aeiou**", we obtain the string `""`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists of only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/Solution.java b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/Solution.java new file mode 100644 index 000000000..6168972e0 --- /dev/null +++ b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/Solution.java @@ -0,0 +1,10 @@ +package g3801_3900.s3857_minimum_cost_to_split_into_ones; + +// #Medium #Dynamic_Programming #Math #Senior #Weekly_Contest_491 +// #2026_07_27_Time_0_ms_(100.00%)_Space_42.09_MB_(96.12%) + +public class Solution { + public int minCost(int n) { + return n * (n - 1) / 2; + } +} diff --git a/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md new file mode 100644 index 000000000..2a2a488d3 --- /dev/null +++ b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md @@ -0,0 +1,123 @@ +3857\. Minimum Cost to Split into Ones + +Medium + +You are given an integer `n`. + +In one operation, you may split an integer `x` into two positive integers `a` and `b` such that `a + b = x`. + +The cost of this operation is `a * b`. + +Return an integer denoting the **minimum** total cost required to split the integer `n` into `n` ones. + +**Example 1:** + +**Input:** n = 3 + +**Output:** 3 + +**Explanation:** + +One optimal set of operations is: + +`x` + +`a` + +`b` + +`a + b` + +`a * b` + +Cost + +3 + +1 + +2 + +3 + +2 + +2 + +2 + +1 + +1 + +2 + +1 + +1 + +Thus, the minimum total cost is `2 + 1 = 3`. + +**Example 2:** + +**Input:** n = 4 + +**Output:** 6 + +**Explanation:** + +One optimal set of operations is: + +`x` + +`a` + +`b` + +`a + b` + +`a * b` + +Cost + +4 + +2 + +2 + +4 + +4 + +4 + +2 + +1 + +1 + +2 + +1 + +1 + +2 + +1 + +1 + +2 + +1 + +1 + +Thus, the minimum total cost is `4 + 1 + 1 = 6`. + +**Constraints:** + +* `1 <= n <= 500` \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/Solution.java b/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/Solution.java new file mode 100644 index 000000000..bf71afa92 --- /dev/null +++ b/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/Solution.java @@ -0,0 +1,28 @@ +package g3801_3900.s3858_minimum_bitwise_or_from_grid; + +// #Medium #Array #Greedy #Matrix #Bit_Manipulation #Staff #Weekly_Contest_491 +// #2026_07_27_Time_4_ms_(81.16%)_Space_136.60_MB_(59.42%) + +public class Solution { + public int minimumOR(int[][] grid) { + int res = 0; + for (int bi = 20; bi >= 0; --bi) { + int b = 1 << bi; + int mask = res | (b - 1); + for (int[] r : grid) { + boolean rowAllBad = true; + for (int a : r) { + if ((a & mask) == a) { + rowAllBad = false; + break; + } + } + if (rowAllBad) { + res |= b; + break; + } + } + } + return res; + } +} diff --git a/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/readme.md b/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/readme.md new file mode 100644 index 000000000..399703469 --- /dev/null +++ b/src/main/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/readme.md @@ -0,0 +1,48 @@ +3858\. Minimum Bitwise OR From Grid + +Medium + +You are given a 2D integer array `grid` of size `m x n`. + +You must select **exactly one** integer from each row of the grid. + +Return an integer denoting the **minimum possible bitwise OR** of the selected integers from each row. + +**Example 1:** + +**Input:** grid = [[1,5],[2,4]] + +**Output:** 3 + +**Explanation:** + +* Choose 1 from the first row and 2 from the second row. +* The bitwise OR of `1 | 2 = 3`, which is the minimum possible. + +**Example 2:** + +**Input:** grid = [[3,5],[6,4]] + +**Output:** 5 + +**Explanation:** + +* Choose 5 from the first row and 4 from the second row. +* The bitwise OR of `5 | 4 = 5`, which is the minimum possible. + +**Example 3:** + +**Input:** grid = [[7,9,8]] + +**Output:** 7 + +**Explanation:** + +* Choosing 7 gives the minimum bitwise OR. + +**Constraints:** + +* 1 <= m == grid.length <= 105 +* 1 <= n == grid[i].length <= 105 +* m * n <= 105 +* 1 <= grid[i][j] <= 105 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java new file mode 100644 index 000000000..27d4742bc --- /dev/null +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -0,0 +1,80 @@ +package g3801_3900.s3859_count_subarrays_with_k_distinct_integers; + +// #Hard #Array #Hash_Table #Counting #Sliding_Window #Senior_Staff #Weekly_Contest_491 +// #2026_07_27_Time_36_ms_(88.89%)_Space_77.36_MB_(87.65%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + + public long countSubarrays(int[] nums, int k, int m) { + int left = 0; + int p = 0; + long subArr = 0; + + HashMap map = new HashMap<>(); + int valid = 0; + + for (int val : nums) { + map.put(val, map.getOrDefault(val, 0) + 1); + + if (map.get(val) == m) { + valid++; + } + + WindowState state = shrinkDistinct(nums, left, p, k, m, map, valid); + left = state.left(); + p = state.p(); + valid = state.valid(); + + WindowState duplicateState = trimDuplicates(nums, left, p, m, map); + left = duplicateState.left(); + p = duplicateState.p(); + + if (map.size() == k && valid == k) { + subArr += 1 + p; + } + } + + return subArr; + } + + private WindowState shrinkDistinct( + int[] nums, int left, int p, int k, int m, Map map, int valid) { + + while (map.size() > k) { + int lv = nums[left]; + + if (map.get(lv) == m) { + valid--; + } + + map.put(lv, map.get(lv) - 1); + + if (map.get(lv) == 0) { + map.remove(lv); + } + + left++; + p = 0; + } + + return new WindowState(left, p, valid); + } + + private WindowState trimDuplicates( + int[] nums, int left, int p, int m, Map map) { + + while (map.size() > 0 && map.get(nums[left]) > m) { + int lv = nums[left]; + map.put(lv, map.get(lv) - 1); + left++; + p++; + } + + return new WindowState(left, p, 0); + } + + private record WindowState(int left, int p, int valid) {} +} diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md new file mode 100644 index 000000000..6434e33b7 --- /dev/null +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md @@ -0,0 +1,84 @@ +3859\. Count Subarrays With K Distinct Integers + +Hard + +You are given an integer array `nums` and two integers `k` and `m`. + +Return an integer denoting the count of **non-empty subarrays** of `nums` such that: + +* The subarray contains **exactly** `k` **distinct** integers. +* Within the subarray, each **distinct** integer appears **at least** `m` times. + +**Example 1:** + +**Input:** nums = [1,2,1,2,2], k = 2, m = 2 + +**Output:** 2 + +**Explanation:** + +The possible subarrays with `k = 2` distinct integers, each appearing at least `m = 2` times are: + +Subarray + +Distinct +numbers + +Frequency + +[1, 2, 1, 2] + +{1, 2} → 2 + +{1: 2, 2: 2} + +[1, 2, 1, 2, 2] + +{1, 2} → 2 + +{1: 2, 2: 3} + +Thus, the answer is 2. + +**Example 2:** + +**Input:** nums = [3,1,2,4], k = 2, m = 1 + +**Output:** 3 + +**Explanation:** + +The possible subarrays with `k = 2` distinct integers, each appearing at least `m = 1` times are: + +Subarray + +Distinct +numbers + +Frequency + +[3, 1] + +{3, 1} → 2 + +{3: 1, 1: 1} + +[1, 2] + +{1, 2} → 2 + +{1: 1, 2: 1} + +[2, 4] + +{2, 4} → 2 + +{2: 1, 4: 1} + +Thus, the answer is 3. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* `1 <= k, m <= nums.length` \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3861_minimum_capacity_box/Solution.java b/src/main/java/g3801_3900/s3861_minimum_capacity_box/Solution.java new file mode 100644 index 000000000..354b37357 --- /dev/null +++ b/src/main/java/g3801_3900/s3861_minimum_capacity_box/Solution.java @@ -0,0 +1,18 @@ +package g3801_3900.s3861_minimum_capacity_box; + +// #Easy #Array #Mid_Level #Weekly_Contest_492 +// #2026_07_27_Time_0_ms_(100.00%)_Space_44.42_MB_(84.62%) + +public class Solution { + public int minimumIndex(int[] capacity, int itemSize) { + int res = Integer.MAX_VALUE; + int idx = -1; + for (int i = 0; i < capacity.length; i++) { + if (itemSize <= capacity[i] && capacity[i] < res) { + res = capacity[i]; + idx = i; + } + } + return idx; + } +} diff --git a/src/main/java/g3801_3900/s3861_minimum_capacity_box/readme.md b/src/main/java/g3801_3900/s3861_minimum_capacity_box/readme.md new file mode 100644 index 000000000..c08343334 --- /dev/null +++ b/src/main/java/g3801_3900/s3861_minimum_capacity_box/readme.md @@ -0,0 +1,47 @@ +3861\. Minimum Capacity Box + +Easy + +You are given an integer array `capacity`, where `capacity[i]` represents the capacity of the ith box, and an integer `itemSize` representing the size of an item. + +The ith box can store the item if `capacity[i] >= itemSize`. + +Return an integer denoting the index of the box with the **minimum** capacity that can store the item. If multiple such boxes exist, return the **smallest index**. + +If no box can store the item, return -1. + +**Example 1:** + +**Input:** capacity = [1,5,3,7], itemSize = 3 + +**Output:** 2 + +**Explanation:** + +The box at index 2 has a capacity of 3, which is the minimum capacity that can store the item. Thus, the answer is 2. + +**Example 2:** + +**Input:** capacity = [3,5,4,3], itemSize = 2 + +**Output:** 0 + +**Explanation:** + +The minimum capacity that can store the item is 3, and it appears at indices 0 and 3. Thus, the answer is 0. + +**Example 3:** + +**Input:** capacity = [4], itemSize = 5 + +**Output:** \-1 + +**Explanation:** + +No box has enough capacity to store the item, so the answer is -1. + +**Constraints:** + +* `1 <= capacity.length <= 100` +* `1 <= capacity[i] <= 100` +* `1 <= itemSize <= 100` \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/Solution.java b/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/Solution.java new file mode 100644 index 000000000..d93e993f2 --- /dev/null +++ b/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/Solution.java @@ -0,0 +1,25 @@ +package g3801_3900.s3862_find_the_smallest_balanced_index; + +// #Medium #Array #Prefix_Sum #Senior #Weekly_Contest_492 +// #2026_07_27_Time_3_ms_(100.00%)_Space_143.54_MB_(51.17%) + +public class Solution { + public int smallestBalancedIndex(int[] nums) { + long lsum = 0; + for (int x : nums) { + lsum += x; + } + long rprod = 1; + for (int i = nums.length - 1; i >= 0; --i) { + lsum -= nums[i]; + if (lsum == rprod) { + return i; + } + if (rprod > lsum / nums[i]) { + break; + } + rprod *= nums[i]; + } + return -1; + } +} diff --git a/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/readme.md b/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/readme.md new file mode 100644 index 000000000..8aea09b66 --- /dev/null +++ b/src/main/java/g3801_3900/s3862_find_the_smallest_balanced_index/readme.md @@ -0,0 +1,62 @@ +3862\. Find the Smallest Balanced Index + +Medium + +You are given an integer array `nums`. + +An index `i` is **balanced** if the sum of elements **strictly** to the left of `i` equals the product of elements **strictly** to the right of `i`. + +If there are no elements to the left, the sum is considered as 0. Similarly, if there are no elements to the right, the product is considered as 1. + +Return an integer denoting the **smallest** balanced index. If no balanced index exists, return -1. + +**Example 1:** + +**Input:** nums = [2,1,2] + +**Output:** 1 + +**Explanation:** + +For index `i = 1`: + +* Left sum = `nums[0] = 2` +* Right product = `nums[2] = 2` +* Since the left sum equals the right product, index 1 is balanced. + +No smaller index satisfies the condition, so the answer is 1. + +**Example 2:** + +**Input:** nums = [2,8,2,2,5] + +**Output:** 2 + +**Explanation:** + +For index `i = 2`: + +* Left sum = `2 + 8 = 10` +* Right product = `2 * 5 = 10` +* Since the left sum equals the right product, index 2 is balanced. + +No smaller index satisfies the condition, so the answer is 2. + +**Example 3:** + +**Input:** nums = [1] + +**Output:** \-1 + +For index `i = 0`: + +* The left side is empty, so the left sum is 0. +* The right side is empty, so the right product is 1. +* Since the left sum does not equal the right product, index 0 is not balanced. + +Therefore, no balanced index exists and the answer is -1. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java new file mode 100644 index 000000000..d415ed482 --- /dev/null +++ b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java @@ -0,0 +1,53 @@ +package g3801_3900.s3863_minimum_operations_to_sort_a_string; + +// #Medium #String #Staff #Weekly_Contest_492 +// #2026_07_27_Time_15_ms_(74.44%)_Space_47.98_MB_(56.67%) + +public class Solution { + + public int minOperations(String s) { + int n = s.length(); + + if (n == 1) { + return 0; + } + + if (n == 2) { + return s.charAt(0) > s.charAt(1) ? -1 : 0; + } + + char min = 'z'; + char max = 'a'; + char first = s.charAt(0); + char last = s.charAt(n - 1); + char prev = 'a'; + int[] cnt = new int[26]; + boolean sorted = true; + + for (char c : s.toCharArray()) { + sorted &= prev <= c; + min = (char) Math.min(min, c); + max = (char) Math.max(max, c); + prev = c; + cnt[c - 'a']++; + } + + if (sorted) { + return 0; + } + + return calculateOperations(first, last, min, max, cnt); + } + + private int calculateOperations(char first, char last, char min, char max, int[] cnt) { + if (first == min || last == max) { + return 1; + } + + if (first != max || last != min) { + return 2; + } + + return cnt[max - 'a'] > 1 || cnt[min - 'a'] > 1 ? 2 : 3; + } +} diff --git a/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/readme.md b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/readme.md new file mode 100644 index 000000000..8d53c9ecb --- /dev/null +++ b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/readme.md @@ -0,0 +1,46 @@ +3863\. Minimum Operations to Sort a String + +Medium + +You are given a string `s` consisting of lowercase English letters. + +In one operation, you can select any **substring** of `s` that is **not** the entire string and **sort** it in **non-descending alphabetical** order. + +Return the **minimum** number of operations required to make `s` sorted in **non-descending** order. If it is not possible, return -1. + +**Example 1:** + +**Input:** s = "dog" + +**Output:** 1 + +**Explanation:** + +* Sort substring `"og"` to `"go"`. +* Now, `s = "dgo"`, which is sorted in ascending order. Thus, the answer is 1. + +**Example 2:** + +**Input:** s = "card" + +**Output:** 2 + +**Explanation:** + +* Sort substring `"car"` to `"acr"`, so `s = "acrd"`. +* Sort substring `"rd"` to `"dr"`, making `s = "acdr"`, which is sorted in ascending order. Thus, the answer is 2. + +**Example 3:** + +**Input:** s = "gf" + +**Output:** \-1 + +**Explanation:** + +* It is impossible to sort `s` under the given constraints. Thus, the answer is -1. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists of only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/Solution.java b/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/Solution.java new file mode 100644 index 000000000..0442b3c88 --- /dev/null +++ b/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/Solution.java @@ -0,0 +1,34 @@ +package g3801_3900.s3864_minimum_cost_to_partition_a_binary_string; + +// #Hard #String #Prefix_Sum #Divide_and_Conquer #Senior_Staff #Weekly_Contest_492 +// #2026_07_28_Time_28_ms_(100.00%)_Space_47.92_MB_(60.00%) + +public class Solution { + private int encCost; + private int flatCost; + private int[] pre; + + public long minCost(String s, int encCost, int flatCost) { + pre = new int[s.length() + 1]; + for (int i = 1; i < pre.length; i++) { + pre[i] = pre[i - 1] + (s.charAt(i - 1) - '0'); + } + this.encCost = encCost; + this.flatCost = flatCost; + return helper(0, s.length()); + } + + private long helper(int l, int r) { + int gap = r - l; + int x = pre[r] - pre[l]; + if (x == 0) { + return flatCost; + } + long cost = (long) gap * x * encCost; + if (gap % 2 == 0) { + int mid = l + (r - l) / 2; + cost = Math.min(cost, helper(l, mid) + helper(mid, r)); + } + return cost; + } +} diff --git a/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/readme.md b/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/readme.md new file mode 100644 index 000000000..b80e44215 --- /dev/null +++ b/src/main/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/readme.md @@ -0,0 +1,59 @@ +3864\. Minimum Cost to Partition a Binary String + +Hard + +You are given a binary string `s` and two integers `encCost` and `flatCost`. + +For each index `i`, `s[i] = '1'` indicates that the ith element is sensitive, and `s[i] = '0'` indicates that it is not. + +The string must be partitioned into **segments**. Initially, the entire string forms a single segment. + +For a segment of length `L` containing `X` sensitive elements: + +* If `X = 0`, the cost is `flatCost`. +* If `X > 0`, the cost is `L * X * encCost`. + +If a segment has **even length**, you may split it into **two contiguous segments** of **equal** length and the cost of this split is the **sum** of **costs** of the resulting segments. + +Return an integer denoting the **minimum possible total cost** over all valid partitions. + +**Example 1:** + +**Input:** s = "1010", encCost = 2, flatCost = 1 + +**Output:** 6 + +**Explanation:** + +* The entire string `s = "1010"` has length 4 and contains 2 sensitive elements, giving a cost of `4 * 2 * 2 = 16`. +* Since the length is even, it can be split into `"10"` and `"10"`. Each segment has length 2 and contains 1 sensitive element, so each costs `2 * 1 * 2 = 4`, giving a total of 8. +* Splitting both segments into four single-character segments yields the segments `"1"`, `"0"`, `"1"`, and `"0"`. A segment containing `"1"` has length 1 and exactly one sensitive element, giving a cost of `1 * 1 * 2 = 2`, while a segment containing `"0"` has no sensitive elements and therefore costs `flatCost = 1`. +* The total cost is thus `2 + 1 + 2 + 1 = 6`, which is the minimum possible total cost. + +**Example 2:** + +**Input:** s = "1010", encCost = 3, flatCost = 10 + +**Output:** 12 + +**Explanation:** + +* The entire string `s = "1010"` has length 4 and contains 2 sensitive elements, giving a cost of `4 * 2 * 3 = 24`. +* Since the length is even, it can be split into two segments `"10"` and `"10"`. +* Each segment has length 2 and contains one sensitive element, so each costs `2 * 1 * 3 = 6`, giving a total of 12, which is the minimum possible total cost. + +**Example 3:** + +**Input:** s = "00", encCost = 1, flatCost = 2 + +**Output:** 2 + +**Explanation:** + +The string `s = "00"` has length 2 and contains no sensitive elements, so storing it as a single segment costs `flatCost = 2`, which is the minimum possible total cost. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists only of `'0'` and `'1'`. +* 1 <= encCost, flatCost <= 105 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java b/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java new file mode 100644 index 000000000..5bc065f40 --- /dev/null +++ b/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java @@ -0,0 +1,20 @@ +package g3801_3900.s3866_first_unique_even_element; + +// #Easy #Array #Hash_Table #Counting #Mid_Level #Biweekly_Contest_178 +// #2026_07_28_Time_1_ms_(99.26%)_Space_46.46_MB_(18.62%) + +public class Solution { + public int firstUniqueEven(int[] nums) { + int[] arr = new int[100]; + for (int num : nums) { + arr[num - 1]++; + } + + for (int num : nums) { + if (num % 2 == 0 && (arr[num - 1] == 1)) { + return num; + } + } + return -1; + } +} diff --git a/src/main/java/g3801_3900/s3866_first_unique_even_element/readme.md b/src/main/java/g3801_3900/s3866_first_unique_even_element/readme.md new file mode 100644 index 000000000..e3e200991 --- /dev/null +++ b/src/main/java/g3801_3900/s3866_first_unique_even_element/readme.md @@ -0,0 +1,34 @@ +3866\. First Unique Even Element + +Easy + +You are given an integer array `nums`. + +Return an integer denoting the first **even** integer (earliest by array index) that appears **exactly** once in `nums`. If no such integer exists, return -1. + +An integer `x` is considered **even** if it is divisible by 2. + +**Example 1:** + +**Input:** nums = [3,4,2,5,4,6] + +**Output:** 2 + +**Explanation:** + +Both 2 and 6 are even and they appear exactly once. Since 2 occurs first in the array, the answer is 2. + +**Example 2:** + +**Input:** nums = [4,4] + +**Output:** \-1 + +**Explanation:** + +No even integer appears exactly once, so return -1. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java new file mode 100644 index 000000000..e3fe262eb --- /dev/null +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java @@ -0,0 +1,41 @@ +package g3801_3900.s3867_sum_of_gcd_of_formed_pairs; + +// #Medium #Array #Math #Sorting #Two_Pointers #Simulation #Number_Theory #Senior +// #Biweekly_Contest_178 #2026_07_28_Time_53_ms_(92.50%)_Space_107.82_MB_(83.19%) + +import java.util.Arrays; + +public class Solution { + public long gcdSum(int[] nums) { + int[] prefixGcd = new int[nums.length]; + int max = -1; + + for (int i = 0; i < nums.length; i++) { + max = Math.max(max, nums[i]); + prefixGcd[i] = gcd(max, nums[i]); + } + + Arrays.sort(prefixGcd); + + long sum = 0; + int i = 0; + int j = nums.length - 1; + + while (i < j) { + sum += gcd(prefixGcd[i], prefixGcd[j]); + i++; + j--; + } + + return sum; + } + + private int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } +} diff --git a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md new file mode 100644 index 000000000..6c0c25064 --- /dev/null +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md @@ -0,0 +1,127 @@ +3867\. Sum of GCD of Formed Pairs + +Medium + +You are given an integer array `nums` of length `n`. + +Construct an array `prefixGcd` where for each index `i`: + +* Let mxi = max(nums[0], nums[1], ..., nums[i]). +* prefixGcd[i] = gcd(nums[i], mxi). + +After constructing `prefixGcd`: + +* Sort `prefixGcd` in **non-decreasing** order. +* Form pairs by taking the **smallest unpaired** element and the **largest unpaired** element. +* Repeat this process until no more pairs can be formed. +* For each formed pair, **compute** the `gcd` of the two elements. +* If `n` is odd, the **middle** element in the `prefixGcd` array remains **unpaired** and should be ignored. + +Return an integer denoting the **sum of the GCD** values of all formed pairs. + +The term `gcd(a, b)` denotes the **greatest common divisor** of `a` and `b`. + +**Example 1:** + +**Input:** nums = [2,6,4] + +**Output:** 2 + +**Explanation:** + +Construct `prefixGcd`: + +`i` + +`nums[i]` + +mxi + +`prefixGcd[i]` + +0 + +2 + +2 + +2 + +1 + +6 + +6 + +6 + +2 + +4 + +6 + +2 + +`prefixGcd = [2, 6, 2]`. After sorting, it forms `[2, 2, 6]`. + +Pair the smallest and largest elements: `gcd(2, 6) = 2`. The remaining middle element 2 is ignored. Thus, the sum is 2. + +**Example 2:** + +**Input:** nums = [3,6,2,8] + +**Output:** 5 + +**Explanation:** + +Construct `prefixGcd`: + +`i` + +`nums[i]` + +mxi + +`prefixGcd[i]` + +0 + +3 + +3 + +3 + +1 + +6 + +6 + +6 + +2 + +2 + +6 + +2 + +3 + +8 + +8 + +8 + +`prefixGcd = [3, 6, 2, 8]`. After sorting, it forms `[2, 3, 6, 8]`. + +Form pairs: `gcd(2, 8) = 2` and `gcd(3, 6) = 3`. Thus, the sum is `2 + 3 = 5`. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/Solution.java b/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/Solution.java new file mode 100644 index 000000000..ea58289db --- /dev/null +++ b/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/Solution.java @@ -0,0 +1,29 @@ +package g3801_3900.s3868_minimum_cost_to_equalize_arrays_using_swaps; + +// #Medium #Array #Hash_Table #Greedy #Counting #Senior #Biweekly_Contest_178 +// #2026_07_28_Time_78_ms_(94.70%)_Space_151.84_MB_(77.48%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minCost(int[] a, int[] b) { + Map m = new HashMap<>(); + for (int x : a) { + m.merge(x, 1, Integer::sum); + } + for (int x : b) { + m.merge(x, -1, Integer::sum); + } + int res = 0; + for (int v : m.values()) { + if (v % 2 != 0) { + return -1; + } + if (v > 0) { + res += v / 2; + } + } + return res; + } +} diff --git a/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/readme.md b/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/readme.md new file mode 100644 index 000000000..e87e20ec3 --- /dev/null +++ b/src/main/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/readme.md @@ -0,0 +1,57 @@ +3868\. Minimum Cost to Equalize Arrays Using Swaps + +Medium + +You are given two integer arrays `nums1` and `nums2` of size `n`. + +You can perform the following two operations any number of times on these two arrays: + +* **Swap within the same array**: Choose two indices `i` and `j`. Then, choose either to swap `nums1[i]` and `nums1[j]`, or `nums2[i]` and `nums2[j]`. This operation is **free of charge**. +* **Swap between two arrays**: Choose an index `i`. Then, swap `nums1[i]` and `nums2[i]`. This operation **incurs a cost of 1**. + +Return an integer denoting the **minimum cost** to make `nums1` and `nums2` **identical**. If this is not possible, return -1. + +**Example 1:** + +**Input:** nums1 = [10,20], nums2 = [20,10] + +**Output:** 0 + +**Explanation:** + +* Swap `nums2[0] = 20` and `nums2[1] = 10`. + * `nums2` becomes `[10, 20]`. + * This operation is free of charge. +* `nums1` and `nums2` are now identical. The cost is 0. + +**Example 2:** + +**Input:** nums1 = [10,10], nums2 = [20,20] + +**Output:** 1 + +**Explanation:** + +* Swap `nums1[0] = 10` and `nums2[0] = 20`. + * `nums1` becomes `[20, 10]`. + * `nums2` becomes `[10, 20]`. + * This operation costs 1. +* Swap `nums2[0] = 10` and `nums2[1] = 20`. + * `nums2` becomes `[20, 10]`. + * This operation is free of charge. +* `nums1` and `nums2` are now identical. The cost is 1. + +**Example 3:** + +**Input:** nums1 = [10,20], nums2 = [30,40] + +**Output:** \-1 + +**Explanation:** + +It is impossible to make the two arrays identical. Therefore, the answer is -1. + +**Constraints:** + +* 2 <= n == nums1.length == nums2.length <= 8 * 104 +* 1 <= nums1[i], nums2[i] <= 8 * 104 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java new file mode 100644 index 000000000..b32e5f4f7 --- /dev/null +++ b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java @@ -0,0 +1,148 @@ +package g3801_3900.s3869_count_fancy_numbers_in_a_range; + +// #Hard #Dynamic_Programming #Math #Senior_Staff #Biweekly_Contest_178 +// #2026_07_28_Time_86_ms_(96.00%)_Space_47.18_MB_(90.00%) + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Solution { + private char[] low; + private char[] high; + private long[][][][] dp; + private boolean[] good; + + public long countFancy(long l, long r) { + initBounds(l, r); + initGood(); + initDp(); + + long ans = dfs(0, 0, 1, 1); + + Set seen = new HashSet<>(); + addIncreasingNumbers(seen, l, r); + addDecreasingNumbers(seen, l, r); + + return ans + seen.size(); + } + + private void initBounds(long l, long r) { + String left = Long.toString(l); + String right = Long.toString(r); + int n = right.length(); + + left = "0".repeat(n - left.length()) + left; + + low = left.toCharArray(); + high = right.toCharArray(); + } + + private void initGood() { + good = new boolean[136]; + for (int i = 1; i <= 135; i++) { + good[i] = isGood(i); + } + } + + private void initDp() { + dp = new long[high.length + 1][136][2][2]; + + for (long[][][] a : dp) { + for (long[][] b : a) { + for (long[] c : b) { + Arrays.fill(c, -1); + } + } + } + } + + private void addIncreasingNumbers(Set seen, long l, long r) { + for (int mask = 1; mask < (1 << 9); mask++) { + long x = 0; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if ((mask & (1 << i)) != 0) { + x = x * 10 + i + 1; + sum += i + 1; + } + } + + if (x >= l && x <= r && !good[sum]) { + seen.add(x); + } + } + } + + private void addDecreasingNumbers(Set seen, long l, long r) { + for (int mask = 1; mask < (1 << 10); mask++) { + long x = 0; + int sum = 0; + boolean started = false; + + for (int i = 0; i < 10; i++) { + if ((mask & (1 << i)) != 0) { + int d = 9 - i; + + if (started || d != 0) { + started = true; + x = x * 10 + d; + sum += d; + } + } + } + + if (started && x >= l && x <= r && !good[sum]) { + seen.add(x); + } + } + } + + private long dfs(int pos, int sum, int tl, int tr) { + if (pos == high.length) { + return good[sum] ? 1 : 0; + } + + if (dp[pos][sum][tl][tr] != -1) { + return dp[pos][sum][tl][tr]; + } + + int lo = tl == 1 ? low[pos] - '0' : 0; + int hi = tr == 1 ? high[pos] - '0' : 9; + + long res = 0; + for (int d = lo; d <= hi; d++) { + res += dfs(pos + 1, sum + d, nextTight(tl, d, lo), nextTight(tr, d, hi)); + } + + dp[pos][sum][tl][tr] = res; + return res; + } + + private int nextTight(int tight, int digit, int limit) { + return tight == 1 && digit == limit ? 1 : 0; + } + + private boolean isGood(int x) { + if (x < 10) { + return true; + } + + char[] s = Integer.toString(x).toCharArray(); + boolean inc = true; + boolean dec = true; + + for (int i = 1; i < s.length; i++) { + if (s[i] <= s[i - 1]) { + inc = false; + } + + if (s[i] >= s[i - 1]) { + dec = false; + } + } + + return inc || dec; + } +} diff --git a/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/readme.md b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/readme.md new file mode 100644 index 000000000..61ce4360a --- /dev/null +++ b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/readme.md @@ -0,0 +1,65 @@ +3869\. Count Fancy Numbers in a Range + +Hard + +You are given two integers `l` and `r`. + +An integer is called **good** if its digits form a **strictly monotone** sequence, meaning the digits are **strictly increasing** or **strictly decreasing**. All single-digit integers are considered good. + +An integer is called **fancy** if it is good, or if the **sum of its digits** is good. + +Return an integer representing the number of fancy integers in the range `[l, r]` (inclusive). + +A sequence is said to be **strictly increasing** if each element is **strictly greater** than its previous one (if exists). + +A sequence is said to be **strictly decreasing** if each element is **strictly less** than its previous one (if exists). + +**Example 1:** + +**Input:** l = 8, r = 10 + +**Output:** 3 + +**Explanation:** + +* 8 and 9 are single-digit integers, so they are good and therefore fancy. +* 10 has digits `[1, 0]`, which form a strictly decreasing sequence, so 10 is good and thus fancy. + +Therefore, the answer is 3. + +**Example 2:** + +**Input:** l = 12340, r = 12341 + +**Output:** 1 + +**Explanation:** + +* 12340 + * 12340 is not good because `[1, 2, 3, 4, 0]` is not strictly monotone. + * The digit sum is `1 + 2 + 3 + 4 + 0 = 10`. + * 10 is good as it has digits `[1, 0]`, which is strictly decreasing. Therefore, 12340 is fancy. +* 12341 + * 12341 is not good because `[1, 2, 3, 4, 1]` is not strictly monotone. + * The digit sum is `1 + 2 + 3 + 4 + 1 = 11`. + * 11 is not good as it has digits `[1, 1]`, which is not strictly monotone. Therefore, 12341 is not fancy. + +Therefore, the answer is 1. + +**Example 3:** + +**Input:** l = 123456788, r = 123456788 + +**Output:** 0 + +**Explanation:** + +* 123456788 is not good because its digits are not strictly monotone. +* The digit sum is `1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 8 = 44`. +* 44 is not good as it has digits `[4, 4]`, which is not strictly monotone. Therefore, 123456788 is not fancy. + +Therefore, the answer is 0. + +**Constraints:** + +* 1 <= l <= r <= 1015 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3870_count_commas_in_range/Solution.java b/src/main/java/g3801_3900/s3870_count_commas_in_range/Solution.java new file mode 100644 index 000000000..3618db2b6 --- /dev/null +++ b/src/main/java/g3801_3900/s3870_count_commas_in_range/Solution.java @@ -0,0 +1,10 @@ +package g3801_3900.s3870_count_commas_in_range; + +// #Easy #Math #Mid_Level #Weekly_Contest_493 +// #2026_07_28_Time_0_ms_(100.00%)_Space_42.54_MB_(61.54%) + +public class Solution { + public int countCommas(int n) { + return Math.max(0, n - 999); + } +} diff --git a/src/main/java/g3801_3900/s3870_count_commas_in_range/readme.md b/src/main/java/g3801_3900/s3870_count_commas_in_range/readme.md new file mode 100644 index 000000000..48c33aeb6 --- /dev/null +++ b/src/main/java/g3801_3900/s3870_count_commas_in_range/readme.md @@ -0,0 +1,36 @@ +3870\. Count Commas in Range + +Easy + +You are given an integer `n`. + +Return the **total** number of commas used when writing all integers from `[1, n]` (inclusive) in **standard** number formatting. + +In **standard** formatting: + +* A comma is inserted after **every three** digits from the right. +* Numbers with **fewer** than 4 digits contain no commas. + +**Example 1:** + +**Input:** n = 1002 + +**Output:** 3 + +**Explanation:** + +The numbers `"1,000"`, `"1,001"`, and `"1,002"` each contain one comma, giving a total of 3. + +**Example 2:** + +**Input:** n = 998 + +**Output:** 0 + +**Explanation:** + +All numbers from 1 to 998 have fewer than four digits. Therefore, no commas are used. + +**Constraints:** + +* 1 <= n <= 105 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/Solution.java b/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/Solution.java new file mode 100644 index 000000000..f1f3e8a6d --- /dev/null +++ b/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/Solution.java @@ -0,0 +1,25 @@ +package g3801_3900.s3871_count_commas_in_range_ii; + +// #Medium #Math #Senior #Weekly_Contest_493 #2026_07_28_Time_1_ms_(99.11%)_Space_42.88_MB_(24.89%) + +public class Solution { + public long countCommas(long n) { + long count = 0; + if (n >= 1000L) { + count += n - 999L; + } + if (n >= 1000000L) { + count += n - 999999L; + } + if (n >= 1000000000L) { + count += n - 999999999L; + } + if (n >= 1000000000000L) { + count += n - 999999999999L; + } + if (n >= 1000000000000000L) { + count += n - 999999999999999L; + } + return count; + } +} diff --git a/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/readme.md b/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/readme.md new file mode 100644 index 000000000..fed983e6a --- /dev/null +++ b/src/main/java/g3801_3900/s3871_count_commas_in_range_ii/readme.md @@ -0,0 +1,36 @@ +3871\. Count Commas in Range II + +Medium + +You are given an integer `n`. + +Return the **total** number of commas used when writing all integers from `[1, n]` (inclusive) in **standard** number formatting. + +In **standard** formatting: + +* A comma is inserted after **every three** digits from the right. +* Numbers with **fewer** than 4 digits contain no commas. + +**Example 1:** + +**Input:** n = 1002 + +**Output:** 3 + +**Explanation:** + +The numbers `"1,000"`, `"1,001"`, and `"1,002"` each contain one comma, giving a total of 3. + +**Example 2:** + +**Input:** n = 998 + +**Output:** 0 + +**Explanation:** + +All numbers from 1 to 998 have fewer than four digits. Therefore, no commas are used. + +**Constraints:** + +* 1 <= n <= 1015 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/Solution.java b/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/Solution.java new file mode 100644 index 000000000..d2835b485 --- /dev/null +++ b/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/Solution.java @@ -0,0 +1,44 @@ +package g3801_3900.s3872_longest_arithmetic_sequence_after_changing_at_most_one_element; + +// #Medium #Array #Enumeration #Staff #Weekly_Contest_493 +// #2026_07_28_Time_11_ms_(100.00%)_Space_145.68_MB_(86.52%) + +public class Solution { + public int longestArithmetic(int[] nums) { + int ans = solve(nums); + reverse(nums); + return Math.max(ans, solve(nums)); + } + + private static int solve(int[] nums) { + int n = nums.length; + int max = 2; + int diff = nums[1] - nums[0]; + int left = 0; + for (int right = 2; right < n; right++) { + if (nums[right] - nums[right - 1] == diff) { + max = Math.max(max, right - left + 1); + continue; + } + int temp = right; + int val = nums[right - 1] + diff; + while (temp + 1 < n && nums[temp + 1] - val == diff) { + val = nums[++temp]; + } + max = Math.max(max, temp - left + 1); + left = right - 1; + diff = nums[right] - nums[right - 1]; + } + return max; + } + + private static void reverse(int[] nums) { + int i = 0; + int j = nums.length - 1; + while (i < j) { + int temp = nums[i]; + nums[i++] = nums[j]; + nums[j--] = temp; + } + } +} diff --git a/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/readme.md b/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/readme.md new file mode 100644 index 000000000..96683a67d --- /dev/null +++ b/src/main/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/readme.md @@ -0,0 +1,38 @@ +3872\. Longest Arithmetic Sequence After Changing At Most One Element + +Medium + +You are given an integer array `nums`. + +A subarray is **arithmetic** if the difference between consecutive elements in the subarray is constant. + +You can replace **at most one** element in `nums` with any **integer**. Then, you select an arithmetic subarray from `nums`. + +Return an integer denoting the **maximum** length of the arithmetic subarray you can select. + +**Example 1:** + +**Input:** nums = [9,7,5,10,1] + +**Output:** 5 + +**Explanation:** + +* Replace `nums[3] = 10` with 3. The array becomes `[9, 7, 5, 3, 1]`. +* Select the subarray [**9, 7, 5, 3, 1**], which is arithmetic because consecutive elements have a common difference of -2. + +**Example 2:** + +**Input:** nums = [1,2,6,7] + +**Output:** 3 + +**Explanation:** + +* Replace `nums[0] = 1` with -2. The array becomes `[-2, 2, 6, 7]`. +* Select the subarray [**-2, 2, 6**, 7], which is arithmetic because consecutive elements have a common difference of 4. + +**Constraints:** + +* 4 <= nums.length <= 105 +* 1 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java new file mode 100644 index 000000000..d8324b8c7 --- /dev/null +++ b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java @@ -0,0 +1,80 @@ +package g3801_3900.s3873_maximum_points_activated_with_one_addition; + +// #Hard #Array #Hash_Table #Senior_Staff #Weekly_Contest_493 #Union_Find +// #2026_07_28_Time_36_ms_(97.50%)_Space_210.60_MB_(92.50%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int maxActivated(int[][] points) { + return maxEnergized(points); + } + + private int maxEnergized(int[][] relays) { + int n = relays.length; + int[] parent = new int[n]; + int[] size = new int[n]; + for (int i = 0; i < n; i++) { + parent[i] = i; + size[i] = 1; + } + Map firstInCol = new HashMap<>(n * 2); + Map firstInRow = new HashMap<>(n * 2); + for (int i = 0; i < n; i++) { + int col = relays[i][0]; + int row = relays[i][1]; + Integer c = firstInCol.putIfAbsent(col, i); + if (c != null) { + union(parent, size, c, i); + } + Integer r = firstInRow.putIfAbsent(row, i); + if (r != null) { + union(parent, size, r, i); + } + } + + int best1 = 0; + int best2 = 0; + for (int i = 0; i < n; i++) { + if (find(parent, i) == i) { + int s = size[i]; + if (s >= best1) { + best2 = best1; + best1 = s; + } else if (s > best2) { + best2 = s; + } + } + } + return best1 + best2 + 1; + } + + private int find(int[] parent, int x) { + int root = x; + while (parent[root] != root) { + root = parent[root]; + } + while (parent[x] != root) { + int next = parent[x]; + parent[x] = root; + x = next; + } + return root; + } + + private void union(int[] parent, int[] size, int a, int b) { + int ra = find(parent, a); + int rb = find(parent, b); + if (ra == rb) { + return; + } + if (size[ra] < size[rb]) { + int t = ra; + ra = rb; + rb = t; + } + parent[rb] = ra; + size[ra] += size[rb]; + } +} diff --git a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/readme.md b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/readme.md new file mode 100644 index 000000000..c80347662 --- /dev/null +++ b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/readme.md @@ -0,0 +1,65 @@ +3873\. Maximum Points Activated with One Addition + +Hard + +You are given a 2D integer array `points`, where points[i] = [xi, yi] represents the coordinates of the ith point. All coordinates in `points` are **distinct**. + +If a point is **activated**, then all points that have the **same** x-coordinate **or** y-coordinate become **activated** as well. + +Activation continues until no additional points can be activated. + +You may add **one additional** point at any integer coordinate `(x, y)` not already present in `points`. Activation begins by **activating** this **newly added point**. + +Return an integer denoting the **maximum** number of points that can be activated, including the newly added point. + +**Example 1:** + +**Input:** points = [[1,1],[1,2],[2,2]] + +**Output:** 4 + +**Explanation:** + +Adding and activating a point such as `(1, 3)` causes activations: + +* `(1, 3)` shares `x = 1` with `(1, 1)` and `(1, 2)` -> `(1, 1)` and `(1, 2)` become activated. +* `(1, 2)` shares `y = 2` with `(2, 2)` -> `(2, 2)` becomes activated. + +Thus, the activated points are `(1, 3)`, `(1, 1)`, `(1, 2)`, `(2, 2)`, so 4 points in total. We can show this is the maximum activated. + +**Example 2:** + +**Input:** points = [[2,2],[1,1],[3,3]] + +**Output:** 3 + +**Explanation:** + +Adding and activating a point such as `(1, 2)` causes activations: + +* `(1, 2)` shares `x = 1` with `(1, 1)` -> `(1, 1)` becomes activated. +* `(1, 2)` shares `y = 2` with `(2, 2)` -> `(2, 2)` becomes activated. + +Thus, the activated points are `(1, 2)`, `(1, 1)`, `(2, 2)`, so 3 points in total. We can show this is the maximum activated. + +**Example 3:** + +**Input:** points = [[2,3],[2,2],[1,1],[4,5]] + +**Output:** 4 + +**Explanation:** + +Adding and activating a point such as `(2, 1)` causes activations: + +* `(2, 1)` shares `x = 2` with `(2, 3)` and `(2, 2)` -> `(2, 3)` and `(2, 2)` become activated. +* `(2, 1)` shares `y = 1` with `(1, 1)` -> `(1, 1)` becomes activated. + +Thus, the activated points are `(2, 1)`, `(2, 3)`, `(2, 2)`, `(1, 1)`, so 4 points in total. + +**Constraints:** + +* 1 <= points.length <= 105 +* points[i] = [xi, yi] +* -109 <= xi, yi <= 109 +* `points` contains all **distinct** coordinates. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java new file mode 100644 index 000000000..88cab4ff1 --- /dev/null +++ b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java @@ -0,0 +1,10 @@ +package g3801_3900.s3875_construct_uniform_parity_array_i; + +// #Easy #Array #Math #Mid_Level #Weekly_Contest_494 +// #2026_07_28_Time_0_ms_(100.00%)_Space_45.37_MB_(16.57%) + +public class Solution { + public boolean uniformArray(int[] nums1) { + return true; + } +} diff --git a/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/readme.md b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/readme.md new file mode 100644 index 000000000..29ac760d8 --- /dev/null +++ b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/readme.md @@ -0,0 +1,44 @@ +3875\. Construct Uniform Parity Array I + +Easy + +You are given an array `nums1` of `n` **distinct** integers. + +You want to construct another array `nums2` of length `n` such that the elements in `nums2` are either **all odd or all even**. + +For each index `i`, you must choose **exactly one** of the following (in any order): + +* `nums2[i] = nums1[i]` +* `nums2[i] = nums1[i] - nums1[j]`, for an index `j != i` + +Return `true` if it is possible to construct such an array, otherwise, return `false`. + +**Example 1:** + +**Input:** nums1 = [2,3] + +**Output:** true + +**Explanation:** + +* Choose `nums2[0] = nums1[0] - nums1[1] = 2 - 3 = -1`. +* Choose `nums2[1] = nums1[1] = 3`. +* `nums2 = [-1, 3]`, and both elements are odd. Thus, the answer is `true`. + +**Example 2:** + +**Input:** nums1 = [4,6] + +**Output:** true + +**Explanation:** + +* Choose `nums2[0] = nums1[0] = 4`. +* Choose `nums2[1] = nums1[1] = 6`. +* `nums2 = [4, 6]`, and all elements are even. Thus, the answer is `true`. + +**Constraints:** + +* `1 <= n == nums1.length <= 100` +* `1 <= nums1[i] <= 100` +* `nums1` consists of distinct integers. \ No newline at end of file diff --git a/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java new file mode 100644 index 000000000..a3f2df2d9 --- /dev/null +++ b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3853_merge_close_characters; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void mergesClosestRepeatedCharacters() { + Solution solution = new Solution(); + + assertThat(solution.mergeCharacters("abca", 3), equalTo("abc")); + assertThat(solution.mergeCharacters("aabca", 2), equalTo("abca")); + assertThat(solution.mergeCharacters("yybyzybz", 2), equalTo("ybzybz")); + } +} diff --git a/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java new file mode 100644 index 000000000..bee76f4e0 --- /dev/null +++ b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java @@ -0,0 +1,19 @@ +package g3801_3900.s3854_minimum_operations_to_make_array_parity_alternating; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findsMinimumOperationsAndResultingRange() { + Solution solution = new Solution(); + + assertThat( + solution.makeParityAlternating(new int[] {-2, -3, 1, 4}), + equalTo(new int[] {2, 6})); + assertThat(solution.makeParityAlternating(new int[] {0, 2, -2}), equalTo(new int[] {1, 3})); + assertThat(solution.makeParityAlternating(new int[] {7}), equalTo(new int[] {0, 0})); + } +} diff --git a/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java b/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java new file mode 100644 index 000000000..b4f564d6e --- /dev/null +++ b/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3855_sum_of_k_digit_numbers_in_a_range; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumsAllKDigitNumbersModuloTheRequiredValue() { + Solution solution = new Solution(); + + assertThat(solution.sumOfNumbers(1, 2, 2), equalTo(66)); + assertThat(solution.sumOfNumbers(0, 1, 3), equalTo(444)); + assertThat(solution.sumOfNumbers(5, 5, 10), equalTo(555555520)); + } +} diff --git a/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java new file mode 100644 index 000000000..85b9ce202 --- /dev/null +++ b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3856_trim_trailing_vowels; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void removesOnlyTrailingVowels() { + Solution solution = new Solution(); + + assertThat(solution.trimTrailingVowels("idea"), equalTo("id")); + assertThat(solution.trimTrailingVowels("day"), equalTo("day")); + assertThat(solution.trimTrailingVowels("aeiou"), equalTo("")); + } +} diff --git a/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java b/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java new file mode 100644 index 000000000..cfb01d4bc --- /dev/null +++ b/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3857_minimum_cost_to_split_into_ones; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void calculatesMinimumSplitCost() { + Solution solution = new Solution(); + + assertThat(solution.minCost(1), equalTo(0)); + assertThat(solution.minCost(3), equalTo(3)); + assertThat(solution.minCost(4), equalTo(6)); + } +} diff --git a/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java b/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java new file mode 100644 index 000000000..7874801a0 --- /dev/null +++ b/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3858_minimum_bitwise_or_from_grid; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void choosesOneValuePerRowWithTheSmallestOr() { + Solution solution = new Solution(); + + assertThat(solution.minimumOR(new int[][] {{1, 5}, {2, 4}}), equalTo(3)); + assertThat(solution.minimumOR(new int[][] {{3, 5}, {6, 4}}), equalTo(5)); + assertThat(solution.minimumOR(new int[][] {{7, 9, 8}}), equalTo(7)); + } +} diff --git a/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java b/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java new file mode 100644 index 000000000..f34d14a5a --- /dev/null +++ b/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3859_count_subarrays_with_k_distinct_integers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countsSubarraysMeetingBothDistinctAndFrequencyRequirements() { + Solution solution = new Solution(); + + assertThat(solution.countSubarrays(new int[] {1, 2, 1, 2, 2}, 2, 2), equalTo(2L)); + assertThat(solution.countSubarrays(new int[] {3, 1, 2, 4}, 2, 1), equalTo(3L)); + assertThat(solution.countSubarrays(new int[] {1, 1, 1}, 1, 2), equalTo(3L)); + } +} diff --git a/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java b/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java new file mode 100644 index 000000000..51317ccc3 --- /dev/null +++ b/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3861_minimum_capacity_box; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void returnsSmallestSuitableCapacityAndItsFirstIndex() { + Solution solution = new Solution(); + + assertThat(solution.minimumIndex(new int[] {1, 5, 3, 7}, 3), equalTo(2)); + assertThat(solution.minimumIndex(new int[] {3, 5, 4, 3}, 2), equalTo(0)); + assertThat(solution.minimumIndex(new int[] {4}, 5), equalTo(-1)); + } +} diff --git a/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java b/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java new file mode 100644 index 000000000..251579852 --- /dev/null +++ b/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3862_find_the_smallest_balanced_index; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findsTheFirstIndexWhoseLeftSumEqualsRightProduct() { + Solution solution = new Solution(); + + assertThat(solution.smallestBalancedIndex(new int[] {2, 1, 2}), equalTo(1)); + assertThat(solution.smallestBalancedIndex(new int[] {2, 8, 2, 2, 5}), equalTo(2)); + assertThat(solution.smallestBalancedIndex(new int[] {1}), equalTo(-1)); + } +} diff --git a/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java new file mode 100644 index 000000000..9de9baea7 --- /dev/null +++ b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java @@ -0,0 +1,18 @@ +package g3801_3900.s3863_minimum_operations_to_sort_a_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void returnsMinimumSortableSubstringOperations() { + Solution solution = new Solution(); + + assertThat(solution.minOperations("dog"), equalTo(1)); + assertThat(solution.minOperations("card"), equalTo(2)); + assertThat(solution.minOperations("gf"), equalTo(-1)); + assertThat(solution.minOperations("abc"), equalTo(0)); + } +} diff --git a/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java b/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java new file mode 100644 index 000000000..ea4e7c6c8 --- /dev/null +++ b/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3864_minimum_cost_to_partition_a_binary_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findsTheCheapestValidHierarchicalPartition() { + Solution solution = new Solution(); + + assertThat(solution.minCost("1010", 2, 1), equalTo(6L)); + assertThat(solution.minCost("1010", 3, 10), equalTo(12L)); + assertThat(solution.minCost("00", 1, 2), equalTo(2L)); + } +} diff --git a/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java b/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java new file mode 100644 index 000000000..623246bfe --- /dev/null +++ b/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3866_first_unique_even_element; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void returnsEarliestUniqueEvenValue() { + Solution solution = new Solution(); + + assertThat(solution.firstUniqueEven(new int[] {3, 4, 2, 5, 4, 6}), equalTo(2)); + assertThat(solution.firstUniqueEven(new int[] {4, 4}), equalTo(-1)); + assertThat(solution.firstUniqueEven(new int[] {2, 3, 2, 4}), equalTo(4)); + } +} diff --git a/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java b/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java new file mode 100644 index 000000000..70f2e71ba --- /dev/null +++ b/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3867_sum_of_gcd_of_formed_pairs; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumsGcdsAfterConstructingAndPairingPrefixGcdValues() { + Solution solution = new Solution(); + + assertThat(solution.gcdSum(new int[] {2, 6, 4}), equalTo(2L)); + assertThat(solution.gcdSum(new int[] {3, 6, 2, 8}), equalTo(5L)); + assertThat(solution.gcdSum(new int[] {7}), equalTo(0L)); + } +} diff --git a/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java b/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java new file mode 100644 index 000000000..b79e6c731 --- /dev/null +++ b/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3868_minimum_cost_to_equalize_arrays_using_swaps; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countsRequiredCrossArraySwapsOrReportsImpossible() { + Solution solution = new Solution(); + + assertThat(solution.minCost(new int[] {10, 20}, new int[] {20, 10}), equalTo(0)); + assertThat(solution.minCost(new int[] {10, 10}, new int[] {20, 20}), equalTo(1)); + assertThat(solution.minCost(new int[] {10, 20}, new int[] {30, 40}), equalTo(-1)); + } +} diff --git a/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java b/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java new file mode 100644 index 000000000..945c60798 --- /dev/null +++ b/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3869_count_fancy_numbers_in_a_range; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countsNumbersThatAreGoodOrHaveAGoodDigitSum() { + Solution solution = new Solution(); + + assertThat(solution.countFancy(8, 10), equalTo(3L)); + assertThat(solution.countFancy(12340, 12341), equalTo(1L)); + assertThat(solution.countFancy(123456788, 123456788), equalTo(0L)); + } +} diff --git a/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java b/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java new file mode 100644 index 000000000..6e4d27b03 --- /dev/null +++ b/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3870_count_commas_in_range; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countsCommasAcrossTheWholeRange() { + Solution solution = new Solution(); + + assertThat(solution.countCommas(998), equalTo(0)); + assertThat(solution.countCommas(1002), equalTo(3)); + assertThat(solution.countCommas(100000), equalTo(99001)); + } +} diff --git a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java new file mode 100644 index 000000000..ab5eebc6d --- /dev/null +++ b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3871_count_commas_in_range_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void handlesEveryCommaMagnitudeBoundary() { + Solution solution = new Solution(); + + assertThat(solution.countCommas(998), equalTo(0L)); + assertThat(solution.countCommas(1002), equalTo(3L)); + assertThat(solution.countCommas(1_000_000), equalTo(999_003L)); + } +} diff --git a/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java new file mode 100644 index 000000000..fa1fbeced --- /dev/null +++ b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3872_longest_arithmetic_sequence_after_changing_at_most_one_element; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void allowsOneReplacementInEitherDirection() { + Solution solution = new Solution(); + + assertThat(solution.longestArithmetic(new int[] {9, 7, 5, 10, 1}), equalTo(5)); + assertThat(solution.longestArithmetic(new int[] {1, 2, 6, 7}), equalTo(3)); + assertThat(solution.longestArithmetic(new int[] {1, 3, 5, 7}), equalTo(4)); + } +} diff --git a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java new file mode 100644 index 000000000..ff1dff505 --- /dev/null +++ b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3873_maximum_points_activated_with_one_addition; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void connectsTheTwoLargestActivationComponents() { + Solution solution = new Solution(); + + assertThat(solution.maxActivated(new int[][] {{1, 1}, {1, 2}, {2, 2}}), equalTo(4)); + assertThat(solution.maxActivated(new int[][] {{2, 2}, {1, 1}, {3, 3}}), equalTo(3)); + assertThat(solution.maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), equalTo(4)); + } +} diff --git a/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java b/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java new file mode 100644 index 000000000..7fb7abf8b --- /dev/null +++ b/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java @@ -0,0 +1,17 @@ +package g3801_3900.s3875_construct_uniform_parity_array_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void constructsAUniformParityArrayWhenPossible() { + Solution solution = new Solution(); + + assertThat(solution.uniformArray(new int[] {2, 3}), equalTo(true)); + assertThat(solution.uniformArray(new int[] {4, 6}), equalTo(true)); + assertThat(solution.uniformArray(new int[] {7}), equalTo(true)); + } +} From 9708b193a3c6c38edf60aafa3db452ead4fe96e3 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 29 Jul 2026 02:00:11 +0700 Subject: [PATCH 02/22] fix style --- .../Solution.java | 8 +++++-- .../Solution.java | 24 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java index b0e0c6783..094123db0 100644 --- a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java +++ b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java @@ -5,7 +5,9 @@ public class Solution { public int[] makeParityAlternating(int[] nums) { - if (nums.length == 1) return new int[] {0, 0}; + if (nums.length == 1) { + return new int[] {0, 0}; + } int zero = 0; int one = 0; for (int i = 0; i < nums.length; i++) { @@ -23,7 +25,9 @@ public int[] makeParityAlternating(int[] nums) { ans[1] = Math.min(fun(nums, 0), fun(nums, 1)); } else if (one > zero) { ans[1] = fun(nums, 0); - } else ans[1] = fun(nums, 1); + } else { + ans[1] = fun(nums, 1); + } return ans; } diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java index 27d4742bc..908435825 100644 --- a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -76,5 +76,27 @@ private WindowState trimDuplicates( return new WindowState(left, p, 0); } - private record WindowState(int left, int p, int valid) {} + private static final class WindowState { + private final int left; + private final int p; + private final int valid; + + private WindowState(int left, int p, int valid) { + this.left = left; + this.p = p; + this.valid = valid; + } + + public int left() { + return left; + } + + public int p() { + return p; + } + + public int valid() { + return valid; + } + } } From 951c37838287ecebd62ac2ee7bf9fac3fc858ae9 Mon Sep 17 00:00:00 2001 From: ThanhNguyen Date: Wed, 29 Jul 2026 09:40:20 +0700 Subject: [PATCH 03/22] Update SolutionTest.java --- .../g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java index ab5eebc6d..c2b42617b 100644 --- a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java +++ b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java @@ -12,6 +12,6 @@ void handlesEveryCommaMagnitudeBoundary() { assertThat(solution.countCommas(998), equalTo(0L)); assertThat(solution.countCommas(1002), equalTo(3L)); - assertThat(solution.countCommas(1_000_000), equalTo(999_003L)); + assertThat(solution.countCommas(1_000_000), equalTo(999_002L)); } } From 45e17a94290e3e232c0d28c54eb7c49c8fae4347 Mon Sep 17 00:00:00 2001 From: ThanhNguyen Date: Wed, 29 Jul 2026 10:24:44 +0700 Subject: [PATCH 04/22] fix sonar --- .../Solution.java | 24 ++----------------- .../Solution.java | 4 ++-- .../Solution.java | 1 + 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java index 908435825..ac14e5198 100644 --- a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -66,7 +66,7 @@ private WindowState shrinkDistinct( private WindowState trimDuplicates( int[] nums, int left, int p, int m, Map map) { - while (map.size() > 0 && map.get(nums[left]) > m) { + while (!map.isEmpty() && map.get(nums[left]) > m) { int lv = nums[left]; map.put(lv, map.get(lv) - 1); left++; @@ -76,27 +76,7 @@ private WindowState trimDuplicates( return new WindowState(left, p, 0); } - private static final class WindowState { - private final int left; - private final int p; - private final int valid; + private record WindowState(int left, int p, int valid) { - private WindowState(int left, int p, int valid) { - this.left = left; - this.p = p; - this.valid = valid; - } - - public int left() { - return left; - } - - public int p() { - return p; - } - - public int valid() { - return valid; - } } } diff --git a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java index d8324b8c7..50cfe04a9 100644 --- a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java +++ b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java @@ -19,8 +19,8 @@ private int maxEnergized(int[][] relays) { parent[i] = i; size[i] = 1; } - Map firstInCol = new HashMap<>(n * 2); - Map firstInRow = new HashMap<>(n * 2); + Map firstInCol = HashMap.newHashMap(n * 2); + Map firstInRow = HashMap.newHashMap(n * 2); for (int i = 0; i < n; i++) { int col = relays[i][0]; int row = relays[i][1]; diff --git a/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java index 88cab4ff1..4b03fed62 100644 --- a/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java +++ b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java @@ -3,6 +3,7 @@ // #Easy #Array #Math #Mid_Level #Weekly_Contest_494 // #2026_07_28_Time_0_ms_(100.00%)_Space_45.37_MB_(16.57%) +@SuppressWarnings("java:S1172") public class Solution { public boolean uniformArray(int[] nums1) { return true; From a4ae0f6eaebbb74da7b35a71e599cb48da20fab8 Mon Sep 17 00:00:00 2001 From: ThanhNguyen Date: Wed, 29 Jul 2026 10:27:36 +0700 Subject: [PATCH 05/22] Update Solution.java --- .../Solution.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java index ac14e5198..d1d0f06e6 100644 --- a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Map; +@SuppressWarnings("java:S6206") public class Solution { public long countSubarrays(int[] nums, int k, int m) { @@ -76,7 +77,27 @@ private WindowState trimDuplicates( return new WindowState(left, p, 0); } - private record WindowState(int left, int p, int valid) { + private static final class WindowState { + private final int left; + private final int p; + private final int valid; + private WindowState(int left, int p, int valid) { + this.left = left; + this.p = p; + this.valid = valid; + } + + public int left() { + return left; + } + + public int p() { + return p; + } + + public int valid() { + return valid; + } } } From 8f30783d32aaf1a61fcca607b25d20a8cd988757 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:08:45 +0300 Subject: [PATCH 06/22] Refactor character merging logic in Solution.java --- .../java/g3801_3900/s3853_merge_close_characters/Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java b/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java index 234a3f7c7..170d4f734 100644 --- a/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java +++ b/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java @@ -14,16 +14,13 @@ public String mergeCharacters(String s, int k) { if (cnt[idx] > 0) { continue; } - result.append(c); cnt[idx]++; - if (result.length() > k) { char drop = result.charAt(result.length() - k - 1); cnt[drop - 'a']--; } } - return result.toString(); } } From eb029de1aa013c39f8c5458c80e6d981b97f6c9b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:09:43 +0300 Subject: [PATCH 07/22] Refactor minimum operations calculation logic --- .../Solution.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java index 094123db0..02e2ec278 100644 --- a/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java +++ b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java @@ -20,7 +20,6 @@ public int[] makeParityAlternating(int[] nums) { } int[] ans = new int[2]; ans[0] = Math.min(one, zero); - if (one == zero) { ans[1] = Math.min(fun(nums, 0), fun(nums, 1)); } else if (one > zero) { @@ -34,7 +33,6 @@ public int[] makeParityAlternating(int[] nums) { private int fun(int[] nums, int parity) { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; - for (int i = 0; i < nums.length; i++) { if ((nums[i] & 1) == ((i + parity) & 1)) { max = Math.max(max, nums[i]); From e63e423558a2d15fe69b5f2f51444a727b4b04d0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:10:31 +0300 Subject: [PATCH 08/22] Refactor sumOfNumbers method in Solution.java --- .../s3855_sum_of_k_digit_numbers_in_a_range/Solution.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java index 2f4e52906..e2c8293c6 100644 --- a/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java +++ b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java @@ -9,17 +9,13 @@ public class Solution { public int sumOfNumbers(int l, int r, int k) { long count = r - (long) l + 1; long sumRange = (l + r) * count / 2; - long t1 = sumRange % MOD; long t2 = power(count, (long) k - 1); - long repunit = (power(10, k) - 1 + MOD) % MOD; long inv9 = power(9, (long) MOD - 2); long t3 = (repunit * inv9) % MOD; - long ans = (t1 * t2) % MOD; ans = (ans * t3) % MOD; - return (int) ans; } From 45f974f70fc81f4cdcd8b26a1cfd186ddd98ceb4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:11:02 +0300 Subject: [PATCH 09/22] Remove trailing vowels from input string --- .../java/g3801_3900/s3856_trim_trailing_vowels/Solution.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java index e509e0a09..cc716c193 100644 --- a/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java +++ b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java @@ -6,7 +6,6 @@ public class Solution { public String trimTrailingVowels(String s) { int i = s.length() - 1; - while (i >= 0 && (s.charAt(i) == 'a' || s.charAt(i) == 'e' @@ -15,7 +14,6 @@ public String trimTrailingVowels(String s) { || s.charAt(i) == 'u')) { i--; } - return s.substring(0, i + 1); } } From 67addbbd587b74c262caddca1b743f69102bb568 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:12:37 +0300 Subject: [PATCH 10/22] Refactor countSubarrays method for clarity and efficiency --- .../Solution.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java index d1d0f06e6..3152b2866 100644 --- a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -8,72 +8,56 @@ @SuppressWarnings("java:S6206") public class Solution { - public long countSubarrays(int[] nums, int k, int m) { int left = 0; int p = 0; long subArr = 0; - HashMap map = new HashMap<>(); int valid = 0; - for (int val : nums) { map.put(val, map.getOrDefault(val, 0) + 1); - if (map.get(val) == m) { valid++; } - WindowState state = shrinkDistinct(nums, left, p, k, m, map, valid); left = state.left(); p = state.p(); valid = state.valid(); - WindowState duplicateState = trimDuplicates(nums, left, p, m, map); left = duplicateState.left(); p = duplicateState.p(); - if (map.size() == k && valid == k) { subArr += 1 + p; } } - return subArr; } private WindowState shrinkDistinct( int[] nums, int left, int p, int k, int m, Map map, int valid) { - while (map.size() > k) { int lv = nums[left]; - if (map.get(lv) == m) { valid--; } - map.put(lv, map.get(lv) - 1); - if (map.get(lv) == 0) { map.remove(lv); } - left++; p = 0; } - return new WindowState(left, p, valid); } private WindowState trimDuplicates( int[] nums, int left, int p, int m, Map map) { - while (!map.isEmpty() && map.get(nums[left]) > m) { int lv = nums[left]; map.put(lv, map.get(lv) - 1); left++; p++; } - return new WindowState(left, p, 0); } From eba95003880c3994fedb2beff0d21a69a6db71e8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:13:52 +0300 Subject: [PATCH 11/22] Refactor minOperations method for string sorting --- .../Solution.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java index d415ed482..6e7da03bf 100644 --- a/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java +++ b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java @@ -4,18 +4,14 @@ // #2026_07_27_Time_15_ms_(74.44%)_Space_47.98_MB_(56.67%) public class Solution { - public int minOperations(String s) { int n = s.length(); - if (n == 1) { return 0; } - if (n == 2) { return s.charAt(0) > s.charAt(1) ? -1 : 0; } - char min = 'z'; char max = 'a'; char first = s.charAt(0); @@ -23,7 +19,6 @@ public int minOperations(String s) { char prev = 'a'; int[] cnt = new int[26]; boolean sorted = true; - for (char c : s.toCharArray()) { sorted &= prev <= c; min = (char) Math.min(min, c); @@ -31,11 +26,9 @@ public int minOperations(String s) { prev = c; cnt[c - 'a']++; } - if (sorted) { return 0; } - return calculateOperations(first, last, min, max, cnt); } @@ -43,11 +36,9 @@ private int calculateOperations(char first, char last, char min, char max, int[] if (first == min || last == max) { return 1; } - if (first != max || last != min) { return 2; } - return cnt[max - 'a'] > 1 || cnt[min - 'a'] > 1 ? 2 : 3; } } From 54df78c69d17674e1d8998310e2e09c7d7867fee Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:14:36 +0300 Subject: [PATCH 12/22] Refactor Solution.java for clarity and formatting --- .../g3801_3900/s3866_first_unique_even_element/Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java b/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java index 5bc065f40..57c963638 100644 --- a/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java +++ b/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java @@ -9,7 +9,6 @@ public int firstUniqueEven(int[] nums) { for (int num : nums) { arr[num - 1]++; } - for (int num : nums) { if (num % 2 == 0 && (arr[num - 1] == 1)) { return num; From 642e4b74b9f0a02e4cee9ee5f100f0ef9b68e29a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:15:45 +0300 Subject: [PATCH 13/22] Optimize GCD sum calculation in Solution.java --- .../g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java index e3fe262eb..9abce46fe 100644 --- a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java @@ -14,19 +14,15 @@ public long gcdSum(int[] nums) { max = Math.max(max, nums[i]); prefixGcd[i] = gcd(max, nums[i]); } - Arrays.sort(prefixGcd); - long sum = 0; int i = 0; int j = nums.length - 1; - while (i < j) { sum += gcd(prefixGcd[i], prefixGcd[j]); i++; j--; } - return sum; } From 49ee5b9840d3d9c1b1c2f2820d2537b5e68ae84a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:16:15 +0300 Subject: [PATCH 14/22] Fix formatting issues in gcdSum method --- .../g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java index 9abce46fe..a1d801089 100644 --- a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java @@ -9,7 +9,6 @@ public class Solution { public long gcdSum(int[] nums) { int[] prefixGcd = new int[nums.length]; int max = -1; - for (int i = 0; i < nums.length; i++) { max = Math.max(max, nums[i]); prefixGcd[i] = gcd(max, nums[i]); From d7fb5d84a09b6c3b2ec3b31edea51c2c5a2ece65 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:18:05 +0300 Subject: [PATCH 15/22] Refactor countFancy method and related functions --- .../Solution.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java index b32e5f4f7..c460bb32d 100644 --- a/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java +++ b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java @@ -17,13 +17,10 @@ public long countFancy(long l, long r) { initBounds(l, r); initGood(); initDp(); - long ans = dfs(0, 0, 1, 1); - Set seen = new HashSet<>(); addIncreasingNumbers(seen, l, r); addDecreasingNumbers(seen, l, r); - return ans + seen.size(); } @@ -31,9 +28,7 @@ private void initBounds(long l, long r) { String left = Long.toString(l); String right = Long.toString(r); int n = right.length(); - left = "0".repeat(n - left.length()) + left; - low = left.toCharArray(); high = right.toCharArray(); } @@ -47,7 +42,6 @@ private void initGood() { private void initDp() { dp = new long[high.length + 1][136][2][2]; - for (long[][][] a : dp) { for (long[][] b : a) { for (long[] c : b) { @@ -61,14 +55,12 @@ private void addIncreasingNumbers(Set seen, long l, long r) { for (int mask = 1; mask < (1 << 9); mask++) { long x = 0; int sum = 0; - for (int i = 0; i < 9; i++) { if ((mask & (1 << i)) != 0) { x = x * 10 + i + 1; sum += i + 1; } } - if (x >= l && x <= r && !good[sum]) { seen.add(x); } @@ -80,11 +72,9 @@ private void addDecreasingNumbers(Set seen, long l, long r) { long x = 0; int sum = 0; boolean started = false; - for (int i = 0; i < 10; i++) { if ((mask & (1 << i)) != 0) { int d = 9 - i; - if (started || d != 0) { started = true; x = x * 10 + d; @@ -92,7 +82,6 @@ private void addDecreasingNumbers(Set seen, long l, long r) { } } } - if (started && x >= l && x <= r && !good[sum]) { seen.add(x); } @@ -103,19 +92,15 @@ private long dfs(int pos, int sum, int tl, int tr) { if (pos == high.length) { return good[sum] ? 1 : 0; } - if (dp[pos][sum][tl][tr] != -1) { return dp[pos][sum][tl][tr]; } - int lo = tl == 1 ? low[pos] - '0' : 0; int hi = tr == 1 ? high[pos] - '0' : 9; - long res = 0; for (int d = lo; d <= hi; d++) { res += dfs(pos + 1, sum + d, nextTight(tl, d, lo), nextTight(tr, d, hi)); } - dp[pos][sum][tl][tr] = res; return res; } @@ -128,21 +113,17 @@ private boolean isGood(int x) { if (x < 10) { return true; } - char[] s = Integer.toString(x).toCharArray(); boolean inc = true; boolean dec = true; - for (int i = 1; i < s.length; i++) { if (s[i] <= s[i - 1]) { inc = false; } - if (s[i] >= s[i - 1]) { dec = false; } } - return inc || dec; } } From 7da7376ceb128296f4fe60b5b558a55fea40e870 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:19:05 +0300 Subject: [PATCH 16/22] Refactor union-find logic in Solution.java --- .../Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java index 50cfe04a9..5cad6294c 100644 --- a/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java +++ b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java @@ -33,7 +33,6 @@ private int maxEnergized(int[][] relays) { union(parent, size, r, i); } } - int best1 = 0; int best2 = 0; for (int i = 0; i < n; i++) { From 09fa6ff6067afab29bc1997570538b50e6fe4422 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:20:41 +0300 Subject: [PATCH 17/22] Fix formatting in SolutionTest.java --- .../g3801_3900/s3853_merge_close_characters/SolutionTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java index a3f2df2d9..dca7386ac 100644 --- a/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java +++ b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java @@ -9,7 +9,6 @@ class SolutionTest { @Test void mergesClosestRepeatedCharacters() { Solution solution = new Solution(); - assertThat(solution.mergeCharacters("abca", 3), equalTo("abc")); assertThat(solution.mergeCharacters("aabca", 2), equalTo("abca")); assertThat(solution.mergeCharacters("yybyzybz", 2), equalTo("ybzybz")); From f3d7e8df7a87eaf6cb36aa6b7b83435e4720f1f0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:36:16 +0300 Subject: [PATCH 18/22] Changed tests --- .../SolutionTest.java | 17 ++++++++++++----- .../SolutionTest.java | 18 ++++++++++++------ .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 17 ++++++++++++----- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 16 +++++++++++----- 11 files changed, 124 insertions(+), 56 deletions(-) diff --git a/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java index dca7386ac..d9f56af74 100644 --- a/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java +++ b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java @@ -7,10 +7,17 @@ class SolutionTest { @Test - void mergesClosestRepeatedCharacters() { - Solution solution = new Solution(); - assertThat(solution.mergeCharacters("abca", 3), equalTo("abc")); - assertThat(solution.mergeCharacters("aabca", 2), equalTo("abca")); - assertThat(solution.mergeCharacters("yybyzybz", 2), equalTo("ybzybz")); + void mergeCharacters() { + assertThat(new Solution().mergeCharacters("abca", 3), equalTo("abc")); + } + + @Test + void mergeCharacters2() { + assertThat(new Solution().mergeCharacters("aabca", 2), equalTo("abca")); + } + + @Test + void mergeCharacters3() { + assertThat(new Solution().mergeCharacters("yybyzybz", 2), equalTo("ybzybz")); } } diff --git a/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java index bee76f4e0..2f6f47f12 100644 --- a/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java +++ b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java @@ -7,13 +7,19 @@ class SolutionTest { @Test - void findsMinimumOperationsAndResultingRange() { - Solution solution = new Solution(); - + void makeParityAlternating() { assertThat( - solution.makeParityAlternating(new int[] {-2, -3, 1, 4}), + new Solution().makeParityAlternating(new int[] {-2, -3, 1, 4}), equalTo(new int[] {2, 6})); - assertThat(solution.makeParityAlternating(new int[] {0, 2, -2}), equalTo(new int[] {1, 3})); - assertThat(solution.makeParityAlternating(new int[] {7}), equalTo(new int[] {0, 0})); + } + + @Test + void makeParityAlternating2() { + assertThat(new Solution().makeParityAlternating(new int[] {0, 2, -2}), equalTo(new int[] {1, 3})); + } + + @Test + void makeParityAlternating3() { + assertThat(new Solution().makeParityAlternating(new int[] {7}), equalTo(new int[] {0, 0})); } } diff --git a/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java b/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java index b4f564d6e..656386b66 100644 --- a/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java +++ b/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void sumsAllKDigitNumbersModuloTheRequiredValue() { - Solution solution = new Solution(); + void sumOfNumbers() { + assertThat(new Solution().sumOfNumbers(1, 2, 2), equalTo(66)); + } - assertThat(solution.sumOfNumbers(1, 2, 2), equalTo(66)); - assertThat(solution.sumOfNumbers(0, 1, 3), equalTo(444)); - assertThat(solution.sumOfNumbers(5, 5, 10), equalTo(555555520)); + @Test + void sumOfNumbers2() { + assertThat(new Solution().sumOfNumbers(0, 1, 3), equalTo(444)); + } + + @Test + void sumOfNumbers3() { + assertThat(new Solution().sumOfNumbers(5, 5, 10), equalTo(555555520)); } } diff --git a/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java b/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java index 70f2e71ba..b200b7fdb 100644 --- a/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java +++ b/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void sumsGcdsAfterConstructingAndPairingPrefixGcdValues() { - Solution solution = new Solution(); + void gcdSum() { + assertThat(new Solution().gcdSum(new int[] {2, 6, 4}), equalTo(2L)); + } - assertThat(solution.gcdSum(new int[] {2, 6, 4}), equalTo(2L)); - assertThat(solution.gcdSum(new int[] {3, 6, 2, 8}), equalTo(5L)); - assertThat(solution.gcdSum(new int[] {7}), equalTo(0L)); + @Test + void gcdSum2() { + assertThat(new Solution().gcdSum(new int[] {3, 6, 2, 8}), equalTo(5L)); + } + + @Test + void gcdSum3() { + assertThat(new Solution().gcdSum(new int[] {7}), equalTo(0L)); } } diff --git a/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java b/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java index b79e6c731..43c140288 100644 --- a/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java +++ b/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void countsRequiredCrossArraySwapsOrReportsImpossible() { - Solution solution = new Solution(); + void minCost() { + assertThat(new Solution().minCost(new int[] {10, 20}, new int[] {20, 10}), equalTo(0)); + } - assertThat(solution.minCost(new int[] {10, 20}, new int[] {20, 10}), equalTo(0)); - assertThat(solution.minCost(new int[] {10, 10}, new int[] {20, 20}), equalTo(1)); - assertThat(solution.minCost(new int[] {10, 20}, new int[] {30, 40}), equalTo(-1)); + @Test + void minCost2() { + assertThat(new Solution().minCost(new int[] {10, 10}, new int[] {20, 20}), equalTo(1)); + } + + @Test + void minCost3() { + assertThat(new Solution().minCost(new int[] {10, 20}, new int[] {30, 40}), equalTo(-1)); } } diff --git a/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java b/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java index 945c60798..1c4333165 100644 --- a/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java +++ b/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void countsNumbersThatAreGoodOrHaveAGoodDigitSum() { - Solution solution = new Solution(); + void countFancy() { + assertThat(new Solution().countFancy(8, 10), equalTo(3L)); + } - assertThat(solution.countFancy(8, 10), equalTo(3L)); - assertThat(solution.countFancy(12340, 12341), equalTo(1L)); - assertThat(solution.countFancy(123456788, 123456788), equalTo(0L)); + @Test + void countFancy2() { + assertThat(new Solution().countFancy(12340, 12341), equalTo(1L)); + } + + @Test + void countFancy3() { + assertThat(new Solution().countFancy(123456788, 123456788), equalTo(0L)); } } diff --git a/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java b/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java index 6e4d27b03..2f5e0ce31 100644 --- a/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java +++ b/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void countsCommasAcrossTheWholeRange() { - Solution solution = new Solution(); + void countCommas() { + assertThat(new Solution().countCommas(998), equalTo(0)); + } - assertThat(solution.countCommas(998), equalTo(0)); - assertThat(solution.countCommas(1002), equalTo(3)); - assertThat(solution.countCommas(100000), equalTo(99001)); + @Test + void countCommas2() { + assertThat(new Solution().countCommas(1002), equalTo(3)); + } + + @Test + void countCommas3() { + assertThat(new Solution().countCommas(100000), equalTo(99001)); } } diff --git a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java index c2b42617b..20c7268df 100644 --- a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java +++ b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void handlesEveryCommaMagnitudeBoundary() { - Solution solution = new Solution(); + void countCommas() { + assertThat(new Solution().countCommas(998), equalTo(0L)); + } - assertThat(solution.countCommas(998), equalTo(0L)); - assertThat(solution.countCommas(1002), equalTo(3L)); - assertThat(solution.countCommas(1_000_000), equalTo(999_002L)); + @Test + void countCommas2() { + assertThat(new Solution().countCommas(1002), equalTo(3L)); + } + + @Test + void countCommas3() { + assertThat(new Solution().countCommas(1_000_000), equalTo(999_002L)); } } diff --git a/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java index fa1fbeced..55909d21e 100644 --- a/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java +++ b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java @@ -7,11 +7,18 @@ class SolutionTest { @Test - void allowsOneReplacementInEitherDirection() { - Solution solution = new Solution(); + void longestArithmetic() { + assertThat(new Solution().longestArithmetic(new int[] {9, 7, 5, 10, 1}), equalTo(5)); + } + + @Test + void longestArithmetic2() { + assertThat(new Solution().longestArithmetic(new int[] {1, 2, 6, 7}), equalTo(3)); + } + - assertThat(solution.longestArithmetic(new int[] {9, 7, 5, 10, 1}), equalTo(5)); - assertThat(solution.longestArithmetic(new int[] {1, 2, 6, 7}), equalTo(3)); - assertThat(solution.longestArithmetic(new int[] {1, 3, 5, 7}), equalTo(4)); + @Test + void longestArithmetic3() { + assertThat(new Solution().longestArithmetic(new int[] {1, 3, 5, 7}), equalTo(4)); } } diff --git a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java index ff1dff505..234f7f525 100644 --- a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java +++ b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void connectsTheTwoLargestActivationComponents() { - Solution solution = new Solution(); + void maxActivated() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}, {1, 2}, {2, 2}}), equalTo(4)); + } - assertThat(solution.maxActivated(new int[][] {{1, 1}, {1, 2}, {2, 2}}), equalTo(4)); - assertThat(solution.maxActivated(new int[][] {{2, 2}, {1, 1}, {3, 3}}), equalTo(3)); - assertThat(solution.maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), equalTo(4)); + @Test + void maxActivated2() { + assertThat(new Solution().maxActivated(new int[][] {{2, 2}, {1, 1}, {3, 3}}), equalTo(3)); + } + + @Test + void maxActivated3() { + assertThat(new Solution().maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), equalTo(4)); } } diff --git a/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java b/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java index 7fb7abf8b..596ff0941 100644 --- a/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java +++ b/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void constructsAUniformParityArrayWhenPossible() { - Solution solution = new Solution(); + void uniformArray() { + assertThat(new Solution().uniformArray(new int[] {2, 3}), equalTo(true)); + } - assertThat(solution.uniformArray(new int[] {2, 3}), equalTo(true)); - assertThat(solution.uniformArray(new int[] {4, 6}), equalTo(true)); - assertThat(solution.uniformArray(new int[] {7}), equalTo(true)); + @Test + void uniformArray2() { + assertThat(new Solution().uniformArray(new int[] {4, 6}), equalTo(true)); + } + + @Test + void uniformArray3() { + assertThat(new Solution().uniformArray(new int[] {7}), equalTo(true)); } } From 11f6d10eafed6177154e083520b68e3496183f76 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Jul 2026 07:40:10 +0300 Subject: [PATCH 19/22] Fixed style --- .../SolutionTest.java | 4 +++- .../SolutionTest.java | 1 - .../SolutionTest.java | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java index 2f6f47f12..0dc602bbe 100644 --- a/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java +++ b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java @@ -15,7 +15,9 @@ void makeParityAlternating() { @Test void makeParityAlternating2() { - assertThat(new Solution().makeParityAlternating(new int[] {0, 2, -2}), equalTo(new int[] {1, 3})); + assertThat( + new Solution().makeParityAlternating(new int[] {0, 2, -2}), + equalTo(new int[] {1, 3})); } @Test diff --git a/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java index 55909d21e..122e1e574 100644 --- a/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java +++ b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java @@ -16,7 +16,6 @@ void longestArithmetic2() { assertThat(new Solution().longestArithmetic(new int[] {1, 2, 6, 7}), equalTo(3)); } - @Test void longestArithmetic3() { assertThat(new Solution().longestArithmetic(new int[] {1, 3, 5, 7}), equalTo(4)); diff --git a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java index 234f7f525..e0fcaf3ed 100644 --- a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java +++ b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java @@ -18,6 +18,8 @@ void maxActivated2() { @Test void maxActivated3() { - assertThat(new Solution().maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), equalTo(4)); + assertThat( + new Solution().maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), + equalTo(4)); } } From 28b10309cf809171ddfd1fb530b251aceb767b57 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 29 Jul 2026 21:34:41 +0700 Subject: [PATCH 20/22] update tests --- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 22 ++++++++++++++----- .../SolutionTest.java | 16 +++++++++----- .../SolutionTest.java | 16 +++++++++----- 9 files changed, 104 insertions(+), 46 deletions(-) diff --git a/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java index 85b9ce202..469fad95b 100644 --- a/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java +++ b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void removesOnlyTrailingVowels() { - Solution solution = new Solution(); + void trimTrailingVowels() { + assertThat(new Solution().trimTrailingVowels("idea"), equalTo("id")); + } - assertThat(solution.trimTrailingVowels("idea"), equalTo("id")); - assertThat(solution.trimTrailingVowels("day"), equalTo("day")); - assertThat(solution.trimTrailingVowels("aeiou"), equalTo("")); + @Test + void trimTrailingVowels2() { + assertThat(new Solution().trimTrailingVowels("day"), equalTo("day")); + } + + @Test + void trimTrailingVowels3() { + assertThat(new Solution().trimTrailingVowels("aeiou"), equalTo("")); } } diff --git a/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java b/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java index cfb01d4bc..47c8611ac 100644 --- a/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java +++ b/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void calculatesMinimumSplitCost() { - Solution solution = new Solution(); + void minCost() { + assertThat(new Solution().minCost(1), equalTo(0)); + } - assertThat(solution.minCost(1), equalTo(0)); - assertThat(solution.minCost(3), equalTo(3)); - assertThat(solution.minCost(4), equalTo(6)); + @Test + void minCost2() { + assertThat(new Solution().minCost(3), equalTo(3)); + } + + @Test + void minCost3() { + assertThat(new Solution().minCost(4), equalTo(6)); } } diff --git a/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java b/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java index 7874801a0..765fea3ba 100644 --- a/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java +++ b/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void choosesOneValuePerRowWithTheSmallestOr() { - Solution solution = new Solution(); + void minimumOR() { + assertThat(new Solution().minimumOR(new int[][] {{1, 5}, {2, 4}}), equalTo(3)); + } - assertThat(solution.minimumOR(new int[][] {{1, 5}, {2, 4}}), equalTo(3)); - assertThat(solution.minimumOR(new int[][] {{3, 5}, {6, 4}}), equalTo(5)); - assertThat(solution.minimumOR(new int[][] {{7, 9, 8}}), equalTo(7)); + @Test + void minimumOR2() { + assertThat(new Solution().minimumOR(new int[][] {{3, 5}, {6, 4}}), equalTo(5)); + } + + @Test + void minimumOR3() { + assertThat(new Solution().minimumOR(new int[][] {{7, 9, 8}}), equalTo(7)); } } diff --git a/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java b/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java index f34d14a5a..aac274c86 100644 --- a/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java +++ b/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void countsSubarraysMeetingBothDistinctAndFrequencyRequirements() { - Solution solution = new Solution(); + void countSubarrays() { + assertThat(new Solution().countSubarrays(new int[] {1, 2, 1, 2, 2}, 2, 2), equalTo(2L)); + } - assertThat(solution.countSubarrays(new int[] {1, 2, 1, 2, 2}, 2, 2), equalTo(2L)); - assertThat(solution.countSubarrays(new int[] {3, 1, 2, 4}, 2, 1), equalTo(3L)); - assertThat(solution.countSubarrays(new int[] {1, 1, 1}, 1, 2), equalTo(3L)); + @Test + void countSubarrays2() { + assertThat(new Solution().countSubarrays(new int[] {3, 1, 2, 4}, 2, 1), equalTo(3L)); + } + + @Test + void countSubarrays3() { + assertThat(new Solution().countSubarrays(new int[] {1, 1, 1}, 1, 2), equalTo(3L)); } } diff --git a/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java b/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java index 51317ccc3..2352c5b4b 100644 --- a/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java +++ b/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void returnsSmallestSuitableCapacityAndItsFirstIndex() { - Solution solution = new Solution(); + void minimumIndex() { + assertThat(new Solution().minimumIndex(new int[] {1, 5, 3, 7}, 3), equalTo(2)); + } - assertThat(solution.minimumIndex(new int[] {1, 5, 3, 7}, 3), equalTo(2)); - assertThat(solution.minimumIndex(new int[] {3, 5, 4, 3}, 2), equalTo(0)); - assertThat(solution.minimumIndex(new int[] {4}, 5), equalTo(-1)); + @Test + void minimumIndex2() { + assertThat(new Solution().minimumIndex(new int[] {3, 5, 4, 3}, 2), equalTo(0)); + } + + @Test + void minimumIndex3() { + assertThat(new Solution().minimumIndex(new int[] {4}, 5), equalTo(-1)); } } diff --git a/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java b/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java index 251579852..b4736eb76 100644 --- a/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java +++ b/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void findsTheFirstIndexWhoseLeftSumEqualsRightProduct() { - Solution solution = new Solution(); + void smallestBalancedIndex() { + assertThat(new Solution().smallestBalancedIndex(new int[] {2, 1, 2}), equalTo(1)); + } - assertThat(solution.smallestBalancedIndex(new int[] {2, 1, 2}), equalTo(1)); - assertThat(solution.smallestBalancedIndex(new int[] {2, 8, 2, 2, 5}), equalTo(2)); - assertThat(solution.smallestBalancedIndex(new int[] {1}), equalTo(-1)); + @Test + void smallestBalancedIndex2() { + assertThat(new Solution().smallestBalancedIndex(new int[] {2, 8, 2, 2, 5}), equalTo(2)); + } + + @Test + void smallestBalancedIndex3() { + assertThat(new Solution().smallestBalancedIndex(new int[] {1}), equalTo(-1)); } } diff --git a/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java index 9de9baea7..7003e8321 100644 --- a/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java +++ b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java @@ -7,12 +7,22 @@ class SolutionTest { @Test - void returnsMinimumSortableSubstringOperations() { - Solution solution = new Solution(); + void minOperations() { + assertThat(new Solution().minOperations("dog"), equalTo(1)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations("card"), equalTo(2)); + } - assertThat(solution.minOperations("dog"), equalTo(1)); - assertThat(solution.minOperations("card"), equalTo(2)); - assertThat(solution.minOperations("gf"), equalTo(-1)); - assertThat(solution.minOperations("abc"), equalTo(0)); + @Test + void minOperations3() { + assertThat(new Solution().minOperations("gf"), equalTo(-1)); + } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations("abc"), equalTo(0)); } } diff --git a/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java b/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java index ea4e7c6c8..3ae743f19 100644 --- a/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java +++ b/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void findsTheCheapestValidHierarchicalPartition() { - Solution solution = new Solution(); + void minCost() { + assertThat(new Solution().minCost("1010", 2, 1), equalTo(6L)); + } - assertThat(solution.minCost("1010", 2, 1), equalTo(6L)); - assertThat(solution.minCost("1010", 3, 10), equalTo(12L)); - assertThat(solution.minCost("00", 1, 2), equalTo(2L)); + @Test + void minCost2() { + assertThat(new Solution().minCost("1010", 3, 10), equalTo(12L)); + } + + @Test + void minCost3() { + assertThat(new Solution().minCost("00", 1, 2), equalTo(2L)); } } diff --git a/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java b/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java index 623246bfe..ac27e1415 100644 --- a/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java +++ b/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java @@ -7,11 +7,17 @@ class SolutionTest { @Test - void returnsEarliestUniqueEvenValue() { - Solution solution = new Solution(); + void firstUniqueEven() { + assertThat(new Solution().firstUniqueEven(new int[] {3, 4, 2, 5, 4, 6}), equalTo(2)); + } - assertThat(solution.firstUniqueEven(new int[] {3, 4, 2, 5, 4, 6}), equalTo(2)); - assertThat(solution.firstUniqueEven(new int[] {4, 4}), equalTo(-1)); - assertThat(solution.firstUniqueEven(new int[] {2, 3, 2, 4}), equalTo(4)); + @Test + void firstUniqueEven2() { + assertThat(new Solution().firstUniqueEven(new int[] {4, 4}), equalTo(-1)); + } + + @Test + void firstUniqueEven3() { + assertThat(new Solution().firstUniqueEven(new int[] {2, 3, 2, 4}), equalTo(4)); } } From eda6572b4c5d66e134e259cfee577b239169d451 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 Jul 2026 08:29:34 +0300 Subject: [PATCH 21/22] Improved readmes --- .../readme.md | 91 ++----------------- .../readme.md | 51 ++--------- .../readme.md | 81 +++-------------- 3 files changed, 29 insertions(+), 194 deletions(-) diff --git a/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md index 2a2a488d3..a39fd07eb 100644 --- a/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md +++ b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md @@ -20,41 +20,10 @@ Return an integer denoting the **minimum** total cost required to split the inte One optimal set of operations is: -`x` - -`a` - -`b` - -`a + b` - -`a * b` - -Cost - -3 - -1 - -2 - -3 - -2 - -2 - -2 - -1 - -1 - -2 - -1 - -1 +| `x` | `a` | `b` | `a + b` | `a * b` | Cost | +|---:|---:|---:|---:|---:|---:| +| 3 | 1 | 2 | 3 | 2 | 2 | +| 2 | 1 | 1 | 2 | 1 | 1 | Thus, the minimum total cost is `2 + 1 = 3`. @@ -68,53 +37,11 @@ Thus, the minimum total cost is `2 + 1 = 3`. One optimal set of operations is: -`x` - -`a` - -`b` - -`a + b` - -`a * b` - -Cost - -4 - -2 - -2 - -4 - -4 - -4 - -2 - -1 - -1 - -2 - -1 - -1 - -2 - -1 - -1 - -2 - -1 - -1 +| `x` | `a` | `b` | `a + b` | `a * b` | Cost | +|---:|---:|---:|---:|---:|---:| +| 4 | 2 | 2 | 4 | 4 | 4 | +| 2 | 1 | 1 | 2 | 1 | 1 | +| 2 | 1 | 1 | 2 | 1 | 1 | Thus, the minimum total cost is `4 + 1 + 1 = 6`. diff --git a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md index 6434e33b7..8b9ab9261 100644 --- a/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md @@ -19,24 +19,10 @@ Return an integer denoting the count of **non-empty subarrays** of `nums` such t The possible subarrays with `k = 2` distinct integers, each appearing at least `m = 2` times are: -Subarray - -Distinct -numbers - -Frequency - -[1, 2, 1, 2] - -{1, 2} → 2 - -{1: 2, 2: 2} - -[1, 2, 1, 2, 2] - -{1, 2} → 2 - -{1: 2, 2: 3} +| Subarray | Distinct
numbers | Frequency | +|---|---|---| +| `[1, 2, 1, 2]` | `{1, 2}` → `2` | `{1: 2, 2: 2}` | +| `[1, 2, 1, 2, 2]` | `{1, 2}` → `2` | `{1: 2, 2: 3}` | Thus, the answer is 2. @@ -50,30 +36,11 @@ Thus, the answer is 2. The possible subarrays with `k = 2` distinct integers, each appearing at least `m = 1` times are: -Subarray - -Distinct -numbers - -Frequency - -[3, 1] - -{3, 1} → 2 - -{3: 1, 1: 1} - -[1, 2] - -{1, 2} → 2 - -{1: 1, 2: 1} - -[2, 4] - -{2, 4} → 2 - -{2: 1, 4: 1} +| Subarray | Distinct
numbers | Frequency | +|---|---|---| +| `[3, 1]` | `{3, 1}` → `2` | `{3: 1, 1: 1}` | +| `[1, 2]` | `{1, 2}` → `2` | `{1: 1, 2: 1}` | +| `[2, 4]` | `{2, 4}` → `2` | `{2: 1, 4: 1}` | Thus, the answer is 3. diff --git a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md index 6c0c25064..f746d17b9 100644 --- a/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md @@ -31,37 +31,11 @@ The term `gcd(a, b)` denotes the **greatest common divisor** of `a` and `b`. Construct `prefixGcd`: -`i` - -`nums[i]` - -mxi - -`prefixGcd[i]` - -0 - -2 - -2 - -2 - -1 - -6 - -6 - -6 - -2 - -4 - -6 - -2 +| `i` | `nums[i]` | `mxi` | `prefixGcd[i]` | +| --: | --------: | ---------------: | -------------: | +| 0 | 2 | 2 | 2 | +| 1 | 6 | 6 | 6 | +| 2 | 4 | 6 | 2 | `prefixGcd = [2, 6, 2]`. After sorting, it forms `[2, 2, 6]`. @@ -77,45 +51,12 @@ Pair the smallest and largest elements: `gcd(2, 6) = 2`. The remaining middle el Construct `prefixGcd`: -`i` - -`nums[i]` - -mxi - -`prefixGcd[i]` - -0 - -3 - -3 - -3 - -1 - -6 - -6 - -6 - -2 - -2 - -6 - -2 - -3 - -8 - -8 - -8 +| `i` | `nums[i]` | `mxi` | `prefixGcd[i]` | +|---:|---:|---:|---:| +| 0 | 3 | 3 | 3 | +| 1 | 6 | 6 | 6 | +| 2 | 2 | 6 | 2 | +| 3 | 8 | 8 | 8 | `prefixGcd = [3, 6, 2, 8]`. After sorting, it forms `[2, 3, 6, 8]`. From 5dacf83f222cf7e62ab2609786105b9bdd619f02 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 Jul 2026 08:49:46 +0300 Subject: [PATCH 22/22] Added tests --- .../SolutionTest.java | 60 +++++++++++++++++++ .../SolutionTest.java | 58 +++++++++++++++++- .../SolutionTest.java | 56 +++++++++++++++++ 3 files changed, 171 insertions(+), 3 deletions(-) diff --git a/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java index 7003e8321..59a1bf1a7 100644 --- a/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java +++ b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java @@ -25,4 +25,64 @@ void minOperations3() { void minOperations4() { assertThat(new Solution().minOperations("abc"), equalTo(0)); } + + @Test + void minOperations5() { + assertThat(new Solution().minOperations("a"), equalTo(0)); + } + + @Test + void minOperations6() { + assertThat(new Solution().minOperations("z"), equalTo(0)); + } + + @Test + void minOperations7() { + assertThat(new Solution().minOperations("ab"), equalTo(0)); + } + + @Test + void minOperations8() { + assertThat(new Solution().minOperations("aa"), equalTo(0)); + } + + @Test + void minOperations9() { + assertThat(new Solution().minOperations("ba"), equalTo(-1)); + } + + @Test + void minOperations10() { + assertThat(new Solution().minOperations("aaa"), equalTo(0)); + } + + @Test + void minOperations11() { + assertThat(new Solution().minOperations("abcde"), equalTo(0)); + } + + @Test + void minOperations12() { + assertThat(new Solution().minOperations("bca"), equalTo(2)); + } + + @Test + void minOperations13() { + assertThat(new Solution().minOperations("bac"), equalTo(1)); + } + + @Test + void minOperations14() { + assertThat(new Solution().minOperations("cba"), equalTo(3)); + } + + @Test + void minOperations15() { + assertThat(new Solution().minOperations("cbba"), equalTo(3)); + } + + @Test + void minOperations16() { + assertThat(new Solution().minOperations("cbca"), equalTo(2)); + } } diff --git a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java index 20c7268df..87ed5f4bd 100644 --- a/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java +++ b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java @@ -8,16 +8,68 @@ class SolutionTest { @Test void countCommas() { - assertThat(new Solution().countCommas(998), equalTo(0L)); + assertThat(new Solution().countCommas(0L), equalTo(0L)); } @Test void countCommas2() { - assertThat(new Solution().countCommas(1002), equalTo(3L)); + assertThat(new Solution().countCommas(998L), equalTo(0L)); } @Test void countCommas3() { - assertThat(new Solution().countCommas(1_000_000), equalTo(999_002L)); + assertThat(new Solution().countCommas(999L), equalTo(0L)); + } + + @Test + void countCommas4() { + assertThat(new Solution().countCommas(1000L), equalTo(1L)); + } + + @Test + void countCommas5() { + assertThat(new Solution().countCommas(1002L), equalTo(3L)); + } + + @Test + void countCommas6() { + assertThat(new Solution().countCommas(999_999L), equalTo(999_000L)); + } + + @Test + void countCommas7() { + assertThat(new Solution().countCommas(1_000_000L), equalTo(999_002L)); + } + + @Test + void countCommas8() { + assertThat(new Solution().countCommas(1_000_005L), equalTo(999_012L)); + } + + @Test + void countCommas9() { + assertThat(new Solution().countCommas(1_000_000_000L), equalTo(1_998_999_003L)); + } + + @Test + void countCommas10() { + assertThat(new Solution().countCommas(1_000_000_000_000L), equalTo(2_998_998_999_004L)); + } + + @Test + void countCommas11() { + assertThat( + new Solution().countCommas(1_000_000_000_000_000L), + equalTo(3_998_998_998_999_005L)); + } + + @Test + void countCommas12() { + assertThat(new Solution().countCommas(999_999_999L), equalTo(1_998_999_000L)); + } + + @Test + void countCommas13() { + assertThat(new Solution().countCommas(999_999_999_999L), equalTo(2_998_998_999_000L)); } } diff --git a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java index e0fcaf3ed..9ba01fe7b 100644 --- a/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java +++ b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java @@ -22,4 +22,60 @@ void maxActivated3() { new Solution().maxActivated(new int[][] {{2, 3}, {2, 2}, {1, 1}, {4, 5}}), equalTo(4)); } + + @Test + void maxActivated4() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}}), equalTo(2)); + } + + @Test + void maxActivated5() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}, {2, 2}}), equalTo(3)); + } + + @Test + void maxActivated6() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}, {1, 2}}), equalTo(3)); + } + + @Test + void maxActivated7() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}, {2, 1}}), equalTo(3)); + } + + @Test + void maxActivated8() { + assertThat( + new Solution().maxActivated(new int[][] {{1, 1}, {1, 2}, {1, 3}, {1, 4}}), + equalTo(5)); + } + + @Test + void maxActivated9() { + assertThat( + new Solution().maxActivated(new int[][] {{1, 1}, {1, 2}, {5, 5}, {5, 6}}), + equalTo(5)); + } + + @Test + void maxActivated10() { + assertThat( + new Solution() + .maxActivated(new int[][] {{1, 1}, {1, 2}, {2, 2}, {3, 3}, {3, 4}, {4, 4}}), + equalTo(7)); + } + + @Test + void maxActivated11() { + assertThat( + new Solution().maxActivated(new int[][] {{1, 1}, {2, 2}, {3, 3}, {4, 4}}), + equalTo(3)); + } + + @Test + void maxActivated12() { + assertThat( + new Solution().maxActivated(new int[][] {{10, 10}, {10, 20}, {20, 20}, {20, 10}}), + equalTo(5)); + } }