diff --git a/src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.java b/src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.java index 84b16cf91..01b006f9a 100644 --- a/src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.java +++ b/src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.java @@ -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_62_ms_(99.82%)_Space_78.09_MB_(20.19%) import java.util.Arrays; @@ -8,8 +8,8 @@ public class Solution { public long maxSum(int[][] grid, int[] limits, int k) { int l = 0; - for (int i = 0; i < limits.length; i++) { - l += limits[i]; + for (int limit : limits) { + l += limit; } int[] dp = new int[l]; int a = 0; diff --git a/src/main/java/g3401_3500/s3467_transform_array_by_parity/Solution.java b/src/main/java/g3401_3500/s3467_transform_array_by_parity/Solution.java new file mode 100644 index 000000000..d063905e5 --- /dev/null +++ b/src/main/java/g3401_3500/s3467_transform_array_by_parity/Solution.java @@ -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.11_MB_(42.10%) + +public class Solution { + public int[] transformArray(int[] nums) { + int size = nums.length; + int[] ans = new int[size]; + int countEven = 0; + for (int num : nums) { + if ((num & 1) == 0) { + countEven++; + } + } + for (int i = countEven; i < size; i++) { + ans[i] = 1; + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3467_transform_array_by_parity/readme.md b/src/main/java/g3401_3500/s3467_transform_array_by_parity/readme.md new file mode 100644 index 000000000..7583bc57d --- /dev/null +++ b/src/main/java/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/java/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.java b/src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.java new file mode 100644 index 000000000..8c23e6aff --- /dev/null +++ b/src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays/Solution.java @@ -0,0 +1,18 @@ +package g3401_3500.s3468_find_the_number_of_copy_arrays; + +// #Medium #Array #Math #2025_03_02_Time_2_ms_(100.00%)_Space_97.78_MB_(100.00%) + +public class Solution { + public int countArrays(int[] original, int[][] bounds) { + int low = bounds[0][0]; + int high = bounds[0][1]; + int ans = high - low + 1; + for (int i = 1; i < original.length; ++i) { + int diff = original[i] - original[i - 1]; + low = Math.max(low + diff, bounds[i][0]); + high = Math.min(high + diff, bounds[i][1]); + ans = Math.min(ans, high - low + 1); + } + return Math.max(ans, 0); + } +} diff --git a/src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays/readme.md b/src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays/readme.md new file mode 100644 index 000000000..7ec22ab01 --- /dev/null +++ b/src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays/readme.md @@ -0,0 +1,58 @@ +3468\. Find the Number of Copy Arrays + +Medium + +You are given an array `original` of length `n` and a 2D array `bounds` of length `n x 2`, where 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/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.java b/src/main/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.java new file mode 100644 index 000000000..a9e78bd91 --- /dev/null +++ b/src/main/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/Solution.java @@ -0,0 +1,34 @@ +package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements; + +// #Medium #Array #Dynamic_Programming #2025_03_06_Time_12_ms_(100.00%)_Space_45.73_MB_(95.77%) + +import java.util.Arrays; + +public class Solution { + private static final int INF = (int) 1e9; + + public int minCost(int[] nums) { + int n = nums.length; + if (n % 2 == 0) { + nums = Arrays.copyOf(nums, ++n); + } + int[] dp = new int[n]; + for (int j = 1; j < n - 1; j += 2) { + int cost1 = INF; + int cost2 = INF; + int max = Math.max(nums[j], nums[j + 1]); + for (int i = 0; i < j; ++i) { + cost1 = Math.min(cost1, dp[i] + Math.max(nums[i], nums[j + 1])); + cost2 = Math.min(cost2, dp[i] + Math.max(nums[i], nums[j])); + dp[i] += max; + } + dp[j] = cost1; + dp[j + 1] = cost2; + } + int result = INF; + for (int i = 0; i < n; ++i) { + result = Math.min(result, dp[i] + nums[i]); + } + return result; + } +} diff --git a/src/main/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md b/src/main/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/readme.md new file mode 100644 index 000000000..9f1be574e --- /dev/null +++ b/src/main/java/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/java/g3401_3500/s3470_permutations_iv/Solution.java b/src/main/java/g3401_3500/s3470_permutations_iv/Solution.java new file mode 100644 index 000000000..fd278cf2d --- /dev/null +++ b/src/main/java/g3401_3500/s3470_permutations_iv/Solution.java @@ -0,0 +1,116 @@ +package g3401_3500.s3470_permutations_iv; + +// #Hard #Array #Math #Enumeration #Combinatorics +// #2025_03_06_Time_11_ms_(59.56%)_Space_45.24_MB_(58.67%) + +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings("java:S6541") +public class Solution { + private static final long INF = 1_000_000_000_000_000_000L; + + private long helper(int a, int b) { + long res = 1; + for (int i = 0; i < b; i++) { + res *= a - i; + if (res > INF) { + return INF; + } + } + return res; + } + + private long solve(int odd, int even, int r, int req) { + if (r == 0) { + return 1; + } + int nOdd; + int nEven; + 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; + } + long oddWays = helper(odd, nOdd); + long evenWays = helper(even, nEven); + long total = oddWays; + if (evenWays == 0 || total > INF / evenWays) { + total = INF; + } else { + total *= evenWays; + } + return total; + } + + public int[] permute(int n, long k) { + List ans = new ArrayList<>(); + boolean first = false; + boolean[] used = new boolean[n + 1]; + int odd = (n + 1) / 2; + int even = n / 2; + int last = -1; + for (int i = 1; i <= n; i++) { + if (!used[i]) { + int odd2 = odd; + int even2 = even; + int cp = i & 1; + int next = (cp == 1 ? 0 : 1); + if (cp == 1) { + odd2--; + } else { + even2--; + } + int r = n - 1; + long 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 new int[0]; + } + for (int z = 1; z < n; z++) { + for (int j = 1; j <= n; j++) { + if (!used[j] && ((j & 1) != last)) { + int odd2 = odd; + int even2 = even; + int cp = j & 1; + if (cp == 1) { + odd2--; + } else { + even2--; + } + int r = n - (z + 1); + int next = (cp == 1 ? 0 : 1); + long cnt2 = solve(odd2, even2, r, next); + if (k > cnt2) { + k -= cnt2; + } else { + ans.add(j); + used[j] = true; + odd = odd2; + even = even2; + last = cp; + break; + } + } + } + } + return ans.stream().mapToInt(i -> i).toArray(); + } +} diff --git a/src/main/java/g3401_3500/s3470_permutations_iv/readme.md b/src/main/java/g3401_3500/s3470_permutations_iv/readme.md new file mode 100644 index 000000000..6ab843e88 --- /dev/null +++ b/src/main/java/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/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.java b/src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.java new file mode 100644 index 000000000..0542c2f7c --- /dev/null +++ b/src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/Solution.java @@ -0,0 +1,27 @@ +package g3401_3500.s3471_find_the_largest_almost_missing_integer; + +// #Easy #Array #Hash_Table #2025_03_06_Time_4_ms_(73.95%)_Space_44.80_MB_(32.76%) + +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public int largestInteger(int[] nums, int k) { + int[] freq = new int[51]; + for (int i = 0; i <= nums.length - k; i++) { + Set set = new HashSet<>(); + for (int j = i; j < i + k; j++) { + set.add(nums[j]); + } + for (int key : set) { + freq[key]++; + } + } + for (int i = 50; i >= 0; i--) { + if (freq[i] == 1) { + return i; + } + } + return -1; + } +} diff --git a/src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/readme.md b/src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/readme.md new file mode 100644 index 000000000..eab24033c --- /dev/null +++ b/src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/readme.md @@ -0,0 +1,59 @@ +3471\. Find the Largest Almost Missing Integer + +Easy + +You are given an integer array `nums` and an integer `k`. + +An integer `x` is **almost missing** from `nums` if `x` appears in _exactly_ one subarray of size `k` within `nums`. + +Return the **largest** **almost missing** integer from `nums`. If no such integer exists, return `-1`. + +A **subarray** is a contiguous sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [3,9,2,1,7], k = 3 + +**Output:** 7 + +**Explanation:** + +* 1 appears in 2 subarrays of size 3: `[9, 2, 1]` and `[2, 1, 7]`. +* 2 appears in 3 subarrays of size 3: `[3, 9, 2]`, `[9, 2, 1]`, `[2, 1, 7]`. +* 3 appears in 1 subarray of size 3: `[3, 9, 2]`. +* 7 appears in 1 subarray of size 3: `[2, 1, 7]`. +* 9 appears in 2 subarrays of size 3: `[3, 9, 2]`, and `[9, 2, 1]`. + +We return 7 since it is the largest integer that appears in exactly one subarray of size `k`. + +**Example 2:** + +**Input:** nums = [3,9,7,2,1,7], k = 4 + +**Output:** 3 + +**Explanation:** + +* 1 appears in 2 subarrays of size 4: `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. +* 2 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. +* 3 appears in 1 subarray of size 4: `[3, 9, 7, 2]`. +* 7 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. +* 9 appears in 2 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`. + +We return 3 since it is the largest and only integer that appears in exactly one subarray of size `k`. + +**Example 3:** + +**Input:** nums = [0,0], k = 1 + +**Output:** \-1 + +**Explanation:** + +There is no integer that appears in only one subarray of size 1. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `0 <= nums[i] <= 50` +* `1 <= k <= nums.length` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.java b/src/main/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.java new file mode 100644 index 000000000..7076f4f39 --- /dev/null +++ b/src/main/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.java @@ -0,0 +1,38 @@ +package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations; + +// #Medium #String #Dynamic_Programming #2025_03_06_Time_153_ms_(85.01%)_Space_87.68_MB_(54.23%) + +public class Solution { + public int longestPalindromicSubsequence(String s, int k) { + int n = s.length(); + int[][] arr = new int[26][26]; + for (int i = 0; i < 26; i++) { + for (int j = 0; j < 26; j++) { + arr[i][j] = Math.min(Math.abs(i - j), 26 - Math.abs(i - j)); + } + } + int[][][] dp = new int[n][n][k + 1]; + for (int i = 0; i < n; i++) { + for (int it = 0; it <= k; it++) { + dp[i][i][it] = 1; + } + } + for (int length = 2; length <= n; length++) { + for (int i = 0; i <= n - length; i++) { + int j = i + length - 1; + for (int it = 0; it <= k; it++) { + if (s.charAt(i) == s.charAt(j)) { + dp[i][j][it] = 2 + dp[i + 1][j - 1][it]; + } else { + int num1 = dp[i + 1][j][it]; + int num2 = dp[i][j - 1][it]; + int c = arr[s.charAt(i) - 'a'][s.charAt(j) - 'a']; + int num3 = (it >= c) ? 2 + dp[i + 1][j - 1][it - c] : 0; + dp[i][j][it] = Math.max(Math.max(num1, num2), num3); + } + } + } + } + return dp[0][n - 1][k]; + } +} diff --git a/src/main/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md b/src/main/java/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/java/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/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.java b/src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.java new file mode 100644 index 000000000..8f71f9891 --- /dev/null +++ b/src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.java @@ -0,0 +1,41 @@ +package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m; + +// #Medium #Array #Dynamic_Programming #Prefix_Sum +// #2025_03_06_Time_191_ms_(52.16%)_Space_83.66_MB_(63.85%) + +public class Solution { + public int maxSum(int[] nums, int k, int m) { + int n = nums.length; + // Calculate prefix sums + int[] prefixSum = new int[n + 1]; + for (int i = 0; i < n; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + // using elements from nums[0...i-1] + int[][] dp = new int[n + 1][k + 1]; + // Initialize dp array + for (int j = 1; j <= k; j++) { + for (int i = 0; i <= n; i++) { + dp[i][j] = Integer.MIN_VALUE / 2; + } + } + // Fill dp array + for (int j = 1; j <= k; j++) { + int[] maxPrev = new int[n + 1]; + for (int i = 0; i < n + 1; i++) { + maxPrev[i] = + i == 0 + ? dp[0][j - 1] - prefixSum[0] + : Math.max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i]); + } + for (int i = m; i <= n; i++) { + // Option 1: Don't include the current element in any new subarray + dp[i][j] = dp[i - 1][j]; + // Option 2: Form a new subarray ending at position i + // Find the best starting position for the subarray + dp[i][j] = Math.max(dp[i][j], prefixSum[i] + maxPrev[i - m]); + } + } + return dp[n][k]; + } +} diff --git a/src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/readme.md b/src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/readme.md new file mode 100644 index 000000000..1ea11fbbf --- /dev/null +++ b/src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/readme.md @@ -0,0 +1,39 @@ +3473\. Sum of K Subarrays With Length at Least M + +Medium + +You are given an integer array `nums` and two integers, `k` and `m`. + +Return the **maximum** sum of `k` non-overlapping subarrays of `nums`, where each subarray has a length of **at least** `m`. + +**Example 1:** + +**Input:** nums = [1,2,-1,3,3,4], k = 2, m = 2 + +**Output:** 13 + +**Explanation:** + +The optimal choice is: + +* Subarray `nums[3..5]` with sum `3 + 3 + 4 = 10` (length is `3 >= 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/java/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.java b/src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.java new file mode 100644 index 000000000..fc5463164 --- /dev/null +++ b/src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string/Solution.java @@ -0,0 +1,64 @@ +package g3401_3500.s3474_lexicographically_smallest_generated_string; + +// #Hard #String #Greedy #String_Matching #2025_03_06_Time_17_ms_(64.86%)_Space_45.66_MB_(14.59%) + +public class Solution { + public String generateString(String str1, String str2) { + int n = str1.length(); + int m = str2.length(); + int l = n + m - 1; + Character[] word = new Character[l]; + for (int i = 0; i < n; i++) { + if (str1.charAt(i) == 'T') { + for (int j = 0; j < m; j++) { + int pos = i + j; + if (word[pos] != null && word[pos] != str2.charAt(j)) { + return ""; + } + word[pos] = str2.charAt(j); + } + } + } + boolean[] free = new boolean[l]; + for (int i = 0; i < l; i++) { + if (word[i] == null) { + word[i] = 'a'; + free[i] = true; + } + } + if (n == 0) { + return String.join("", java.util.Collections.nCopies(l, "a")); + } + for (int i = 0; i < n; i++) { + if (str1.charAt(i) == 'F' && intervalEquals(word, str2, i, m)) { + boolean fixed = false; + for (int j = m - 1; j >= 0; j--) { + int pos = i + j; + if (free[pos]) { + word[pos] = 'b'; + free[pos] = false; + fixed = true; + break; + } + } + if (!fixed) { + return ""; + } + } + } + StringBuilder sb = new StringBuilder(); + for (Character c : word) { + sb.append(c); + } + return sb.toString(); + } + + private boolean intervalEquals(Character[] word, String str2, int i, int m) { + for (int j = 0; j < m; j++) { + if (word[i + j] == null || word[i + j] != str2.charAt(j)) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string/readme.md b/src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string/readme.md new file mode 100644 index 000000000..6eae25336 --- /dev/null +++ b/src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string/readme.md @@ -0,0 +1,56 @@ +3474\. Lexicographically Smallest Generated String + +Hard + +You are given two strings, `str1` and `str2`, of lengths `n` and `m`, respectively. + +A string `word` of length `n + m - 1` is defined to be **generated** by `str1` and `str2` if it satisfies the following conditions for **each** index `0 <= i <= n - 1`: + +* If `str1[i] == 'T'`, the **substring** of `word` with size `m` starting at index `i` is **equal** to `str2`, i.e., `word[i..(i + m - 1)] == str2`. +* If `str1[i] == 'F'`, the **substring** of `word` with size `m` starting at index `i` is **not equal** to `str2`, i.e., `word[i..(i + m - 1)] != str2`. + +Return the **lexicographically smallest** possible string that can be **generated** by `str1` and `str2`. If no string can be generated, return an empty string `""`. + +**Example 1:** + +**Input:** str1 = "TFTF", str2 = "ab" + +**Output:** "ababa" + +**Explanation:** + +#### The table below represents the string `"ababa"` + +| Index | T/F | Substring of length `m` | +|-------|-----|-------------------------| +| 0 | 'T' | "ab" | +| 1 | 'F' | "ba" | +| 2 | 'T' | "ab" | +| 3 | 'F' | "ba" | + +The strings `"ababa"` and `"ababb"` can be generated by `str1` and `str2`. + +Return `"ababa"` since it is the lexicographically smaller string. + +**Example 2:** + +**Input:** str1 = "TFTF", str2 = "abc" + +**Output:** "" + +**Explanation:** + +No string that satisfies the conditions can be generated. + +**Example 3:** + +**Input:** str1 = "F", str2 = "d" + +**Output:** "a" + +**Constraints:** + +* 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/java/g3401_3500/s3475_dna_pattern_recognition/readme.md b/src/main/java/g3401_3500/s3475_dna_pattern_recognition/readme.md new file mode 100644 index 000000000..34cf7c2a0 --- /dev/null +++ b/src/main/java/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/java/g3401_3500/s3475_dna_pattern_recognition/script.sql b/src/main/java/g3401_3500/s3475_dna_pattern_recognition/script.sql new file mode 100644 index 000000000..d5bb52a20 --- /dev/null +++ b/src/main/java/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/java/g3401_3500/s3467_transform_array_by_parity/SolutionTest.java b/src/test/java/g3401_3500/s3467_transform_array_by_parity/SolutionTest.java new file mode 100644 index 000000000..9259c2ac5 --- /dev/null +++ b/src/test/java/g3401_3500/s3467_transform_array_by_parity/SolutionTest.java @@ -0,0 +1,22 @@ +package g3401_3500.s3467_transform_array_by_parity; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void transformArray() { + assertThat( + new Solution().transformArray(new int[] {4, 3, 2, 1}), + equalTo(new int[] {0, 0, 1, 1})); + } + + @Test + void transformArray2() { + assertThat( + new Solution().transformArray(new int[] {1, 5, 1, 4, 2}), + equalTo(new int[] {0, 0, 1, 1, 1})); + } +} diff --git a/src/test/java/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.java b/src/test/java/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.java new file mode 100644 index 000000000..f4dc4204c --- /dev/null +++ b/src/test/java/g3401_3500/s3468_find_the_number_of_copy_arrays/SolutionTest.java @@ -0,0 +1,38 @@ +package g3401_3500.s3468_find_the_number_of_copy_arrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countArrays() { + assertThat( + new Solution() + .countArrays( + new int[] {1, 2, 3, 4}, + new int[][] {{1, 2}, {2, 3}, {3, 4}, {4, 5}}), + equalTo(2)); + } + + @Test + void countArrays2() { + assertThat( + new Solution() + .countArrays( + new int[] {1, 2, 3, 4}, + new int[][] {{1, 10}, {2, 9}, {3, 8}, {4, 7}}), + equalTo(4)); + } + + @Test + void countArrays3() { + assertThat( + new Solution() + .countArrays( + new int[] {1, 2, 1, 2}, + new int[][] {{1, 1}, {2, 3}, {3, 3}, {2, 3}}), + equalTo(0)); + } +} diff --git a/src/test/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.java b/src/test/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.java new file mode 100644 index 000000000..8ab4ba542 --- /dev/null +++ b/src/test/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements/SolutionTest.java @@ -0,0 +1,23 @@ +package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minCost() { + assertThat(new Solution().minCost(new int[] {6, 2, 8, 4}), equalTo(12)); + } + + @Test + void minCost2() { + assertThat(new Solution().minCost(new int[] {2, 1, 3, 3}), equalTo(5)); + } + + @Test + void minCost3() { + assertThat(new Solution().minCost(new int[] {83, 47, 66, 24, 57, 85, 16}), equalTo(224)); + } +} diff --git a/src/test/java/g3401_3500/s3470_permutations_iv/SolutionTest.java b/src/test/java/g3401_3500/s3470_permutations_iv/SolutionTest.java new file mode 100644 index 000000000..ac855179d --- /dev/null +++ b/src/test/java/g3401_3500/s3470_permutations_iv/SolutionTest.java @@ -0,0 +1,35 @@ +package g3401_3500.s3470_permutations_iv; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void permute() { + assertThat(new Solution().permute(4, 6L), equalTo(new int[] {3, 4, 1, 2})); + } + + @Test + void permute2() { + assertThat(new Solution().permute(3, 2L), equalTo(new int[] {3, 2, 1})); + } + + @Test + void permute3() { + assertThat(new Solution().permute(2, 3L), equalTo(new int[] {})); + } + + @Test + void permute4() { + assertThat( + new Solution().permute(43, 142570305460935L), + equalTo( + new int[] { + 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/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.java b/src/test/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.java new file mode 100644 index 000000000..d9725ec4b --- /dev/null +++ b/src/test/java/g3401_3500/s3471_find_the_largest_almost_missing_integer/SolutionTest.java @@ -0,0 +1,23 @@ +package g3401_3500.s3471_find_the_largest_almost_missing_integer; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void largestInteger() { + assertThat(new Solution().largestInteger(new int[] {3, 9, 2, 1, 7}, 3), equalTo(7)); + } + + @Test + void largestInteger2() { + assertThat(new Solution().largestInteger(new int[] {3, 9, 7, 2, 1, 7}, 4), equalTo(3)); + } + + @Test + void largestInteger3() { + assertThat(new Solution().largestInteger(new int[] {0, 0}, 1), equalTo(-1)); + } +} diff --git a/src/test/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.java b/src/test/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.java new file mode 100644 index 000000000..9787b994e --- /dev/null +++ b/src/test/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void longestPalindromicSubsequence() { + assertThat(new Solution().longestPalindromicSubsequence("abced", 2), equalTo(3)); + } + + @Test + void longestPalindromicSubsequence2() { + assertThat(new Solution().longestPalindromicSubsequence("aaazzz", 4), equalTo(6)); + } +} diff --git a/src/test/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.java b/src/test/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.java new file mode 100644 index 000000000..f4963fcd3 --- /dev/null +++ b/src/test/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxSum() { + assertThat(new Solution().maxSum(new int[] {1, 2, -1, 3, 3, 4}, 2, 2), equalTo(13)); + } + + @Test + void maxSum2() { + assertThat(new Solution().maxSum(new int[] {-10, 3, -1, -2}, 4, 1), equalTo(-10)); + } +} diff --git a/src/test/java/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.java b/src/test/java/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.java new file mode 100644 index 000000000..47cc058b4 --- /dev/null +++ b/src/test/java/g3401_3500/s3474_lexicographically_smallest_generated_string/SolutionTest.java @@ -0,0 +1,33 @@ +package g3401_3500.s3474_lexicographically_smallest_generated_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void generateString() { + assertThat(new Solution().generateString("TFTF", "ab"), equalTo("ababa")); + } + + @Test + void generateString2() { + assertThat(new Solution().generateString("TFTF", "abc"), equalTo("")); + } + + @Test + void generateString3() { + assertThat(new Solution().generateString("F", "d"), equalTo("a")); + } + + @Test + void generateString4() { + assertThat(new Solution().generateString("TTFFT", "fff"), equalTo("")); + } + + @Test + void generateString5() { + assertThat(new Solution().generateString("FFTFFF", "a"), equalTo("bbabbb")); + } +} diff --git a/src/test/java/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.java b/src/test/java/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.java new file mode 100644 index 000000000..44038c2ac --- /dev/null +++ b/src/test/java/g3401_3500/s3475_dna_pattern_recognition/MysqlTest.java @@ -0,0 +1,113 @@ +package g3401_3500.s3475_dna_pattern_recognition; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.stream.Collectors; +import javax.sql.DataSource; +import org.junit.jupiter.api.Test; +import org.zapodot.junit.db.annotations.EmbeddedDatabase; +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; +import org.zapodot.junit.db.common.CompatibilityMode; + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = + " CREATE TABLE 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');") +class MysqlTest { + @Test + void testScript(@EmbeddedDatabase DataSource dataSource) + throws SQLException, FileNotFoundException { + try (final Connection connection = dataSource.getConnection()) { + try (final Statement statement = connection.createStatement(); + final ResultSet resultSet = + statement.executeQuery( + new BufferedReader( + new FileReader( + "src/main/java/g3401_3500/" + + "s3475_dna_pattern_recognition/" + + "script.sql")) + .lines() + .collect(Collectors.joining("\n")) + .replaceAll("#.*?\\r?\\n", ""))) { + 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 void checkRow( + ResultSet resultSet, + int sampleId, + String dnaSequence, + String species, + String hasStart, + String hasStop, + String hasAtat, + String hasGgg) + throws SQLException { + 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)); + } +}