diff --git a/src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.java b/src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.java new file mode 100644 index 000000000..aec777e6e --- /dev/null +++ b/src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/Solution.java @@ -0,0 +1,28 @@ +package g3301_3400.s3314_construct_the_minimum_bitwise_array_i; + +// #Easy #Array #Bit_Manipulation #2024_10_15_Time_3_ms_(92.32%)_Space_44.5_MB_(92.59%) + +import java.util.List; + +public class Solution { + public int[] minBitwiseArray(List nums) { + int l = nums.size(); + int[] r = new int[l]; + for (int i = 0; i < l; i++) { + r[i] = check(nums.get(i)); + } + return r; + } + + private int check(int v) { + if (v % 2 == 0) { + return -1; + } + for (int j = 1; j < v; j++) { + if ((j | (j + 1)) == v) { + return j; + } + } + return -1; + } +} diff --git a/src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/readme.md b/src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/readme.md new file mode 100644 index 000000000..8116fba19 --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.java b/src/main/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.java new file mode 100644 index 000000000..c8bf7561f --- /dev/null +++ b/src/main/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/Solution.java @@ -0,0 +1,26 @@ +package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii; + +// #Medium #Array #Bit_Manipulation #2024_10_15_Time_1_ms_(100.00%)_Space_44.8_MB_(77.74%) + +import java.util.List; + +public class Solution { + public int[] minBitwiseArray(List nums) { + final int n = nums.size(); + int[] result = new int[n]; + for (int i = 0; i < n; i++) { + int num = nums.get(i); + result[i] = -1; + int p = 0; + for (; p < 31; p++) { + if (((num >> p) & 1) == 0) { + break; + } + } + if (p > 0) { + result[i] = ((num >> p) << p) | ((1 << (p - 1)) - 1); + } + } + return result; + } +} diff --git a/src/main/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/readme.md b/src/main/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/readme.md new file mode 100644 index 000000000..1a69ca108 --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.java b/src/main/java/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.java new file mode 100644 index 000000000..9dd4db32d --- /dev/null +++ b/src/main/java/g3301_3400/s3316_find_maximum_removals_from_source_string/Solution.java @@ -0,0 +1,45 @@ +package g3301_3400.s3316_find_maximum_removals_from_source_string; + +// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers +// #2024_10_15_Time_10_ms_(100.00%)_Space_44.6_MB_(41.97%) + +public class Solution { + public int maxRemovals(String source, String pattern, int[] targetIndices) { + char[] sChars = source.toCharArray(); + int sn = sChars.length; + char[] pChars = (pattern + '#').toCharArray(); + int pn = pattern.length(); + int tn = targetIndices.length; + int[] maxPat = new int[tn + 1]; + int i = 0; + int di = 0; + int nextTI = targetIndices[0]; + while (i < sn) { + char c = sChars[i]; + if (i == nextTI) { + int p = maxPat[di + 1] = maxPat[di]; + for (int j = di; j > 0; j--) { + int q = maxPat[j - 1]; + maxPat[j] = c != pChars[p] ? q : Math.max(p + 1, q); + p = q; + } + if (c == pChars[p]) { + maxPat[0] = p + 1; + } + nextTI = ++di < tn ? targetIndices[di] : -1; + } else { + for (int j = 0; j <= di; j++) { + int p = maxPat[j]; + if (c == pChars[p]) { + maxPat[j] = p + 1; + } + } + } + i++; + } + while (maxPat[tn] < pn) { + tn--; + } + return tn; + } +} diff --git a/src/main/java/g3301_3400/s3316_find_maximum_removals_from_source_string/readme.md b/src/main/java/g3301_3400/s3316_find_maximum_removals_from_source_string/readme.md new file mode 100644 index 000000000..1f36bf34f --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.java b/src/main/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.java new file mode 100644 index 000000000..4a7d0125c --- /dev/null +++ b/src/main/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/Solution.java @@ -0,0 +1,57 @@ +package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event; + +// #Hard #Dynamic_Programming #Math #Combinatorics +// #2024_10_15_Time_20_ms_(97.08%)_Space_41.6_MB_(97.66%) + +public class Solution { + private static final int MOD = 1_000_000_007; + + public int numberOfWays(int n, int x, int y) { + long[] fact = new long[x + 1]; + fact[0] = 1; + for (int i = 1; i <= x; i++) { + fact[i] = fact[i - 1] * i % MOD; + } + long[] invFact = new long[x + 1]; + invFact[x] = powMod(fact[x], MOD - 2L); + for (int i = x - 1; i >= 0; i--) { + invFact[i] = invFact[i + 1] * (i + 1) % MOD; + } + long[] powY = new long[x + 1]; + powY[0] = 1; + for (int k = 1; k <= x; k++) { + powY[k] = powY[k - 1] * y % MOD; + } + long[] localArray = new long[x + 1]; + localArray[0] = 1; + for (int i = 1; i <= n; i++) { + int kMax = Math.min(i, x); + for (int k = kMax; k >= 1; k--) { + localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD; + } + localArray[0] = 0; + } + long sum = 0; + int kLimit = Math.min(n, x); + for (int k = 1; k <= kLimit; k++) { + long localValue = fact[x] * invFact[x - k] % MOD; + long term = localValue * localArray[k] % MOD; + term = term * powY[k] % MOD; + sum = (sum + term) % MOD; + } + return (int) sum; + } + + private long powMod(long a, long b) { + long res = 1; + a = a % MOD; + while (b > 0) { + if ((b & 1) == 1) { + res = res * a % MOD; + } + a = a * a % MOD; + b >>= 1; + } + return res; + } +} diff --git a/src/main/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.java b/src/main/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.java new file mode 100644 index 000000000..178817555 --- /dev/null +++ b/src/main/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.java @@ -0,0 +1,51 @@ +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_11_ms_(77.35%)_Space_45.4_MB_(54.28%) + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class Solution { + private static class Pair { + int num; + int freq; + + Pair(int num, int freq) { + this.num = num; + this.freq = freq; + } + } + + public int[] findXSum(int[] nums, int k, int x) { + int n = nums.length; + int[] ans = new int[n - k + 1]; + for (int i = 0; i < n - k + 1; i++) { + HashMap map = new HashMap<>(); + PriorityQueue pq = + new PriorityQueue<>( + (a, b) -> { + if (a.freq == b.freq) { + return b.num - a.num; + } + return b.freq - a.freq; + }); + for (int j = i; j < i + k; j++) { + map.put(nums[j], map.getOrDefault(nums[j], 0) + 1); + } + for (Map.Entry entry : map.entrySet()) { + pq.add(new Pair(entry.getKey(), entry.getValue())); + } + int count = x; + int sum = 0; + while (!pq.isEmpty() && count > 0) { + Pair pair = pq.remove(); + sum += pair.num * pair.freq; + count--; + } + ans[i] = sum; + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.java b/src/main/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.java new file mode 100644 index 000000000..06d91a55b --- /dev/null +++ b/src/main/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.java @@ -0,0 +1,47 @@ +package g3301_3400.s3319_k_th_largest_perfect_subtree_size_in_binary_tree; + +// #Medium #Sorting #Tree #Binary_Tree #Depth_First_Search +// #2024_10_15_Time_10_ms_(87.48%)_Space_45.3_MB_(50.46%) + +import com_github_leetcode.TreeNode; +import java.util.PriorityQueue; +import java.util.Queue; + +/* + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + private final Queue pq = new PriorityQueue<>(); + + public int kthLargestPerfectSubtree(TreeNode root, int k) { + dfs(root, k); + return pq.isEmpty() || pq.size() < k ? -1 : pq.peek(); + } + + private int dfs(TreeNode root, int k) { + if (root == null) { + return 0; + } + int left = dfs(root.left, k); + int right = dfs(root.right, k); + if (left == right) { + pq.offer(1 + left + right); + } + if (pq.size() > k) { + pq.poll(); + } + return left == right ? 1 + left + right : -1; + } +} diff --git a/src/main/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.java b/src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.java new file mode 100644 index 000000000..ed72db12d --- /dev/null +++ b/src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.java @@ -0,0 +1,65 @@ +package g3301_3400.s3320_count_the_number_of_winning_sequences; + +// #Hard #String #Dynamic_Programming #2024_10_15_Time_43_ms_(99.76%)_Space_74.6_MB_(88.56%) + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + + public int countWinningSequences(String s) { + int n = s.length(); + int[][][] dp = new int[n][3][2 * n + 1]; + if (s.charAt(0) == 'F') { + dp[0][0][n] = 1; + dp[0][1][1 + n] = 1; + dp[0][2][-1 + n] = 1; + } else if (s.charAt(0) == 'W') { + dp[0][0][-1 + n] = 1; + dp[0][1][n] = 1; + dp[0][2][1 + n] = 1; + } else if (s.charAt(0) == 'E') { + dp[0][0][1 + n] = 1; + dp[0][1][-1 + n] = 1; + dp[0][2][n] = 1; + } + for (int i = 1; i < n; i++) { + if (s.charAt(i) == 'F') { + for (int j = 0; j < 2 * n + 1; j++) { + dp[i][0][j] = (dp[i - 1][1][j] + dp[i - 1][2][j]) % MOD; + } + for (int j = 1; j < 2 * n + 1; j++) { + dp[i][1][j] = (dp[i - 1][0][j - 1] + dp[i - 1][2][j - 1]) % MOD; + } + for (int j = 0; j < 2 * n; j++) { + dp[i][2][j] = (dp[i - 1][0][j + 1] + dp[i - 1][1][j + 1]) % MOD; + } + } else if (s.charAt(i) == 'W') { + for (int j = 0; j < 2 * n + 1; j++) { + dp[i][1][j] = (dp[i - 1][0][j] + dp[i - 1][2][j]) % MOD; + } + for (int j = 1; j < 2 * n + 1; j++) { + dp[i][2][j] = (dp[i - 1][0][j - 1] + dp[i - 1][1][j - 1]) % MOD; + } + for (int j = 0; j < 2 * n; j++) { + dp[i][0][j] = (dp[i - 1][1][j + 1] + dp[i - 1][2][j + 1]) % MOD; + } + } else if (s.charAt(i) == 'E') { + for (int j = 0; j < 2 * n; j++) { + dp[i][2][j] = (dp[i - 1][0][j] + dp[i - 1][1][j]) % MOD; + } + for (int j = 1; j < 2 * n + 1; j++) { + dp[i][0][j] = (dp[i - 1][1][j - 1] + dp[i - 1][2][j - 1]) % MOD; + } + for (int j = 0; j < 2 * n; j++) { + dp[i][1][j] = (dp[i - 1][0][j + 1] + dp[i - 1][2][j + 1]) % MOD; + } + } + } + int count = 0; + for (int j = n + 1; j < 2 * n + 1; j++) { + 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; + } +} diff --git a/src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences/readme.md b/src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences/readme.md new file mode 100644 index 000000000..083242ab6 --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.java b/src/main/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.java new file mode 100644 index 000000000..0d860b8b3 --- /dev/null +++ b/src/main/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.java @@ -0,0 +1,90 @@ +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_410_ms_(94.03%)_Space_66.1_MB_(82.09%) + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeSet; + +@SuppressWarnings("java:S1210") +public class Solution { + private static class RC implements Comparable { + int val; + int cnt; + + RC(int v, int c) { + val = v; + cnt = c; + } + + public int compareTo(RC o) { + if (cnt != o.cnt) { + return cnt - o.cnt; + } + return val - o.val; + } + } + + public long[] findXSum(int[] nums, int k, int x) { + int n = nums.length; + long[] ans = new long[n - k + 1]; + Map cnt = new HashMap<>(); + TreeSet s1 = new TreeSet<>(); + TreeSet s2 = new TreeSet<>(); + long sum = 0; + long xSum = 0; + for (int i = 0; i < n; ++i) { + sum += nums[i]; + int curCnt = cnt.getOrDefault(nums[i], 0); + cnt.put(nums[i], curCnt + 1); + RC tmp = new RC(nums[i], curCnt); + if (s1.contains(tmp)) { + s1.remove(tmp); + s1.add(new RC(nums[i], curCnt + 1)); + xSum += nums[i]; + } else { + s2.remove(tmp); + s1.add(new RC(nums[i], curCnt + 1)); + xSum += (long) nums[i] * (curCnt + 1); + while (s1.size() > x) { + RC l = s1.first(); + s1.remove(l); + xSum -= (long) l.val * l.cnt; + s2.add(l); + } + } + if (i >= k - 1) { + ans[i - k + 1] = s1.size() == x ? xSum : sum; + int v = nums[i - k + 1]; + sum -= v; + curCnt = cnt.get(v); + if (curCnt > 1) { + cnt.put(v, curCnt - 1); + } else { + cnt.remove(v); + } + tmp = new RC(v, curCnt); + if (s2.contains(tmp)) { + s2.remove(tmp); + if (curCnt > 1) { + s2.add(new RC(v, curCnt - 1)); + } + } else { + s1.remove(tmp); + xSum -= (long) v * curCnt; + if (curCnt > 1) { + s2.add(new RC(v, curCnt - 1)); + } + while (s1.size() < x && !s2.isEmpty()) { + RC r = s2.last(); + s2.remove(r); + s1.add(r); + xSum += (long) r.val * r.cnt; + } + } + } + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.java b/src/test/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.java new file mode 100644 index 000000000..f56e37043 --- /dev/null +++ b/src/test/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3314_construct_the_minimum_bitwise_array_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minBitwiseArray() { + assertThat( + new Solution().minBitwiseArray(List.of(2, 3, 5, 7)), + equalTo(new int[] {-1, 1, 4, 3})); + } + + @Test + void minBitwiseArray2() { + assertThat( + new Solution().minBitwiseArray(List.of(11, 13, 31)), + equalTo(new int[] {9, 12, 15})); + } +} diff --git a/src/test/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.java b/src/test/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.java new file mode 100644 index 000000000..a97e01373 --- /dev/null +++ b/src/test/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minBitwiseArray() { + assertThat( + new Solution().minBitwiseArray(List.of(2, 3, 5, 7)), + equalTo(new int[] {-1, 1, 4, 3})); + } + + @Test + void minBitwiseArray2() { + assertThat( + new Solution().minBitwiseArray(List.of(11, 13, 31)), + equalTo(new int[] {9, 12, 15})); + } +} diff --git a/src/test/java/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.java b/src/test/java/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.java new file mode 100644 index 000000000..0e6377035 --- /dev/null +++ b/src/test/java/g3301_3400/s3316_find_maximum_removals_from_source_string/SolutionTest.java @@ -0,0 +1,30 @@ +package g3301_3400.s3316_find_maximum_removals_from_source_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxRemovals() { + assertThat(new Solution().maxRemovals("abbaa", "aba", new int[] {0, 1, 2}), equalTo(1)); + } + + @Test + void maxRemovals2() { + assertThat(new Solution().maxRemovals("bcda", "d", new int[] {0, 3}), equalTo(2)); + } + + @Test + void maxRemovals3() { + assertThat(new Solution().maxRemovals("dda", "dda", new int[] {0, 1, 2}), equalTo(0)); + } + + @Test + void maxRemovals4() { + assertThat( + new Solution().maxRemovals("yeyeykyded", "yeyyd", new int[] {0, 2, 3, 4}), + equalTo(2)); + } +} diff --git a/src/test/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.java b/src/test/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.java new file mode 100644 index 000000000..d49e983bd --- /dev/null +++ b/src/test/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfWays() { + assertThat(new Solution().numberOfWays(1, 2, 3), equalTo(6)); + } + + @Test + void numberOfWays2() { + assertThat(new Solution().numberOfWays(5, 2, 1), equalTo(32)); + } + + @Test + void numberOfWays3() { + assertThat(new Solution().numberOfWays(3, 3, 4), equalTo(684)); + } +} diff --git a/src/test/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.java b/src/test/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.java new file mode 100644 index 000000000..ae573ee8b --- /dev/null +++ b/src/test/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/SolutionTest.java @@ -0,0 +1,22 @@ +package g3301_3400.s3318_find_x_sum_of_all_k_long_subarrays_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findXSum() { + assertThat( + new Solution().findXSum(new int[] {1, 1, 2, 2, 3, 4, 2, 3}, 6, 2), + equalTo(new int[] {6, 10, 12})); + } + + @Test + void findXSum2() { + assertThat( + new Solution().findXSum(new int[] {3, 8, 7, 8, 7, 5}, 2, 2), + equalTo(new int[] {11, 15, 15, 15, 12})); + } +} diff --git a/src/test/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.java b/src/test/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.java new file mode 100644 index 000000000..35c7340c8 --- /dev/null +++ b/src/test/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/SolutionTest.java @@ -0,0 +1,39 @@ +package g3301_3400.s3319_k_th_largest_perfect_subtree_size_in_binary_tree; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.TreeNode; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kthLargestPerfectSubtree() { + assertThat( + new Solution() + .kthLargestPerfectSubtree( + TreeNode.create( + Arrays.asList(5, 3, 6, 5, 2, 5, 7, 1, 8, null, null, 6, 8)), + 2), + equalTo(3)); + } + + @Test + void kthLargestPerfectSubtree2() { + assertThat( + new Solution() + .kthLargestPerfectSubtree( + TreeNode.create(Arrays.asList(1, 2, 3, 4, 5, 6, 7)), 1), + equalTo(7)); + } + + @Test + void kthLargestPerfectSubtree3() { + assertThat( + new Solution() + .kthLargestPerfectSubtree( + TreeNode.create(Arrays.asList(1, 2, 3, null, 4)), 3), + equalTo(-1)); + } +} diff --git a/src/test/java/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.java b/src/test/java/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.java new file mode 100644 index 000000000..ecb4312f7 --- /dev/null +++ b/src/test/java/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3320_count_the_number_of_winning_sequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countWinningSequences() { + assertThat(new Solution().countWinningSequences("FFF"), equalTo(3)); + } + + @Test + void countWinningSequences2() { + assertThat(new Solution().countWinningSequences("FWEFW"), equalTo(18)); + } +} diff --git a/src/test/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.java b/src/test/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.java new file mode 100644 index 000000000..bf33aec96 --- /dev/null +++ b/src/test/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/SolutionTest.java @@ -0,0 +1,22 @@ +package g3301_3400.s3321_find_x_sum_of_all_k_long_subarrays_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findXSum() { + assertThat( + new Solution().findXSum(new int[] {1, 1, 2, 2, 3, 4, 2, 3}, 6, 2), + equalTo(new long[] {6L, 10L, 12L})); + } + + @Test + void findXSum2() { + assertThat( + new Solution().findXSum(new int[] {3, 8, 7, 8, 7, 5}, 2, 2), + equalTo(new long[] {11L, 15L, 15L, 15L, 12L})); + } +}