From 79f5407c34eb02383cf918301030e1bfe79bd868 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 2 Nov 2025 16:13:33 +0200 Subject: [PATCH 1/5] Added tasks 3731-3734 --- .../s3731_find_missing_elements/Solution.java | 22 +++++ .../s3731_find_missing_elements/readme.md | 46 ++++++++++ .../Solution.java | 20 ++++ .../readme.md | 51 ++++++++++ .../Solution.java | 41 +++++++++ .../readme.md | 53 +++++++++++ .../Solution.java | 92 +++++++++++++++++++ .../readme.md | 55 +++++++++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 28 ++++++ 12 files changed, 478 insertions(+) create mode 100644 src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java create mode 100644 src/main/java/g3701_3800/s3731_find_missing_elements/readme.md create mode 100644 src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java create mode 100644 src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md create mode 100644 src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java create mode 100644 src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md create mode 100644 src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java create mode 100644 src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md create mode 100644 src/test/java/g3701_3800/s3731_find_missing_elements/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java b/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java new file mode 100644 index 000000000..9c8c064fd --- /dev/null +++ b/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java @@ -0,0 +1,22 @@ +package g3701_3800.s3731_find_missing_elements; + +// #Easy #Weekly_Contest_474 #2025_11_02_Time_6_ms_(100.00%)_Space_47.09_MB_(_%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + public List findMissingElements(int[] nums) { + List list = new ArrayList<>(); + Arrays.sort(nums); + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] - nums[i] > 1) { + for (int j = nums[i] + 1; j < nums[i + 1]; j++) { + list.add(j); + } + } + } + return list; + } +} diff --git a/src/main/java/g3701_3800/s3731_find_missing_elements/readme.md b/src/main/java/g3701_3800/s3731_find_missing_elements/readme.md new file mode 100644 index 000000000..e48870f98 --- /dev/null +++ b/src/main/java/g3701_3800/s3731_find_missing_elements/readme.md @@ -0,0 +1,46 @@ +3731\. Find Missing Elements + +Easy + +You are given an integer array `nums` consisting of **unique** integers. + +Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array. + +The **smallest** and **largest** integers of the original range are still present in `nums`. + +Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list. + +**Example 1:** + +**Input:** nums = [1,4,2,5] + +**Output:** [3] + +**Explanation:** + +The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. Among these, only 3 is missing. + +**Example 2:** + +**Input:** nums = [7,8,6,9] + +**Output:** [] + +**Explanation:** + +The smallest integer is 6 and the largest is 9, so the full range is `[6,7,8,9]`. All integers are already present, so no integer is missing. + +**Example 3:** + +**Input:** nums = [5,1] + +**Output:** [2,3,4] + +**Explanation:** + +The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. The missing integers are 2, 3, and 4. + +**Constraints:** + +* `2 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java new file mode 100644 index 000000000..8a2a6b4d6 --- /dev/null +++ b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java @@ -0,0 +1,20 @@ +package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement; + +// #Medium #Weekly_Contest_474 #2025_11_02_Time_4_ms_(100.00%)_Space_96.67_MB_(_%) + +public class Solution { + public long maxProduct(int[] nums) { + long a = 0; + long b = 0; + for (int x : nums) { + long ax = Math.abs(x); + if (ax >= a) { + b = a; + a = ax; + } else if (ax > b) { + b = ax; + } + } + return 100000L * a * b; + } +} diff --git a/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md new file mode 100644 index 000000000..48ad50d93 --- /dev/null +++ b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md @@ -0,0 +1,51 @@ +3732\. Maximum Product of Three Elements After One Replacement + +Medium + +You are given an integer array `nums`. + +You **must** replace **exactly one** element in the array with **any** integer value in the range [-105, 105] (inclusive). + +After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array. + +Return an integer denoting the **maximum product** achievable. + +**Example 1:** + +**Input:** nums = [-5,7,0] + +**Output:** 3500000 + +**Explanation:** + +Replacing 0 with -105 gives the array [-5, 7, -105], which has a product (-5) * 7 * (-105) = 3500000. The maximum product is 3500000. + +**Example 2:** + +**Input:** nums = [-4,-2,-1,-3] + +**Output:** 1200000 + +**Explanation:** + +Two ways to achieve the maximum product include: + +* `[-4, -2, -3]` → replace -2 with 105 → product = (-4) * 105 * (-3) = 1200000. +* `[-4, -1, -3]` → replace -1 with 105 → product = (-4) * 105 * (-3) = 1200000. + +The maximum product is 1200000. + +**Example 3:** + +**Input:** nums = [0,10,0] + +**Output:** 0 + +**Explanation:** + +There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0. + +**Constraints:** + +* 3 <= nums.length <= 105 +* -105 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java new file mode 100644 index 000000000..da80e2aa5 --- /dev/null +++ b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java @@ -0,0 +1,41 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries; + +// #Medium #Weekly_Contest_474 #2025_11_02_Time_2_ms_(100.00%)_Space_44.04_MB_(_%) + +public class Solution { + private boolean func(long mid, int[] d, int[] r) { + long lcm = (long) r[0] * r[1] / gcd(r[0], r[1]); + long a1 = mid / r[0]; + long a2 = mid / r[1]; + long a3 = mid / lcm; + long b = mid - a1 - a2 + a3; + long o1 = a2 - a3; + long o2 = a1 - a3; + long d0 = Math.max(d[0] - o1, 0); + long d1 = Math.max(d[1] - o2, 0); + return b >= d0 + d1; + } + + private long gcd(long a, long b) { + if (b == 0) { + return a; + } + return gcd(b, a % b); + } + + public long minimumTime(int[] d, int[] r) { + long lo = 0; + long hi = (long) 1e12; + long ans = Long.MAX_VALUE; + while (lo <= hi) { + long mid = (lo + hi) / 2; + if (func(mid, d, r)) { + ans = mid; + hi = mid - 1; + } else { + lo = mid + 1; + } + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md new file mode 100644 index 000000000..4fbcff723 --- /dev/null +++ b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md @@ -0,0 +1,53 @@ +3733\. Minimum Time to Complete All Deliveries + +Medium + +You are given two integer arrays of size 2: d = [d1, d2] and r = [r1, r2]. + +Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete di deliveries. + +Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour. + +Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone `i` must recharge every ri hours (i.e. at hours that are multiples of ri). + +Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries. + +**Example 1:** + +**Input:** d = [3,1], r = [2,3] + +**Output:** 5 + +**Explanation:** + +* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4). +* The second drone delivers at hour 2 (recharges at hour 3). + +**Example 2:** + +**Input:** d = [1,3], r = [2,2] + +**Output:** 7 + +**Explanation:** + +* The first drone delivers at hour 3 (recharges at hours 2, 4, 6). +* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6). + +**Example 3:** + +**Input:** d = [2,1], r = [3,4] + +**Output:** 3 + +**Explanation:** + +* The first drone delivers at hours 1, 2 (recharges at hour 3). +* The second drone delivers at hour 3. + +**Constraints:** + +* d = [d1, d2] +* 1 <= di <= 109 +* r = [r1, r2] +* 2 <= ri <= 3 * 104 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java new file mode 100644 index 000000000..52b2a2cfe --- /dev/null +++ b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java @@ -0,0 +1,92 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target; + +// #Hard #Weekly_Contest_474 #2025_11_02_Time_3_ms_(50.00%)_Space_46.55_MB_(_%) + +import java.util.Arrays; + +public class Solution { + public boolean func( + int i, String target, char[] ans, int l, int r, int[] freq, boolean end) { + if (l > r) { + return new String(ans).compareTo(target) > 0; + } + if (l == r) { + char left = '#'; + for (int k = 0; k < 26; k++) { + if (freq[k] > 0) { + left = (char) (k + 'a'); + } + } + freq[left - 'a']--; + ans[l] = left; + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true; + } + freq[left - 'a']++; + ans[l] = '#'; + return false; + } + if (end) { + for (int j = 0; j < 26; j++) { + if (freq[j] > 1) { + freq[j] -= 2; + ans[l] = ans[r] = (char) (j + 'a'); + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true; + } + ans[l] = ans[r] = '#'; + freq[j] += 2; + } + } + return false; + } + char curr = target.charAt(i); + char next = '1'; + for (int k = target.charAt(i) - 'a' + 1; k < 26; k++) { + if (freq[k] > 1) { + next = (char) (k + 'a'); + break; + } + } + if (freq[curr - 'a'] > 1) { + ans[l] = ans[r] = curr; + freq[curr - 'a'] -= 2; + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true; + } + freq[curr - 'a'] += 2; + } + if (next != '1') { + ans[l] = ans[r] = next; + freq[next - 'a'] -= 2; + if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { + return true; + } + freq[next - 'a'] += 2; + } + ans[l] = ans[r] = '#'; + return false; + } + + public String lexPalindromicPermutation(String s, String target) { + int[] freq = new int[26]; + for (int i = 0; i < s.length(); i++) { + freq[s.charAt(i) - 'a']++; + } + int oddc = 0; + for (int i = 0; i < 26; i++) { + if (freq[i] % 2 == 1) { + oddc++; + } + } + if (oddc > 1) { + return ""; + } + char[] ans = new char[s.length()]; + Arrays.fill(ans, '#'); + if (func(0, target, ans, 0, s.length() - 1, freq, false)) { + return new String(ans); + } + return ""; + } +} diff --git a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md new file mode 100644 index 000000000..fbf7ec289 --- /dev/null +++ b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md @@ -0,0 +1,55 @@ +3734\. Lexicographically Smallest Palindromic Permutation Greater Than Target + +Hard + +You are given two strings `s` and `target`, each of length `n`, consisting of lowercase English letters. + +Return the **lexicographically smallest string** that is **both** a **palindromic permutation** of `s` and **strictly** greater than `target`. If no such permutation exists, return an empty string. + +**Example 1:** + +**Input:** s = "baba", target = "abba" + +**Output:** "baab" + +**Explanation:** + +* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`. +* The lexicographically smallest permutation that is strictly greater than `target` is `"baab"`. + +**Example 2:** + +**Input:** s = "baba", target = "bbaa" + +**Output:** "" + +**Explanation:** + +* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`. +* None of them is lexicographically strictly greater than `target`. Therefore, the answer is `""`. + +**Example 3:** + +**Input:** s = "abc", target = "abb" + +**Output:** "" + +**Explanation:** + +`s` has no palindromic permutations. Therefore, the answer is `""`. + +**Example 4:** + +**Input:** s = "aac", target = "abb" + +**Output:** "aca" + +**Explanation:** + +* The only palindromic permutation of `s` is `"aca"`. +* `"aca"` is strictly greater than `target`. Therefore, the answer is `"aca"`. + +**Constraints:** + +* `1 <= n == s.length == target.length <= 300` +* `s` and `target` consist of only lowercase English letters. \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3731_find_missing_elements/SolutionTest.java b/src/test/java/g3701_3800/s3731_find_missing_elements/SolutionTest.java new file mode 100644 index 000000000..2d5436c5f --- /dev/null +++ b/src/test/java/g3701_3800/s3731_find_missing_elements/SolutionTest.java @@ -0,0 +1,24 @@ +package g3701_3800.s3731_find_missing_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMissingElements() { + assertThat(new Solution().findMissingElements(new int[] {1, 4, 2, 5}), equalTo(List.of(3))); + } + + @Test + void findMissingElements2() { + assertThat(new Solution().findMissingElements(new int[] {7, 8, 6, 9}), equalTo(List.of())); + } + + @Test + void findMissingElements3() { + assertThat(new Solution().findMissingElements(new int[] {5, 1}), equalTo(List.of(2, 3, 4))); + } +} diff --git a/src/test/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.java b/src/test/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.java new file mode 100644 index 000000000..7a36d7bfd --- /dev/null +++ b/src/test/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxProduct() { + assertThat(new Solution().maxProduct(new int[] {-5, 7, 0}), equalTo(3500000L)); + } + + @Test + void maxProduct2() { + assertThat(new Solution().maxProduct(new int[] {-4, -2, -1, -3}), equalTo(1200000L)); + } + + @Test + void maxProduct3() { + assertThat(new Solution().maxProduct(new int[] {0, 10, 0}), equalTo(0L)); + } +} diff --git a/src/test/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.java b/src/test/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.java new file mode 100644 index 000000000..3948372c7 --- /dev/null +++ b/src/test/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumTime() { + assertThat(new Solution().minimumTime(new int[] {3, 1}, new int[] {2, 3}), equalTo(5L)); + } + + @Test + void minimumTime2() { + assertThat(new Solution().minimumTime(new int[] {1, 3}, new int[] {2, 2}), equalTo(7L)); + } + + @Test + void minimumTime3() { + assertThat(new Solution().minimumTime(new int[] {2, 1}, new int[] {3, 4}), equalTo(3L)); + } +} diff --git a/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java new file mode 100644 index 000000000..4f3da0e75 --- /dev/null +++ b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java @@ -0,0 +1,28 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void lexPalindromicPermutation() { + assertThat(new Solution().lexPalindromicPermutation("baba", "abba"), equalTo("baab")); + } + + @Test + void lexPalindromicPermutation2() { + assertThat(new Solution().lexPalindromicPermutation("baba", "bbaa"), equalTo("")); + } + + @Test + void lexPalindromicPermutation3() { + assertThat(new Solution().lexPalindromicPermutation("abc", "abb"), equalTo("")); + } + + @Test + void lexPalindromicPermutation4() { + assertThat(new Solution().lexPalindromicPermutation("aac", "abb"), equalTo("aca")); + } +} From 706efdebd795d25c02aa0b6bc58b7d7052cbc7ac Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 2 Nov 2025 16:16:50 +0200 Subject: [PATCH 2/5] Fixed format --- .../Solution.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java index 52b2a2cfe..d9e0490f7 100644 --- a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java +++ b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java @@ -5,8 +5,7 @@ import java.util.Arrays; public class Solution { - public boolean func( - int i, String target, char[] ans, int l, int r, int[] freq, boolean end) { + public boolean func(int i, String target, char[] ans, int l, int r, int[] freq, boolean end) { if (l > r) { return new String(ans).compareTo(target) > 0; } From cb2d535b7769949e56818a375614bf489903a2c3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 3 Nov 2025 13:49:26 +0200 Subject: [PATCH 3/5] Added tests --- .../Solution.java | 3 +- .../SolutionTest.java | 277 ++++++++++++++++++ 2 files changed, 278 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java index d9e0490f7..5785f4bf2 100644 --- a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java +++ b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java @@ -5,7 +5,7 @@ import java.util.Arrays; public class Solution { - public boolean func(int i, String target, char[] ans, int l, int r, int[] freq, boolean end) { + boolean func(int i, String target, char[] ans, int l, int r, int[] freq, boolean end) { if (l > r) { return new String(ans).compareTo(target) > 0; } @@ -61,7 +61,6 @@ public boolean func(int i, String target, char[] ans, int l, int r, int[] freq, if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { return true; } - freq[next - 'a'] += 2; } ans[l] = ans[r] = '#'; return false; diff --git a/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java index 4f3da0e75..c900dfee6 100644 --- a/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java +++ b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java @@ -2,6 +2,13 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.hasLength; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import org.junit.jupiter.api.Test; @@ -25,4 +32,274 @@ void lexPalindromicPermutation3() { void lexPalindromicPermutation4() { assertThat(new Solution().lexPalindromicPermutation("aac", "abb"), equalTo("aca")); } + + @Test + void lexPalindromicPermutation5() { + // Branch: oddc > 1 + String result = new Solution().lexPalindromicPermutation("abc", "a"); + assertThat(result, equalTo("")); + } + + @Test + void lexPalindromicPermutation6() { + // Branch: oddc = 1 + String result = new Solution().lexPalindromicPermutation("aab", "a"); + assertThat(result, allOf(not(equalTo("")), hasLength(3))); + } + + @Test + void lexPalindromicPermutation7() { + // Branch: oddc = 0 + String result = new Solution().lexPalindromicPermutation("aabb", "ab"); + assertThat(result, not(equalTo(""))); + } + + @Test + void lexPalindromicPermutation8() { + // Branch: func returns false + String result = new Solution().lexPalindromicPermutation("abc", "xyz"); + assertThat(result, equalTo("")); + } + + @Test + void lexPalindromicPermutation9() { + // Edge case: length = 1 + String result = new Solution().lexPalindromicPermutation("a", "a"); + assertThat(result, equalTo("")); + } + + @Test + void lexPalindromicPermutation10() { + // Branch: l > r and comparison result > 0 + String target = "a"; + int[] freq = new int[26]; + char[] ans = {'b', 'b'}; + + boolean result = new Solution().func(0, target, ans, 1, 0, freq, false); + assertThat(result, equalTo(true)); + } + + @Test + void lexPalindromicPermutation11() { + // Branch: l > r and comparison result <= 0 + String target = "z"; + int[] freq = new int[26]; + char[] ans = {'a', 'a'}; + + boolean result = new Solution().func(0, target, ans, 1, 0, freq, false); + assertThat(result, equalTo(false)); + } + + @Test + void lexPalindromicPermutation12() { + // Branch: l == r with available character + String target = "a"; + int[] freq = new int[26]; + freq[0] = 1; // 'a' has frequency 1 + char[] ans = new char[1]; + + boolean result = new Solution().func(0, target, ans, 0, 0, freq, false); + assertThat(result, equalTo(false)); + assertThat(ans[0], equalTo('#')); + } + + @Test + void lexPalindromicPermutation13() { + // Branch: end = true, finds char with freq > 1 + String target = "ab"; + int[] freq = new int[26]; + freq[0] = 2; // 'a' can form a pair + freq[1] = 0; + char[] ans = new char[2]; + + boolean result = new Solution().func(1, target, ans, 0, 1, freq, true); + assertThat(result, anyOf(equalTo(true), equalTo(false))); + assertThat(freq[0], equalTo(2)); // Frequency restored + } + + @Test + void lexPalindromicPermutation14() { + // Branch: end = true, no char has freq > 1 + String target = "ab"; + int[] freq = new int[26]; + freq[0] = 1; + freq[1] = 1; + char[] ans = new char[2]; + + boolean result = new Solution().func(1, target, ans, 0, 1, freq, true); + assertThat(result, equalTo(false)); + } + + @Test + void lexPalindromicPermutation15() { + // Branch: end = true, tries different pairs + String target = "abc"; + int[] freq = new int[26]; + freq[0] = 2; + freq[1] = 2; + freq[2] = 2; + char[] ans = new char[3]; + + new Solution().func(1, target, ans, 0, 2, freq, true); + assertThat(freq[0], equalTo(0)); + assertThat(freq[1], equalTo(2)); + assertThat(freq[2], equalTo(1)); + } + + @Test + void lexPalindromicPermutation16() { + // Branch: end = false, curr has freq > 1 + String target = "a"; + int[] freq = new int[26]; + freq[0] = 2; + char[] ans = new char[2]; + + new Solution().func(0, target, ans, 0, 1, freq, false); + assertThat(freq[0], equalTo(0)); + } + + @Test + void lexPalindromicPermutation17() { + // Branch: end = false, curr has freq <= 1 + String target = "a"; + int[] freq = new int[26]; + freq[0] = 0; + char[] ans = new char[2]; + + new Solution().func(0, target, ans, 0, 1, freq, false); + assertThat(freq[0], equalTo(0)); + } + + @Test + void lexPalindromicPermutation18() { + // Branch: end = false, finds next char with freq > 1 + String target = "a"; + int[] freq = new int[26]; + freq[0] = 0; + freq[1] = 2; + char[] ans = new char[2]; + + new Solution().func(0, target, ans, 0, 1, freq, false); + assertThat(freq[0], equalTo(0)); + assertThat(freq[1], equalTo(0)); + } + + @Test + void lexPalindromicPermutation19() { + // Branch: end = false, no next char with freq > 1 + String target = "z"; + int[] freq = new int[26]; + freq[25] = 1; + char[] ans = new char[2]; + + boolean result = new Solution().func(0, target, ans, 0, 1, freq, false); + assertThat(result, equalTo(false)); + } + + @Test + void lexPalindromicPermutation20() { + // Branch: end = false transitions to end = true + String target = "ab"; + int[] freq = new int[26]; + freq[0] = 2; + freq[1] = 2; + char[] ans = new char[2]; + + new Solution().func(0, target, ans, 0, 1, freq, false); + + assertThat(freq[0], equalTo(2)); + assertThat(freq[1], equalTo(0)); + } + + @Test + void lexPalindromicPermutation21() { + // Verify result is always a palindrome + String result = new Solution().lexPalindromicPermutation("aabbcc", "abc"); + if (!result.isEmpty()) { + String reversed = new StringBuilder(result).reverse().toString(); + assertThat(result, equalTo(reversed)); + } + } + + @Test + void lexPalindromicPermutation22() { + // Verify character frequencies are preserved + String input = "aabbcc"; + String result = new Solution().lexPalindromicPermutation(input, "abc"); + + if (!result.isEmpty()) { + int[] inputFreq = new int[26]; + int[] resultFreq = new int[26]; + + for (char c : input.toCharArray()) { + inputFreq[c - 'a']++; + } + for (char c : result.toCharArray()) { + resultFreq[c - 'a']++; + } + + assertThat(resultFreq, equalTo(inputFreq)); + } + } + + @Test + void lexPalindromicPermutation23() { + // Result length should match input length + String input = "aabbccdd"; + String result = new Solution().lexPalindromicPermutation(input, "abcd"); + + if (!result.isEmpty()) { + assertThat(result.length(), equalTo(input.length())); + } + } + + @Test + void lexPalindromicPermutation24() { + // Result should be >= target in lexicographical order + String result = new Solution().lexPalindromicPermutation("aabbcc", "abc"); + + if (!result.isEmpty()) { + assertThat(result.compareTo("abc"), greaterThanOrEqualTo(0)); + } + } + + @Test + void lexPalindromicPermutation25() { + // Complex scenario with multiple characters + String result = new Solution().lexPalindromicPermutation("aabbccdd", "abcd"); + + assertThat( + result, + anyOf(equalTo(""), allOf(hasLength(8), containsString("a"), containsString("b")))); + } + + @Test + void lexPalindromicPermutation26() { + // Edge case: empty string (if applicable) + String result = new Solution().lexPalindromicPermutation("", ""); + assertThat(result, anyOf(equalTo(""), not(nullValue()))); + } + + @Test + void lexPalindromicPermutation27() { + // Verify frequency array is restored after recursion + String target = "aabb"; + int[] freq = new int[26]; + int[] freqCopy = freq.clone(); + + char[] ans = new char[4]; + new Solution().func(0, target, ans, 0, 3, freq, false); + + assertThat(freq, equalTo(freqCopy)); + } + + @Test + void lexPalindromicPermutation28() { + // Verify char array is properly initialized + String result = new Solution().lexPalindromicPermutation("aa", "a"); + + if (!result.isEmpty()) { + assertThat(result, not(containsString("#"))); + } + } } From efeb3941fdfb7deb039494a4c15183ae086f6cec Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 3 Nov 2025 15:47:03 +0200 Subject: [PATCH 4/5] Fixed style --- .../SolutionTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java index c900dfee6..c1f4cd339 100644 --- a/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java +++ b/src/test/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.java @@ -95,7 +95,8 @@ void lexPalindromicPermutation12() { // Branch: l == r with available character String target = "a"; int[] freq = new int[26]; - freq[0] = 1; // 'a' has frequency 1 + // 'a' has frequency 1 + freq[0] = 1; char[] ans = new char[1]; boolean result = new Solution().func(0, target, ans, 0, 0, freq, false); @@ -108,13 +109,15 @@ void lexPalindromicPermutation13() { // Branch: end = true, finds char with freq > 1 String target = "ab"; int[] freq = new int[26]; - freq[0] = 2; // 'a' can form a pair + // 'a' can form a pair + freq[0] = 2; freq[1] = 0; char[] ans = new char[2]; boolean result = new Solution().func(1, target, ans, 0, 1, freq, true); assertThat(result, anyOf(equalTo(true), equalTo(false))); - assertThat(freq[0], equalTo(2)); // Frequency restored + // Frequency restored + assertThat(freq[0], equalTo(2)); } @Test From 862ffda6b11bcb94519e413375d250fe5e93857a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 5 Nov 2025 18:31:45 +0200 Subject: [PATCH 5/5] Added tags --- .../s3731_find_missing_elements/Solution.java | 26 ++++++---- .../Solution.java | 3 +- .../Solution.java | 48 +++++++++++-------- .../Solution.java | 3 +- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java b/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java index 9c8c064fd..868f197cf 100644 --- a/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java +++ b/src/main/java/g3701_3800/s3731_find_missing_elements/Solution.java @@ -1,21 +1,31 @@ package g3701_3800.s3731_find_missing_elements; -// #Easy #Weekly_Contest_474 #2025_11_02_Time_6_ms_(100.00%)_Space_47.09_MB_(_%) +// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474 +// #2025_11_05_Time_2_ms_(94.89%)_Space_46.54_MB_(95.16%) import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class Solution { public List findMissingElements(int[] nums) { + int maxi = 0; + int mini = 101; List list = new ArrayList<>(); - Arrays.sort(nums); - for (int i = 0; i < nums.length - 1; i++) { - if (nums[i + 1] - nums[i] > 1) { - for (int j = nums[i] + 1; j < nums[i + 1]; j++) { - list.add(j); - } + boolean[] array = new boolean[101]; + for (int num : nums) { + array[num] = true; + if (maxi < num) { + maxi = num; } + if (mini > num) { + mini = num; + } + } + for (int index = mini + 1; index < maxi; index++) { + if (array[index]) { + continue; + } + list.add(index); } return list; } diff --git a/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java index 8a2a6b4d6..1f5a7f916 100644 --- a/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java +++ b/src/main/java/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement; -// #Medium #Weekly_Contest_474 #2025_11_02_Time_4_ms_(100.00%)_Space_96.67_MB_(_%) +// #Medium #Array #Math #Sorting #Greedy #Weekly_Contest_474 +// #2025_11_05_Time_4_ms_(95.32%)_Space_97.32_MB_(28.84%) public class Solution { public long maxProduct(int[] nums) { diff --git a/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java index da80e2aa5..af52529e3 100644 --- a/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java +++ b/src/main/java/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.java @@ -1,41 +1,47 @@ package g3701_3800.s3733_minimum_time_to_complete_all_deliveries; -// #Medium #Weekly_Contest_474 #2025_11_02_Time_2_ms_(100.00%)_Space_44.04_MB_(_%) +// #Medium #Math #Binary_Search #Weekly_Contest_474 +// #2025_11_05_Time_1_ms_(100.00%)_Space_44.16_MB_(55.61%) public class Solution { - private boolean func(long mid, int[] d, int[] r) { - long lcm = (long) r[0] * r[1] / gcd(r[0], r[1]); - long a1 = mid / r[0]; - long a2 = mid / r[1]; - long a3 = mid / lcm; - long b = mid - a1 - a2 + a3; - long o1 = a2 - a3; - long o2 = a1 - a3; - long d0 = Math.max(d[0] - o1, 0); - long d1 = Math.max(d[1] - o2, 0); - return b >= d0 + d1; + private boolean pos(long k, long n1, long n2, int p1, int p2, long lVal) { + long kP1 = k / p1; + long kP2 = k / p2; + long kL = k / lVal; + long s1 = kP2 - kL; + long s2 = kP1 - kL; + long sB = k - kP1 - kP2 + kL; + long w1 = Math.max(0L, n1 - s1); + long w2 = Math.max(0L, n2 - s2); + return (w1 + w2) <= sB; } - private long gcd(long a, long b) { + private long findGcd(long a, long b) { if (b == 0) { return a; } - return gcd(b, a % b); + return findGcd(b, a % b); } public long minimumTime(int[] d, int[] r) { - long lo = 0; - long hi = (long) 1e12; - long ans = Long.MAX_VALUE; + long n1 = d[0]; + long n2 = d[1]; + int p1 = r[0]; + int p2 = r[1]; + long g = findGcd(p1, p2); + long l = (long) p1 * p2 / g; + long lo = n1 + n2; + long hi = (n1 + n2) * Math.max(p1, p2); + long res = hi; while (lo <= hi) { - long mid = (lo + hi) / 2; - if (func(mid, d, r)) { - ans = mid; + long mid = lo + (hi - lo) / 2; + if (pos(mid, n1, n2, p1, p2, l)) { + res = mid; hi = mid - 1; } else { lo = mid + 1; } } - return ans; + return res; } } diff --git a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java index 5785f4bf2..175a26c28 100644 --- a/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java +++ b/src/main/java/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target; -// #Hard #Weekly_Contest_474 #2025_11_02_Time_3_ms_(50.00%)_Space_46.55_MB_(_%) +// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474 +// #2025_11_05_Time_2_ms_(100.00%)_Space_46.34_MB_(84.73%) import java.util.Arrays;