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/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..22902fa10 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3467_transform_array_by_parity/Solution.kt @@ -0,0 +1,20 @@ +package g3401_3500.s3467_transform_array_by_parity + +// #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 { + val size = nums.size + val ans = IntArray(size) + var countEven = 0 + for (i in nums.indices) { + if (nums[i] and 1 == 0) { + countEven++ + } + } + for (i in countEven until size) { + ans[i] = 1 + } + return ans + } +} 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..aa67822f6 --- /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 #Array #Math #2025_03_06_Time_3_ms_(100.00%)_Space_111.85_MB_(22.73%) + +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..58e539bbe --- /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 #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 + +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..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..80abd4507 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt @@ -0,0 +1,75 @@ +package g3401_3500.s3470_permutations_iv + +// #Hard #Array #Math #Enumeration #Combinatorics +// #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 { + var res = IntArray(n) + var k = k - 1 + 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 + } + } + 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) + } + } + 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 + } + } + if (res[i] % 2 == 0) { + evenNum-- + } else { + oddNum-- + } + } + return res + } +} 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/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..76fb915a2 --- /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 #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 { + 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..ddb5fb49d --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt @@ -0,0 +1,45 @@ +package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m + +// #Medium #Array #Dynamic_Programming #Prefix_Sum +// #2025_03_06_Time_227_ms_(24.47%)_Space_99.61_MB_(48.94%) + +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..38197d032 --- /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 #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 { + 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/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..d5bb52a20 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition/script.sql @@ -0,0 +1,14 @@ +# Write your MySQL query statement below +# #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, + 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 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/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..357d64c1e --- /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..5498cee56 --- /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..6fde0a60e --- /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..2116c0520 --- /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.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun permute() { + assertThat( + Solution().permute(4, 6L), + equalTo(intArrayOf(3, 4, 1, 2)), + ) + } + + @Test + fun permute2() { + assertThat( + Solution().permute(3, 2L), + equalTo(intArrayOf(3, 2, 1)), + ) + } + + @Test + fun permute3() { + assertThat(Solution().permute(2, 3L), equalTo(intArrayOf())) + } + + @Test + fun permute4() { + assertThat( + Solution().permute(43, 142570305460935L), + 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, + ), + ), + ) + } +} 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"), + ) + } +} 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..cd5b2da45 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.kt @@ -0,0 +1,101 @@ +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.ResultSet +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)) + checkRow(resultSet, 1, "ATGCTAGCTAGCTAA", "Human", "TRUE", "TRUE", "FALSE", "FALSE") + assertThat(resultSet.next(), equalTo(true)) + checkRow(resultSet, 2, "GGGTCAATCATC", "Human", "FALSE", "FALSE", "FALSE", "TRUE") + assertThat(resultSet.next(), equalTo(true)) + checkRow(resultSet, 3, "ATATATCGTAGCTA", "Human", "FALSE", "FALSE", "TRUE", "FALSE") + assertThat(resultSet.next(), equalTo(true)) + checkRow(resultSet, 4, "ATGGGGTCATCATAA", "Human", "TRUE", "TRUE", "FALSE", "TRUE") + assertThat(resultSet.next(), equalTo(true)) + checkRow(resultSet, 5, "TCAGTCAGTCAG", "Human", "FALSE", "FALSE", "FALSE", "FALSE") + assertThat(resultSet.next(), equalTo(true)) + checkRow(resultSet, 6, "ATATCGCGCTAG", "Zebrafish", "FALSE", "TRUE", "TRUE", "FALSE") + assertThat(resultSet.next(), equalTo(true)) + 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)) + } +}