diff --git a/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.kt b/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.kt new file mode 100644 index 000000000..088c1ec0d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.kt @@ -0,0 +1,26 @@ +package g3301_3400.s3314_construct_the_minimum_bitwise_array_i + +// #Easy #Array #Bit_Manipulation #2024_10_15_Time_226_ms_(57.14%)_Space_38_MB_(25.71%) + +class Solution { + fun minBitwiseArray(nums: List): IntArray { + val l = nums.size + val r = IntArray(l) + for (i in 0 until l) { + r[i] = check(nums[i]) + } + return r + } + + private fun check(v: Int): Int { + if (v % 2 == 0) { + return -1 + } + for (j in 1 until v) { + if ((j or (j + 1)) == v) { + return j + } + } + return -1 + } +} diff --git a/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/readme.md b/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/readme.md new file mode 100644 index 000000000..8116fba19 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/readme.md @@ -0,0 +1,42 @@ +3314\. Construct the Minimum Bitwise Array I + +Easy + +You are given an array `nums` consisting of `n` prime integers. + +You need to construct an array `ans` of length `n`, such that, for each index `i`, the bitwise `OR` of `ans[i]` and `ans[i] + 1` is equal to `nums[i]`, i.e. `ans[i] OR (ans[i] + 1) == nums[i]`. + +Additionally, you must **minimize** each value of `ans[i]` in the resulting array. + +If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`. + +**Example 1:** + +**Input:** nums = [2,3,5,7] + +**Output:** [-1,1,4,3] + +**Explanation:** + +* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`. +* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`. +* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`. +* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`. + +**Example 2:** + +**Input:** nums = [11,13,31] + +**Output:** [9,12,15] + +**Explanation:** + +* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`. +* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`. +* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `2 <= nums[i] <= 1000` +* `nums[i]` is a prime number. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.kt new file mode 100644 index 000000000..f5043b59b --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.kt @@ -0,0 +1,25 @@ +package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii + +// #Medium #Array #Bit_Manipulation #2024_10_15_Time_231_ms_(77.27%)_Space_37.1_MB_(93.18%) + +class Solution { + fun minBitwiseArray(nums: List): IntArray { + val n = nums.size + val result = IntArray(n) + for (i in 0 until n) { + val num: Int = nums[i] + result[i] = -1 + var p = 0 + while (p < 31) { + if (((num shr p) and 1) == 0) { + break + } + p++ + } + if (p > 0) { + result[i] = ((num shr p) shl p) or ((1 shl (p - 1)) - 1) + } + } + return result + } +} diff --git a/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/readme.md b/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/readme.md new file mode 100644 index 000000000..1a69ca108 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/readme.md @@ -0,0 +1,42 @@ +3315\. Construct the Minimum Bitwise Array II + +Medium + +You are given an array `nums` consisting of `n` prime integers. + +You need to construct an array `ans` of length `n`, such that, for each index `i`, the bitwise `OR` of `ans[i]` and `ans[i] + 1` is equal to `nums[i]`, i.e. `ans[i] OR (ans[i] + 1) == nums[i]`. + +Additionally, you must **minimize** each value of `ans[i]` in the resulting array. + +If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`. + +**Example 1:** + +**Input:** nums = [2,3,5,7] + +**Output:** [-1,1,4,3] + +**Explanation:** + +* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`. +* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`. +* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`. +* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`. + +**Example 2:** + +**Input:** nums = [11,13,31] + +**Output:** [9,12,15] + +**Explanation:** + +* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`. +* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`. +* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 2 <= nums[i] <= 109 +* `nums[i]` is a prime number. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.kt b/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.kt new file mode 100644 index 000000000..cb28b16cb --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.kt @@ -0,0 +1,48 @@ +package g3301_3400.s3316_find_maximum_removals_from_source_string + +// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers +// #2024_10_15_Time_220_ms_(100.00%)_Space_37.5_MB_(45.45%) + +import kotlin.math.max + +class Solution { + fun maxRemovals(source: String, pattern: String, targetIndices: IntArray): Int { + val sChars = source.toCharArray() + val sn = sChars.size + val pChars = ("$pattern#").toCharArray() + val pn = pattern.length + var tn = targetIndices.size + val maxPat = IntArray(tn + 1) + var i = 0 + var di = 0 + var nextTI = targetIndices[0] + while (i < sn) { + val c = sChars[i] + if (i == nextTI) { + maxPat[di + 1] = maxPat[di] + var p = maxPat[di + 1] + for (j in di downTo 1) { + val q = maxPat[j - 1] + maxPat[j] = if (c != pChars[p]) q else max((p + 1), q) + p = q + } + if (c == pChars[p]) { + maxPat[0] = p + 1 + } + nextTI = if (++di < tn) targetIndices[di] else -1 + } else { + for (j in 0..di) { + val p = maxPat[j] + if (c == pChars[p]) { + maxPat[j] = p + 1 + } + } + } + i++ + } + while (maxPat[tn] < pn) { + tn-- + } + return tn + } +} diff --git a/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/readme.md b/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/readme.md new file mode 100644 index 000000000..1f36bf34f --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/readme.md @@ -0,0 +1,67 @@ +3316\. Find Maximum Removals From Source String + +Medium + +You are given a string `source` of size `n`, a string `pattern` that is a subsequence of `source`, and a **sorted** integer array `targetIndices` that contains **distinct** numbers in the range `[0, n - 1]`. + +We define an **operation** as removing a character at an index `idx` from `source` such that: + +* `idx` is an element of `targetIndices`. +* `pattern` remains a subsequence of `source` after removing the character. + +Performing an operation **does not** change the indices of the other characters in `source`. For example, if you remove `'c'` from `"acb"`, the character at index 2 would still be `'b'`. + +Return the **maximum** number of _operations_ that can be performed. + +**Example 1:** + +**Input:** source = "abbaa", pattern = "aba", targetIndices = [0,1,2] + +**Output:** 1 + +**Explanation:** + +We can't remove `source[0]` but we can do either of these two operations: + +* Remove `source[1]`, so that `source` becomes `"a_baa"`. +* Remove `source[2]`, so that `source` becomes `"ab_aa"`. + +**Example 2:** + +**Input:** source = "bcda", pattern = "d", targetIndices = [0,3] + +**Output:** 2 + +**Explanation:** + +We can remove `source[0]` and `source[3]` in two operations. + +**Example 3:** + +**Input:** source = "dda", pattern = "dda", targetIndices = [0,1,2] + +**Output:** 0 + +**Explanation:** + +We can't remove any character from `source`. + +**Example 4:** + +**Input:** source = "yeyeykyded", pattern = "yeyyd", targetIndices = [0,2,3,4] + +**Output:** 2 + +**Explanation:** + +We can remove `source[2]` and `source[3]` in two operations. + +**Constraints:** + +* 1 <= n == source.length <= 3 * 103 +* `1 <= pattern.length <= n` +* `1 <= targetIndices.length <= n` +* `targetIndices` is sorted in ascending order. +* The input is generated such that `targetIndices` contains distinct elements in the range `[0, n - 1]`. +* `source` and `pattern` consist only of lowercase English letters. +* The input is generated such that `pattern` appears as a subsequence in `source`. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.kt b/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.kt new file mode 100644 index 000000000..4c5d77997 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.kt @@ -0,0 +1,63 @@ +package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event + +// #Hard #Dynamic_Programming #Math #Combinatorics +// #2024_10_15_Time_166_ms_(100.00%)_Space_35_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun numberOfWays(n: Int, x: Int, y: Int): Int { + val fact = LongArray(x + 1) + fact[0] = 1 + for (i in 1..x) { + fact[i] = fact[i - 1] * i % MOD + } + val invFact = LongArray(x + 1) + invFact[x] = powMod(fact[x], MOD - 2L) + for (i in x - 1 downTo 0) { + invFact[i] = invFact[i + 1] * (i + 1) % MOD + } + val powY = LongArray(x + 1) + powY[0] = 1 + for (k in 1..x) { + powY[k] = powY[k - 1] * y % MOD + } + val localArray = LongArray(x + 1) + localArray[0] = 1 + for (i in 1..n) { + val kMax: Int = min(i, x) + for (k in kMax downTo 1) { + localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD + } + localArray[0] = 0 + } + var sum: Long = 0 + val kLimit: Int = min(n, x) + for (k in 1..kLimit) { + val localValue: Long = fact[x] * invFact[x - k] % MOD + var term: Long = localValue * localArray[k] % MOD + term = term * powY[k] % MOD + sum = (sum + term) % MOD + } + return sum.toInt() + } + + private fun powMod(a: Long, b: Long): Long { + var a = a + var b = b + var res: Long = 1 + a = a % MOD + while (b > 0) { + if ((b and 1L) == 1L) { + res = res * a % MOD + } + a = a * a % MOD + b = b shr 1 + } + return res + } + + companion object { + private const val MOD = 1000000007 + } +} diff --git a/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/readme.md b/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/readme.md new file mode 100644 index 000000000..55486d3d8 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/readme.md @@ -0,0 +1,50 @@ +3317\. Find the Number of Possible Ways for an Event + +Hard + +You are given three integers `n`, `x`, and `y`. + +An event is being held for `n` performers. When a performer arrives, they are **assigned** to one of the `x` stages. All performers assigned to the **same** stage will perform together as a band, though some stages _might_ remain **empty**. + +After all performances are completed, the jury will **award** each band a score in the range `[1, y]`. + +Return the **total** number of possible ways the event can take place. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Note** that two events are considered to have been held **differently** if **either** of the following conditions is satisfied: + +* **Any** performer is _assigned_ a different stage. +* **Any** band is _awarded_ a different score. + +**Example 1:** + +**Input:** n = 1, x = 2, y = 3 + +**Output:** 6 + +**Explanation:** + +* There are 2 ways to assign a stage to the performer. +* The jury can award a score of either 1, 2, or 3 to the only band. + +**Example 2:** + +**Input:** n = 5, x = 2, y = 1 + +**Output:** 32 + +**Explanation:** + +* Each performer will be assigned either stage 1 or stage 2. +* All bands will be awarded a score of 1. + +**Example 3:** + +**Input:** n = 3, x = 3, y = 4 + +**Output:** 684 + +**Constraints:** + +* `1 <= n, x, y <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt new file mode 100644 index 000000000..9f2a0aff4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt @@ -0,0 +1,52 @@ +package g3301_3400.s3318_find_x_sum_of_all_k_long_subarrays_i + +// #Easy #Array #Hash_Table #Heap_Priority_Queue #Sliding_Window +// #2024_10_15_Time_262_ms_(86.21%)_Space_37.7_MB_(100.00%) + +import java.util.Comparator +import java.util.HashMap +import java.util.PriorityQueue + +class Solution { + private class Pair(num: Int, freq: Int) { + var num: Int + var freq: Int + + init { + this.num = num + this.freq = freq + } + } + + fun findXSum(nums: IntArray, k: Int, x: Int): IntArray { + val n = nums.size + val ans = IntArray(n - k + 1) + for (i in 0 until n - k + 1) { + val map = HashMap() + val pq = + PriorityQueue( + Comparator { a: Pair, b: Pair -> + if (a.freq == b.freq) { + return@Comparator b.num - a.num + } + b.freq - a.freq + } + ) + for (j in i until i + k) { + map.put(nums[j], map.getOrDefault(nums[j], 0)!! + 1) + } + for (entry in map.entries) { + pq.add(Pair(entry.key!!, entry.value!!)) + } + var count = x + var sum = 0 + while (pq.isNotEmpty() && count > 0) { + val pair = pq.remove() + sum += pair.num * pair.freq + count-- + } + ans[i] = sum + } + return ans + } +} diff --git a/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/readme.md b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/readme.md new file mode 100644 index 000000000..ff683e461 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/readme.md @@ -0,0 +1,43 @@ +3318\. Find X-Sum of All K-Long Subarrays I + +Easy + +You are given an array `nums` of `n` integers and two integers `k` and `x`. + +The **x-sum** of an array is calculated by the following procedure: + +* Count the occurrences of all elements in the array. +* Keep only the occurrences of the top `x` most frequent elements. If two elements have the same number of occurrences, the element with the **bigger** value is considered more frequent. +* Calculate the sum of the resulting array. + +**Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array. + +Return an integer array `answer` of length `n - k + 1` where `answer[i]` is the **x-sum** of the subarray `nums[i..i + k - 1]`. + +**Example 1:** + +**Input:** nums = [1,1,2,2,3,4,2,3], k = 6, x = 2 + +**Output:** [6,10,12] + +**Explanation:** + +* For subarray `[1, 1, 2, 2, 3, 4]`, only elements 1 and 2 will be kept in the resulting array. Hence, `answer[0] = 1 + 1 + 2 + 2`. +* For subarray `[1, 2, 2, 3, 4, 2]`, only elements 2 and 4 will be kept in the resulting array. Hence, `answer[1] = 2 + 2 + 2 + 4`. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times. +* For subarray `[2, 2, 3, 4, 2, 3]`, only elements 2 and 3 are kept in the resulting array. Hence, `answer[2] = 2 + 2 + 2 + 3 + 3`. + +**Example 2:** + +**Input:** nums = [3,8,7,8,7,5], k = 2, x = 2 + +**Output:** [11,15,15,15,12] + +**Explanation:** + +Since `k == x`, `answer[i]` is equal to the sum of the subarray `nums[i..i + k - 1]`. + +**Constraints:** + +* `1 <= n == nums.length <= 50` +* `1 <= nums[i] <= 50` +* `1 <= x <= k <= nums.length` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt new file mode 100644 index 000000000..8e327a68b --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt @@ -0,0 +1,42 @@ +package g3301_3400.s3319_k_th_largest_perfect_subtree_size_in_binary_tree + +// #Medium #Sorting #Depth_First_Search #Tree #Binary_Tree +// #2024_10_15_Time_332_ms_(45.45%)_Space_39.6_MB_(100.00%) + +import com_github_leetcode.TreeNode +import java.util.PriorityQueue +import java.util.Queue + +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ +class Solution { + private val pq: Queue = PriorityQueue() + + fun kthLargestPerfectSubtree(root: TreeNode?, k: Int): Int { + dfs(root, k) + return (if (pq.isEmpty() || pq.size < k) -1 else pq.peek())!! + } + + private fun dfs(root: TreeNode?, k: Int): Int { + if (root == null) { + return 0 + } + val left = dfs(root.left, k) + val right = dfs(root.right, k) + if (left == right) { + pq.offer(1 + left + right) + } + if (pq.size > k) { + pq.poll() + } + return if (left == right) 1 + left + right else -1 + } +} diff --git a/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/readme.md b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/readme.md new file mode 100644 index 000000000..9a4008e37 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/readme.md @@ -0,0 +1,52 @@ +3319\. K-th Largest Perfect Subtree Size in Binary Tree + +Medium + +You are given the `root` of a **binary tree** and an integer `k`. + +Return an integer denoting the size of the kth **largest perfect binary** subtree, or `-1` if it doesn't exist. + +A **perfect binary tree** is a tree where all leaves are on the same level, and every parent has two children. + +**Example 1:** + +**Input:** root = [5,3,6,5,2,5,7,1,8,null,null,6,8], k = 2 + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/14/tmpresl95rp-1.png) + +The roots of the perfect binary subtrees are highlighted in black. Their sizes, in non-increasing order are `[3, 3, 1, 1, 1, 1, 1, 1]`. + The 2nd largest size is 3. + +**Example 2:** + +**Input:** root = [1,2,3,4,5,6,7], k = 1 + +**Output:** 7 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/14/tmp_s508x9e-1.png) + +The sizes of the perfect binary subtrees in non-increasing order are `[7, 3, 3, 1, 1, 1, 1]`. The size of the largest perfect binary subtree is 7. + +**Example 3:** + +**Input:** root = [1,2,3,null,4], k = 3 + +**Output:** \-1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/14/tmp74xnmpj4-1.png) + +The sizes of the perfect binary subtrees in non-increasing order are `[1, 1]`. There are fewer than 3 perfect binary subtrees. + +**Constraints:** + +* The number of nodes in the tree is in the range `[1, 2000]`. +* `1 <= Node.val <= 2000` +* `1 <= k <= 1024` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt new file mode 100644 index 000000000..771a418ad --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt @@ -0,0 +1,67 @@ +package g3301_3400.s3320_count_the_number_of_winning_sequences + +// #Hard #String #Dynamic_Programming #2024_10_15_Time_335_ms_(100.00%)_Space_78.8_MB_(66.67%) + +class Solution { + fun countWinningSequences(s: String): Int { + val n = s.length + val dp = Array?>(n) { Array(3) { IntArray(2 * n + 1) } } + if (s[0] == 'F') { + dp[0]!![0]!![n] = 1 + dp[0]!![1]!![1 + n] = 1 + dp[0]!![2]!![-1 + n] = 1 + } else if (s[0] == 'W') { + dp[0]!![0]!![-1 + n] = 1 + dp[0]!![1]!![n] = 1 + dp[0]!![2]!![1 + n] = 1 + } else if (s[0] == 'E') { + dp[0]!![0]!![1 + n] = 1 + dp[0]!![1]!![-1 + n] = 1 + dp[0]!![2]!![n] = 1 + } + for (i in 1 until n) { + if (s[i] == 'F') { + for (j in 0 until 2 * n + 1) { + dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j] + dp[i - 1]!![2]!![j]) % MOD + } + for (j in 1 until 2 * n + 1) { + dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j - 1] + dp[i - 1]!![2]!![j - 1]) % MOD + } + for (j in 0 until 2 * n) { + dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j + 1] + dp[i - 1]!![1]!![j + 1]) % MOD + } + } else if (s[i] == 'W') { + for (j in 0 until 2 * n + 1) { + dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j] + dp[i - 1]!![2]!![j]) % MOD + } + for (j in 1 until 2 * n + 1) { + dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j - 1] + dp[i - 1]!![1]!![j - 1]) % MOD + } + for (j in 0 until 2 * n) { + dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j + 1] + dp[i - 1]!![2]!![j + 1]) % MOD + } + } else if (s[i] == 'E') { + for (j in 0 until 2 * n) { + dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j] + dp[i - 1]!![1]!![j]) % MOD + } + for (j in 1 until 2 * n + 1) { + dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j - 1] + dp[i - 1]!![2]!![j - 1]) % MOD + } + for (j in 0 until 2 * n) { + dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j + 1] + dp[i - 1]!![2]!![j + 1]) % MOD + } + } + } + var count = 0 + for (j in n + 1 until 2 * n + 1) { + count = (count + dp[n - 1]!![0]!![j]) % MOD + count = (count + dp[n - 1]!![1]!![j]) % MOD + count = (count + dp[n - 1]!![2]!![j]) % MOD + } + return count % MOD + } + + companion object { + private const val MOD = 1e9.toInt() + 7 + } +} diff --git a/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/readme.md b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/readme.md new file mode 100644 index 000000000..083242ab6 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/readme.md @@ -0,0 +1,47 @@ +3320\. Count The Number of Winning Sequences + +Hard + +Alice and Bob are playing a fantasy battle game consisting of `n` rounds where they summon one of three magical creatures each round: a Fire Dragon, a Water Serpent, or an Earth Golem. In each round, players **simultaneously** summon their creature and are awarded points as follows: + +* If one player summons a Fire Dragon and the other summons an Earth Golem, the player who summoned the **Fire Dragon** is awarded a point. +* If one player summons a Water Serpent and the other summons a Fire Dragon, the player who summoned the **Water Serpent** is awarded a point. +* If one player summons an Earth Golem and the other summons a Water Serpent, the player who summoned the **Earth Golem** is awarded a point. +* If both players summon the same creature, no player is awarded a point. + +You are given a string `s` consisting of `n` characters `'F'`, `'W'`, and `'E'`, representing the sequence of creatures Alice will summon in each round: + +* If `s[i] == 'F'`, Alice summons a Fire Dragon. +* If `s[i] == 'W'`, Alice summons a Water Serpent. +* If `s[i] == 'E'`, Alice summons an Earth Golem. + +Bob’s sequence of moves is unknown, but it is guaranteed that Bob will never summon the same creature in two consecutive rounds. Bob _beats_ Alice if the total number of points awarded to Bob after `n` rounds is **strictly greater** than the points awarded to Alice. + +Return the number of distinct sequences Bob can use to beat Alice. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** s = "FFF" + +**Output:** 3 + +**Explanation:** + +Bob can beat Alice by making one of the following sequences of moves: `"WFW"`, `"FWF"`, or `"WEW"`. Note that other winning sequences like `"WWE"` or `"EWW"` are invalid since Bob cannot make the same move twice in a row. + +**Example 2:** + +**Input:** s = "FWEFW" + +**Output:** 18 + +**Explanation:** + +Bob can beat Alice by making one of the following sequences of moves: `"FWFWF"`, `"FWFWE"`, `"FWEFE"`, `"FWEWE"`, `"FEFWF"`, `"FEFWE"`, `"FEFEW"`, `"FEWFE"`, `"WFEFE"`, `"WFEWE"`, `"WEFWF"`, `"WEFWE"`, `"WEFEF"`, `"WEFEW"`, `"WEWFW"`, `"WEWFE"`, `"EWFWE"`, or `"EWEWE"`. + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s[i]` is one of `'F'`, `'W'`, or `'E'`. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt new file mode 100644 index 000000000..f3ed9a24e --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt @@ -0,0 +1,88 @@ +package g3301_3400.s3321_find_x_sum_of_all_k_long_subarrays_ii + +// #Hard #Array #Hash_Table #Heap_Priority_Queue #Sliding_Window +// #2024_10_15_Time_1660_ms_(100.00%)_Space_81.8_MB_(100.00%) + +import java.util.HashMap +import java.util.TreeSet + +class Solution { + private class RC(v: Int, c: Int) : Comparable { + var `val`: Int + var cnt: Int + + init { + `val` = v + cnt = c + } + + override fun compareTo(o: RC): Int { + if (cnt != o.cnt) { + return cnt - o.cnt + } + return `val` - o.`val` + } + } + + fun findXSum(nums: IntArray, k: Int, x: Int): LongArray { + val n = nums.size + val ans = LongArray(n - k + 1) + val cnt: MutableMap = HashMap() + val s1 = TreeSet() + val s2 = TreeSet() + var sum: Long = 0 + var xSum: Long = 0 + for (i in 0 until n) { + sum += nums[i].toLong() + var curCnt: Int = cnt.getOrDefault(nums[i], 0)!! + cnt.put(nums[i], curCnt + 1) + var tmp = RC(nums[i], curCnt) + if (s1.contains(tmp)) { + s1.remove(tmp) + s1.add(RC(nums[i], curCnt + 1)) + xSum += nums[i].toLong() + } else { + s2.remove(tmp) + s1.add(RC(nums[i], curCnt + 1)) + xSum += nums[i].toLong() * (curCnt + 1) + while (s1.size > x) { + val l = s1.first() + s1.remove(l) + xSum -= l.`val`.toLong() * l.cnt + s2.add(l) + } + } + if (i >= k - 1) { + ans[i - k + 1] = if (s1.size == x) xSum else sum + val v = nums[i - k + 1] + sum -= v.toLong() + curCnt = cnt[v]!! + if (curCnt > 1) { + cnt.put(v, curCnt - 1) + } else { + cnt.remove(v) + } + tmp = RC(v, curCnt) + if (s2.contains(tmp)) { + s2.remove(tmp) + if (curCnt > 1) { + s2.add(RC(v, curCnt - 1)) + } + } else { + s1.remove(tmp) + xSum -= v.toLong() * curCnt + if (curCnt > 1) { + s2.add(RC(v, curCnt - 1)) + } + while (s1.size < x && s2.isNotEmpty()) { + val r = s2.last() + s2.remove(r) + s1.add(r) + xSum += r.`val`.toLong() * r.cnt + } + } + } + } + return ans + } +} diff --git a/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/readme.md b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/readme.md new file mode 100644 index 000000000..99badc00b --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/readme.md @@ -0,0 +1,44 @@ +3321\. Find X-Sum of All K-Long Subarrays II + +Hard + +You are given an array `nums` of `n` integers and two integers `k` and `x`. + +The **x-sum** of an array is calculated by the following procedure: + +* Count the occurrences of all elements in the array. +* Keep only the occurrences of the top `x` most frequent elements. If two elements have the same number of occurrences, the element with the **bigger** value is considered more frequent. +* Calculate the sum of the resulting array. + +**Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array. + +Return an integer array `answer` of length `n - k + 1` where `answer[i]` is the **x-sum** of the subarray `nums[i..i + k - 1]`. + +**Example 1:** + +**Input:** nums = [1,1,2,2,3,4,2,3], k = 6, x = 2 + +**Output:** [6,10,12] + +**Explanation:** + +* For subarray `[1, 1, 2, 2, 3, 4]`, only elements 1 and 2 will be kept in the resulting array. Hence, `answer[0] = 1 + 1 + 2 + 2`. +* For subarray `[1, 2, 2, 3, 4, 2]`, only elements 2 and 4 will be kept in the resulting array. Hence, `answer[1] = 2 + 2 + 2 + 4`. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times. +* For subarray `[2, 2, 3, 4, 2, 3]`, only elements 2 and 3 are kept in the resulting array. Hence, `answer[2] = 2 + 2 + 2 + 3 + 3`. + +**Example 2:** + +**Input:** nums = [3,8,7,8,7,5], k = 2, x = 2 + +**Output:** [11,15,15,15,12] + +**Explanation:** + +Since `k == x`, `answer[i]` is equal to the sum of the subarray `nums[i..i + k - 1]`. + +**Constraints:** + +* `nums.length == n` +* 1 <= n <= 105 +* 1 <= nums[i] <= 109 +* `1 <= x <= k <= nums.length` \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.kt new file mode 100644 index 000000000..9dd7fb008 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3314_construct_the_minimum_bitwise_array_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minBitwiseArray() { + assertThat( + Solution().minBitwiseArray(mutableListOf(2, 3, 5, 7)), + equalTo(intArrayOf(-1, 1, 4, 3)) + ) + } + + @Test + fun minBitwiseArray2() { + assertThat( + Solution().minBitwiseArray(mutableListOf(11, 13, 31)), + equalTo(intArrayOf(9, 12, 15)) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.kt new file mode 100644 index 000000000..5e7708c11 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minBitwiseArray() { + assertThat( + Solution().minBitwiseArray(mutableListOf(2, 3, 5, 7)), + equalTo(intArrayOf(-1, 1, 4, 3)) + ) + } + + @Test + fun minBitwiseArray2() { + assertThat( + Solution().minBitwiseArray(mutableListOf(11, 13, 31)), + equalTo(intArrayOf(9, 12, 15)) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.kt new file mode 100644 index 000000000..9e4a79831 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3316_find_maximum_removals_from_source_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxRemovals() { + assertThat( + Solution().maxRemovals("abbaa", "aba", intArrayOf(0, 1, 2)), + equalTo(1) + ) + } + + @Test + fun maxRemovals2() { + assertThat( + Solution().maxRemovals("bcda", "d", intArrayOf(0, 3)), + equalTo(2) + ) + } + + @Test + fun maxRemovals3() { + assertThat( + Solution().maxRemovals("dda", "dda", intArrayOf(0, 1, 2)), + equalTo(0) + ) + } + + @Test + fun maxRemovals4() { + assertThat( + Solution().maxRemovals("yeyeykyded", "yeyyd", intArrayOf(0, 2, 3, 4)), + equalTo(2) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.kt new file mode 100644 index 000000000..a2ce9dfd0 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numberOfWays() { + assertThat(Solution().numberOfWays(1, 2, 3), equalTo(6)) + } + + @Test + fun numberOfWays2() { + assertThat(Solution().numberOfWays(5, 2, 1), equalTo(32)) + } + + @Test + fun numberOfWays3() { + assertThat(Solution().numberOfWays(3, 3, 4), equalTo(684)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.kt new file mode 100644 index 000000000..568b2a647 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3318_find_x_sum_of_all_k_long_subarrays_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findXSum() { + assertThat( + Solution().findXSum(intArrayOf(1, 1, 2, 2, 3, 4, 2, 3), 6, 2), + equalTo(intArrayOf(6, 10, 12)) + ) + } + + @Test + fun findXSum2() { + assertThat( + Solution().findXSum(intArrayOf(3, 8, 7, 8, 7, 5), 2, 2), + equalTo(intArrayOf(11, 15, 15, 15, 12)) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.kt new file mode 100644 index 000000000..326fbd292 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.kt @@ -0,0 +1,44 @@ +package g3301_3400.s3319_k_th_largest_perfect_subtree_size_in_binary_tree + +import com_github_leetcode.TreeNode +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun kthLargestPerfectSubtree() { + assertThat( + Solution() + .kthLargestPerfectSubtree( + TreeNode.create( + mutableListOf(5, 3, 6, 5, 2, 5, 7, 1, 8, null, null, 6, 8) + ), + 2 + ), + equalTo(3) + ) + } + + @Test + fun kthLargestPerfectSubtree2() { + assertThat( + Solution() + .kthLargestPerfectSubtree( + TreeNode.create(mutableListOf(1, 2, 3, 4, 5, 6, 7)), 1 + ), + equalTo(7) + ) + } + + @Test + fun kthLargestPerfectSubtree3() { + assertThat( + Solution() + .kthLargestPerfectSubtree( + TreeNode.create(mutableListOf(1, 2, 3, null, 4)), 3 + ), + equalTo(-1) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt new file mode 100644 index 000000000..ad2713ce5 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3301_3400.s3320_count_the_number_of_winning_sequences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countWinningSequences() { + assertThat(Solution().countWinningSequences("FFF"), equalTo(3)) + } + + @Test + fun countWinningSequences2() { + assertThat(Solution().countWinningSequences("FWEFW"), equalTo(18)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.kt new file mode 100644 index 000000000..3c24229c6 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3321_find_x_sum_of_all_k_long_subarrays_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findXSum() { + assertThat( + Solution().findXSum(intArrayOf(1, 1, 2, 2, 3, 4, 2, 3), 6, 2), + equalTo(longArrayOf(6L, 10L, 12L)) + ) + } + + @Test + fun findXSum2() { + assertThat( + Solution().findXSum(intArrayOf(3, 8, 7, 8, 7, 5), 2, 2), + equalTo(longArrayOf(11L, 15L, 15L, 15L, 12L)) + ) + } +}