From e8f9176ef7f764d5a64e868aca09e8d397981f2b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 2 Mar 2025 08:07:37 +0200 Subject: [PATCH 01/14] Added tasks 3467-3470 --- .../Solution.kt | 17 +++ .../s3467_transform_array_by_parity/readme.md | 38 ++++++ .../Solution.kt | 21 ++++ .../readme.md | 58 +++++++++ .../Solution.kt | 36 ++++++ .../readme.md | 45 +++++++ .../s3470_permutations_iv/Solution.kt | 114 ++++++++++++++++++ .../s3470_permutations_iv/readme.md | 63 ++++++++++ .../SolutionTest.kt | 23 ++++ .../SolutionTest.kt | 43 +++++++ .../SolutionTest.kt | 25 ++++ .../s3470_permutations_iv/SolutionTest.kt | 42 +++++++ 12 files changed, 525 insertions(+) create mode 100644 src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3470_permutations_iv/readme.md create mode 100644 src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt diff --git a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt new file mode 100644 index 000000000..b7e463d79 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt @@ -0,0 +1,17 @@ +package g3401_3500.s3467_transform_array_by_parity + +// #Easy #2025_03_02_Time_11_ms_(100.00%)_Space_42.68_MB_(100.00%) + +class Solution { + fun transformArray(nums: IntArray): IntArray { + for (i in nums.indices) { + if (nums[i] % 2 == 0) { + nums[i] = 0 + } else { + nums[i] = 1 + } + } + nums.sort() + return nums + } +} diff --git a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/readme.md b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/readme.md new file mode 100644 index 000000000..7583bc57d --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/readme.md @@ -0,0 +1,38 @@ +3467\. Transform Array by Parity + +Easy + +You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified: + +1. Replace each even number with 0. +2. Replace each odd numbers with 1. +3. Sort the modified array in **non-decreasing** order. + +Return the resulting array after performing these operations. + +**Example 1:** + +**Input:** nums = [4,3,2,1] + +**Output:** [0,0,1,1] + +**Explanation:** + +* Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`. +* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`. + +**Example 2:** + +**Input:** nums = [1,5,1,4,2] + +**Output:** [0,0,1,1,1] + +**Explanation:** + +* Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, `nums = [1, 1, 1, 0, 0]`. +* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt b/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt new file mode 100644 index 000000000..cd76baafb --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt @@ -0,0 +1,21 @@ +package g3401_3500.s3468_find_the_number_of_copy_arrays + +// #Medium #2025_03_02_Time_4_ms_(100.00%)_Space_112.45_MB_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun countArrays(original: IntArray, bounds: Array): Int { + var low = bounds[0][0] + var high = bounds[0][1] + var ans = high - low + 1 + for (i in 1..bounds[i] = [ui, vi]. + +You need to find the number of **possible** arrays `copy` of length `n` such that: + +1. `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`. +2. ui <= copy[i] <= vi for `0 <= i <= n - 1`. + +Return the number of such arrays. + +**Example 1:** + +**Input:** original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]] + +**Output:** 2 + +**Explanation:** + +The possible arrays are: + +* `[1, 2, 3, 4]` +* `[2, 3, 4, 5]` + +**Example 2:** + +**Input:** original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]] + +**Output:** 4 + +**Explanation:** + +The possible arrays are: + +* `[1, 2, 3, 4]` +* `[2, 3, 4, 5]` +* `[3, 4, 5, 6]` +* `[4, 5, 6, 7]` + +**Example 3:** + +**Input:** original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]] + +**Output:** 0 + +**Explanation:** + +No array is possible. + +**Constraints:** + +* 2 <= n == original.length <= 105 +* 1 <= original[i] <= 109 +* `bounds.length == n` +* `bounds[i].length == 2` +* 1 <= bounds[i][0] <= bounds[i][1] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt new file mode 100644 index 000000000..7bfe116db --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt @@ -0,0 +1,36 @@ +package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements + +// #Medium #2025_03_02_Time_320_ms_(100.00%)_Space_71.87_MB_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + private lateinit var dp: Array + + fun minCost(nums: IntArray): Int { + dp = Array(1001) { IntArray(1001) } + dp.forEach { row -> row.fill(-1) } + return solve(nums, 1, 0) + } + + private fun solve(nums: IntArray, i: Int, last: Int): Int { + if (i + 1 >= nums.size) { + return max(nums[last], if (i < nums.size) nums[i] else 0) + } + if (dp[i][last] != -1) { + return dp[i][last] + } + var res: Int = max(nums[i], nums[i + 1]) + solve(nums, i + 2, last) + res = min( + res, + max(nums[i], nums[last]) + solve(nums, i + 2, i + 1) + ) + res = min( + res, + max(nums[i + 1], nums[last]) + solve(nums, i + 2, i) + ) + dp[i][last] = res + return res + } +} diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md new file mode 100644 index 000000000..9f1be574e --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md @@ -0,0 +1,45 @@ +3469\. Find Minimum Cost to Remove Array Elements + +Medium + +You are given an integer array `nums`. Your task is to remove **all elements** from the array by performing one of the following operations at each step until `nums` is empty: + +* Choose any two elements from the first three elements of `nums` and remove them. The cost of this operation is the **maximum** of the two elements removed. +* If fewer than three elements remain in `nums`, remove all the remaining elements in a single operation. The cost of this operation is the **maximum** of the remaining elements. + +Return the **minimum** cost required to remove all the elements. + +**Example 1:** + +**Input:** nums = [6,2,8,4] + +**Output:** 12 + +**Explanation:** + +Initially, `nums = [6, 2, 8, 4]`. + +* In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`. +* In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`. + +The cost to remove all elements is `8 + 4 = 12`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 12. + +**Example 2:** + +**Input:** nums = [2,1,3,3] + +**Output:** 5 + +**Explanation:** + +Initially, `nums = [2, 1, 3, 3]`. + +* In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`. +* In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`. + +The cost to remove all elements is `2 + 3 = 5`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 5. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt new file mode 100644 index 000000000..2dcbf24d1 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -0,0 +1,114 @@ +package g3401_3500.s3470_permutations_iv + +// #Hard #2025_03_02_Time_63_ms_(100.00%)_Space_38.70_MB_(100.00%) + +class Solution { + private fun helper(a: Int, b: Int): Long { + var res: Long = 1 + for (i in 0.. INF) { + return INF + } + } + return res + } + + private fun solve(odd: Int, even: Int, r: Int, req: Int): Long { + if (r == 0) { + return 1 + } + val nOdd: Int + val nEven: Int + if (req == 1) { + nOdd = (r + 1) / 2 + nEven = r / 2 + } else { + nEven = (r + 1) / 2 + nOdd = r / 2 + } + if (odd < nOdd || even < nEven) { + return 0 + } + val oddWays = helper(odd, nOdd) + val evenWays = helper(even, nEven) + var total = oddWays + if (evenWays == 0L || total > INF / evenWays) { + total = INF + } else { + total *= evenWays + } + return total + } + + fun permute(n: Int, k: Long): IntArray { + var k = k + val ans: MutableList = ArrayList() + var first = false + val used = BooleanArray(n + 1) + var odd = (n + 1) / 2 + var even = n / 2 + var last = -1 + for (i in 1..n) { + if (!used[i]) { + var odd2 = odd + var even2 = even + val cp = i and 1 + val next = (if (cp == 1) 0 else 1) + if (cp == 1) { + odd2-- + } else { + even2-- + } + val r = n - 1 + val cnt = solve(odd2, even2, r, next) + if (k > cnt) { + k -= cnt + } else { + ans.add(i) + used[i] = true + odd = odd2 + even = even2 + last = cp + first = true + break + } + } + } + if (!first) { + return IntArray(0) + } + for (z in 1.. cnt2) { + k -= cnt2 + } else { + ans.add(j) + used[j] = true + odd = odd2 + even = even2 + last = cp + break + } + } + } + } + return ans.stream().mapToInt { i: Int? -> i!! }.toArray() + } + + companion object { + private const val INF = 1000000000000000000L + } +} diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/readme.md b/src/main/kotlin/g3401_3500/s3470_permutations_iv/readme.md new file mode 100644 index 000000000..6ab843e88 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/readme.md @@ -0,0 +1,63 @@ +3470\. Permutations IV + +Hard + +Given two integers, `n` and `k`, an **alternating permutation** is a permutation of the first `n` positive integers such that no **two** adjacent elements are both odd or both even. + +Return the **k-th** **alternating permutation** sorted in _lexicographical order_. If there are fewer than `k` valid **alternating permutations**, return an empty list. + +**Example 1:** + +**Input:** n = 4, k = 6 + +**Output:** [3,4,1,2] + +**Explanation:** + +The lexicographically-sorted alternating permutations of `[1, 2, 3, 4]` are: + +1. `[1, 2, 3, 4]` +2. `[1, 4, 3, 2]` +3. `[2, 1, 4, 3]` +4. `[2, 3, 4, 1]` +5. `[3, 2, 1, 4]` +6. `[3, 4, 1, 2]` ← 6th permutation +7. `[4, 1, 2, 3]` +8. `[4, 3, 2, 1]` + +Since `k = 6`, we return `[3, 4, 1, 2]`. + +**Example 2:** + +**Input:** n = 3, k = 2 + +**Output:** [3,2,1] + +**Explanation:** + +The lexicographically-sorted alternating permutations of `[1, 2, 3]` are: + +1. `[1, 2, 3]` +2. `[3, 2, 1]` ← 2nd permutation + +Since `k = 2`, we return `[3, 2, 1]`. + +**Example 3:** + +**Input:** n = 2, k = 3 + +**Output:** [] + +**Explanation:** + +The lexicographically-sorted alternating permutations of `[1, 2]` are: + +1. `[1, 2]` +2. `[2, 1]` + +There are only 2 alternating permutations, but `k = 3`, which is out of range. Thus, we return an empty list `[]`. + +**Constraints:** + +* `1 <= n <= 100` +* 1 <= k <= 1015 \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt new file mode 100644 index 000000000..cbda213ec --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3467_transform_array_by_parity + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun transformArray() { + assertThat( + Solution().transformArray(intArrayOf(4, 3, 2, 1)), + equalTo(intArrayOf(0, 0, 1, 1)) + ) + } + + @Test + fun transformArray2() { + assertThat( + Solution().transformArray(intArrayOf(1, 5, 1, 4, 2)), + equalTo(intArrayOf(0, 0, 1, 1, 1)) + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt new file mode 100644 index 000000000..50b7a16d3 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt @@ -0,0 +1,43 @@ +package g3401_3500.s3468_find_the_number_of_copy_arrays + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countArrays() { + assertThat( + Solution() + .countArrays( + intArrayOf(1, 2, 3, 4), + arrayOf(intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 4), intArrayOf(4, 5)) + ), + equalTo(2) + ) + } + + @Test + fun countArrays2() { + assertThat( + Solution() + .countArrays( + intArrayOf(1, 2, 3, 4), + arrayOf(intArrayOf(1, 10), intArrayOf(2, 9), intArrayOf(3, 8), intArrayOf(4, 7)) + ), + equalTo(4) + ) + } + + @Test + fun countArrays3() { + assertThat( + Solution() + .countArrays( + intArrayOf(1, 2, 1, 2), + arrayOf(intArrayOf(1, 1), intArrayOf(2, 3), intArrayOf(3, 3), intArrayOf(2, 3)) + ), + equalTo(0) + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt new file mode 100644 index 000000000..55a4159df --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minCost() { + assertThat(Solution().minCost(intArrayOf(6, 2, 8, 4)), equalTo(12)) + } + + @Test + fun minCost2() { + assertThat(Solution().minCost(intArrayOf(2, 1, 3, 3)), equalTo(5)) + } + + @Test + fun minCost3() { + assertThat( + Solution().minCost(intArrayOf(83, 47, 66, 24, 57, 85, 16)), + equalTo(224) + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt new file mode 100644 index 000000000..aba3ff67a --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt @@ -0,0 +1,42 @@ +package g3401_3500.s3470_permutations_iv + +import org.hamcrest.CoreMatchers +import org.hamcrest.MatcherAssert +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun permute() { + MatcherAssert.assertThat( + Solution().permute(4, 6L), + CoreMatchers.equalTo(intArrayOf(3, 4, 1, 2)) + ) + } + + @Test + fun permute2() { + MatcherAssert.assertThat( + Solution().permute(3, 2L), + CoreMatchers.equalTo(intArrayOf(3, 2, 1)) + ) + } + + @Test + fun permute3() { + MatcherAssert.assertThat(Solution().permute(2, 3L), CoreMatchers.equalTo(intArrayOf())) + } + + @Test + fun permute4() { + MatcherAssert.assertThat( + Solution().permute(43, 142570305460935L), + CoreMatchers.equalTo( + intArrayOf( + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 43, 40, 27, 36, 25, 34, 31, 32, 29, 28, 33, 24, 23, 26, 41, 42, + 35, 38, 37, 30, 39 + ) + ) + ) + } +} From c86a441ea833b91639b11c318e07005924031db7 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 2 Mar 2025 08:24:59 +0200 Subject: [PATCH 02/14] Fixed format --- .../Solution.kt | 4 ++-- .../s3467_transform_array_by_parity/SolutionTest.kt | 4 ++-- .../SolutionTest.kt | 12 ++++++------ .../SolutionTest.kt | 2 +- .../g3401_3500/s3470_permutations_iv/SolutionTest.kt | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt index 7bfe116db..f4a9a3575 100644 --- a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt @@ -24,11 +24,11 @@ class Solution { var res: Int = max(nums[i], nums[i + 1]) + solve(nums, i + 2, last) res = min( res, - max(nums[i], nums[last]) + solve(nums, i + 2, i + 1) + max(nums[i], nums[last]) + solve(nums, i + 2, i + 1), ) res = min( res, - max(nums[i + 1], nums[last]) + solve(nums, i + 2, i) + max(nums[i + 1], nums[last]) + solve(nums, i + 2, i), ) dp[i][last] = res return res diff --git a/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt index cbda213ec..357d64c1e 100644 --- a/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt +++ b/src/test/kotlin/g3401_3500/s3467_transform_array_by_parity/SolutionTest.kt @@ -9,7 +9,7 @@ internal class SolutionTest { fun transformArray() { assertThat( Solution().transformArray(intArrayOf(4, 3, 2, 1)), - equalTo(intArrayOf(0, 0, 1, 1)) + equalTo(intArrayOf(0, 0, 1, 1)), ) } @@ -17,7 +17,7 @@ internal class SolutionTest { fun transformArray2() { assertThat( Solution().transformArray(intArrayOf(1, 5, 1, 4, 2)), - equalTo(intArrayOf(0, 0, 1, 1, 1)) + equalTo(intArrayOf(0, 0, 1, 1, 1)), ) } } diff --git a/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt index 50b7a16d3..5498cee56 100644 --- a/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt +++ b/src/test/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.kt @@ -11,9 +11,9 @@ internal class SolutionTest { Solution() .countArrays( intArrayOf(1, 2, 3, 4), - arrayOf(intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 4), intArrayOf(4, 5)) + arrayOf(intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 4), intArrayOf(4, 5)), ), - equalTo(2) + equalTo(2), ) } @@ -23,9 +23,9 @@ internal class SolutionTest { Solution() .countArrays( intArrayOf(1, 2, 3, 4), - arrayOf(intArrayOf(1, 10), intArrayOf(2, 9), intArrayOf(3, 8), intArrayOf(4, 7)) + arrayOf(intArrayOf(1, 10), intArrayOf(2, 9), intArrayOf(3, 8), intArrayOf(4, 7)), ), - equalTo(4) + equalTo(4), ) } @@ -35,9 +35,9 @@ internal class SolutionTest { Solution() .countArrays( intArrayOf(1, 2, 1, 2), - arrayOf(intArrayOf(1, 1), intArrayOf(2, 3), intArrayOf(3, 3), intArrayOf(2, 3)) + arrayOf(intArrayOf(1, 1), intArrayOf(2, 3), intArrayOf(3, 3), intArrayOf(2, 3)), ), - equalTo(0) + equalTo(0), ) } } diff --git a/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt index 55a4159df..6fde0a60e 100644 --- a/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt +++ b/src/test/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.kt @@ -19,7 +19,7 @@ internal class SolutionTest { fun minCost3() { assertThat( Solution().minCost(intArrayOf(83, 47, 66, 24, 57, 85, 16)), - equalTo(224) + equalTo(224), ) } } diff --git a/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt index aba3ff67a..b115ee84a 100644 --- a/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt +++ b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt @@ -9,7 +9,7 @@ internal class SolutionTest { fun permute() { MatcherAssert.assertThat( Solution().permute(4, 6L), - CoreMatchers.equalTo(intArrayOf(3, 4, 1, 2)) + CoreMatchers.equalTo(intArrayOf(3, 4, 1, 2)), ) } @@ -17,7 +17,7 @@ internal class SolutionTest { fun permute2() { MatcherAssert.assertThat( Solution().permute(3, 2L), - CoreMatchers.equalTo(intArrayOf(3, 2, 1)) + CoreMatchers.equalTo(intArrayOf(3, 2, 1)), ) } @@ -34,9 +34,9 @@ internal class SolutionTest { intArrayOf( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 43, 40, 27, 36, 25, 34, 31, 32, 29, 28, 33, 24, 23, 26, 41, 42, - 35, 38, 37, 30, 39 - ) - ) + 35, 38, 37, 30, 39, + ), + ), ) } } From e5ced962229d7bcfd1c5ac2a91dee27db715d114 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 2 Mar 2025 08:26:24 +0200 Subject: [PATCH 03/14] Fixed format --- .../s3470_permutations_iv/SolutionTest.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt index b115ee84a..2116c0520 100644 --- a/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt +++ b/src/test/kotlin/g3401_3500/s3470_permutations_iv/SolutionTest.kt @@ -1,36 +1,36 @@ package g3401_3500.s3470_permutations_iv -import org.hamcrest.CoreMatchers -import org.hamcrest.MatcherAssert +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat import org.junit.jupiter.api.Test internal class SolutionTest { @Test fun permute() { - MatcherAssert.assertThat( + assertThat( Solution().permute(4, 6L), - CoreMatchers.equalTo(intArrayOf(3, 4, 1, 2)), + equalTo(intArrayOf(3, 4, 1, 2)), ) } @Test fun permute2() { - MatcherAssert.assertThat( + assertThat( Solution().permute(3, 2L), - CoreMatchers.equalTo(intArrayOf(3, 2, 1)), + equalTo(intArrayOf(3, 2, 1)), ) } @Test fun permute3() { - MatcherAssert.assertThat(Solution().permute(2, 3L), CoreMatchers.equalTo(intArrayOf())) + assertThat(Solution().permute(2, 3L), equalTo(intArrayOf())) } @Test fun permute4() { - MatcherAssert.assertThat( + assertThat( Solution().permute(43, 142570305460935L), - CoreMatchers.equalTo( + equalTo( intArrayOf( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 43, 40, 27, 36, 25, 34, 31, 32, 29, 28, 33, 24, 23, 26, 41, 42, From 07ecc0e47223a10e6102c27462fe065311c0325f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 4 Mar 2025 03:55:28 +0200 Subject: [PATCH 04/14] Added tasks 3471-3474 --- .../Solution.kt | 2 +- .../Solution.kt | 24 +++++++ .../readme.md | 59 +++++++++++++++++ .../Solution.kt | 42 ++++++++++++ .../readme.md | 42 ++++++++++++ .../Solution.kt | 44 +++++++++++++ .../readme.md | 39 +++++++++++ .../Solution.kt | 64 +++++++++++++++++++ .../readme.md | 56 ++++++++++++++++ .../SolutionTest.kt | 28 ++++++++ .../SolutionTest.kt | 23 +++++++ .../SolutionTest.kt | 23 +++++++ .../SolutionTest.kt | 38 +++++++++++ 13 files changed, 483 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/readme.md create mode 100644 src/test/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.kt diff --git a/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt index 2ac8e7d3a..6c9a3355e 100644 --- a/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3462_maximum_sum_with_at_most_k_elements -// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue) +// #Medium #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue // #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%) class Solution { diff --git a/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt b/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt new file mode 100644 index 000000000..21937c80f --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt @@ -0,0 +1,24 @@ +package g3401_3500.s3471_find_the_largest_almost_missing_integer + +// #Easy #2025_03_02_Time_10_ms_(100.00%)_Space_37.57_MB_(100.00%) + +class Solution { + fun largestInteger(nums: IntArray, k: Int): Int { + val freq = IntArray(51) + for (i in 0..nums.size - k) { + val set: MutableSet = HashSet() + for (j in i..(26) { IntArray(26) } + for (i in 0..25) { + for (j in 0..25) { + arr[i][j] = min(abs(i - j), (26 - abs(i - j))) + } + } + val dp = Array>(n) { Array(n) { IntArray(k + 1) } } + for (i in 0..= c) 2 + dp[i + 1][j - 1][it - c] else 0 + dp[i][j][it] = max(max(num1, num2), num3) + } + } + } + } + return dp[0][n - 1][k] + } +} diff --git a/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md b/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md new file mode 100644 index 000000000..c37fe73bb --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md @@ -0,0 +1,42 @@ +3472\. Longest Palindromic Subsequence After at Most K Operations + +Medium + +You are given a string `s` and an integer `k`. + +In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that `'a'` is after `'z'`). For example, replacing `'a'` with the next letter results in `'b'`, and replacing `'a'` with the previous letter results in `'z'`. Similarly, replacing `'z'` with the next letter results in `'a'`, and replacing `'z'` with the previous letter results in `'y'`. + +Return the length of the **longest palindromic subsequence** of `s` that can be obtained after performing **at most** `k` operations. + +**Example 1:** + +**Input:** s = "abced", k = 2 + +**Output:** 3 + +**Explanation:** + +* Replace `s[1]` with the next letter, and `s` becomes `"acced"`. +* Replace `s[4]` with the previous letter, and `s` becomes `"accec"`. + +The subsequence `"ccc"` forms a palindrome of length 3, which is the maximum. + +**Example 2:** + +**Input:** s = "aaazzz", k = 4 + +**Output:** 6 + +**Explanation:** + +* Replace `s[0]` with the previous letter, and `s` becomes `"zaazzz"`. +* Replace `s[4]` with the next letter, and `s` becomes `"zaazaz"`. +* Replace `s[3]` with the next letter, and `s` becomes `"zaaaaz"`. + +The entire string forms a palindrome of length 6. + +**Constraints:** + +* `1 <= s.length <= 200` +* `1 <= k <= 200` +* `s` consists of only lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt new file mode 100644 index 000000000..fb1820f9d --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt @@ -0,0 +1,44 @@ +package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m + +// #Medium #2025_03_02_Time_305_ms_(100.00%)_Space_76.92_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun maxSum(nums: IntArray, k: Int, m: Int): Int { + val n = nums.size + // Calculate prefix sums + val prefixSum = IntArray(n + 1) + for (i in 0..(n + 1) { IntArray(k + 1) } + // Initialize dp array + for (j in 1..k) { + for (i in 0..n) { + dp[i][j] = Int.Companion.MIN_VALUE / 2 + } + } + // Fill dp array + for (j in 1..k) { + val maxPrev = IntArray(n + 1) + for (i in 0..= m`). +* Subarray `nums[0..1]` with sum `1 + 2 = 3` (length is `2 >= m`). + +The total sum is `10 + 3 = 13`. + +**Example 2:** + +**Input:** nums = [-10,3,-1,-2], k = 4, m = 1 + +**Output:** \-10 + +**Explanation:** + +The optimal choice is choosing each element as a subarray. The output is `(-10) + 3 + (-1) + (-2) = -10`. + +**Constraints:** + +* `1 <= nums.length <= 2000` +* -104 <= nums[i] <= 104 +* `1 <= k <= floor(nums.length / m)` +* `1 <= m <= 3` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt b/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt new file mode 100644 index 000000000..346d8fd11 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt @@ -0,0 +1,64 @@ +package g3401_3500.s3474_lexicographically_smallest_generated_string + +// #Hard #2025_03_02_Time_41_ms_(100.00%)_Space_39.75_MB_(100.00%) + +class Solution { + fun generateString(str1: String, str2: String): String { + val n = str1.length + val m = str2.length + val l = n + m - 1 + val word = arrayOfNulls(l) + for (i in 0.., str2: String, i: Int, m: Int): Boolean { + for (j in 0..1 <= n == str1.length <= 104 +* `1 <= m == str2.length <= 500` +* `str1` consists only of `'T'` or `'F'`. +* `str2` consists only of lowercase English characters. \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.kt new file mode 100644 index 000000000..2313e62b7 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3401_3500.s3471_find_the_largest_almost_missing_integer + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun largestInteger() { + assertThat( + Solution().largestInteger(intArrayOf(3, 9, 2, 1, 7), 3), + equalTo(7), + ) + } + + @Test + fun largestInteger2() { + assertThat( + Solution().largestInteger(intArrayOf(3, 9, 7, 2, 1, 7), 4), + equalTo(3), + ) + } + + @Test + fun largestInteger3() { + assertThat(Solution().largestInteger(intArrayOf(0, 0), 1), equalTo(-1)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.kt new file mode 100644 index 000000000..dfcbdf427 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun longestPalindromicSubsequence() { + assertThat( + Solution().longestPalindromicSubsequence("abced", 2), + equalTo(3), + ) + } + + @Test + fun longestPalindromicSubsequence2() { + assertThat( + Solution().longestPalindromicSubsequence("aaazzz", 4), + equalTo(6), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.kt new file mode 100644 index 000000000..9e43e115e --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSum() { + assertThat( + Solution().maxSum(intArrayOf(1, 2, -1, 3, 3, 4), 2, 2), + equalTo(13), + ) + } + + @Test + fun maxSum2() { + assertThat( + Solution().maxSum(intArrayOf(-10, 3, -1, -2), 4, 1), + equalTo(-10), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.kt new file mode 100644 index 000000000..15b8ff5bf --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.kt @@ -0,0 +1,38 @@ +package g3401_3500.s3474_lexicographically_smallest_generated_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun generateString() { + assertThat( + Solution().generateString("TFTF", "ab"), + equalTo("ababa"), + ) + } + + @Test + fun generateString2() { + assertThat(Solution().generateString("TFTF", "abc"), equalTo("")) + } + + @Test + fun generateString3() { + assertThat(Solution().generateString("F", "d"), equalTo("a")) + } + + @Test + fun generateString4() { + assertThat(Solution().generateString("TTFFT", "fff"), equalTo("")) + } + + @Test + fun generateString5() { + assertThat( + Solution().generateString("FFTFFF", "a"), + equalTo("bbabbb"), + ) + } +} From 64ca61a633ba9e3eda10488ec814cd7a3981c661 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 4 Mar 2025 07:38:21 +0200 Subject: [PATCH 05/14] Added task 3475 --- .../s3475_dna_pattern_recognition/readme.md | 98 ++++++++++++ .../s3475_dna_pattern_recognition/script.sql | 13 ++ .../MysqlTest.kt | 150 ++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql create mode 100644 src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt diff --git a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/readme.md b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/readme.md new file mode 100644 index 000000000..34cf7c2a0 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/readme.md @@ -0,0 +1,98 @@ +3475\. DNA Pattern Recognition + +Medium + +Table: `Samples` + + +----------------+---------+ + | Column Name | Type | + +----------------+---------+ + | sample_id | int | + | dna_sequence | varchar | + | species | varchar | + +----------------+---------+ + sample_id is the unique key for this table. + Each row contains a DNA sequence represented as a string of characters (A, T, G, C) and the species it was collected from. + +Biologists are studying basic patterns in DNA sequences. Write a solution to identify `sample_id` with the following patterns: + +* Sequences that **start** with **ATG** (a common **start codon**) +* Sequences that **end** with either **TAA**, **TAG**, or **TGA** (**stop codons**) +* Sequences containing the motif **ATAT** (a simple repeated pattern) +* Sequences that have **at least** `3` **consecutive** **G** (like **GGG** or **GGGG**) + +Return _the result table ordered by __sample\_id in **ascending** order_. + +The result format is in the following example. + +**Example:** + +**Input:** + +Samples table: + + +-----------+------------------+-----------+ + | sample_id | dna_sequence | species | + +-----------+------------------+-----------+ + | 1 | ATGCTAGCTAGCTAA | Human | + | 2 | GGGTCAATCATC | Human | + | 3 | ATATATCGTAGCTA | Human | + | 4 | ATGGGGTCATCATAA | Mouse | + | 5 | TCAGTCAGTCAG | Mouse | + | 6 | ATATCGCGCTAG | Zebrafish | + | 7 | CGTATGCGTCGTA | Zebrafish | + +-----------+------------------+-----------+ + +**Output:** + + +-----------+------------------+-------------+-------------+------------+------------+------------+ + | sample_id | dna_sequence | species | has_start | has_stop | has_atat | has_ggg | + +-----------+------------------+-------------+-------------+------------+------------+------------+ + | 1 | ATGCTAGCTAGCTAA | Human | 1 | 1 | 0 | 0 | + | 2 | GGGTCAATCATC | Human | 0 | 0 | 0 | 1 | + | 3 | ATATATCGTAGCTA | Human | 0 | 0 | 1 | 0 | + | 4 | ATGGGGTCATCATAA | Mouse | 1 | 1 | 0 | 1 | + | 5 | TCAGTCAGTCAG | Mouse | 0 | 0 | 0 | 0 | + | 6 | ATATCGCGCTAG | Zebrafish | 0 | 1 | 1 | 0 | + | 7 | CGTATGCGTCGTA | Zebrafish | 0 | 0 | 0 | 0 | + +-----------+------------------+-------------+-------------+------------+------------+------------+ + +**Explanation:** + +* Sample 1 (ATGCTAGCTAGCTAA): + * Starts with ATG (has\_start = 1) + * Ends with TAA (has\_stop = 1) + * Does not contain ATAT (has\_atat = 0) + * Does not contain at least 3 consecutive 'G's (has\_ggg = 0) +* Sample 2 (GGGTCAATCATC): + * Does not start with ATG (has\_start = 0) + * Does not end with TAA, TAG, or TGA (has\_stop = 0) + * Does not contain ATAT (has\_atat = 0) + * Contains GGG (has\_ggg = 1) +* Sample 3 (ATATATCGTAGCTA): + * Does not start with ATG (has\_start = 0) + * Does not end with TAA, TAG, or TGA (has\_stop = 0) + * Contains ATAT (has\_atat = 1) + * Does not contain at least 3 consecutive 'G's (has\_ggg = 0) +* Sample 4 (ATGGGGTCATCATAA): + * Starts with ATG (has\_start = 1) + * Ends with TAA (has\_stop = 1) + * Does not contain ATAT (has\_atat = 0) + * Contains GGGG (has\_ggg = 1) +* Sample 5 (TCAGTCAGTCAG): + * Does not match any patterns (all fields = 0) +* Sample 6 (ATATCGCGCTAG): + * Does not start with ATG (has\_start = 0) + * Ends with TAG (has\_stop = 1) + * Starts with ATAT (has\_atat = 1) + * Does not contain at least 3 consecutive 'G's (has\_ggg = 0) +* Sample 7 (CGTATGCGTCGTA): + * Does not start with ATG (has\_start = 0) + * Does not end with TAA, "TAG", or "TGA" (has\_stop = 0) + * Does not contain ATAT (has\_atat = 0) + * Does not contain at least 3 consecutive 'G's (has\_ggg = 0) + +**Note:** + +* The result is ordered by sample\_id in ascending order +* For each pattern, 1 indicates the pattern is present and 0 indicates it is not present \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql new file mode 100644 index 000000000..525e9b798 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql @@ -0,0 +1,13 @@ +# Write your MySQL query statement below +# #Medium #2025_03_04_Time_645_ms_(100.00%)_Space_0.0_MB_(100.00%) +WITH SampleAnalysisCte AS ( + SELECT sample_id, dna_sequence, species, + dna_sequence REGEXP '^ATG' AS has_start, + dna_sequence REGEXP 'TAA$|TAG$|TGA$' AS has_stop, + dna_sequence REGEXP '.*ATAT.*' AS has_atat, + dna_sequence REGEXP '.*GGG.*' AS has_ggg + FROM Samples +) + +SELECT * FROM SampleAnalysisCte +ORDER BY sample_id; diff --git a/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt b/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt new file mode 100644 index 000000000..d91236d98 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt @@ -0,0 +1,150 @@ +package g3401_3500.s3475_dna_pattern_recognition + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +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 +import java.io.BufferedReader +import java.io.FileNotFoundException +import java.io.FileReader +import java.sql.SQLException +import java.util.stream.Collectors +import javax.sql.DataSource + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = [ + ( + " CREATE TABLE Samples (" + + " sample_id INT," + + " dna_sequence VARCHAR(100)," + + " species VARCHAR(100)" + + ");" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(1, 'ATGCTAGCTAGCTAA', 'Human');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(2, 'GGGTCAATCATC', 'Human');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(3, 'ATATATCGTAGCTA', 'Human');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(4, 'ATGGGGTCATCATAA', 'Human');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(5, 'TCAGTCAGTCAG', 'Human');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(6, 'ATATCGCGCTAG', 'Zebrafish');" + + "insert into Samples (sample_id, dna_sequence, species) values " + + "(7, 'CGTATGCGTCGTA', 'Zebrafish');" + ), + ], +) +internal class MysqlTest { + @Test + @Throws(SQLException::class, FileNotFoundException::class) + fun testScript(@EmbeddedDatabase dataSource: DataSource) { + dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery( + BufferedReader( + FileReader( + ( + "src/main/kotlin/g3401_3500/" + + "s3475_dna_pattern_recognition/" + + "script.sql" + ), + ), + ) + .lines() + .collect(Collectors.joining("\n")) + .replace("#.*?\\r?\\n".toRegex(), ""), + ).use { resultSet -> + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("1")) + assertThat( + resultSet.getNString(2), + equalTo("ATGCTAGCTAGCTAA"), + ) + assertThat(resultSet.getNString(3), equalTo("Human")) + assertThat(resultSet.getNString(4), equalTo("TRUE")) + assertThat(resultSet.getNString(5), equalTo("TRUE")) + assertThat(resultSet.getNString(6), equalTo("FALSE")) + assertThat(resultSet.getNString(7), equalTo("FALSE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("2")) + assertThat( + resultSet.getNString(2), + equalTo("GGGTCAATCATC"), + ) + assertThat(resultSet.getNString(3), equalTo("Human")) + assertThat(resultSet.getNString(4), equalTo("FALSE")) + assertThat(resultSet.getNString(5), equalTo("FALSE")) + assertThat(resultSet.getNString(6), equalTo("FALSE")) + assertThat(resultSet.getNString(7), equalTo("TRUE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("3")) + assertThat( + resultSet.getNString(2), + equalTo("ATATATCGTAGCTA"), + ) + assertThat(resultSet.getNString(3), equalTo("Human")) + assertThat(resultSet.getNString(4), equalTo("FALSE")) + assertThat(resultSet.getNString(5), equalTo("FALSE")) + assertThat(resultSet.getNString(6), equalTo("TRUE")) + assertThat(resultSet.getNString(7), equalTo("FALSE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("4")) + assertThat( + resultSet.getNString(2), + equalTo("ATGGGGTCATCATAA"), + ) + assertThat(resultSet.getNString(3), equalTo("Human")) + assertThat(resultSet.getNString(4), equalTo("TRUE")) + assertThat(resultSet.getNString(5), equalTo("TRUE")) + assertThat(resultSet.getNString(6), equalTo("FALSE")) + assertThat(resultSet.getNString(7), equalTo("TRUE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("5")) + assertThat( + resultSet.getNString(2), + equalTo("TCAGTCAGTCAG"), + ) + assertThat(resultSet.getNString(3), equalTo("Human")) + assertThat(resultSet.getNString(4), equalTo("FALSE")) + assertThat(resultSet.getNString(5), equalTo("FALSE")) + assertThat(resultSet.getNString(6), equalTo("FALSE")) + assertThat(resultSet.getNString(7), equalTo("FALSE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("6")) + assertThat( + resultSet.getNString(2), + equalTo("ATATCGCGCTAG"), + ) + assertThat( + resultSet.getNString(3), + equalTo("Zebrafish"), + ) + assertThat(resultSet.getNString(4), equalTo("FALSE")) + assertThat(resultSet.getNString(5), equalTo("TRUE")) + assertThat(resultSet.getNString(6), equalTo("TRUE")) + assertThat(resultSet.getNString(7), equalTo("FALSE")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("7")) + assertThat( + resultSet.getNString(2), + equalTo("CGTATGCGTCGTA"), + ) + assertThat( + resultSet.getNString(3), + equalTo("Zebrafish"), + ) + assertThat(resultSet.getNString(4), equalTo("FALSE")) + assertThat(resultSet.getNString(5), equalTo("FALSE")) + assertThat(resultSet.getNString(6), equalTo("FALSE")) + assertThat(resultSet.getNString(7), equalTo("FALSE")) + assertThat(resultSet.next(), equalTo(false)) + } + } + } + } +} From 745ad356304b732c839b8472b984a9c872dfc08c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 4 Mar 2025 07:55:34 +0200 Subject: [PATCH 06/14] Fixed sonar --- .../s3475_dna_pattern_recognition/script.sql | 5 +- .../MysqlTest.kt | 103 +++++------------- 2 files changed, 30 insertions(+), 78 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql index 525e9b798..75e98b5d1 100644 --- a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql +++ b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql @@ -9,5 +9,6 @@ WITH SampleAnalysisCte AS ( FROM Samples ) -SELECT * FROM SampleAnalysisCte -ORDER BY sample_id; +SELECT sample_id, dna_sequence, species, has_start, has_stop, has_atat, has_ggg +FROM SampleAnalysisCte +ORDER BY sample_id ASC; diff --git a/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt b/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt index d91236d98..cd5b2da45 100644 --- a/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt +++ b/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt @@ -9,6 +9,7 @@ import org.zapodot.junit.db.common.CompatibilityMode import java.io.BufferedReader import java.io.FileNotFoundException import java.io.FileReader +import java.sql.ResultSet import java.sql.SQLException import java.util.stream.Collectors import javax.sql.DataSource @@ -60,91 +61,41 @@ internal class MysqlTest { .replace("#.*?\\r?\\n".toRegex(), ""), ).use { resultSet -> assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("1")) - assertThat( - resultSet.getNString(2), - equalTo("ATGCTAGCTAGCTAA"), - ) - assertThat(resultSet.getNString(3), equalTo("Human")) - assertThat(resultSet.getNString(4), equalTo("TRUE")) - assertThat(resultSet.getNString(5), equalTo("TRUE")) - assertThat(resultSet.getNString(6), equalTo("FALSE")) - assertThat(resultSet.getNString(7), equalTo("FALSE")) + checkRow(resultSet, 1, "ATGCTAGCTAGCTAA", "Human", "TRUE", "TRUE", "FALSE", "FALSE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("2")) - assertThat( - resultSet.getNString(2), - equalTo("GGGTCAATCATC"), - ) - assertThat(resultSet.getNString(3), equalTo("Human")) - assertThat(resultSet.getNString(4), equalTo("FALSE")) - assertThat(resultSet.getNString(5), equalTo("FALSE")) - assertThat(resultSet.getNString(6), equalTo("FALSE")) - assertThat(resultSet.getNString(7), equalTo("TRUE")) + checkRow(resultSet, 2, "GGGTCAATCATC", "Human", "FALSE", "FALSE", "FALSE", "TRUE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("3")) - assertThat( - resultSet.getNString(2), - equalTo("ATATATCGTAGCTA"), - ) - assertThat(resultSet.getNString(3), equalTo("Human")) - assertThat(resultSet.getNString(4), equalTo("FALSE")) - assertThat(resultSet.getNString(5), equalTo("FALSE")) - assertThat(resultSet.getNString(6), equalTo("TRUE")) - assertThat(resultSet.getNString(7), equalTo("FALSE")) + checkRow(resultSet, 3, "ATATATCGTAGCTA", "Human", "FALSE", "FALSE", "TRUE", "FALSE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("4")) - assertThat( - resultSet.getNString(2), - equalTo("ATGGGGTCATCATAA"), - ) - assertThat(resultSet.getNString(3), equalTo("Human")) - assertThat(resultSet.getNString(4), equalTo("TRUE")) - assertThat(resultSet.getNString(5), equalTo("TRUE")) - assertThat(resultSet.getNString(6), equalTo("FALSE")) - assertThat(resultSet.getNString(7), equalTo("TRUE")) + checkRow(resultSet, 4, "ATGGGGTCATCATAA", "Human", "TRUE", "TRUE", "FALSE", "TRUE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("5")) - assertThat( - resultSet.getNString(2), - equalTo("TCAGTCAGTCAG"), - ) - assertThat(resultSet.getNString(3), equalTo("Human")) - assertThat(resultSet.getNString(4), equalTo("FALSE")) - assertThat(resultSet.getNString(5), equalTo("FALSE")) - assertThat(resultSet.getNString(6), equalTo("FALSE")) - assertThat(resultSet.getNString(7), equalTo("FALSE")) + checkRow(resultSet, 5, "TCAGTCAGTCAG", "Human", "FALSE", "FALSE", "FALSE", "FALSE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("6")) - assertThat( - resultSet.getNString(2), - equalTo("ATATCGCGCTAG"), - ) - assertThat( - resultSet.getNString(3), - equalTo("Zebrafish"), - ) - assertThat(resultSet.getNString(4), equalTo("FALSE")) - assertThat(resultSet.getNString(5), equalTo("TRUE")) - assertThat(resultSet.getNString(6), equalTo("TRUE")) - assertThat(resultSet.getNString(7), equalTo("FALSE")) + checkRow(resultSet, 6, "ATATCGCGCTAG", "Zebrafish", "FALSE", "TRUE", "TRUE", "FALSE") assertThat(resultSet.next(), equalTo(true)) - assertThat(resultSet.getNString(1), equalTo("7")) - assertThat( - resultSet.getNString(2), - equalTo("CGTATGCGTCGTA"), - ) - assertThat( - resultSet.getNString(3), - equalTo("Zebrafish"), - ) - assertThat(resultSet.getNString(4), equalTo("FALSE")) - assertThat(resultSet.getNString(5), equalTo("FALSE")) - assertThat(resultSet.getNString(6), equalTo("FALSE")) - assertThat(resultSet.getNString(7), equalTo("FALSE")) + checkRow(resultSet, 7, "CGTATGCGTCGTA", "Zebrafish", "FALSE", "FALSE", "FALSE", "FALSE") assertThat(resultSet.next(), equalTo(false)) } } } } + + private fun checkRow( + resultSet: ResultSet, + sampleId: Int, + dnaSequence: String, + species: String, + hasStart: String, + hasStop: String, + hasAtat: String, + hasGgg: String, + ) { + assertThat(resultSet.getInt(1), equalTo(sampleId)) + assertThat(resultSet.getNString(2), equalTo(dnaSequence)) + assertThat(resultSet.getNString(3), equalTo(species)) + assertThat(resultSet.getNString(4), equalTo(hasStart)) + assertThat(resultSet.getNString(5), equalTo(hasStop)) + assertThat(resultSet.getNString(6), equalTo(hasAtat)) + assertThat(resultSet.getNString(7), equalTo(hasGgg)) + } } From 9e84d052535069c253f9303564cfbdbd5fa94664 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 06:42:03 +0200 Subject: [PATCH 07/14] Improved task 3467 --- .../s3467_transform_array_by_parity/Solution.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt index b7e463d79..17fdfa0cb 100644 --- a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt @@ -1,17 +1,20 @@ package g3401_3500.s3467_transform_array_by_parity -// #Easy #2025_03_02_Time_11_ms_(100.00%)_Space_42.68_MB_(100.00%) +// #Easy #2025_03_06_Time_1_ms_(100.00%)_Space_38.10_MB_(100.00%) class Solution { fun transformArray(nums: IntArray): IntArray { + val size = nums.size + val ans = IntArray(size) + var countEven = 0 for (i in nums.indices) { - if (nums[i] % 2 == 0) { - nums[i] = 0 - } else { - nums[i] = 1 + if (nums[i] and 1 == 0) { + countEven++ } } - nums.sort() - return nums + for (i in countEven until size) { + ans[i] = 1 + } + return ans } } From 69f2cfe3f8d2ca85ebc2219126c883fe3c806edb Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 06:45:46 +0200 Subject: [PATCH 08/14] Improved task 3469 --- .../Solution.kt | 36 ------------------- 1 file changed, 36 deletions(-) delete mode 100644 src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt deleted file mode 100644 index f4a9a3575..000000000 --- a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt +++ /dev/null @@ -1,36 +0,0 @@ -package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements - -// #Medium #2025_03_02_Time_320_ms_(100.00%)_Space_71.87_MB_(100.00%) - -import kotlin.math.max -import kotlin.math.min - -class Solution { - private lateinit var dp: Array - - fun minCost(nums: IntArray): Int { - dp = Array(1001) { IntArray(1001) } - dp.forEach { row -> row.fill(-1) } - return solve(nums, 1, 0) - } - - private fun solve(nums: IntArray, i: Int, last: Int): Int { - if (i + 1 >= nums.size) { - return max(nums[last], if (i < nums.size) nums[i] else 0) - } - if (dp[i][last] != -1) { - return dp[i][last] - } - var res: Int = max(nums[i], nums[i + 1]) + solve(nums, i + 2, last) - res = min( - res, - max(nums[i], nums[last]) + solve(nums, i + 2, i + 1), - ) - res = min( - res, - max(nums[i + 1], nums[last]) + solve(nums, i + 2, i), - ) - dp[i][last] = res - return res - } -} From d0d1cc99859adb85877a04a39fc1f06a44b51d23 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 06:46:31 +0200 Subject: [PATCH 09/14] Improved task 3469 --- .../Solution.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt new file mode 100644 index 000000000..2c3a61bca --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt @@ -0,0 +1,41 @@ +package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements + +// #Medium #2025_03_06_Time_51_ms_(100.00%)_Space_46.26_MB_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minCost(nums: IntArray): Int { + var nums = nums + var n = nums.size + if (n % 2 == 0) { + nums = nums.copyOf(++n) + } + val dp = IntArray(n) + var j = 1 + while (j < n - 1) { + var cost1: Int = INF + var cost2: Int = INF + val max = max(nums[j], nums[j + 1]) + for (i in 0.. Date: Thu, 6 Mar 2025 12:04:32 +0200 Subject: [PATCH 10/14] Updated tags --- .../g3401_3500/s3467_transform_array_by_parity/Solution.kt | 2 +- .../s3468_find_the_number_of_copy_arrays/Solution.kt | 2 +- .../Solution.kt | 2 +- src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt | 3 ++- .../s3471_find_the_largest_almost_missing_integer/Solution.kt | 2 +- .../Solution.kt | 2 +- .../Solution.kt | 3 ++- .../Solution.kt | 2 +- 8 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt index 17fdfa0cb..22902fa10 100644 --- a/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3467_transform_array_by_parity -// #Easy #2025_03_06_Time_1_ms_(100.00%)_Space_38.10_MB_(100.00%) +// #Easy #Array #Sorting #Counting #2025_03_06_Time_1_ms_(100.00%)_Space_45.41_MB_(5.41%) class Solution { fun transformArray(nums: IntArray): IntArray { diff --git a/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt b/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt index cd76baafb..aa67822f6 100644 --- a/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3468_find_the_number_of_copy_arrays -// #Medium #2025_03_02_Time_4_ms_(100.00%)_Space_112.45_MB_(100.00%) +// #Medium #Array #Math #2025_03_06_Time_3_ms_(100.00%)_Space_111.85_MB_(22.73%) import kotlin.math.max import kotlin.math.min diff --git a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt index 2c3a61bca..58e539bbe 100644 --- a/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements -// #Medium #2025_03_06_Time_51_ms_(100.00%)_Space_46.26_MB_(100.00%) +// #Medium #Array #Dynamic_Programming #2025_03_06_Time_27_ms_(100.00%)_Space_49.13_MB_(93.33%) import kotlin.math.max import kotlin.math.min diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt index 2dcbf24d1..a911a7d9b 100644 --- a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -1,6 +1,7 @@ package g3401_3500.s3470_permutations_iv -// #Hard #2025_03_02_Time_63_ms_(100.00%)_Space_38.70_MB_(100.00%) +// #Hard #Array #Math #Enumeration #Combinatorics +// #2025_03_06_Time_21_ms_(25.81%)_Space_47.38_MB_(6.45%) class Solution { private fun helper(a: Int, b: Int): Long { diff --git a/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt b/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt index 21937c80f..76fb915a2 100644 --- a/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3471_find_the_largest_almost_missing_integer -// #Easy #2025_03_02_Time_10_ms_(100.00%)_Space_37.57_MB_(100.00%) +// #Easy #Array #Hash_Table #2025_03_06_Time_6_ms_(86.49%)_Space_46.27_MB_(5.41%) class Solution { fun largestInteger(nums: IntArray, k: Int): Int { diff --git a/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt b/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt index bc8b5bdae..cdd131ecd 100644 --- a/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations -// #Medium #2025_03_02_Time_137_ms_(100.00%)_Space_99.18_MB_(100.00%) +// #Medium #String #Dynamic_Programming #2025_03_06_Time_142_ms_(55.00%)_Space_116.30_MB_(10.00%) import kotlin.math.abs import kotlin.math.max diff --git a/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt index fb1820f9d..ddb5fb49d 100644 --- a/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt @@ -1,6 +1,7 @@ package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m -// #Medium #2025_03_02_Time_305_ms_(100.00%)_Space_76.92_MB_(100.00%) +// #Medium #Array #Dynamic_Programming #Prefix_Sum +// #2025_03_06_Time_227_ms_(24.47%)_Space_99.61_MB_(48.94%) import kotlin.math.max diff --git a/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt b/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt index 346d8fd11..38197d032 100644 --- a/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3474_lexicographically_smallest_generated_string -// #Hard #2025_03_02_Time_41_ms_(100.00%)_Space_39.75_MB_(100.00%) +// #Hard #String #Greedy #String_Matching #2025_03_06_Time_30_ms_(100.00%)_Space_46.67_MB_(20.00%) class Solution { fun generateString(str1: String, str2: String): String { From 13dad5eb512d2598a3c95a6dd46c368cd5ecc3bb Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 12:05:37 +0200 Subject: [PATCH 11/14] Updated tag --- .../kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql index 75e98b5d1..d5bb52a20 100644 --- a/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql +++ b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql @@ -1,5 +1,5 @@ # Write your MySQL query statement below -# #Medium #2025_03_04_Time_645_ms_(100.00%)_Space_0.0_MB_(100.00%) +# #Medium #Database #2025_03_06_Time_362_ms_(83.49%)_Space_0.0_MB_(100.00%) WITH SampleAnalysisCte AS ( SELECT sample_id, dna_sequence, species, dna_sequence REGEXP '^ATG' AS has_start, From 4912d122549eb6ac4713d1aa538472e9f88891cd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 12:33:03 +0200 Subject: [PATCH 12/14] Improved task 3470 --- .../s3470_permutations_iv/Solution.kt | 151 ++++++------------ 1 file changed, 53 insertions(+), 98 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt index a911a7d9b..40a565116 100644 --- a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -1,115 +1,70 @@ package g3401_3500.s3470_permutations_iv // #Hard #Array #Math #Enumeration #Combinatorics -// #2025_03_06_Time_21_ms_(25.81%)_Space_47.38_MB_(6.45%) +// #2025_03_06_Time_3_ms_(100.00%)_Space_46.15_MB_(6.45%) class Solution { - private fun helper(a: Int, b: Int): Long { - var res: Long = 1 - for (i in 0.. INF) { - return INF + fun permute(n: Int, k: Long): IntArray { + val perm = LongArray(n + 1) + perm[1] = 1L + for (i in 2..n) { + if (perm[i - 1] > 10_000_000_000_000_000) { + perm[i] = perm[i - 1] + } else { + perm[i] = ((i + 2) / 2) * perm[i - 1] } } - return res - } - - private fun solve(odd: Int, even: Int, r: Int, req: Int): Long { - if (r == 0) { - return 1 - } - val nOdd: Int - val nEven: Int - if (req == 1) { - nOdd = (r + 1) / 2 - nEven = r / 2 - } else { - nEven = (r + 1) / 2 - nOdd = r / 2 - } - if (odd < nOdd || even < nEven) { - return 0 + val used = BooleanArray(n + 1) + val result = IntArray(n) + var index = 0 + // Deal with the edge cases first + if (n < 3) { + if (k > n) return intArrayOf() + if (k == 2L) return intArrayOf(2, 1) + if (n == 1) return intArrayOf(1) + return intArrayOf(1, 2) } - val oddWays = helper(odd, nOdd) - val evenWays = helper(even, nEven) - var total = oddWays - if (evenWays == 0L || total > INF / evenWays) { - total = INF + val firstCycle = (((k.toLong() - 1L) / perm[n - 2]).toInt()) + 1 + var odd = 2 + if (n % 2 == 0) { + if (firstCycle > n) return intArrayOf() + result[index++] = firstCycle + used[firstCycle] = true + if (firstCycle % 2 == 0) { + odd = 1 + } } else { - total *= evenWays - } - return total - } - - fun permute(n: Int, k: Long): IntArray { - var k = k - val ans: MutableList = ArrayList() - var first = false - val used = BooleanArray(n + 1) - var odd = (n + 1) / 2 - var even = n / 2 - var last = -1 - for (i in 1..n) { - if (!used[i]) { - var odd2 = odd - var even2 = even - val cp = i and 1 - val next = (if (cp == 1) 0 else 1) - if (cp == 1) { - odd2-- - } else { - even2-- - } - val r = n - 1 - val cnt = solve(odd2, even2, r, next) - if (k > cnt) { - k -= cnt - } else { - ans.add(i) - used[i] = true - odd = odd2 - even = even2 - last = cp - first = true - break - } + val first = firstCycle * 2 - 1 + if (first > n) { + return intArrayOf() } + result[index++] = first + used[first] = true } - if (!first) { - return IntArray(0) - } - for (z in 1.. cnt2) { - k -= cnt2 - } else { - ans.add(j) - used[j] = true - odd = odd2 - even = even2 - last = cp - break + var rem = ((k - 1L) % perm[n - 2]) + 1L + fun findNum(start: Int, nth: Int): Int { + var toFind = nth + for (i in start..n step 2) { + if (used[i] == false) { + toFind-- + if (toFind == 0) { + return i } } } + return -1 } - return ans.stream().mapToInt { i: Int? -> i!! }.toArray() - } - - companion object { - private const val INF = 1000000000000000000L + for (i in n - 3 downTo 1) { + val nextNum = ((rem - 1) / perm[i]).toInt() + val nextDigit = findNum(odd, nextNum + 1) + result[index++] = nextDigit + used[nextDigit] = true + rem = ((rem - 1L) % perm[i]) + 1L + odd = 3 - odd + } + result[index++] = findNum(odd, 1) + odd = 3 - odd + result[index] = findNum(odd, 1) + return result } } From 79fbd9d15e61bf2ccc2e1a00e594d48a78224465 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 13:19:53 +0200 Subject: [PATCH 13/14] Improved task 3470 --- .../s3470_permutations_iv/Solution.kt | 113 +++++++++--------- 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt index 40a565116..fdd20d5e2 100644 --- a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -1,70 +1,73 @@ package g3401_3500.s3470_permutations_iv // #Hard #Array #Math #Enumeration #Combinatorics -// #2025_03_06_Time_3_ms_(100.00%)_Space_46.15_MB_(6.45%) +// #2025_03_06_Time_4_ms_(96.77%)_Space_45.40_MB_(9.68%) class Solution { + private val maxFac = 100_000_000L + fun permute(n: Int, k: Long): IntArray { - val perm = LongArray(n + 1) - perm[1] = 1L - for (i in 2..n) { - if (perm[i - 1] > 10_000_000_000_000_000) { - perm[i] = perm[i - 1] - } else { - perm[i] = ((i + 2) / 2) * perm[i - 1] - } + var res = IntArray(n) + var k = k - 1 + var i = 0 + val sqrtK = sqrt(k.toDouble()).toLong() + val fac = LongArray(n / 2 + 1) + fac[0] = 1 + for (i in 1..n / 2) { + fac[i] = fac[i - 1] * i + if (fac[i] >= maxFac) { fac[i] = maxFac } } - val used = BooleanArray(n + 1) - val result = IntArray(n) - var index = 0 - // Deal with the edge cases first - if (n < 3) { - if (k > n) return intArrayOf() - if (k == 2L) return intArrayOf(2, 1) - if (n == 1) return intArrayOf(1) - return intArrayOf(1, 2) - } - val firstCycle = (((k.toLong() - 1L) / perm[n - 2]).toInt()) + 1 - var odd = 2 - if (n % 2 == 0) { - if (firstCycle > n) return intArrayOf() - result[index++] = firstCycle - used[firstCycle] = true - if (firstCycle % 2 == 0) { - odd = 1 - } - } else { - val first = firstCycle * 2 - 1 - if (first > n) { - return intArrayOf() + var evenNum = n / 2 + var oddNum = n - evenNum + var evens = mutableListOf() + var odds = mutableListOf() + for (i in 1..n) { + if (i % 2 == 0) { + evens.add(i) + } else { + odds.add(i) } - result[index++] = first - used[first] = true } - var rem = ((k - 1L) % perm[n - 2]) + 1L - fun findNum(start: Int, nth: Int): Int { - var toFind = nth - for (i in start..n step 2) { - if (used[i] == false) { - toFind-- - if (toFind == 0) { - return i + for (i in 0.. n) return IntArray(0) + res[i] = leadIdx + 1 + if ((leadIdx + 1) % 2 == 0) { + evens.remove(leadIdx + 1) + } else { + odds.remove(leadIdx + 1) } + k = k % trailCombs + } else { + val trailCombs = fac[oddNum - 1] * fac[evenNum] + val leadIdx = (k / trailCombs).toInt() + if (leadIdx >= odds.size) return IntArray(0) + val num = odds.removeAt(leadIdx) + res[i] = num + k = k % trailCombs + } + } else { + if (res[i - 1] % 2 == 0) { + val trailCombs = fac[evenNum] * fac[oddNum - 1] + val leadIdx = (k / trailCombs).toInt() + val num = odds.removeAt(leadIdx) + res[i] = num + k = k % trailCombs + } else { + val trailCombs = fac[evenNum - 1] * fac[oddNum ] + val leadIdx = (k / trailCombs).toInt() + val num = evens.removeAt(leadIdx) + res[i] = num + k = k % trailCombs } } - return -1 - } - for (i in n - 3 downTo 1) { - val nextNum = ((rem - 1) / perm[i]).toInt() - val nextDigit = findNum(odd, nextNum + 1) - result[index++] = nextDigit - used[nextDigit] = true - rem = ((rem - 1L) % perm[i]) + 1L - odd = 3 - odd + if (res[i] % 2 == 0) { + evenNum-- + } else { oddNum-- } } - result[index++] = findNum(odd, 1) - odd = 3 - odd - result[index] = findNum(odd, 1) - return result + return res } } From 7a046431bebc87596eb4cd39ac92e06b8e6f3f4f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 6 Mar 2025 14:36:41 +0200 Subject: [PATCH 14/14] Fixed compilation --- .../g3401_3500/s3470_permutations_iv/Solution.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt index fdd20d5e2..80abd4507 100644 --- a/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -9,13 +9,13 @@ class Solution { fun permute(n: Int, k: Long): IntArray { var res = IntArray(n) var k = k - 1 - var i = 0 - val sqrtK = sqrt(k.toDouble()).toLong() val fac = LongArray(n / 2 + 1) fac[0] = 1 for (i in 1..n / 2) { fac[i] = fac[i - 1] * i - if (fac[i] >= maxFac) { fac[i] = maxFac } + if (fac[i] >= maxFac) { + fac[i] = maxFac + } } var evenNum = n / 2 var oddNum = n - evenNum @@ -66,7 +66,9 @@ class Solution { } if (res[i] % 2 == 0) { evenNum-- - } else { oddNum-- } + } else { + oddNum-- + } } return res }