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..170d4f734 --- /dev/null +++ b/src/main/java/g3801_3900/s3853_merge_close_characters/Solution.java @@ -0,0 +1,26 @@ +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..02e2ec278 --- /dev/null +++ b/src/main/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/Solution.java @@ -0,0 +1,47 @@ +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..e2c8293c6 --- /dev/null +++ b/src/main/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/Solution.java @@ -0,0 +1,34 @@ +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..cc716c193 --- /dev/null +++ b/src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java @@ -0,0 +1,19 @@ +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..a39fd07eb --- /dev/null +++ b/src/main/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/readme.md @@ -0,0 +1,50 @@ +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..3152b2866 --- /dev/null +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/Solution.java @@ -0,0 +1,87 @@ +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; + +@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); + } + + 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; + } + } +} 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..8b9ab9261 --- /dev/null +++ b/src/main/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/readme.md @@ -0,0 +1,51 @@ +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..6e7da03bf --- /dev/null +++ b/src/main/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/Solution.java @@ -0,0 +1,44 @@ +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..57c963638 --- /dev/null +++ b/src/main/java/g3801_3900/s3866_first_unique_even_element/Solution.java @@ -0,0 +1,19 @@ +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..a1d801089 --- /dev/null +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/Solution.java @@ -0,0 +1,36 @@ +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..f746d17b9 --- /dev/null +++ b/src/main/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/readme.md @@ -0,0 +1,68 @@ +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..c460bb32d --- /dev/null +++ b/src/main/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/Solution.java @@ -0,0 +1,129 @@ +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..5cad6294c --- /dev/null +++ b/src/main/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/Solution.java @@ -0,0 +1,79 @@ +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 = 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]; + 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..4b03fed62 --- /dev/null +++ b/src/main/java/g3801_3900/s3875_construct_uniform_parity_array_i/Solution.java @@ -0,0 +1,11 @@ +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%) + +@SuppressWarnings("java:S1172") +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..d9f56af74 --- /dev/null +++ b/src/test/java/g3801_3900/s3853_merge_close_characters/SolutionTest.java @@ -0,0 +1,23 @@ +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 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 new file mode 100644 index 000000000..0dc602bbe --- /dev/null +++ b/src/test/java/g3801_3900/s3854_minimum_operations_to_make_array_parity_alternating/SolutionTest.java @@ -0,0 +1,27 @@ +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 makeParityAlternating() { + assertThat( + new Solution().makeParityAlternating(new int[] {-2, -3, 1, 4}), + equalTo(new int[] {2, 6})); + } + + @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 new file mode 100644 index 000000000..656386b66 --- /dev/null +++ b/src/test/java/g3801_3900/s3855_sum_of_k_digit_numbers_in_a_range/SolutionTest.java @@ -0,0 +1,23 @@ +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 sumOfNumbers() { + assertThat(new Solution().sumOfNumbers(1, 2, 2), equalTo(66)); + } + + @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/s3856_trim_trailing_vowels/SolutionTest.java b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java new file mode 100644 index 000000000..469fad95b --- /dev/null +++ b/src/test/java/g3801_3900/s3856_trim_trailing_vowels/SolutionTest.java @@ -0,0 +1,23 @@ +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 trimTrailingVowels() { + assertThat(new Solution().trimTrailingVowels("idea"), equalTo("id")); + } + + @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 new file mode 100644 index 000000000..47c8611ac --- /dev/null +++ b/src/test/java/g3801_3900/s3857_minimum_cost_to_split_into_ones/SolutionTest.java @@ -0,0 +1,23 @@ +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 minCost() { + assertThat(new Solution().minCost(1), equalTo(0)); + } + + @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 new file mode 100644 index 000000000..765fea3ba --- /dev/null +++ b/src/test/java/g3801_3900/s3858_minimum_bitwise_or_from_grid/SolutionTest.java @@ -0,0 +1,23 @@ +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 minimumOR() { + assertThat(new Solution().minimumOR(new int[][] {{1, 5}, {2, 4}}), equalTo(3)); + } + + @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 new file mode 100644 index 000000000..aac274c86 --- /dev/null +++ b/src/test/java/g3801_3900/s3859_count_subarrays_with_k_distinct_integers/SolutionTest.java @@ -0,0 +1,23 @@ +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 countSubarrays() { + assertThat(new Solution().countSubarrays(new int[] {1, 2, 1, 2, 2}, 2, 2), equalTo(2L)); + } + + @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 new file mode 100644 index 000000000..2352c5b4b --- /dev/null +++ b/src/test/java/g3801_3900/s3861_minimum_capacity_box/SolutionTest.java @@ -0,0 +1,23 @@ +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 minimumIndex() { + assertThat(new Solution().minimumIndex(new int[] {1, 5, 3, 7}, 3), equalTo(2)); + } + + @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 new file mode 100644 index 000000000..b4736eb76 --- /dev/null +++ b/src/test/java/g3801_3900/s3862_find_the_smallest_balanced_index/SolutionTest.java @@ -0,0 +1,23 @@ +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 smallestBalancedIndex() { + assertThat(new Solution().smallestBalancedIndex(new int[] {2, 1, 2}), 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 new file mode 100644 index 000000000..59a1bf1a7 --- /dev/null +++ b/src/test/java/g3801_3900/s3863_minimum_operations_to_sort_a_string/SolutionTest.java @@ -0,0 +1,88 @@ +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 minOperations() { + assertThat(new Solution().minOperations("dog"), equalTo(1)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations("card"), equalTo(2)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations("gf"), equalTo(-1)); + } + + @Test + 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/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..3ae743f19 --- /dev/null +++ b/src/test/java/g3801_3900/s3864_minimum_cost_to_partition_a_binary_string/SolutionTest.java @@ -0,0 +1,23 @@ +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 minCost() { + assertThat(new Solution().minCost("1010", 2, 1), equalTo(6L)); + } + + @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 new file mode 100644 index 000000000..ac27e1415 --- /dev/null +++ b/src/test/java/g3801_3900/s3866_first_unique_even_element/SolutionTest.java @@ -0,0 +1,23 @@ +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 firstUniqueEven() { + assertThat(new Solution().firstUniqueEven(new int[] {3, 4, 2, 5, 4, 6}), equalTo(2)); + } + + @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)); + } +} 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..b200b7fdb --- /dev/null +++ b/src/test/java/g3801_3900/s3867_sum_of_gcd_of_formed_pairs/SolutionTest.java @@ -0,0 +1,23 @@ +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 gcdSum() { + assertThat(new Solution().gcdSum(new int[] {2, 6, 4}), equalTo(2L)); + } + + @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 new file mode 100644 index 000000000..43c140288 --- /dev/null +++ b/src/test/java/g3801_3900/s3868_minimum_cost_to_equalize_arrays_using_swaps/SolutionTest.java @@ -0,0 +1,23 @@ +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 minCost() { + assertThat(new Solution().minCost(new int[] {10, 20}, new int[] {20, 10}), equalTo(0)); + } + + @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 new file mode 100644 index 000000000..1c4333165 --- /dev/null +++ b/src/test/java/g3801_3900/s3869_count_fancy_numbers_in_a_range/SolutionTest.java @@ -0,0 +1,23 @@ +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 countFancy() { + assertThat(new Solution().countFancy(8, 10), equalTo(3L)); + } + + @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 new file mode 100644 index 000000000..2f5e0ce31 --- /dev/null +++ b/src/test/java/g3801_3900/s3870_count_commas_in_range/SolutionTest.java @@ -0,0 +1,23 @@ +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 countCommas() { + assertThat(new Solution().countCommas(998), equalTo(0)); + } + + @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 new file mode 100644 index 000000000..87ed5f4bd --- /dev/null +++ b/src/test/java/g3801_3900/s3871_count_commas_in_range_ii/SolutionTest.java @@ -0,0 +1,75 @@ +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 countCommas() { + assertThat(new Solution().countCommas(0L), equalTo(0L)); + } + + @Test + void countCommas2() { + assertThat(new Solution().countCommas(998L), equalTo(0L)); + } + + @Test + void countCommas3() { + 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/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..122e1e574 --- /dev/null +++ b/src/test/java/g3801_3900/s3872_longest_arithmetic_sequence_after_changing_at_most_one_element/SolutionTest.java @@ -0,0 +1,23 @@ +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 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)); + } + + @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 new file mode 100644 index 000000000..9ba01fe7b --- /dev/null +++ b/src/test/java/g3801_3900/s3873_maximum_points_activated_with_one_addition/SolutionTest.java @@ -0,0 +1,81 @@ +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 maxActivated() { + assertThat(new Solution().maxActivated(new int[][] {{1, 1}, {1, 2}, {2, 2}}), 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)); + } + + @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)); + } +} 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..596ff0941 --- /dev/null +++ b/src/test/java/g3801_3900/s3875_construct_uniform_parity_array_i/SolutionTest.java @@ -0,0 +1,23 @@ +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 uniformArray() { + assertThat(new Solution().uniformArray(new int[] {2, 3}), 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)); + } +}