From 4aedb5492870912bba2e7e6d3c52fd442e2d9058 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 3 Nov 2025 12:18:21 +0200 Subject: [PATCH 1/5] Added tasks 3731-3734 --- .../s3731_find_missing_elements/Solution.kt | 18 ++++ .../s3731_find_missing_elements/readme.md | 46 +++++++++ .../Solution.kt | 22 +++++ .../readme.md | 51 ++++++++++ .../Solution.kt | 43 +++++++++ .../readme.md | 53 +++++++++++ .../Solution.kt | 94 +++++++++++++++++++ .../readme.md | 55 +++++++++++ .../SolutionTest.kt | 31 ++++++ .../SolutionTest.kt | 28 ++++++ .../SolutionTest.kt | 31 ++++++ .../SolutionTest.kt | 39 ++++++++ 12 files changed, 511 insertions(+) create mode 100644 src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3731_find_missing_elements/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md create mode 100644 src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt diff --git a/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt new file mode 100644 index 00000000..1bb87c3f --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt @@ -0,0 +1,18 @@ +package g3701_3800.s3731_find_missing_elements + +// #Easy #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_48.81_MB_(11.11%) + +class Solution { + fun findMissingElements(nums: IntArray): MutableList { + val list: MutableList = ArrayList() + nums.sort() + for (i in 0.. 1) { + for (j in nums[i] + 1..= a) { + b = a + a = ax + } else if (ax > b) { + b = ax + } + } + return 100000L * a * b + } +} diff --git a/src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md b/src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md new file mode 100644 index 00000000..48ad50d9 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt new file mode 100644 index 00000000..42cf610a --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt @@ -0,0 +1,43 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries + +// #Medium #Weekly_Contest_474 #2025_11_03_Time_2_ms_(100.00%)_Space_42.94_MB_(100.00%) + +import kotlin.math.max + +class Solution { + private fun func(mid: Long, d: IntArray, r: IntArray): Boolean { + val lcm = r[0].toLong() * r[1] / gcd(r[0].toLong(), r[1].toLong()) + val a1 = mid / r[0] + val a2 = mid / r[1] + val a3 = mid / lcm + val b = mid - a1 - a2 + a3 + val o1 = a2 - a3 + val o2 = a1 - a3 + val d0 = max(d[0] - o1, 0) + val d1 = max(d[1] - o2, 0) + return b >= d0 + d1 + } + + private fun gcd(a: Long, b: Long): Long { + if (b == 0L) { + return a + } + return gcd(b, a % b) + } + + fun minimumTime(d: IntArray, r: IntArray): Long { + var lo: Long = 0 + var hi = 1e12.toLong() + var ans = Long.MAX_VALUE + while (lo <= hi) { + val 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/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md new file mode 100644 index 00000000..4fbcff72 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt new file mode 100644 index 00000000..9b03fc3e --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt @@ -0,0 +1,94 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target + +// #Hard #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_47.24_MB_(100.00%) + +class Solution { + fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { + if (l > r) { + return String(ans).compareTo(target) > 0 + } + if (l == r) { + var left = '#' + for (k in 0..25) { + if (freq[k] > 0) { + left = (k + 'a'.code).toChar() + } + } + freq[left.code - 'a'.code]-- + ans[l] = left + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true + } + freq[left.code - 'a'.code]++ + ans[l] = '#' + return false + } + if (end) { + for (j in 0..25) { + if (freq[j] > 1) { + freq[j] -= 2 + ans[r] = (j + 'a'.code).toChar() + ans[l] = ans[r] + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true + } + ans[r] = '#' + ans[l] = ans[r] + freq[j] += 2 + } + } + return false + } + val curr = target.get(i) + var next = '1' + for (k in target.get(i).code - 'a'.code + 1..25) { + if (freq[k] > 1) { + next = (k + 'a'.code).toChar() + break + } + } + if (freq[curr.code - 'a'.code] > 1) { + ans[r] = curr + ans[l] = ans[r] + freq[curr.code - 'a'.code] -= 2 + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true + } + freq[curr.code - 'a'.code] += 2 + } + if (next != '1') { + ans[r] = next + ans[l] = ans[r] + freq[next.code - 'a'.code] -= 2 + if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { + return true + } + freq[next.code - 'a'.code] += 2 + } + ans[r] = '#' + ans[l] = ans[r] + return false + } + + fun lexPalindromicPermutation(s: String, target: String): String { + val freq = IntArray(26) + for (i in 0.. 1) { + return "" + } + val ans = CharArray(s.length) + ans.fill('#') + if (func(0, target, ans, 0, s.length - 1, freq, false)) { + return String(ans) + } + return "" + } +} diff --git a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md new file mode 100644 index 00000000..fbf7ec28 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt new file mode 100644 index 00000000..404be6d3 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3731_find_missing_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findMissingElements() { + assertThat>( + Solution().findMissingElements(intArrayOf(1, 4, 2, 5)), + equalTo>(mutableListOf(3)), + ) + } + + @Test + fun findMissingElements2() { + assertThat>( + Solution().findMissingElements(intArrayOf(7, 8, 6, 9)), + equalTo>(mutableListOf()), + ) + } + + @Test + fun findMissingElements3() { + assertThat>( + Solution().findMissingElements(intArrayOf(5, 1)), + equalTo>(mutableListOf(2, 3, 4)), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt new file mode 100644 index 00000000..7269907e --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxProduct() { + assertThat( + Solution().maxProduct(intArrayOf(-5, 7, 0)), + equalTo(3500000L), + ) + } + + @Test + fun maxProduct2() { + assertThat( + Solution().maxProduct(intArrayOf(-4, -2, -1, -3)), + equalTo(1200000L), + ) + } + + @Test + fun maxProduct3() { + assertThat(Solution().maxProduct(intArrayOf(0, 10, 0)), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt new file mode 100644 index 00000000..ad9dfc07 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumTime() { + assertThat( + Solution().minimumTime(intArrayOf(3, 1), intArrayOf(2, 3)), + equalTo(5L), + ) + } + + @Test + fun minimumTime2() { + assertThat( + Solution().minimumTime(intArrayOf(1, 3), intArrayOf(2, 2)), + equalTo(7L), + ) + } + + @Test + fun minimumTime3() { + assertThat( + Solution().minimumTime(intArrayOf(2, 1), intArrayOf(3, 4)), + equalTo(3L), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt new file mode 100644 index 00000000..52f8975b --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun lexPalindromicPermutation() { + assertThat( + Solution().lexPalindromicPermutation("baba", "abba"), + equalTo("baab"), + ) + } + + @Test + fun lexPalindromicPermutation2() { + assertThat( + Solution().lexPalindromicPermutation("baba", "bbaa"), + equalTo(""), + ) + } + + @Test + fun lexPalindromicPermutation3() { + assertThat( + Solution().lexPalindromicPermutation("abc", "abb"), + equalTo(""), + ) + } + + @Test + fun lexPalindromicPermutation4() { + assertThat( + Solution().lexPalindromicPermutation("aac", "abb"), + equalTo("aca"), + ) + } +} From 0171d29791cc10089f015e2ecec1edebd072ed8e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 3 Nov 2025 13:03:17 +0200 Subject: [PATCH 2/5] Fixed sonar --- .../Solution.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt index 9b03fc3e..30a5947c 100644 --- a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt @@ -39,9 +39,9 @@ class Solution { } return false } - val curr = target.get(i) + val curr = target[i] var next = '1' - for (k in target.get(i).code - 'a'.code + 1..25) { + for (k in target[i].code - 'a'.code + 1..25) { if (freq[k] > 1) { next = (k + 'a'.code).toChar() break @@ -73,7 +73,7 @@ class Solution { fun lexPalindromicPermutation(s: String, target: String): String { val freq = IntArray(26) for (i in 0.. Date: Wed, 5 Nov 2025 18:49:46 +0200 Subject: [PATCH 3/5] Added tags --- .../s3731_find_missing_elements/Solution.kt | 27 +++++++---- .../Solution.kt | 3 +- .../Solution.kt | 48 +++++++++++-------- .../Solution.kt | 38 +++++++-------- 4 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt index 1bb87c3f..799091fe 100644 --- a/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt @@ -1,17 +1,28 @@ package g3701_3800.s3731_find_missing_elements -// #Easy #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_48.81_MB_(11.11%) +// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474 +// #2025_11_05_Time_2_ms_(100.00%)_Space_46.48_MB_(84.44%) class Solution { - fun findMissingElements(nums: IntArray): MutableList { + fun findMissingElements(nums: IntArray): List { + var maxi = 0 + var mini = 101 val list: MutableList = ArrayList() - nums.sort() - for (i in 0.. 1) { - for (j in nums[i] + 1.. num) { + mini = num + } + } + for (index in mini + 1..= d0 + d1 + private fun pos(k: Long, n1: Long, n2: Long, p1: Int, p2: Int, lVal: Long): Boolean { + val kP1 = k / p1 + val kP2 = k / p2 + val kL = k / lVal + val s1 = kP2 - kL + val s2 = kP1 - kL + val sB = k - kP1 - kP2 + kL + val w1 = max(0L, n1 - s1) + val w2 = max(0L, n2 - s2) + return (w1 + w2) <= sB } - private fun gcd(a: Long, b: Long): Long { + private fun findGcd(a: Long, b: Long): Long { if (b == 0L) { return a } - return gcd(b, a % b) + return findGcd(b, a % b) } fun minimumTime(d: IntArray, r: IntArray): Long { - var lo: Long = 0 - var hi = 1e12.toLong() - var ans = Long.MAX_VALUE + val n1 = d[0].toLong() + val n2 = d[1].toLong() + val p1 = r[0] + val p2 = r[1] + val g = findGcd(p1.toLong(), p2.toLong()) + val l = p1.toLong() * p2 / g + var lo = n1 + n2 + var hi = (n1 + n2) * max(p1, p2) + var res = hi while (lo <= hi) { - val mid = (lo + hi) / 2 - if (func(mid, d, r)) { - ans = mid + val 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/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt index 30a5947c..d8f20297 100644 --- a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt @@ -1,15 +1,16 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target -// #Hard #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_47.24_MB_(100.00%) +// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474 +// #2025_11_05_Time_4_ms_(100.00%)_Space_46.12_MB_(77.78%) class Solution { - fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { + private fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { if (l > r) { return String(ans).compareTo(target) > 0 } if (l == r) { var left = '#' - for (k in 0..25) { + for (k in 0 until 26) { if (freq[k] > 0) { left = (k + 'a'.code).toChar() } @@ -24,16 +25,17 @@ class Solution { return false } if (end) { - for (j in 0..25) { + for (j in 0 until 26) { if (freq[j] > 1) { freq[j] -= 2 - ans[r] = (j + 'a'.code).toChar() - ans[l] = ans[r] + val charJ = (j + 'a'.code).toChar() + ans[l] = charJ + ans[r] = charJ if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { return true } + ans[l] = '#' ans[r] = '#' - ans[l] = ans[r] freq[j] += 2 } } @@ -41,42 +43,41 @@ class Solution { } val curr = target[i] var next = '1' - for (k in target[i].code - 'a'.code + 1..25) { + for (k in (curr.code - 'a'.code + 1) until 26) { if (freq[k] > 1) { next = (k + 'a'.code).toChar() break } } if (freq[curr.code - 'a'.code] > 1) { + ans[l] = curr ans[r] = curr - ans[l] = ans[r] freq[curr.code - 'a'.code] -= 2 - if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + if (func(i + 1, target, ans, l + 1, r - 1, freq, false)) { // end = false return true } freq[curr.code - 'a'.code] += 2 } if (next != '1') { + ans[l] = next ans[r] = next - ans[l] = ans[r] freq[next.code - 'a'.code] -= 2 - if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { + if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { // end = true return true } - freq[next.code - 'a'.code] += 2 } + ans[l] = '#' ans[r] = '#' - ans[l] = ans[r] return false } fun lexPalindromicPermutation(s: String, target: String): String { val freq = IntArray(26) - for (i in 0.. 1) { return "" } - val ans = CharArray(s.length) - ans.fill('#') + val ans = CharArray(s.length) { '#' } if (func(0, target, ans, 0, s.length - 1, freq, false)) { return String(ans) } From 6efca54b93938c3c320027df1ddc7b1e1b8795ad Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 5 Nov 2025 18:54:25 +0200 Subject: [PATCH 4/5] Added tests --- .../Solution.kt | 2 +- .../SolutionTest.kt | 297 ++++++++++++++++++ 2 files changed, 298 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt index d8f20297..2728cdde 100644 --- a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt @@ -4,7 +4,7 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_grea // #2025_11_05_Time_4_ms_(100.00%)_Space_46.12_MB_(77.78%) class Solution { - private fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { + internal fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { if (l > r) { return String(ans).compareTo(target) > 0 } diff --git a/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt index 52f8975b..7d52f275 100644 --- a/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt +++ b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt @@ -2,6 +2,13 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_grea import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.allOf +import org.hamcrest.Matchers.anyOf +import org.hamcrest.Matchers.containsString +import org.hamcrest.Matchers.greaterThanOrEqualTo +import org.hamcrest.Matchers.hasLength +import org.hamcrest.Matchers.not +import org.hamcrest.Matchers.nullValue import org.junit.jupiter.api.Test internal class SolutionTest { @@ -36,4 +43,294 @@ internal class SolutionTest { equalTo("aca"), ) } + + @Test + fun lexPalindromicPermutation5() { + // Branch: oddc > 1 + val result = Solution().lexPalindromicPermutation("abc", "a") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation6() { + // Branch: oddc = 1 + val result = Solution().lexPalindromicPermutation("aab", "a") + assertThat( + result, + allOf(not(equalTo("")), hasLength(3)), + ) + } + + @Test + fun lexPalindromicPermutation7() { + // Branch: oddc = 0 + val result = Solution().lexPalindromicPermutation("aabb", "ab") + assertThat(result, not(equalTo(""))) + } + + @Test + fun lexPalindromicPermutation8() { + // Branch: func returns false + val result = Solution().lexPalindromicPermutation("abc", "xyz") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation9() { + // Edge case: length = 1 + val result = Solution().lexPalindromicPermutation("a", "a") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation10() { + // Branch: l > r and comparison result > 0 + val target = "a" + val freq = IntArray(26) + val ans = charArrayOf('b', 'b') + + val result = Solution().func(0, target, ans, 1, 0, freq, false) + assertThat(result, equalTo(true)) + } + + @Test + fun lexPalindromicPermutation11() { + // Branch: l > r and comparison result <= 0 + val target = "z" + val freq = IntArray(26) + val ans = charArrayOf('a', 'a') + + val result = Solution().func(0, target, ans, 1, 0, freq, false) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation12() { + // Branch: l == r with available character + val target = "a" + val freq = IntArray(26) + // 'a' has frequency 1 + freq[0] = 1 + val ans = CharArray(1) + + val result = Solution().func(0, target, ans, 0, 0, freq, false) + assertThat(result, equalTo(false)) + assertThat(ans[0], equalTo('#')) + } + + @Test + fun lexPalindromicPermutation13() { + // Branch: end = true, finds char with freq > 1 + val target = "ab" + val freq = IntArray(26) + // 'a' can form a pair + freq[0] = 2 + freq[1] = 0 + val ans = CharArray(2) + + val result = Solution().func(1, target, ans, 0, 1, freq, true) + assertThat( + result, + anyOf(equalTo(true), equalTo(false)), + ) + // Frequency restored + assertThat(freq[0], equalTo(2)) + } + + @Test + fun lexPalindromicPermutation14() { + // Branch: end = true, no char has freq > 1 + val target = "ab" + val freq = IntArray(26) + freq[0] = 1 + freq[1] = 1 + val ans = CharArray(2) + + val result = Solution().func(1, target, ans, 0, 1, freq, true) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation15() { + // Branch: end = true, tries different pairs + val target = "abc" + val freq = IntArray(26) + freq[0] = 2 + freq[1] = 2 + freq[2] = 2 + val ans = CharArray(3) + + 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 + fun lexPalindromicPermutation16() { + // Branch: end = false, curr has freq > 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation17() { + // Branch: end = false, curr has freq <= 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 0 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation18() { + // Branch: end = false, finds next char with freq > 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 0 + freq[1] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + assertThat(freq[1], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation19() { + // Branch: end = false, no next char with freq > 1 + val target = "z" + val freq = IntArray(26) + freq[25] = 1 + val ans = CharArray(2) + + val result = Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation20() { + // Branch: end = false transitions to end = true + val target = "ab" + val freq = IntArray(26) + freq[0] = 2 + freq[1] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + + assertThat(freq[0], equalTo(2)) + assertThat(freq[1], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation21() { + // Verify result is always a palindrome + val result = Solution().lexPalindromicPermutation("aabbcc", "abc") + if (!result.isEmpty()) { + val reversed = StringBuilder(result).reverse().toString() + assertThat(result, equalTo(reversed)) + } + } + + @Test + fun lexPalindromicPermutation22() { + // Verify character frequencies are preserved + val input = "aabbcc" + val result = Solution().lexPalindromicPermutation(input, "abc") + + if (!result.isEmpty()) { + val inputFreq = IntArray(26) + val resultFreq = IntArray(26) + + for (c in input.toCharArray()) { + inputFreq[c.code - 'a'.code]++ + } + for (c in result.toCharArray()) { + resultFreq[c.code - 'a'.code]++ + } + + assertThat(resultFreq, equalTo(inputFreq)) + } + } + + @Test + fun lexPalindromicPermutation23() { + // Result length should match input length + val input = "aabbccdd" + val result = Solution().lexPalindromicPermutation(input, "abcd") + + if (!result.isEmpty()) { + assertThat(result.length, equalTo(input.length)) + } + } + + @Test + fun lexPalindromicPermutation24() { + // Result should be >= target in lexicographical order + val result = Solution().lexPalindromicPermutation("aabbcc", "abc") + + if (!result.isEmpty()) { + assertThat(result.compareTo("abc"), greaterThanOrEqualTo(0)) + } + } + + @Test + fun lexPalindromicPermutation25() { + // Complex scenario with multiple characters + val result = Solution().lexPalindromicPermutation("aabbccdd", "abcd") + + assertThat( + result, + anyOf( + equalTo(""), + allOf( + hasLength(8), + containsString("a"), + containsString("b"), + ), + ), + ) + } + + @Test + fun lexPalindromicPermutation26() { + // Edge case: empty string (if applicable) + val result = Solution().lexPalindromicPermutation("", "") + assertThat( + result, + anyOf(equalTo(""), not(nullValue())), + ) + } + + @Test + fun lexPalindromicPermutation27() { + // Verify frequency array is restored after recursion + val target = "aabb" + val freq = IntArray(26) + val freqCopy: IntArray = freq.clone() + + val ans = CharArray(4) + Solution().func(0, target, ans, 0, 3, freq, false) + + assertThat(freq, equalTo(freqCopy)) + } + + @Test + fun lexPalindromicPermutation28() { + // Verify char array is properly initialized + val result = Solution().lexPalindromicPermutation("aa", "a") + + if (!result.isEmpty()) { + assertThat(result, not(containsString("#"))) + } + } } From 4f0d620d23491020444355d65152451d5bd0d57a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 5 Nov 2025 18:59:04 +0200 Subject: [PATCH 5/5] Fixed compile --- .../s3731_find_missing_elements/SolutionTest.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt index 404be6d3..1053f307 100644 --- a/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt +++ b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt @@ -7,25 +7,25 @@ import org.junit.jupiter.api.Test internal class SolutionTest { @Test fun findMissingElements() { - assertThat>( + assertThat>( Solution().findMissingElements(intArrayOf(1, 4, 2, 5)), - equalTo>(mutableListOf(3)), + equalTo>(mutableListOf(3)), ) } @Test fun findMissingElements2() { - assertThat>( + assertThat>( Solution().findMissingElements(intArrayOf(7, 8, 6, 9)), - equalTo>(mutableListOf()), + equalTo>(mutableListOf()), ) } @Test fun findMissingElements3() { - assertThat>( + assertThat>( Solution().findMissingElements(intArrayOf(5, 1)), - equalTo>(mutableListOf(2, 3, 4)), + equalTo>(mutableListOf(2, 3, 4)), ) } }