From 0444f8c4871ef5b6ee979a59772ba70809ae33eb Mon Sep 17 00:00:00 2001 From: ThanhNIT <93962044+ThanhNIT@users.noreply.github.com> Date: Sat, 23 May 2026 20:43:44 +0700 Subject: [PATCH] Added tasks 3783-3805 --- .../Solution.java | 19 +++ .../readme.md | 48 ++++++++ .../Solution.java | 22 ++++ .../readme.md | 46 +++++++ .../Solution.java | 41 +++++++ .../readme.md | 63 ++++++++++ .../Solution.java | 51 ++++++++ .../readme.md | 78 ++++++++++++ .../Solution.java | 22 ++++ .../s3788_maximum_score_of_a_split/readme.md | 53 ++++++++ .../Solution.java | 18 +++ .../readme.md | 52 ++++++++ .../Solution.java | 20 +++ .../readme.md | 43 +++++++ .../Solution.java | 49 ++++++++ .../readme.md | 46 +++++++ .../readme.md | 76 ++++++++++++ .../script.sql | 7 ++ .../s3794_reverse_string_prefix/Solution.java | 16 +++ .../s3794_reverse_string_prefix/readme.md | 43 +++++++ .../Solution.java | 43 +++++++ .../readme.md | 43 +++++++ .../Solution.java | 28 +++++ .../readme.md | 48 ++++++++ .../Solution.java | 55 +++++++++ .../readme.md | 88 ++++++++++++++ .../s3798_largest_even_number/Solution.java | 16 +++ .../s3798_largest_even_number/readme.md | 44 +++++++ .../s3799_word_squares_ii/Solution.java | 37 ++++++ .../s3799_word_squares_ii/readme.md | 56 +++++++++ .../Solution.java | 39 ++++++ .../readme.md | 57 +++++++++ .../Solution.java | 114 ++++++++++++++++++ .../readme.md | 89 ++++++++++++++ .../Solution.java | 28 +++++ .../s3803_count_residue_prefixes/readme.md | 50 ++++++++ .../Solution.java | 24 ++++ .../readme.md | 37 ++++++ .../Solution.java | 27 +++++ .../s3805_count_caesar_cipher_pairs/readme.md | 52 ++++++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 30 +++++ .../SolutionTest.java | 40 ++++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../MysqlTest.java | 73 +++++++++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 26 ++++ .../SolutionTest.java | 28 +++++ .../SolutionTest.java | 23 ++++ .../s3799_word_squares_ii/SolutionTest.java | 29 +++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 31 +++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 18 +++ 60 files changed, 2334 insertions(+) create mode 100644 src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/Solution.java create mode 100644 src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/readme.md create mode 100644 src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/Solution.java create mode 100644 src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/readme.md create mode 100644 src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/Solution.java create mode 100644 src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/readme.md create mode 100644 src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/Solution.java create mode 100644 src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/readme.md create mode 100644 src/main/java/g3701_3800/s3788_maximum_score_of_a_split/Solution.java create mode 100644 src/main/java/g3701_3800/s3788_maximum_score_of_a_split/readme.md create mode 100644 src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/Solution.java create mode 100644 src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/readme.md create mode 100644 src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/Solution.java create mode 100644 src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/readme.md create mode 100644 src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/Solution.java create mode 100644 src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/readme.md create mode 100644 src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/readme.md create mode 100644 src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/script.sql create mode 100644 src/main/java/g3701_3800/s3794_reverse_string_prefix/Solution.java create mode 100644 src/main/java/g3701_3800/s3794_reverse_string_prefix/readme.md create mode 100644 src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/Solution.java create mode 100644 src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/readme.md create mode 100644 src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/Solution.java create mode 100644 src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/readme.md create mode 100644 src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/Solution.java create mode 100644 src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/readme.md create mode 100644 src/main/java/g3701_3800/s3798_largest_even_number/Solution.java create mode 100644 src/main/java/g3701_3800/s3798_largest_even_number/readme.md create mode 100644 src/main/java/g3701_3800/s3799_word_squares_ii/Solution.java create mode 100644 src/main/java/g3701_3800/s3799_word_squares_ii/readme.md create mode 100644 src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/Solution.java create mode 100644 src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/readme.md create mode 100644 src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/Solution.java create mode 100644 src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/readme.md create mode 100644 src/main/java/g3801_3900/s3803_count_residue_prefixes/Solution.java create mode 100644 src/main/java/g3801_3900/s3803_count_residue_prefixes/readme.md create mode 100644 src/main/java/g3801_3900/s3804_number_of_centered_subarrays/Solution.java create mode 100644 src/main/java/g3801_3900/s3804_number_of_centered_subarrays/readme.md create mode 100644 src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/Solution.java create mode 100644 src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/readme.md create mode 100644 src/test/java/g3701_3800/s3783_mirror_distance_of_an_integer/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3788_maximum_score_of_a_split/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3790_smallest_all_ones_multiple/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3793_find_users_with_high_token_usage/MysqlTest.java create mode 100644 src/test/java/g3701_3800/s3794_reverse_string_prefix/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3798_largest_even_number/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3799_word_squares_ii/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3803_count_residue_prefixes/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3804_number_of_centered_subarrays/SolutionTest.java create mode 100644 src/test/java/g3801_3900/s3805_count_caesar_cipher_pairs/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/Solution.java b/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/Solution.java new file mode 100644 index 000000000..8707b773f --- /dev/null +++ b/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/Solution.java @@ -0,0 +1,19 @@ +package g3701_3800.s3783_mirror_distance_of_an_integer; + +// #Easy #Math #Mid_Level #Weekly_Contest_481 #2026_05_22_Time_1_ms_(99.88%)_Space_42.74_MB_(18.96%) + +public class Solution { + private int rev(int n) { + int a = 0; + while (n > 0) { + a = a * 10 + (n % 10); + n /= 10; + } + return a; + } + + public int mirrorDistance(int n) { + int m = rev(n); + return Math.abs(m - n); + } +} diff --git a/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/readme.md b/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/readme.md new file mode 100644 index 000000000..72f2e1bf9 --- /dev/null +++ b/src/main/java/g3701_3800/s3783_mirror_distance_of_an_integer/readme.md @@ -0,0 +1,48 @@ +3783\. Mirror Distance of an Integer + +Easy + +You are given an integer `n`. + +Define its **mirror distance** as: `abs(n - reverse(n))` where `reverse(n)` is the integer formed by reversing the digits of `n`. + +Return an integer denoting the mirror distance of `n`. + +`abs(x)` denotes the absolute value of `x`. + +**Example 1:** + +**Input:** n = 25 + +**Output:** 27 + +**Explanation:** + +* `reverse(25) = 52`. +* Thus, the answer is `abs(25 - 52) = 27`. + +**Example 2:** + +**Input:** n = 10 + +**Output:** 9 + +**Explanation:** + +* `reverse(10) = 01` which is 1. +* Thus, the answer is `abs(10 - 1) = 9`. + +**Example 3:** + +**Input:** n = 7 + +**Output:** 0 + +**Explanation:** + +* `reverse(7) = 7`. +* Thus, the answer is `abs(7 - 7) = 0`. + +**Constraints:** + +* 1 <= n <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/Solution.java b/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/Solution.java new file mode 100644 index 000000000..f1affc006 --- /dev/null +++ b/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/Solution.java @@ -0,0 +1,22 @@ +package g3701_3800.s3784_minimum_deletion_cost_to_make_all_characters_equal; + +// #Medium #Array #String #Hash_Table #Enumeration #Senior #Weekly_Contest_481 +// #2026_05_22_Time_3_ms_(94.77%)_Space_104.58_MB_(97.09%) + +public class Solution { + public long minCost(String s, int[] cost) { + long[] arr = new long[26]; + long m = Integer.MIN_VALUE; + long sum = 0; + for (int i = 0; i < s.length(); i++) { + arr[s.charAt(i) - 'a'] += cost[i]; + } + for (long i : arr) { + if (i > m) { + m = i; + } + sum += i; + } + return sum - m; + } +} diff --git a/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/readme.md b/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/readme.md new file mode 100644 index 000000000..a40c95528 --- /dev/null +++ b/src/main/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/readme.md @@ -0,0 +1,46 @@ +3784\. Minimum Deletion Cost to Make All Characters Equal + +Medium + +You are given a string `s` of length `n` and an integer array `cost` of the same length, where `cost[i]` is the cost to **delete** the ith character of `s`. + +You may delete any number of characters from `s` (possibly none), such that the resulting string is **non-empty** and consists of **equal** characters. + +Return an integer denoting the **minimum** total deletion cost required. + +**Example 1:** + +**Input:** s = "aabaac", cost = [1,2,3,4,1,10] + +**Output:** 11 + +**Explanation:** + +Deleting the characters at indices 0, 1, 2, 3, 4 results in the string `"c"`, which consists of equal characters, and the total cost is `cost[0] + cost[1] + cost[2] + cost[3] + cost[4] = 1 + 2 + 3 + 4 + 1 = 11`. + +**Example 2:** + +**Input:** s = "abc", cost = [10,5,8] + +**Output:** 13 + +**Explanation:** + +Deleting the characters at indices 1 and 2 results in the string `"a"`, which consists of equal characters, and the total cost is `cost[1] + cost[2] = 5 + 8 = 13`. + +**Example 3:** + +**Input:** s = "zzzzz", cost = [67,67,67,67,67] + +**Output:** 0 + +**Explanation:** + +All characters in `s` are equal, so the deletion cost is 0. + +**Constraints:** + +* `n == s.length == cost.length` +* 1 <= n <= 105 +* 1 <= cost[i] <= 109 +* `s` consists of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/Solution.java b/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/Solution.java new file mode 100644 index 000000000..2dbf8ed20 --- /dev/null +++ b/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/Solution.java @@ -0,0 +1,41 @@ +package g3701_3800.s3785_minimum_swaps_to_avoid_forbidden_values; + +// #Hard #Array #Hash_Table #Greedy #Counting #Senior_Staff #Weekly_Contest_481 +// #2026_05_22_Time_88_ms_(86.89%)_Space_159.68_MB_(81.97%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minSwaps(int[] nums, int[] forbidden) { + int n = nums.length; + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + for (int num : forbidden) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + for (int val : map.values()) { + if (val > n) { + return -1; + } + } + Map map2 = new HashMap<>(); + int total = 0; + for (int i = 0; i < n; i++) { + if (nums[i] == forbidden[i]) { + map2.put(nums[i], map2.getOrDefault(nums[i], 0) + 1); + total++; + } + } + if (total == 0) { + return 0; + } + int maxSwaps = 0; + for (int num : map2.values()) { + maxSwaps = Math.max(maxSwaps, num); + } + return Math.max(maxSwaps, (total + 1) / 2); + } +} diff --git a/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/readme.md b/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/readme.md new file mode 100644 index 000000000..1d51949cc --- /dev/null +++ b/src/main/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/readme.md @@ -0,0 +1,63 @@ +3785\. Minimum Swaps to Avoid Forbidden Values + +Hard + +You are given two integer arrays, `nums` and `forbidden`, each of length `n`. + +You may perform the following operation any number of times (including zero): + +* Choose two **distinct** indices `i` and `j`, and swap `nums[i]` with `nums[j]`. + +Return the **minimum** number of swaps required such that, for every index `i`, the value of `nums[i]` is **not equal** to `forbidden[i]`. If no amount of swaps can ensure that every index avoids its forbidden value, return -1. + +**Example 1:** + +**Input:** nums = [1,2,3], forbidden = [3,2,1] + +**Output:** 1 + +**Explanation:** + +One optimal set of swaps: + +* Select indices `i = 0` and `j = 1` in `nums` and swap them, resulting in `nums = [2, 1, 3]`. +* After this swap, for every index `i`, `nums[i]` is not equal to `forbidden[i]`. + +**Example 2:** + +**Input:** nums = [4,6,6,5], forbidden = [4,6,5,5] + +**Output:** 2 + +**Explanation:** + +One optimal set of swaps: + +* Select indices `i = 0` and `j = 2` in `nums` and swap them, resulting in `nums = [6, 6, 4, 5]`. +* Select indices `i = 1` and `j = 3` in `nums` and swap them, resulting in `nums = [6, 5, 4, 6]`. +* After these swaps, for every index `i`, `nums[i]` is not equal to `forbidden[i]`. + +**Example 3:** + +**Input:** nums = [7,7], forbidden = [8,7] + +**Output:** \-1 + +**Explanation:** + +It is not possible to make `nums[i]` different from `forbidden[i]` for all indices. + +**Example 4:** + +**Input:** nums = [1,2], forbidden = [2,1] + +**Output:** 0 + +**Explanation:** + +No swaps are required because `nums[i]` is already different from `forbidden[i]` for all indices, so the answer is 0. + +**Constraints:** + +* 1 <= n == nums.length == forbidden.length <= 105 +* 1 <= nums[i], forbidden[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/Solution.java b/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/Solution.java new file mode 100644 index 000000000..19a5f634a --- /dev/null +++ b/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/Solution.java @@ -0,0 +1,51 @@ +package g3701_3800.s3786_total_sum_of_interaction_cost_in_tree_groups; + +// #Hard #Array #Tree #Senior_Staff #Weekly_Contest_481 #Depth_First_Search +// #2026_05_22_Time_82_ms_(90.67%)_Space_296.78_MB_(21.33%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + private long totalCost = 0; + private int[][] counts; + private int[] totalInGroup; + private List> adj; + + public long interactionCosts(int n, int[][] edges, int[] group) { + adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int[] edge : edges) { + adj.get(edge[0]).add(edge[1]); + adj.get(edge[1]).add(edge[0]); + } + totalInGroup = new int[21]; + for (int g : group) { + totalInGroup[g]++; + } + counts = new int[n][21]; + dfs(0, -1, group); + return totalCost; + } + + private void dfs(int u, int p, int[] group) { + counts[u][group[u]] = 1; + for (int v : adj.get(u)) { + if (v == p) { + continue; + } + dfs(v, u, group); + for (int g = 1; g <= 20; g++) { + if (totalInGroup[g] < 2) { + continue; + } + long inSubtree = counts[v][g]; + long outsideSubtree = totalInGroup[g] - inSubtree; + totalCost += inSubtree * outsideSubtree; + counts[u][g] += counts[v][g]; + } + } + } +} diff --git a/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/readme.md b/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/readme.md new file mode 100644 index 000000000..e4f28b2ab --- /dev/null +++ b/src/main/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/readme.md @@ -0,0 +1,78 @@ +3786\. Total Sum of Interaction Cost in Tree Groups + +Hard + +You are given an integer `n` and an undirected tree with `n` nodes numbered from 0 to `n - 1`. This is represented by a 2D array `edges` of length `n - 1`, where edges[i] = [ui, vi] indicates an undirected edge between nodes ui and vi. + +You are also given an integer array `group` of length `n`, where `group[i]` denotes the group label assigned to node `i`. + +* Two nodes `u` and `v` are considered part of the same group if `group[u] == group[v]`. +* The **interaction cost** between `u` and `v` is defined as the number of edges on the unique path connecting them in the tree. + +Return an integer denoting the **sum** of interaction costs over all **unordered** pairs `(u, v)` with `u != v` such that `group[u] == group[v]`. + +**Example 1:** + +**Input:** n = 3, edges = [[0,1],[1,2]], group = [1,1,1] + +**Output:** 4 + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-50538-pm.png)** + +All nodes belong to group 1. The interaction costs between the pairs of nodes are: + +* Nodes `(0, 1)`: 1 +* Nodes `(1, 2)`: 1 +* Nodes `(0, 2)`: 2 + +Thus, the total interaction cost is `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** n = 3, edges = [[0,1],[1,2]], group = [3,2,3] + +**Output:** 2 + +**Explanation:** + +* Nodes 0 and 2 belong to group 3. The interaction cost between this pair is 2. +* Node 1 belongs to a different group and forms no valid pair. Therefore, the total interaction cost is 2. + +**Example 3:** + +**Input:** n = 4, edges = [[0,1],[0,2],[0,3]], group = [1,1,4,4] + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-51312-pm.png) + +Nodes belonging to the same groups and their interaction costs are: + +* Group 1: Nodes `(0, 1)`: 1 +* Group 4: Nodes `(2, 3)`: 2 + +Thus, the total interaction cost is `1 + 2 = 3`. + +**Example 4:** + +**Input:** n = 2, edges = [[0,1]], group = [9,8] + +**Output:** 0 + +**Explanation:** + +All nodes belong to different groups and there are no valid pairs. Therefore, the total interaction cost is 0. + +**Constraints:** + +* 1 <= n <= 105 +* `edges.length == n - 1` +* edges[i] = [ui, vi] +* 0 <= ui, vi <= n - 1 +* `group.length == n` +* `1 <= group[i] <= 20` +* The input is generated such that `edges` represents a valid tree. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/Solution.java b/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/Solution.java new file mode 100644 index 000000000..76fccd3f6 --- /dev/null +++ b/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/Solution.java @@ -0,0 +1,22 @@ +package g3701_3800.s3788_maximum_score_of_a_split; + +// #Medium #Array #Prefix_Sum #Senior #Weekly_Contest_482 +// #2026_05_22_Time_2_ms_(100.00%)_Space_113.41_MB_(96.82%) + +public class Solution { + public long maximumScore(int[] nums) { + long ans = nums[0] - (long) nums[1]; + long prefixSum = 0; + int n = nums.length; + int min = nums[n - 1]; + for (int i = 0; i < n - 1; i++) { + prefixSum += nums[i]; + } + for (int i = n - 2; i >= 0; i--) { + ans = Math.max(ans, prefixSum - min); + min = Math.min(min, nums[i]); + prefixSum -= nums[i]; + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/readme.md b/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/readme.md new file mode 100644 index 000000000..4b142a2e2 --- /dev/null +++ b/src/main/java/g3701_3800/s3788_maximum_score_of_a_split/readme.md @@ -0,0 +1,53 @@ +3788\. Maximum Score of a Split + +Medium + +You are given an integer array `nums` of length `n`. + +Choose an index `i` such that `0 <= i < n - 1`. + +For a chosen split index `i`: + +* Let `prefixSum(i)` be the sum of `nums[0] + nums[1] + ... + nums[i]`. +* Let `suffixMin(i)` be the minimum value among `nums[i + 1], nums[i + 2], ..., nums[n - 1]`. + +The **score** of a split at index `i` is defined as: + +`score(i) = prefixSum(i) - suffixMin(i)` + +Return an integer denoting the **maximum** score over all valid split indices. + +**Example 1:** + +**Input:** nums = [10,-1,3,-4,-5] + +**Output:** 17 + +**Explanation:** + +The optimal split is at `i = 2`, `score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17`. + +**Example 2:** + +**Input:** nums = [-7,-5,3] + +**Output:** \-2 + +**Explanation:** + +The optimal split is at `i = 0`, `score(0) = prefixSum(0) - suffixMin(0) = (-7) - (-5) = -2`. + +**Example 3:** + +**Input:** nums = [1,1] + +**Output:** 0 + +**Explanation:** + +The only valid split is at `i = 0`, `score(0) = prefixSum(0) - suffixMin(0) = 1 - 1 = 0`. + +**Constraints:** + +* 2 <= nums.length <= 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/Solution.java b/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/Solution.java new file mode 100644 index 000000000..cee0972e2 --- /dev/null +++ b/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/Solution.java @@ -0,0 +1,18 @@ +package g3701_3800.s3789_minimum_cost_to_acquire_required_items; + +// #Medium #Math #Greedy #Staff #Weekly_Contest_482 +// #2026_05_22_Time_1_ms_(100.00%)_Space_42.78_MB_(58.24%) + +public class Solution { + public long minimumCost(int cost1, int cost2, int costBoth, int need1, int need2) { + long min; + long max; + long ans; + min = Math.min(need1, need2); + max = Math.max(need1, need2); + ans = (long) need1 * cost1 + (long) need2 * cost2; + ans = Math.min(ans, min * costBoth + (need1 - min) * cost1 + (need2 - min) * cost2); + ans = Math.min(ans, max * costBoth); + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/readme.md b/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/readme.md new file mode 100644 index 000000000..da4152326 --- /dev/null +++ b/src/main/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/readme.md @@ -0,0 +1,52 @@ +3789\. Minimum Cost to Acquire Required Items + +Medium + +You are given five integers `cost1`, `cost2`, `costBoth`, `need1`, and `need2`. + +There are three types of items available: + +* An item of **type 1** costs `cost1` and contributes 1 unit to the type 1 requirement only. +* An item of **type 2** costs `cost2` and contributes 1 unit to the type 2 requirement only. +* An item of **type 3** costs `costBoth` and contributes 1 unit to **both** type 1 and type 2 requirements. + +You must collect enough items so that the total contribution toward type 1 is **at least** `need1` and the total contribution toward type 2 is **at least** `need2`. + +Return an integer representing the **minimum** possible total cost to achieve these requirements. + +**Example 1:** + +**Input:** cost1 = 3, cost2 = 2, costBoth = 1, need1 = 3, need2 = 2 + +**Output:** 3 + +**Explanation:** + +After buying three type 3 items, which cost `3 * 1 = 3`, the total contribution to type 1 is 3 (`>= need1 = 3`) and to type 2 is 3 (`>= need2 = 2`). +Any other valid combination would cost more, so the minimum total cost is 3. + +**Example 2:** + +**Input:** cost1 = 5, cost2 = 4, costBoth = 15, need1 = 2, need2 = 3 + +**Output:** 22 + +**Explanation:** + +We buy `need1 = 2` items of type 1 and `need2 = 3` items of type 2: `2 * 5 + 3 * 4 = 10 + 12 = 22`. +Any other valid combination would cost more, so the minimum total cost is 22. + +**Example 3:** + +**Input:** cost1 = 5, cost2 = 4, costBoth = 15, need1 = 0, need2 = 0 + +**Output:** 0 + +**Explanation:** + +Since no items are required (`need1 = need2 = 0`), we buy nothing and pay 0. + +**Constraints:** + +* 1 <= cost1, cost2, costBoth <= 106 +* 0 <= need1, need2 <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/Solution.java b/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/Solution.java new file mode 100644 index 000000000..a41c46eb8 --- /dev/null +++ b/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/Solution.java @@ -0,0 +1,20 @@ +package g3701_3800.s3790_smallest_all_ones_multiple; + +// #Medium #Hash_Table #Math #Staff #Weekly_Contest_482 +// #2026_05_22_Time_8_ms_(100.00%)_Space_42.48_MB_(81.03%) + +public class Solution { + public int minAllOneMultiple(int k) { + if (k % 2 == 0 || k % 5 == 0) { + return -1; + } + int rem = 0; + for (int i = 1; i <= k; i++) { + rem = (rem * 10 + 1) % k; + if (rem == 0) { + return i; + } + } + return -1; + } +} diff --git a/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/readme.md b/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/readme.md new file mode 100644 index 000000000..820508e36 --- /dev/null +++ b/src/main/java/g3701_3800/s3790_smallest_all_ones_multiple/readme.md @@ -0,0 +1,43 @@ +3790\. Smallest All-Ones Multiple + +Medium + +You are given a positive integer `k`. + +Find the **smallest** integer `n` divisible by `k` that consists of **only the digit 1** in its decimal representation (e.g., 1, 11, 111, ...). + +Return an integer denoting the **number of digits** in the decimal representation of `n`. If no such `n` exists, return `-1`. + +**Example 1:** + +**Input:** k = 3 + +**Output:** 3 + +**Explanation:** + +`n = 111` because 111 is divisible by 3, but 1 and 11 are not. The length of `n = 111` is 3. + +**Example 2:** + +**Input:** k = 7 + +**Output:** 6 + +**Explanation:** + +`n = 111111`. The length of `n = 111111` is 6. + +**Example 3:** + +**Input:** k = 2 + +**Output:** \-1 + +**Explanation:** + +There does not exist a valid `n` that is a multiple of 2. + +**Constraints:** + +* 2 <= k <= 105 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/Solution.java b/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/Solution.java new file mode 100644 index 000000000..e2fd758e4 --- /dev/null +++ b/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/Solution.java @@ -0,0 +1,49 @@ +package g3701_3800.s3791_number_of_balanced_integers_in_a_range; + +// #Hard #Dynamic_Programming #Principal #Weekly_Contest_482 +// #2026_05_22_Time_75_ms_(83.12%)_Space_46.63_MB_(70.13%) + +import java.util.Arrays; + +public class Solution { + private long solve(String number, int idx, int diff, int tight, long[][][] dp) { + if (idx == number.length()) { + return diff == 0 ? 1 : 0; + } + if (dp[idx][diff + dp[0].length / 2][tight] != -1) { + return dp[idx][diff + dp[0].length / 2][tight]; + } + long ans = 0; + int ub = tight == 1 ? number.charAt(idx) - '0' : 9; + for (int i = 0; i <= ub; i++) { + int newTight = (tight == 1 && i == ub) ? 1 : 0; + if (idx % 2 == 0) { + ans += solve(number, idx + 1, diff - i, newTight, dp); + } else { + ans += solve(number, idx + 1, diff + i, newTight, dp); + } + } + dp[idx][diff + dp[0].length / 2][tight] = ans; + return dp[idx][diff + dp[0].length / 2][tight]; + } + + private long solve(long num) { + if (num == 0) { + return 1; + } + String number = Long.toString(num); + int len = number.length(); + long[][][] dp = + len % 2 == 1 ? new long[len][(len + 1) * 9 + 1][2] : new long[len][len * 9 + 1][2]; + for (long[][] tbl : dp) { + for (long[] row : tbl) { + Arrays.fill(row, -1); + } + } + return solve(number, 0, 0, 1, dp); + } + + public long countBalanced(long low, long high) { + return solve(high) - solve(low - 1); + } +} diff --git a/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/readme.md b/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/readme.md new file mode 100644 index 000000000..1ec67fa8e --- /dev/null +++ b/src/main/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/readme.md @@ -0,0 +1,46 @@ +3791\. Number of Balanced Integers in a Range + +Hard + +You are given two integers `low` and `high`. + +An integer is called **balanced** if it satisfies **both** of the following conditions: + +* It contains **at least** two digits. +* The **sum of digits at even positions** is equal to the **sum of digits at odd positions** (the leftmost digit has position 1). + +Return an integer representing the number of balanced integers in the range `[low, high]` (both inclusive). + +**Example 1:** + +**Input:** low = 1, high = 100 + +**Output:** 9 + +**Explanation:** + +The 9 balanced numbers between 1 and 100 are 11, 22, 33, 44, 55, 66, 77, 88, and 99. + +**Example 2:** + +**Input:** low = 120, high = 129 + +**Output:** 1 + +**Explanation:** + +Only 121 is balanced because the sum of digits at even and odd positions are both 2. + +**Example 3:** + +**Input:** low = 1234, high = 1234 + +**Output:** 0 + +**Explanation:** + +1234 is not balanced because the sum of digits at odd positions `(1 + 3 = 4)` does not equal the sum at even positions `(2 + 4 = 6)`. + +**Constraints:** + +* 1 <= low <= high <= 1015 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/readme.md b/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/readme.md new file mode 100644 index 000000000..87b54026a --- /dev/null +++ b/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/readme.md @@ -0,0 +1,76 @@ +3793\. Find Users with High Token Usage + +Easy + +Table: prompts + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | user_id | int | + | prompt | varchar | + | tokens | int | + +-------------+---------+ + +(user_id, prompt) is the primary key (unique value) for this table. +Each row represents a prompt submitted by a user to an AI system along with the number of tokens consumed. + +Write a solution to analyze AI prompt usage patterns based on the following requirements: + +* For each user, calculate the total number of prompts they have submitted. +* For each user, calculate the average tokens used per prompt (Rounded to 2 decimal places). +* Only include users who have submitted at least 3 prompts. +* Only include users who have submitted at least one prompt with tokens greater than their own average token usage. + +Return the result table ordered by average tokens in descending order, and then by user_id in ascending order. + +The result format is in the following example. + +Example: + +Input: + +prompts table: + + +---------+--------------------------+--------+ + | user_id | prompt | tokens | + +---------+--------------------------+--------+ + | 1 | Write a blog outline | 120 | + | 1 | Generate SQL query | 80 | + | 1 | Summarize an article | 200 | + | 2 | Create resume bullet | 60 | + | 2 | Improve LinkedIn bio | 70 | + | 3 | Explain neural networks | 300 | + | 3 | Generate interview Q&A | 250 | + | 3 | Write cover letter | 180 | + | 3 | Optimize Python code | 220 | + +---------+--------------------------+--------+ + +Output: + + +---------+--------------+------------+ + | user_id | prompt_count | avg_tokens | + +---------+--------------+------------+ + | 3 | 4 | 237.50 | + | 1 | 3 | 133.33 | + +---------+--------------+------------+ + +Explanation: + +* User 1: + * Total prompts = 3 + * Average tokens = (120 + 80 + 200) / 3 = 133.33 + * Has a prompt with 200 tokens, which is greater than the average + * Included in the result + +* User 2: + * Total prompts = 2 (less than the required minimum) + * Excluded from the result + +* User 3: + * Total prompts = 4 + * Average tokens = (300 + 250 + 180 + 220) / 4 = 237.50 + * Has prompts with 300 and 250 tokens, both greater than the average + * Included in the result + +The result table is ordered by avg_tokens in descending order, then by user_id in ascending order. diff --git a/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/script.sql b/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/script.sql new file mode 100644 index 000000000..5ec8d02e9 --- /dev/null +++ b/src/main/java/g3701_3800/s3793_find_users_with_high_token_usage/script.sql @@ -0,0 +1,7 @@ +# Write your MySQL query statement below +# #Easy #2026_05_22_Time_286_ms_(70.33%)_Space_0.0_MB_(100.00%) +SELECT user_id, COUNT(prompt) as prompt_count, ROUND(AVG(tokens),2) as avg_tokens +FROM prompts +GROUP BY user_id +HAVING prompt_count > 2 AND MAX(tokens) > avg_tokens +ORDER BY avg_tokens DESC, user_id asc; diff --git a/src/main/java/g3701_3800/s3794_reverse_string_prefix/Solution.java b/src/main/java/g3701_3800/s3794_reverse_string_prefix/Solution.java new file mode 100644 index 000000000..9b03a966c --- /dev/null +++ b/src/main/java/g3701_3800/s3794_reverse_string_prefix/Solution.java @@ -0,0 +1,16 @@ +package g3701_3800.s3794_reverse_string_prefix; + +// #Easy #String #Two_Pointers #Mid_Level #Biweekly_Contest_173 +// #2026_05_22_Time_1_ms_(100.00%)_Space_44.69_MB_(49.90%) + +public class Solution { + public String reversePrefix(String s, int k) { + char[] arr = s.toCharArray(); + for (int i = 0; i < k / 2; i++) { + char temp = arr[i]; + arr[i] = arr[k - 1 - i]; + arr[k - 1 - i] = temp; + } + return new String(arr); + } +} diff --git a/src/main/java/g3701_3800/s3794_reverse_string_prefix/readme.md b/src/main/java/g3701_3800/s3794_reverse_string_prefix/readme.md new file mode 100644 index 000000000..61cea26bd --- /dev/null +++ b/src/main/java/g3701_3800/s3794_reverse_string_prefix/readme.md @@ -0,0 +1,43 @@ +3794\. Reverse String Prefix + +Easy + +You are given a string `s` and an integer `k`. + +Reverse the first `k` characters of `s` and return the resulting string. + +**Example 1:** + +**Input:** s = "abcd", k = 2 + +**Output:** "bacd" + +**Explanation:** + +The first `k = 2` characters `"ab"` are reversed to `"ba"`. The final resulting string is `"bacd"`. + +**Example 2:** + +**Input:** s = "xyz", k = 3 + +**Output:** "zyx" + +**Explanation:** + +The first `k = 3` characters `"xyz"` are reversed to `"zyx"`. The final resulting string is `"zyx"`. + +**Example 3:** + +**Input:** s = "hey", k = 1 + +**Output:** "hey" + +**Explanation:** + +The first `k = 1` character `"h"` remains unchanged on reversal. The final resulting string is `"hey"`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists of lowercase English letters. +* `1 <= k <= s.length` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/Solution.java b/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/Solution.java new file mode 100644 index 000000000..82b64f2a8 --- /dev/null +++ b/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/Solution.java @@ -0,0 +1,43 @@ +package g3701_3800.s3795_minimum_subarray_length_with_distinct_sum_at_least_k; + +// #Medium #Array #Hash_Table #Sliding_Window #Senior #Biweekly_Contest_173 +// #2026_05_22_Time_88_ms_(85.14%)_Space_207.14_MB_(6.02%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minLength(int[] nums, int k) { + for (int i : nums) { + if (i >= k) { + return 1; + } + } + int ans = Integer.MAX_VALUE; + int alt = -1; + int i = 0; + int j = 1; + int sum = nums[0]; + if (sum >= k) { + return 1; + } + Map hm = new HashMap<>(); + hm.put(nums[0], 1); + while (i < nums.length && j < nums.length) { + hm.put(nums[j], hm.getOrDefault(nums[j], 0) + 1); + if (hm.get(nums[j]) == 1) { + sum += nums[j]; + } + while (sum >= k) { + ans = Math.min(ans, j - i + 1); + hm.put(nums[i], hm.get(nums[i]) - 1); + if (hm.get(nums[i]) == 0) { + sum -= nums[i]; + } + i++; + } + j++; + } + return (ans != Integer.MAX_VALUE) ? ans : alt; + } +} diff --git a/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/readme.md b/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/readme.md new file mode 100644 index 000000000..039f74fa5 --- /dev/null +++ b/src/main/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/readme.md @@ -0,0 +1,43 @@ +3795\. Minimum Subarray Length With Distinct Sum At Least K + +Medium + +You are given an integer array `nums` and an integer `k`. + +Return the **minimum** length of a **non-empty subarrays** whose sum of the **distinct** values present in that subarray (each value counted once) is **at least** `k`. If no such subarray exists, return -1. + +**Example 1:** + +**Input:** nums = [2,2,3,1], k = 4 + +**Output:** 2 + +**Explanation:** + +The subarray `[2, 3]` has distinct elements `{2, 3}` whose sum is `2 + 3 = 5`, which is at least `k = 4`. Thus, the answer is 2. + +**Example 2:** + +**Input:** nums = [3,2,3,4], k = 5 + +**Output:** 2 + +**Explanation:** + +The subarray `[3, 2]` has distinct elements `{3, 2}` whose sum is `3 + 2 = 5`, which is at least `k = 5`. Thus, the answer is 2. + +**Example 3:** + +**Input:** nums = [5,5,4], k = 5 + +**Output:** 1 + +**Explanation:** + +The subarray `[5]` has distinct elements `{5}` whose sum is `5`, which is at least `k = 5`. Thus, the answer is 1. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/Solution.java b/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/Solution.java new file mode 100644 index 000000000..cc24214bd --- /dev/null +++ b/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/Solution.java @@ -0,0 +1,28 @@ +package g3701_3800.s3796_find_maximum_value_in_a_constrained_sequence; + +// #Medium #Array #Greedy #Staff #Biweekly_Contest_173 +// #2026_05_22_Time_6_ms_(98.18%)_Space_208.10_MB_(32.73%) + +public class Solution { + public int findMaxVal(int n, int[][] restrictions, int[] diff) { + int[] a = new int[n]; + a[0] = 0; + for (int i = 1; i < n; i++) { + a[i] = Integer.MAX_VALUE; + } + for (int[] r : restrictions) { + a[r[0]] = Math.min(a[r[0]], r[1]); + } + for (int i = 1; i < n; i++) { + a[i] = Math.min(a[i], a[i - 1] + diff[i - 1]); + } + for (int i = n - 2; i >= 0; i--) { + a[i] = Math.min(a[i], a[i + 1] + diff[i]); + } + int maxi = 0; + for (int i : a) { + maxi = Math.max(maxi, i); + } + return maxi; + } +} diff --git a/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/readme.md b/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/readme.md new file mode 100644 index 000000000..151f6554f --- /dev/null +++ b/src/main/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/readme.md @@ -0,0 +1,48 @@ +3796\. Find Maximum Value in a Constrained Sequence + +Medium + +You are given an integer `n`, a 2D integer array `restrictions`, and an integer array `diff` of length `n - 1`. Your task is to construct a sequence of length `n`, denoted by `a[0], a[1], ..., a[n - 1]`, such that it satisfies the following conditions: + +* `a[0]` is 0. +* All elements in the sequence are **non-negative**. +* For every index `i` (`0 <= i <= n - 2`), `abs(a[i] - a[i + 1]) <= diff[i]`. +* For each `restrictions[i] = [idx, maxVal]`, the value at position `idx` in the sequence must not exceed `maxVal` (i.e., `a[idx] <= maxVal`). + +Your goal is to construct a valid sequence that **maximizes** the **largest** value within the sequence while satisfying all the above conditions. + +Return an integer denoting the **largest** value present in such an optimal sequence. + +**Example 1:** + +**Input:** n = 10, restrictions = [[3,1],[8,1]], diff = [2,2,3,1,4,5,1,1,2] + +**Output:** 6 + +**Explanation:** + +* The sequence `a = [0, 2, 4, 1, 2, 6, 2, 1, 1, 3]` satisfies the given constraints (`a[3] <= 1` and `a[8] <= 1`). +* The maximum value in the sequence is 6. + +**Example 2:** + +**Input:** n = 8, restrictions = [[3,2]], diff = [3,5,2,4,2,3,1] + +**Output:** 12 + +**Explanation:** + +* The sequence `a = [0, 3, 3, 2, 6, 8, 11, 12]` satisfies the given constraints (`a[3] <= 2`). +* The maximum value in the sequence is 12. + +**Constraints:** + +* 2 <= n <= 105 +* `1 <= restrictions.length <= n - 1` +* `restrictions[i].length == 2` +* `restrictions[i] = [idx, maxVal]` +* `1 <= idx < n` +* 1 <= maxVal <= 106 +* `diff.length == n - 1` +* `1 <= diff[i] <= 10` +* The values of `restrictions[i][0]` are unique. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/Solution.java b/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/Solution.java new file mode 100644 index 000000000..d88da09bb --- /dev/null +++ b/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/Solution.java @@ -0,0 +1,55 @@ +package g3701_3800.s3797_count_routes_to_climb_a_rectangular_grid; + +// #Hard #Array #Dynamic_Programming #Matrix #Prefix_Sum #Senior_Staff #Biweekly_Contest_173 +// #2026_05_22_Time_89_ms_(89.19%)_Space_57.00_MB_(81.08%) + +import java.util.Arrays; + +public class Solution { + public int numberOfRoutes(String[] grid, int d) { + int n = grid.length; + int m = grid[0].length(); + long mod = 1_000_000_007; + long[] dp = null; + for (int i = n - 1; i >= 0; i--) { + String r = grid[i]; + if (dp == null) { + long[] init = new long[m]; + Arrays.fill(init, 1); + dp = f(init, 0, r, m, mod); + } else { + int d2 = (int) Math.sqrt((double) d * d - 1); + dp = f(dp, d2, r, m, mod); + } + dp = f(dp, d, r, m, mod); + } + long res = 0; + for (long v : dp) { + res = (res + v) % mod; + } + return (int) (res); + } + + private long[] f(long[] dp, int dist, String r, int m, long mod) { + long[] dp2 = new long[m]; + long window = 0; + for (int k = 0; k <= Math.min(m - 1, dist); k++) { + window += dp[k]; + } + dp2[0] = window; + for (int j = 1; j < m; j++) { + window = dp2[j - 1]; + if (j - dist - 1 >= 0) { + window -= dp[j - dist - 1]; + } + if (j + dist < m) { + window += dp[j + dist]; + } + dp2[j] = window; + } + for (int j = 0; j < m; j++) { + dp2[j] = (r.charAt(j) == '#') ? 0 : dp2[j] % mod; + } + return dp2; + } +} diff --git a/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/readme.md b/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/readme.md new file mode 100644 index 000000000..797a3ba3c --- /dev/null +++ b/src/main/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/readme.md @@ -0,0 +1,88 @@ +3797\. Count Routes to Climb a Rectangular Grid + +Hard + +You are given a string array `grid` of size `n`, where each string `grid[i]` has length `m`. The character `grid[i][j]` is one of the following symbols: + +* `'.'`: The cell is available. +* `'#'`: The cell is blocked. + +You want to count the number of different routes to climb `grid`. Each route must start from _any cell_ in the bottom row (row `n - 1`) and end in the top row (row 0). + +However, there are some constraints on the route. + +* You can only move from one available cell to **another** available cell. +* The **Euclidean distance** of each move is **at most** `d`, where `d` is an integer parameter given to you. The Euclidean distance between two cells `(r1, c1)`, `(r2, c2)` is sqrt((r1 - r2)2 + (c1 - c2)2). +* Each move either stays on the same row or moves to the row directly above (from row `r` to `r - 1`). +* You cannot stay on the same row for two consecutive turns. If you stay on the same row in a move (and this move is not the last move), your next move must go to the row above. + +Return an integer denoting the number of such routes. Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** grid = ["..","#."], d = 1 + +**Output:** 2 + +**Explanation:** + +We label the cells we visit in the routes sequentially, starting from 1. The two routes are: + +.2 #1 + +32 #1 + +We can move from the cell (1, 1) to the cell (0, 1) because the Euclidean distance is sqrt((1 - 0)2 + (1 - 1)2) = sqrt(1) <= d. + +However, we cannot move from the cell (1, 1) to the cell (0, 0) because the Euclidean distance is sqrt((1 - 0)2 + (1 - 0)2) = sqrt(2) > d. + +**Example 2:** + +**Input:** grid = ["..","#."], d = 2 + +**Output:** 4 + +**Explanation:** + +Two of the routes are given in example 1. The other two routes are: + +2\. #1 + +23 #1 + +Note that we can move from (1, 1) to (0, 0) because the Euclidean distance is `sqrt(2) <= d`. + +**Example 3:** + +**Input:** grid = ["#"], d = 750 + +**Output:** 0 + +**Explanation:** + +We cannot choose any cell as the starting cell. Therefore, there are no routes. + +**Example 4:** + +**Input:** grid = [".."], d = 1 + +**Output:** 4 + +**Explanation:** + +The possible routes are: + +.1 + +1\. + +12 + +21 + +**Constraints:** + +* `1 <= n == grid.length <= 750` +* `1 <= m == grid[i].length <= 750` +* `grid[i][j]` is `'.'` or `'#'`. +* `1 <= d <= 750` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3798_largest_even_number/Solution.java b/src/main/java/g3701_3800/s3798_largest_even_number/Solution.java new file mode 100644 index 000000000..bcf702a14 --- /dev/null +++ b/src/main/java/g3701_3800/s3798_largest_even_number/Solution.java @@ -0,0 +1,16 @@ +package g3701_3800.s3798_largest_even_number; + +// #Easy #String #Mid_Level #Weekly_Contest_483 +// #2026_05_22_Time_0_ms_(100.00%)_Space_43.67_MB_(94.28%) + +public class Solution { + public String largestEven(String s) { + int i; + for (i = s.length() - 1; i >= 0; i--) { + if (s.charAt(i) == '2') { + break; + } + } + return s.substring(0, i + 1); + } +} diff --git a/src/main/java/g3701_3800/s3798_largest_even_number/readme.md b/src/main/java/g3701_3800/s3798_largest_even_number/readme.md new file mode 100644 index 000000000..a2f771819 --- /dev/null +++ b/src/main/java/g3701_3800/s3798_largest_even_number/readme.md @@ -0,0 +1,44 @@ +3798\. Largest Even Number + +Easy + +You are given a string `s` consisting only of the characters `'1'` and `'2'`. + +You may delete any number of characters from `s` without changing the order of the remaining characters. + +Return the **largest possible resultant string** that represents an **even** integer. If there is no such string, return the empty string `""`. + +**Example 1:** + +**Input:** s = "1112" + +**Output:** "1112" + +**Explanation:** + +The string already represents the largest possible even number, so no deletions are needed. + +**Example 2:** + +**Input:** s = "221" + +**Output:** "22" + +**Explanation:** + +Deleting `'1'` results in the largest possible even number which is equal to 22. + +**Example 3:** + +**Input:** s = "1" + +**Output:** "" + +**Explanation:** + +There is no way to get an even number. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists only of the characters `'1'` and `'2'`. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3799_word_squares_ii/Solution.java b/src/main/java/g3701_3800/s3799_word_squares_ii/Solution.java new file mode 100644 index 000000000..24d394a14 --- /dev/null +++ b/src/main/java/g3701_3800/s3799_word_squares_ii/Solution.java @@ -0,0 +1,37 @@ +package g3701_3800.s3799_word_squares_ii; + +// #Medium #Array #String #Sorting #Backtracking #Enumeration #Senior #Weekly_Contest_483 +// #2026_05_22_Time_5_ms_(99.15%)_Space_56.77_MB_(55.56%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + public List> wordSquares(String[] words) { + String[] s = words.clone(); + Arrays.sort(s); + List> result = new ArrayList<>(); + int n = s.length; + for (int a = 0; a < n; a++) { + for (int b = 0; b < n; b++) { + if (a != b && s[a].charAt(0) == s[b].charAt(0)) { + for (int c = 0; c < n; c++) { + if (c != a && c != b && s[a].charAt(3) == s[c].charAt(0)) { + for (int d = 0; d < n; d++) { + if (d != a + && d != b + && d != c + && s[d].charAt(0) == s[b].charAt(3) + && s[d].charAt(3) == s[c].charAt(3)) { + result.add(Arrays.asList(s[a], s[b], s[c], s[d])); + } + } + } + } + } + } + } + return result; + } +} diff --git a/src/main/java/g3701_3800/s3799_word_squares_ii/readme.md b/src/main/java/g3701_3800/s3799_word_squares_ii/readme.md new file mode 100644 index 000000000..2878f9ea0 --- /dev/null +++ b/src/main/java/g3701_3800/s3799_word_squares_ii/readme.md @@ -0,0 +1,56 @@ +3799\. Word Squares II + +Medium + +You are given a string array `words`, consisting of **distinct** 4-letter strings, each containing lowercase English letters. + +A **word square** consists of 4 **distinct** words: `top`, `left`, `right` and `bottom`, arranged as follows: + +* `top` forms the **top row**. +* `bottom` forms the **bottom row**. +* `left` forms the **left column** (top to bottom). +* `right` forms the **right column** (top to bottom). + +It must satisfy: + +* `top[0] == left[0]`, `top[3] == right[0]` +* `bottom[0] == left[3]`, `bottom[3] == right[3]` + +Return all valid **distinct** word squares, sorted in **ascending lexicographic** order by the 4-tuple `(top, left, right, bottom)`. + +**Example 1:** + +**Input:** words = ["able","area","echo","also"] + +**Output:** [["able","area","echo","also"],["area","able","also","echo"]] + +**Explanation:** + +There are exactly two valid 4-word squares that satisfy all corner constraints: + +* `"able"` (top), `"area"` (left), `"echo"` (right), `"also"` (bottom) + * `top[0] == left[0] == 'a'` + * `top[3] == right[0] == 'e'` + * `bottom[0] == left[3] == 'a'` + * `bottom[3] == right[3] == 'o'` +* `"area"` (top), `"able"` (left), `"also"` (right), `"echo"` (bottom) + * All corner constraints are satisfied. + +Thus, the answer is `[["able","area","echo","also"],["area","able","also","echo"]]`. + +**Example 2:** + +**Input:** words = ["code","cafe","eden","edge"] + +**Output:** [] + +**Explanation:** + +No combination of four words satisfies all four corner constraints. Thus, the answer is empty array `[]`. + +**Constraints:** + +* `4 <= words.length <= 15` +* `words[i].length == 4` +* `words[i]` consists of only lowercase English letters. +* All `words[i]` are **distinct**. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/Solution.java b/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/Solution.java new file mode 100644 index 000000000..850d504f4 --- /dev/null +++ b/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/Solution.java @@ -0,0 +1,39 @@ +package g3701_3800.s3800_minimum_cost_to_make_two_binary_strings_equal; + +// #Medium #String #Greedy #Staff #Weekly_Contest_483 +// #2026_05_22_Time_8_ms_(85.71%)_Space_47.61_MB_(85.71%) + +public class Solution { + public long minimumCost(String s, String t, int flipCost, int swapCost, int crossCost) { + int len = t.length(); + int diff = 0; + int one = 0; + int zero = 0; + char[] arr1 = s.toCharArray(); + char[] arr2 = t.toCharArray(); + for (int i = 0; i < len; i++) { + if (arr1[i] != arr2[i]) { + diff++; + if (arr1[i] == '0') { + zero++; + } else { + one++; + } + } + } + int min = Math.min(one, zero); + long ans = 0; + long costOfFlip = ((long) min * flipCost) * 2; + long costOfSwap = ((long) min * swapCost); + ans += Math.min(costOfFlip, costOfSwap); + diff -= (min * 2); + int oddLeft = diff % 2; + int evenLeft = diff - oddLeft; + long costOfCrossSwap = + (((long) (evenLeft / 2) * crossCost) + ((long) (evenLeft / 2) * swapCost)); + long costOfFlipMain = ((long) evenLeft * flipCost); + ans += Math.min(costOfCrossSwap, costOfFlipMain); + ans += (long) oddLeft * flipCost; + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/readme.md b/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/readme.md new file mode 100644 index 000000000..72889f339 --- /dev/null +++ b/src/main/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/readme.md @@ -0,0 +1,57 @@ +3800\. Minimum Cost to Make Two Binary Strings Equal + +Medium + +You are given two binary strings `s` and `t`, both of length `n`, and three **positive** integers `flipCost`, `swapCost`, and `crossCost`. + +You are allowed to apply the following operations any number of times (in any order) to the strings `s` and `t`: + +* Choose any index `i` and flip `s[i]` or `t[i]` (change `'0'` to `'1'` or `'1'` to `'0'`). The cost of this operation is `flipCost`. +* Choose two **distinct** indices `i` and `j`, and swap either `s[i]` and `s[j]` or `t[i]` and `t[j]`. The cost of this operation is `swapCost`. +* Choose an index `i` and swap `s[i]` with `t[i]`. The cost of this operation is `crossCost`. + +Return an integer denoting the **minimum** total cost needed to make the strings `s` and `t` equal. + +**Example 1:** + +**Input:** s = "01000", t = "10111", flipCost = 10, swapCost = 2, crossCost = 2 + +**Output:** 16 + +**Explanation:** + +We can perform the following operations: + +* Swap `s[0]` and `s[1]` (`swapCost = 2`). After this operation, `s = "10000"` and `t = "10111"`. +* Cross swap `s[2]` and `t[2]` (`crossCost = 2`). After this operation, `s = "10100"` and `t = "10011"`. +* Swap `s[2]` and `s[3]` (`swapCost = 2`). After this operation, `s = "10010"` and `t = "10011"`. +* Flip `s[4]` (`flipCost = 10`). After this operation, `s = t = "10011"`. + +The total cost is `2 + 2 + 2 + 10 = 16`. + +**Example 2:** + +**Input:** s = "001", t = "110", flipCost = 2, swapCost = 100, crossCost = 100 + +**Output:** 6 + +**Explanation:** + +Flipping all the bits of `s` makes the strings equal, and the total cost is `3 * flipCost = 3 * 2 = 6`. + +**Example 3:** + +**Input:** s = "1010", t = "1010", flipCost = 5, swapCost = 5, crossCost = 5 + +**Output:** 0 + +**Explanation:** + +The strings are already equal, so no operations are required. + +**Constraints:** + +* `n == s.length == t.length` +* 1 <= n <= 105 +* 1 <= flipCost, swapCost, crossCost <= 109 +* `s` and `t` consist only of the characters `'0'` and `'1'`. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/Solution.java b/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/Solution.java new file mode 100644 index 000000000..878412814 --- /dev/null +++ b/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/Solution.java @@ -0,0 +1,114 @@ +package g3801_3900.s3801_minimum_cost_to_merge_sorted_lists; + +// #Hard #Array #Dynamic_Programming #Binary_Search #Two_Pointers #Bit_Manipulation #Senior_Staff +// #Weekly_Contest_483 #2026_05_22_Time_251_ms_(89.19%)_Space_47.69_MB_(83.78%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class Solution { + private int numLt(int[][] lists, int enabled, int guess) { + int result = 0; + for (int i = 0; i < lists.length; i++) { + if (((enabled >> i) & 1) == 1) { + int[] list = lists[i]; + int low = 0; + int high = list.length; + while (low < high) { + int mid = (low + high) / 2; + if (list[mid] < guess) { + low = mid + 1; + } else { + high = mid; + } + } + result += low; + } + } + return result; + } + + public long minMergeCost(int[][] lists) { + int n = lists.length; + List allNums = getSortedNumbers(lists); + long[][] subsets = buildSubsetInfo(lists, allNums); + long[] dp = new long[1 << n]; + Arrays.fill(dp, Long.MAX_VALUE); + for (int subset = 0; subset < (1 << n); subset++) { + if (Integer.bitCount(subset) <= 1) { + dp[subset] = 0; + continue; + } + dp[subset] = calculateMinCost(subset, subsets, dp); + } + return dp[(1 << n) - 1]; + } + + private List getSortedNumbers(int[][] lists) { + List allNums = new ArrayList<>(); + for (int[] lst : lists) { + for (int num : lst) { + allNums.add(num); + } + } + Collections.sort(allNums); + return allNums; + } + + private long[][] buildSubsetInfo(int[][] lists, List allNums) { + int n = lists.length; + long[][] subsets = new long[1 << n][2]; + for (int subset = 1; subset < (1 << n); subset++) { + int resultLen = getSubsetLength(lists, subset); + int medianLt = (resultLen - 1) / 2; + subsets[subset][0] = resultLen; + subsets[subset][1] = findMedian(lists, subset, medianLt, allNums); + } + return subsets; + } + + private int getSubsetLength(int[][] lists, int subset) { + int resultLen = 0; + for (int i = 0; i < lists.length; i++) { + if (((subset >> i) & 1) == 1) { + resultLen += lists[i].length; + } + } + return resultLen; + } + + private int findMedian(int[][] lists, int subset, int medianLt, List allNums) { + int low = 0; + int high = allNums.size() - 1; + int actualMedian = -1; + while (low <= high) { + int mid = (low + high) / 2; + int num = allNums.get(mid); + if (numLt(lists, subset, num) <= medianLt) { + actualMedian = num; + low = mid + 1; + } else { + high = mid - 1; + } + } + return actualMedian; + } + + private long calculateMinCost(int subset, long[][] subsets, long[] dp) { + long minCost = Long.MAX_VALUE; + int a = (subset - 1) & subset; + while (a > 0) { + int b = subset ^ a; + long aLen = subsets[a][0]; + long aMedian = subsets[a][1]; + long bLen = subsets[b][0]; + long bMedian = subsets[b][1]; + long cost = aLen + bLen + Math.abs(aMedian - bMedian); + minCost = Math.min(minCost, dp[a] + dp[b] + cost); + a = (a - 1) & subset; + } + return minCost; + } +} diff --git a/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/readme.md b/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/readme.md new file mode 100644 index 000000000..bd58c4656 --- /dev/null +++ b/src/main/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/readme.md @@ -0,0 +1,89 @@ +3801\. Minimum Cost to Merge Sorted Lists + +Hard + +You are given a 2D integer array `lists`, where each `lists[i]` is a non-empty array of integers **sorted** in **non-decreasing** order. + +You may **repeatedly** choose two lists `a = lists[i]` and `b = lists[j]`, where `i != j`, and merge them. The **cost** to merge `a` and `b` is: + +`len(a) + len(b) + abs(median(a) - median(b))`, where `len` and `median` denote the list length and median, respectively. + +After merging `a` and `b`, remove both `a` and `b` from `lists` and insert the new merged **sorted list** in **any** position. Repeat merges until only **one** list remains. + +Return an integer denoting the **minimum total cost** required to merge all lists into one single sorted list. + +The **median** of an array is the middle element after sorting it in non-decreasing order. If the array has an even number of elements, the median is the left middle element. + +**Example 1:** + +**Input:** lists = [[1,3,5],[2,4],[6,7,8]] + +**Output:** 18 + +**Explanation:** + +Merge `a = [1, 3, 5]` and `b = [2, 4]`: + +* `len(a) = 3` and `len(b) = 2` +* `median(a) = 3` and `median(b) = 2` +* `cost = len(a) + len(b) + abs(median(a) - median(b)) = 3 + 2 + abs(3 - 2) = 6` + +So `lists` becomes `[[1, 2, 3, 4, 5], [6, 7, 8]]`. + +Merge `a = [1, 2, 3, 4, 5]` and `b = [6, 7, 8]`: + +* `len(a) = 5` and `len(b) = 3` +* `median(a) = 3` and `median(b) = 7` +* `cost = len(a) + len(b) + abs(median(a) - median(b)) = 5 + 3 + abs(3 - 7) = 12` + +So `lists` becomes `[[1, 2, 3, 4, 5, 6, 7, 8]]`, and total cost is `6 + 12 = 18`. + +**Example 2:** + +**Input:** lists = [[1,1,5],[1,4,7,8]] + +**Output:** 10 + +**Explanation:** + +Merge `a = [1, 1, 5]` and `b = [1, 4, 7, 8]`: + +* `len(a) = 3` and `len(b) = 4` +* `median(a) = 1` and `median(b) = 4` +* `cost = len(a) + len(b) + abs(median(a) - median(b)) = 3 + 4 + abs(1 - 4) = 10` + +So `lists` becomes `[[1, 1, 1, 4, 5, 7, 8]]`, and total cost is 10. + +**Example 3:** + +**Input:** lists = [[1],[3]] + +**Output:** 4 + +**Explanation:** + +Merge `a = [1]` and `b = [3]`: + +* `len(a) = 1` and `len(b) = 1` +* `median(a) = 1` and `median(b) = 3` +* `cost = len(a) + len(b) + abs(median(a) - median(b)) = 1 + 1 + abs(1 - 3) = 4` + +So `lists` becomes `[[1, 3]]`, and total cost is 4. + +**Example 4:** + +**Input:** lists = [[1],[1]] + +**Output:** 2 + +**Explanation:** + +The total cost is `len(a) + len(b) + abs(median(a) - median(b)) = 1 + 1 + abs(1 - 1) = 2`. + +**Constraints:** + +* `2 <= lists.length <= 12` +* `1 <= lists[i].length <= 500` +* -109 <= lists[i][j] <= 109 +* `lists[i]` is sorted in non-decreasing order. +* The **sum** of `lists[i].length` will not exceed 2000. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3803_count_residue_prefixes/Solution.java b/src/main/java/g3801_3900/s3803_count_residue_prefixes/Solution.java new file mode 100644 index 000000000..5525fa092 --- /dev/null +++ b/src/main/java/g3801_3900/s3803_count_residue_prefixes/Solution.java @@ -0,0 +1,28 @@ +package g3801_3900.s3803_count_residue_prefixes; + +// #Easy #String #Hash_Table #Mid_Level #Weekly_Contest_484 +// #2026_05_22_Time_1_ms_(100.00%)_Space_43.67_MB_(91.69%) + +public class Solution { + public int residuePrefixes(String s) { + int n = s.length(); + int count = 0; + int p = 0; + char c1 = s.charAt(p); + while (p < n && c1 == s.charAt(p)) { + if (++p % 3 == 1) { + count++; + } + } + if (p >= n) { + return count; + } + char c2 = s.charAt(p); + while (p < n && (c1 == s.charAt(p) || c2 == s.charAt(p))) { + if (++p % 3 == 2) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g3801_3900/s3803_count_residue_prefixes/readme.md b/src/main/java/g3801_3900/s3803_count_residue_prefixes/readme.md new file mode 100644 index 000000000..2006a7b3f --- /dev/null +++ b/src/main/java/g3801_3900/s3803_count_residue_prefixes/readme.md @@ -0,0 +1,50 @@ +3803\. Count Residue Prefixes + +Easy + +You are given a string `s` consisting only of lowercase English letters. + +A **prefix** of `s` is called a **residue** if the number of **distinct characters** in the **prefix** is equal to `len(prefix) % 3`. + +Return the count of **residue** prefixes in `s`. + +A **prefix** of a string is a **non-empty substring** that starts from the beginning of the string and extends to any point within it. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** 2 + +**Explanation:** + +* Prefix `"a"` has 1 distinct character and length modulo 3 is 1, so it is a residue. +* Prefix `"ab"` has 2 distinct characters and length modulo 3 is 2, so it is a residue. +* Prefix `"abc"` does not satisfy the condition. Thus, the answer is 2. + +**Example 2:** + +**Input:** s = "dd" + +**Output:** 1 + +**Explanation:** + +* Prefix `"d"` has 1 distinct character and length modulo 3 is 1, so it is a residue. +* Prefix `"dd"` has 1 distinct character but length modulo 3 is 2, so it is not a residue. Thus, the answer is 1. + +**Example 3:** + +**Input:** s = "bob" + +**Output:** 2 + +**Explanation:** + +* Prefix `"b"` has 1 distinct character and length modulo 3 is 1, so it is a residue. +* Prefix `"bo"` has 2 distinct characters and length mod 3 is 2, so it is a residue. Thus, the answer is 2. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` contains only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/Solution.java b/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/Solution.java new file mode 100644 index 000000000..fe99dd4ff --- /dev/null +++ b/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/Solution.java @@ -0,0 +1,24 @@ +package g3801_3900.s3804_number_of_centered_subarrays; + +// #Medium #Array #Hash_Table #Enumeration #Senior #Weekly_Contest_484 +// #2026_05_22_Time_84_ms_(80.43%)_Space_47.00_MB_(47.83%) + +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public int centeredSubarrays(int[] nums) { + int n = nums.length; + int result = 0; + for (int i = 0; i < n; i++) { + int subsum = 0; + Set subnums = new HashSet<>(); + for (int j = i; j < n; j++) { + subsum += nums[j]; + subnums.add(nums[j]); + result += subnums.contains(subsum) ? 1 : 0; + } + } + return result; + } +} diff --git a/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/readme.md b/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/readme.md new file mode 100644 index 000000000..dc8af7f74 --- /dev/null +++ b/src/main/java/g3801_3900/s3804_number_of_centered_subarrays/readme.md @@ -0,0 +1,37 @@ +3804\. Number of Centered Subarrays + +Medium + +You are given an integer array `nums`. + +A **non-empty subarrays** of `nums` is called **centered** if the sum of its elements is **equal to at least one** element within that **same subarray**. + +Return the number of **centered subarrays** of `nums`. + +**Example 1:** + +**Input:** nums = [-1,1,0] + +**Output:** 5 + +**Explanation:** + +* All single-element subarrays (`[-1]`, `[1]`, `[0]`) are centered. +* The subarray `[1, 0]` has a sum of 1, which is present in the subarray. +* The subarray `[-1, 1, 0]` has a sum of 0, which is present in the subarray. +* Thus, the answer is 5. + +**Example 2:** + +**Input:** nums = [2,-3] + +**Output:** 2 + +**Explanation:** + +Only single-element subarrays (`[2]`, `[-3]`) are centered. + +**Constraints:** + +* `1 <= nums.length <= 500` +* -105 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/Solution.java b/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/Solution.java new file mode 100644 index 000000000..871703c42 --- /dev/null +++ b/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/Solution.java @@ -0,0 +1,27 @@ +package g3801_3900.s3805_count_caesar_cipher_pairs; + +// #Medium #Array #String #Hash_Table #Math #Counting #Senior #Weekly_Contest_484 +// #2026_05_22_Time_34_ms_(98.75%)_Space_67.70_MB_(82.50%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public long countPairs(String[] words) { + String[] a = words; + Map b = new HashMap<>(); + long c = 0; + for (String d : a) { + char[] e = d.toCharArray(); + int f = e[0]; + for (int g = 0; g < e.length; g++) { + e[g] = (char) ((e[g] - f + 26) % 26); + } + String h = new String(e); + int i = b.getOrDefault(h, 0); + c += i; + b.put(h, i + 1); + } + return c; + } +} diff --git a/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/readme.md b/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/readme.md new file mode 100644 index 000000000..06fd3e06a --- /dev/null +++ b/src/main/java/g3801_3900/s3805_count_caesar_cipher_pairs/readme.md @@ -0,0 +1,52 @@ +3805\. Count Caesar Cipher Pairs + +Medium + +You are given an array `words` of `n` strings. Each string has length `m` and contains only lowercase English letters. + +Two strings `s` and `t` are **similar** if we can apply the following operation any number of times (possibly zero times) so that `s` and `t` become **equal**. + +* Choose either `s` or `t`. +* Replace **every** letter in the chosen string with the next letter in the alphabet cyclically. The next letter after `'z'` is `'a'`. + +Count the number of pairs of indices `(i, j)` such that: + +* `i < j` +* `words[i]` and `words[j]` are **similar**. + +Return an integer denoting the number of such pairs. + +**Example 1:** + +**Input:** words = ["fusion","layout"] + +**Output:** 1 + +**Explanation:** + +`words[0] = "fusion"` and `words[1] = "layout"` are similar because we can apply the operation to `"fusion"` 6 times. The string `"fusion"` changes as follows. + +* `"fusion"` +* `"gvtjpo"` +* `"hwukqp"` +* `"ixvlrq"` +* `"jywmsr"` +* `"kzxnts"` +* `"layout"` + +**Example 2:** + +**Input:** words = ["ab","aa","za","aa"] + +**Output:** 2 + +**Explanation:** + +`words[0] = "ab"` and `words[2] = "za"` are similar. `words[1] = "aa"` and `words[3] = "aa"` are similar. + +**Constraints:** + +* 1 <= n == words.length <= 105 +* 1 <= m == words[i].length <= 105 +* 1 <= n * m <= 105 +* `words[i]` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3783_mirror_distance_of_an_integer/SolutionTest.java b/src/test/java/g3701_3800/s3783_mirror_distance_of_an_integer/SolutionTest.java new file mode 100644 index 000000000..5269219d1 --- /dev/null +++ b/src/test/java/g3701_3800/s3783_mirror_distance_of_an_integer/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3783_mirror_distance_of_an_integer; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void mirrorDistance() { + assertThat(new Solution().mirrorDistance(25), equalTo(27)); + } + + @Test + void mirrorDistance2() { + assertThat(new Solution().mirrorDistance(10), equalTo(9)); + } + + @Test + void mirrorDistance3() { + assertThat(new Solution().mirrorDistance(7), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/SolutionTest.java b/src/test/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/SolutionTest.java new file mode 100644 index 000000000..340d9b555 --- /dev/null +++ b/src/test/java/g3701_3800/s3784_minimum_deletion_cost_to_make_all_characters_equal/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3784_minimum_deletion_cost_to_make_all_characters_equal; + +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("aabaac", new int[] {1, 2, 3, 4, 1, 10}), equalTo(11L)); + } + + @Test + void minCost2() { + assertThat(new Solution().minCost("abc", new int[] {10, 5, 8}), equalTo(13L)); + } + + @Test + void minCost3() { + assertThat(new Solution().minCost("zzzzz", new int[] {67, 67, 67, 67, 67}), equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/SolutionTest.java b/src/test/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/SolutionTest.java new file mode 100644 index 000000000..f62722b8d --- /dev/null +++ b/src/test/java/g3701_3800/s3785_minimum_swaps_to_avoid_forbidden_values/SolutionTest.java @@ -0,0 +1,30 @@ +package g3701_3800.s3785_minimum_swaps_to_avoid_forbidden_values; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minSwaps() { + assertThat(new Solution().minSwaps(new int[] {1, 2, 3}, new int[] {3, 2, 1}), equalTo(1)); + } + + @Test + void minSwaps2() { + assertThat( + new Solution().minSwaps(new int[] {4, 6, 6, 5}, new int[] {4, 6, 5, 5}), + equalTo(2)); + } + + @Test + void minSwaps3() { + assertThat(new Solution().minSwaps(new int[] {7, 7}, new int[] {8, 7}), equalTo(-1)); + } + + @Test + void minSwaps4() { + assertThat(new Solution().minSwaps(new int[] {1, 2}, new int[] {2, 1}), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/SolutionTest.java b/src/test/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/SolutionTest.java new file mode 100644 index 000000000..612c39f55 --- /dev/null +++ b/src/test/java/g3701_3800/s3786_total_sum_of_interaction_cost_in_tree_groups/SolutionTest.java @@ -0,0 +1,40 @@ +package g3701_3800.s3786_total_sum_of_interaction_cost_in_tree_groups; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void interactionCosts() { + assertThat( + new Solution() + .interactionCosts(3, new int[][] {{0, 1}, {1, 2}}, new int[] {1, 1, 1}), + equalTo(4L)); + } + + @Test + void interactionCosts2() { + assertThat( + new Solution() + .interactionCosts(3, new int[][] {{0, 1}, {1, 2}}, new int[] {3, 2, 3}), + equalTo(2L)); + } + + @Test + void interactionCosts3() { + assertThat( + new Solution() + .interactionCosts( + 4, new int[][] {{0, 1}, {0, 2}, {0, 3}}, new int[] {1, 1, 4, 4}), + equalTo(3L)); + } + + @Test + void interactionCosts4() { + assertThat( + new Solution().interactionCosts(2, new int[][] {{0, 1}}, new int[] {9, 8}), + equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3788_maximum_score_of_a_split/SolutionTest.java b/src/test/java/g3701_3800/s3788_maximum_score_of_a_split/SolutionTest.java new file mode 100644 index 000000000..488eb2d06 --- /dev/null +++ b/src/test/java/g3701_3800/s3788_maximum_score_of_a_split/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3788_maximum_score_of_a_split; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumScore() { + assertThat(new Solution().maximumScore(new int[] {10, -1, 3, -4, -5}), equalTo(17L)); + } + + @Test + void maximumScore2() { + assertThat(new Solution().maximumScore(new int[] {-7, -5, 3}), equalTo(-2L)); + } + + @Test + void maximumScore3() { + assertThat(new Solution().maximumScore(new int[] {1, 1}), equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/SolutionTest.java b/src/test/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/SolutionTest.java new file mode 100644 index 000000000..82a47d855 --- /dev/null +++ b/src/test/java/g3701_3800/s3789_minimum_cost_to_acquire_required_items/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3789_minimum_cost_to_acquire_required_items; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumCost() { + assertThat(new Solution().minimumCost(3, 2, 1, 3, 2), equalTo(3L)); + } + + @Test + void minimumCost2() { + assertThat(new Solution().minimumCost(5, 4, 15, 2, 3), equalTo(22L)); + } + + @Test + void minimumCost3() { + assertThat(new Solution().minimumCost(5, 4, 15, 0, 0), equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3790_smallest_all_ones_multiple/SolutionTest.java b/src/test/java/g3701_3800/s3790_smallest_all_ones_multiple/SolutionTest.java new file mode 100644 index 000000000..fa7d6d4f5 --- /dev/null +++ b/src/test/java/g3701_3800/s3790_smallest_all_ones_multiple/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3790_smallest_all_ones_multiple; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minAllOneMultiple() { + assertThat(new Solution().minAllOneMultiple(3), equalTo(3)); + } + + @Test + void minAllOneMultiple2() { + assertThat(new Solution().minAllOneMultiple(7), equalTo(6)); + } + + @Test + void minAllOneMultiple3() { + assertThat(new Solution().minAllOneMultiple(2), equalTo(-1)); + } +} diff --git a/src/test/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/SolutionTest.java b/src/test/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/SolutionTest.java new file mode 100644 index 000000000..cc0019291 --- /dev/null +++ b/src/test/java/g3701_3800/s3791_number_of_balanced_integers_in_a_range/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3791_number_of_balanced_integers_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 countBalanced() { + assertThat(new Solution().countBalanced(1, 100), equalTo(9L)); + } + + @Test + void countBalanced2() { + assertThat(new Solution().countBalanced(120, 129), equalTo(1L)); + } + + @Test + void countBalanced3() { + assertThat(new Solution().countBalanced(1234, 1234), equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3793_find_users_with_high_token_usage/MysqlTest.java b/src/test/java/g3701_3800/s3793_find_users_with_high_token_usage/MysqlTest.java new file mode 100644 index 000000000..63d0903a8 --- /dev/null +++ b/src/test/java/g3701_3800/s3793_find_users_with_high_token_usage/MysqlTest.java @@ -0,0 +1,73 @@ +package g3701_3800.s3793_find_users_with_high_token_usage; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import javax.sql.DataSource; +import org.junit.jupiter.api.Test; +import org.zapodot.junit.db.annotations.EmbeddedDatabase; +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; +import org.zapodot.junit.db.common.CompatibilityMode; + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = + "CREATE TABLE prompts (" + + " user_id INTEGER," + + " prompt VARCHAR(512)," + + " tokens INTEGER," + + " PRIMARY KEY (user_id, prompt)" + + ");" + + "INSERT INTO prompts (user_id, prompt, tokens) VALUES" + + "(1, 'Write a blog outline', 120)," + + "(1, 'Generate SQL query', 80)," + + "(1, 'Summarize an article', 200)," + + "(2, 'Create resume bullet', 60)," + + "(2, 'Improve LinkedIn bio', 70)," + + "(3, 'Explain neural networks', 300)," + + "(3, 'Generate interview Q&A', 250)," + + "(3, 'Write cover letter', 180)," + + "(3, 'Optimize Python code', 220);") +class MysqlTest { + @Test + void testScript(@EmbeddedDatabase DataSource dataSource) + throws SQLException, FileNotFoundException { + try (final Connection connection = dataSource.getConnection()) { + try (final Statement statement = connection.createStatement(); + final ResultSet resultSet = + statement.executeQuery( + new BufferedReader( + new FileReader( + "src/main/java/g3701_3800/" + + "s3793_find_users_with_high_token_usage/" + + "script.sql")) + .lines() + .collect(Collectors.joining("\n")) + .replaceAll("#.*?\\r?\\n", ""))) { + List actualRows = new ArrayList<>(); + while (resultSet.next()) { + actualRows.add( + resultSet.getString(1) + + "|" + + resultSet.getString(2) + + "|" + + resultSet.getString(3)); + } + + List expectedRows = Arrays.asList("3|4|237.5", "1|3|133.33"); + assertThat(actualRows, equalTo(expectedRows)); + } + } + } +} diff --git a/src/test/java/g3701_3800/s3794_reverse_string_prefix/SolutionTest.java b/src/test/java/g3701_3800/s3794_reverse_string_prefix/SolutionTest.java new file mode 100644 index 000000000..921f2eb96 --- /dev/null +++ b/src/test/java/g3701_3800/s3794_reverse_string_prefix/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3794_reverse_string_prefix; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void reversePrefix() { + assertThat(new Solution().reversePrefix("abcd", 2), equalTo("bacd")); + } + + @Test + void reversePrefix2() { + assertThat(new Solution().reversePrefix("xyz", 3), equalTo("zyx")); + } + + @Test + void reversePrefix3() { + assertThat(new Solution().reversePrefix("hey", 1), equalTo("hey")); + } +} diff --git a/src/test/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/SolutionTest.java b/src/test/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/SolutionTest.java new file mode 100644 index 000000000..75aab4dd9 --- /dev/null +++ b/src/test/java/g3701_3800/s3795_minimum_subarray_length_with_distinct_sum_at_least_k/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3795_minimum_subarray_length_with_distinct_sum_at_least_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minLength() { + assertThat(new Solution().minLength(new int[] {2, 2, 3, 1}, 4), equalTo(2)); + } + + @Test + void minLength2() { + assertThat(new Solution().minLength(new int[] {3, 2, 3, 4}, 5), equalTo(2)); + } + + @Test + void minLength3() { + assertThat(new Solution().minLength(new int[] {5, 5, 4}, 5), equalTo(1)); + } +} diff --git a/src/test/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/SolutionTest.java b/src/test/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/SolutionTest.java new file mode 100644 index 000000000..d1f5e30af --- /dev/null +++ b/src/test/java/g3701_3800/s3796_find_maximum_value_in_a_constrained_sequence/SolutionTest.java @@ -0,0 +1,26 @@ +package g3701_3800.s3796_find_maximum_value_in_a_constrained_sequence; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMaxVal() { + assertThat( + new Solution() + .findMaxVal( + 10, + new int[][] {{3, 1}, {8, 1}}, + new int[] {2, 2, 3, 1, 4, 5, 1, 1, 2}), + equalTo(6)); + } + + @Test + void findMaxVal2() { + assertThat( + new Solution().findMaxVal(8, new int[][] {{3, 2}}, new int[] {3, 5, 2, 4, 2, 3, 1}), + equalTo(12)); + } +} diff --git a/src/test/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/SolutionTest.java b/src/test/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/SolutionTest.java new file mode 100644 index 000000000..dc8aae9cb --- /dev/null +++ b/src/test/java/g3701_3800/s3797_count_routes_to_climb_a_rectangular_grid/SolutionTest.java @@ -0,0 +1,28 @@ +package g3701_3800.s3797_count_routes_to_climb_a_rectangular_grid; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfRoutes() { + assertThat(new Solution().numberOfRoutes(new String[] {"..", "#."}, 1), equalTo(2)); + } + + @Test + void numberOfRoutes2() { + assertThat(new Solution().numberOfRoutes(new String[] {"..", "#."}, 2), equalTo(4)); + } + + @Test + void numberOfRoutes3() { + assertThat(new Solution().numberOfRoutes(new String[] {"#"}, 750), equalTo(0)); + } + + @Test + void numberOfRoutes4() { + assertThat(new Solution().numberOfRoutes(new String[] {".."}, 1), equalTo(4)); + } +} diff --git a/src/test/java/g3701_3800/s3798_largest_even_number/SolutionTest.java b/src/test/java/g3701_3800/s3798_largest_even_number/SolutionTest.java new file mode 100644 index 000000000..529a91bbd --- /dev/null +++ b/src/test/java/g3701_3800/s3798_largest_even_number/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3798_largest_even_number; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void largestEven() { + assertThat(new Solution().largestEven("1112"), equalTo("1112")); + } + + @Test + void largestEven2() { + assertThat(new Solution().largestEven("221"), equalTo("22")); + } + + @Test + void largestEven3() { + assertThat(new Solution().largestEven("1"), equalTo("")); + } +} diff --git a/src/test/java/g3701_3800/s3799_word_squares_ii/SolutionTest.java b/src/test/java/g3701_3800/s3799_word_squares_ii/SolutionTest.java new file mode 100644 index 000000000..27aad4968 --- /dev/null +++ b/src/test/java/g3701_3800/s3799_word_squares_ii/SolutionTest.java @@ -0,0 +1,29 @@ +package g3701_3800.s3799_word_squares_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void wordSquares() { + List> result = + new Solution().wordSquares(new String[] {"able", "area", "echo", "also"}); + assertThat( + result, + equalTo( + Arrays.asList( + Arrays.asList("able", "area", "echo", "also"), + Arrays.asList("area", "able", "also", "echo")))); + } + + @Test + void wordSquares2() { + assertThat( + new Solution().wordSquares(new String[] {"code", "cafe", "eden", "edge"}), + equalTo(Arrays.asList())); + } +} diff --git a/src/test/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/SolutionTest.java b/src/test/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/SolutionTest.java new file mode 100644 index 000000000..0c160a72b --- /dev/null +++ b/src/test/java/g3701_3800/s3800_minimum_cost_to_make_two_binary_strings_equal/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3800_minimum_cost_to_make_two_binary_strings_equal; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumCost() { + assertThat(new Solution().minimumCost("01000", "10111", 10, 2, 2), equalTo(16L)); + } + + @Test + void minimumCost2() { + assertThat(new Solution().minimumCost("001", "110", 2, 100, 100), equalTo(6L)); + } + + @Test + void minimumCost3() { + assertThat(new Solution().minimumCost("1010", "1010", 5, 5, 5), equalTo(0L)); + } +} diff --git a/src/test/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/SolutionTest.java b/src/test/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/SolutionTest.java new file mode 100644 index 000000000..99b2cf30d --- /dev/null +++ b/src/test/java/g3801_3900/s3801_minimum_cost_to_merge_sorted_lists/SolutionTest.java @@ -0,0 +1,31 @@ +package g3801_3900.s3801_minimum_cost_to_merge_sorted_lists; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMergeCost() { + assertThat( + new Solution().minMergeCost(new int[][] {{1, 3, 5}, {2, 4}, {6, 7, 8}}), + equalTo(18L)); + } + + @Test + void minMergeCost2() { + assertThat( + new Solution().minMergeCost(new int[][] {{1, 1, 5}, {1, 4, 7, 8}}), equalTo(10L)); + } + + @Test + void minMergeCost3() { + assertThat(new Solution().minMergeCost(new int[][] {{1}, {3}}), equalTo(4L)); + } + + @Test + void minMergeCost4() { + assertThat(new Solution().minMergeCost(new int[][] {{1}, {1}}), equalTo(2L)); + } +} diff --git a/src/test/java/g3801_3900/s3803_count_residue_prefixes/SolutionTest.java b/src/test/java/g3801_3900/s3803_count_residue_prefixes/SolutionTest.java new file mode 100644 index 000000000..d09c3f3f6 --- /dev/null +++ b/src/test/java/g3801_3900/s3803_count_residue_prefixes/SolutionTest.java @@ -0,0 +1,23 @@ +package g3801_3900.s3803_count_residue_prefixes; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void residuePrefixes() { + assertThat(new Solution().residuePrefixes("abc"), equalTo(2)); + } + + @Test + void residuePrefixes2() { + assertThat(new Solution().residuePrefixes("dd"), equalTo(1)); + } + + @Test + void residuePrefixes3() { + assertThat(new Solution().residuePrefixes("bob"), equalTo(2)); + } +} diff --git a/src/test/java/g3801_3900/s3804_number_of_centered_subarrays/SolutionTest.java b/src/test/java/g3801_3900/s3804_number_of_centered_subarrays/SolutionTest.java new file mode 100644 index 000000000..1dc8b981c --- /dev/null +++ b/src/test/java/g3801_3900/s3804_number_of_centered_subarrays/SolutionTest.java @@ -0,0 +1,18 @@ +package g3801_3900.s3804_number_of_centered_subarrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void centeredSubarrays() { + assertThat(new Solution().centeredSubarrays(new int[] {-1, 1, 0}), equalTo(5)); + } + + @Test + void centeredSubarrays2() { + assertThat(new Solution().centeredSubarrays(new int[] {2, -3}), equalTo(2)); + } +} diff --git a/src/test/java/g3801_3900/s3805_count_caesar_cipher_pairs/SolutionTest.java b/src/test/java/g3801_3900/s3805_count_caesar_cipher_pairs/SolutionTest.java new file mode 100644 index 000000000..c8f166f2b --- /dev/null +++ b/src/test/java/g3801_3900/s3805_count_caesar_cipher_pairs/SolutionTest.java @@ -0,0 +1,18 @@ +package g3801_3900.s3805_count_caesar_cipher_pairs; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countPairs() { + assertThat(new Solution().countPairs(new String[] {"fusion", "layout"}), equalTo(1L)); + } + + @Test + void countPairs2() { + assertThat(new Solution().countPairs(new String[] {"ab", "aa", "za", "aa"}), equalTo(2L)); + } +}