diff --git a/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.kt b/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.kt new file mode 100644 index 000000000..be62bd7b4 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.kt @@ -0,0 +1,21 @@ +package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences + +// #Hard #Array #Dynamic_Programming #2024_04_18_Time_176_ms_(90.00%)_Space_35.3_MB_(100.00%) + +class Solution { + fun sumOfPower(nums: IntArray, k: Int): Int { + val kMod = 1000000007 + val dp = IntArray(k + 1) + dp[0] = 1 + for (num in nums) { + for (i in k downTo 0) { + if (i < num) { + dp[i] = ((dp[i] * 2L) % kMod).toInt() + } else { + dp[i] = ((dp[i] * 2L + dp[i - num]) % kMod).toInt() + } + } + } + return dp[k] + } +} diff --git a/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md b/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md new file mode 100644 index 000000000..c37a9a0b4 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md @@ -0,0 +1,59 @@ +3082\. Find the Sum of the Power of All Subsequences + +Hard + +You are given an integer array `nums` of length `n` and a **positive** integer `k`. + +The **power** of an array of integers is defined as the number of subsequences with their sum **equal** to `k`. + +Return _the **sum** of **power** of all subsequences of_ `nums`_._ + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 3 + +**Output:** 6 + +**Explanation:** + +There are `5` subsequences of nums with non-zero power: + +* The subsequence [**1**,**2**,**3**] has `2` subsequences with `sum == 3`: [1,2,3] and [1,2,3]. +* The subsequence [**1**,2,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [1,**2**,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [**1**,**2**,3] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [1,2,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. + +Hence the answer is `2 + 1 + 1 + 1 + 1 = 6`. + +**Example 2:** + +**Input:** nums = [2,3,3], k = 5 + +**Output:** 4 + +**Explanation:** + +There are `3` subsequences of nums with non-zero power: + +* The subsequence [**2**,**3**,**3**] has 2 subsequences with `sum == 5`: [2,3,3] and [2,3,3]. +* The subsequence [**2**,3,**3**] has 1 subsequence with `sum == 5`: [2,3,3]. +* The subsequence [**2**,**3**,3] has 1 subsequence with `sum == 5`: [2,3,3]. + +Hence the answer is `2 + 1 + 1 = 4`. + +**Example 3:** + +**Input:** nums = [1,2,3], k = 7 + +**Output:** 0 + +**Explanation: **There exists no subsequence with sum `7`. Hence all subsequences of nums have `power = 0`. + +**Constraints:** + +* `1 <= n <= 100` +* 1 <= nums[i] <= 104 +* `1 <= k <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.kt b/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.kt new file mode 100644 index 000000000..953b39f74 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.kt @@ -0,0 +1,23 @@ +package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse + +// #Easy #String #Hash_Table #2024_04_18_Time_168_ms_(79.49%)_Space_37.5_MB_(24.36%) + +class Solution { + fun isSubstringPresent(s: String): Boolean { + if (s.length == 1) { + return false + } + val revSb = StringBuilder() + for (i in s.length - 1 downTo 0) { + revSb.append(s[i]) + } + val rev = revSb.toString() + for (i in 0 until s.length - 1) { + val sub = s.substring(i, i + 2) + if (rev.contains(sub)) { + return true + } + } + return false + } +} diff --git a/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md b/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md new file mode 100644 index 000000000..19c5832f1 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md @@ -0,0 +1,36 @@ +3083\. Existence of a Substring in a String and Its Reverse + +Easy + +Given a string `s`, find any substring of length `2` which is also present in the reverse of `s`. + +Return `true` _if such a substring exists, and_ `false` _otherwise._ + +**Example 1:** + +**Input:** s = "leetcode" + +**Output:** true + +**Explanation:** Substring `"ee"` is of length `2` which is also present in `reverse(s) == "edocteel"`. + +**Example 2:** + +**Input:** s = "abcba" + +**Output:** true + +**Explanation:** All of the substrings of length `2` `"ab"`, `"bc"`, `"cb"`, `"ba"` are also present in `reverse(s) == "abcba"`. + +**Example 3:** + +**Input:** s = "abcd" + +**Output:** false + +**Explanation:** There is no substring of length `2` in `s`, which is also present in the reverse of `s`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.kt b/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.kt new file mode 100644 index 000000000..517185833 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.kt @@ -0,0 +1,15 @@ +package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character + +// #Medium #String #Math #Counting #2024_04_18_Time_177_ms_(98.55%)_Space_38.1_MB_(37.68%) + +class Solution { + fun countSubstrings(s: String, c: Char): Long { + var count: Long = 0 + for (element in s) { + if (element == c) { + count++ + } + } + return count * (count + 1) / 2 + } +} diff --git a/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md b/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md new file mode 100644 index 000000000..ff1689f7e --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md @@ -0,0 +1,26 @@ +3084\. Count Substrings Starting and Ending with Given Character + +Medium + +You are given a string `s` and a character `c`. Return _the total number of substrings of_ `s` _that start and end with_ `c`_._ + +**Example 1:** + +**Input:** s = "abada", c = "a" + +**Output:** 6 + +**Explanation:** Substrings starting and ending with `"a"` are: "**a**bada", "**aba**da", "**abada**", "ab**a**da", "ab**ada**", "abad**a**". + +**Example 2:** + +**Input:** s = "zzz", c = "z" + +**Output:** 6 + +**Explanation:** There are a total of `6` substrings in `s` and all start and end with `"z"`. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` and `c` consist only of lowercase English letters. diff --git a/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.kt b/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.kt new file mode 100644 index 000000000..49601af9b --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.kt @@ -0,0 +1,31 @@ +package g3001_3100.s3085_minimum_deletions_to_make_string_k_special + +// #Medium #String #Hash_Table #Sorting #Greedy #Counting +// #2024_04_18_Time_221_ms_(93.33%)_Space_38.3_MB_(93.33%) + +import kotlin.math.min + +class Solution { + fun minimumDeletions(word: String, k: Int): Int { + val arr = IntArray(26) + for (element in word) { + arr[element.code - 'a'.code]++ + } + var min = Int.MAX_VALUE + for (value in arr) { + if (value != 0) { + val u = value + k + var res = 0 + for (i in arr) { + if (i < value) { + res += i + } else if (i > u) { + res += (i - u) + } + } + min = min(res, min) + } + } + return min + } +} diff --git a/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md b/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md new file mode 100644 index 000000000..b334c1fa7 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md @@ -0,0 +1,41 @@ +3085\. Minimum Deletions to Make String K-Special + +Medium + +You are given a string `word` and an integer `k`. + +We consider `word` to be **k-special** if `|freq(word[i]) - freq(word[j])| <= k` for all indices `i` and `j` in the string. + +Here, `freq(x)` denotes the frequency of the character `x` in `word`, and `|y|` denotes the absolute value of `y`. + +Return _the **minimum** number of characters you need to delete to make_ `word` **_k-special_**. + +**Example 1:** + +**Input:** word = "aabcaba", k = 0 + +**Output:** 3 + +**Explanation:** We can make `word` `0`\-special by deleting `2` occurrences of `"a"` and `1` occurrence of `"c"`. Therefore, `word` becomes equal to `"baba"` where `freq('a') == freq('b') == 2`. + +**Example 2:** + +**Input:** word = "dabdcbdcdcd", k = 2 + +**Output:** 2 + +**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"a"` and `1` occurrence of `"d"`. Therefore, `word` becomes equal to "bdcbdcdcd" where `freq('b') == 2`, `freq('c') == 3`, and `freq('d') == 4`. + +**Example 3:** + +**Input:** word = "aaabaaa", k = 2 + +**Output:** 1 + +**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"b"`. Therefore, `word` becomes equal to `"aaaaaa"` where each letter's frequency is now uniformly `6`. + +**Constraints:** + +* 1 <= word.length <= 105 +* 0 <= k <= 105 +* `word` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.kt b/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.kt new file mode 100644 index 000000000..b5ebb00a6 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.kt @@ -0,0 +1,62 @@ +package g3001_3100.s3086_minimum_moves_to_pick_k_ones + +// #Hard #Array #Greedy #Prefix_Sum #Sliding_Window +// #2024_04_18_Time_368_ms_(100.00%)_Space_56.8_MB_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minimumMoves(nums: IntArray, k: Int, maxChanges: Int): Long { + var maxAdjLen = 0 + val n = nums.size + var numOne = 0 + var l = 0 + var r = 0 + while (r < n) { + if (nums[r] != 1) { + maxAdjLen = max(maxAdjLen, (r - l)) + l = r + 1 + } else { + numOne++ + } + r++ + } + maxAdjLen = min(3, max(maxAdjLen, (r - l))) + if (maxAdjLen + maxChanges >= k) { + return if (maxAdjLen >= k) { + k - 1L + } else { + max(0, (maxAdjLen - 1)) + (k - maxAdjLen) * 2L + } + } + val ones = IntArray(numOne) + var ind = 0 + for (i in 0 until n) { + if (nums[i] == 1) { + ones[ind++] = i + } + } + val preSum = LongArray(ones.size + 1) + for (i in 1 until preSum.size) { + preSum[i] = preSum[i - 1] + ones[i - 1] + } + val target = k - maxChanges + l = 0 + var res = Long.MAX_VALUE + while (l <= ones.size - target) { + r = l + target - 1 + val mid = (l + r) / 2 + val median = ones[mid] + val sum1 = preSum[mid + 1] - preSum[l] + val sum2 = preSum[r + 1] - preSum[mid + 1] + val area1 = (mid - l + 1).toLong() * median + val area2 = (r - mid).toLong() * median + val curRes = area1 - sum1 + sum2 - area2 + res = min(res.toDouble(), curRes.toDouble()).toLong() + l++ + } + res += 2L * maxChanges + return res + } +} diff --git a/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md b/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md new file mode 100644 index 000000000..338a3b62a --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md @@ -0,0 +1,48 @@ +3086\. Minimum Moves to Pick K Ones + +Hard + +You are given a binary array `nums` of length `n`, a **positive** integer `k` and a **non-negative** integer `maxChanges`. + +Alice plays a game, where the goal is for Alice to pick up `k` ones from `nums` using the **minimum** number of **moves**. When the game starts, Alice picks up any index `aliceIndex` in the range `[0, n - 1]` and stands there. If `nums[aliceIndex] == 1` , Alice picks up the one and `nums[aliceIndex]` becomes `0`(this **does not** count as a move). After this, Alice can make **any** number of **moves** (**including** **zero**) where in each move Alice must perform **exactly** one of the following actions: + +* Select any index `j != aliceIndex` such that `nums[j] == 0` and set `nums[j] = 1`. This action can be performed **at** **most** `maxChanges` times. +* Select any two adjacent indices `x` and `y` (`|x - y| == 1`) such that `nums[x] == 1`, `nums[y] == 0`, then swap their values (set `nums[y] = 1` and `nums[x] = 0`). If `y == aliceIndex`, Alice picks up the one after this move and `nums[y]` becomes `0`. + +Return _the **minimum** number of moves required by Alice to pick **exactly**_ `k` _ones_. + +**Example 1:** + +**Input:** nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1 + +**Output:** 3 + +**Explanation:** Alice can pick up `3` ones in `3` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 1`: + +* At the start of the game Alice picks up the one and `nums[1]` becomes `0`. `nums` becomes [1,**1**,1,0,0,1,1,0,0,1]. +* Select `j == 2` and perform an action of the first type. `nums` becomes [1,**0**,1,0,0,1,1,0,0,1] +* Select `x == 2` and `y == 1`, and perform an action of the second type. `nums` becomes [1,**1**,0,0,0,1,1,0,0,1]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [1,**0**,0,0,0,1,1,0,0,1]. +* Select `x == 0` and `y == 1`, and perform an action of the second type. `nums` becomes [0,**1**,0,0,0,1,1,0,0,1]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [0,**0**,0,0,0,1,1,0,0,1]. + +Note that it may be possible for Alice to pick up `3` ones using some other sequence of `3` moves. + +**Example 2:** + +**Input:** nums = [0,0,0,0], k = 2, maxChanges = 3 + +**Output:** 4 + +**Explanation:** Alice can pick up `2` ones in `4` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 0`: + +* Select `j == 1` and perform an action of the first type. `nums` becomes [**0**,1,0,0]. +* Select `x == 1` and `y == 0`, and perform an action of the second type. `nums` becomes [**1**,0,0,0]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [**0**,0,0,0]. +* Select `j == 1` again and perform an action of the first type. `nums` becomes [**0**,1,0,0]. +* Select `x == 1` and `y == 0` again, and perform an action of the second type. `nums` becomes [**1**,0,0,0]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [**0**,0,0,0]. + +**Constraints:** + +* 2 <= n <= 105 +* `0 <= nums[i] <= 1` +* 1 <= k <= 105 +* 0 <= maxChanges <= 105 +* `maxChanges + sum(nums) >= k` \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/Solution.kt b/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/Solution.kt new file mode 100644 index 000000000..af0cce95c --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/Solution.kt @@ -0,0 +1,24 @@ +package g3001_3100.s3090_maximum_length_substring_with_two_occurrences + +// #Easy #String #Hash_Table #Sliding_Window #2024_04_18_Time_157_ms_(90.24%)_Space_35_MB_(67.07%) + +import kotlin.math.max + +class Solution { + fun maximumLengthSubstring(s: String): Int { + val freq = IntArray(26) + val chars = s.toCharArray() + var i = 0 + val len = s.length + var max = 0 + for (j in 0 until len) { + ++freq[chars[j].code - 'a'.code] + while (freq[chars[j].code - 'a'.code] == 3) { + --freq[chars[i].code - 'a'.code] + i++ + } + max = max(max, (j - i + 1)) + } + return max + } +} diff --git a/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/readme.md b/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/readme.md new file mode 100644 index 000000000..3b38da787 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/readme.md @@ -0,0 +1,30 @@ +3090\. Maximum Length Substring With Two Occurrences + +Easy + +Given a string `s`, return the **maximum** length of a substring such that it contains _at most two occurrences_ of each character. + +**Example 1:** + +**Input:** s = "bcbbbcba" + +**Output:** 4 + +**Explanation:** + +The following substring has a length of 4 and contains at most two occurrences of each character: "bcbbbcba". + +**Example 2:** + +**Input:** s = "aaaa" + +**Output:** 2 + +**Explanation:** + +The following substring has a length of 2 and contains at most two occurrences of each character: "aaaa". + +**Constraints:** + +* `2 <= s.length <= 100` +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/Solution.kt b/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/Solution.kt new file mode 100644 index 000000000..7b08facfb --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/Solution.kt @@ -0,0 +1,12 @@ +package g3001_3100.s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k + +// #Medium #Math #Greedy #Enumeration #2024_04_18_Time_134_ms_(73.21%)_Space_33.2_MB_(98.21%) + +import kotlin.math.sqrt + +class Solution { + fun minOperations(k: Int): Int { + val a = sqrt(k.toDouble()).toInt() + return a + (k - 1) / a - 1 + } +} diff --git a/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/readme.md b/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/readme.md new file mode 100644 index 000000000..8df43c7ca --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/readme.md @@ -0,0 +1,42 @@ +3091\. Apply Operations to Make Sum of Array Greater Than or Equal to k + +Medium + +You are given a **positive** integer `k`. Initially, you have an array `nums = [1]`. + +You can perform **any** of the following operations on the array **any** number of times (**possibly zero**): + +* Choose any element in the array and **increase** its value by `1`. +* Duplicate any element in the array and add it to the end of the array. + +Return _the **minimum** number of operations required to make the **sum** of elements of the final array greater than or equal to_ `k`. + +**Example 1:** + +**Input:** k = 11 + +**Output:** 5 + +**Explanation:** + +We can do the following operations on the array `nums = [1]`: + +* Increase the element by `1` three times. The resulting array is `nums = [4]`. +* Duplicate the element two times. The resulting array is `nums = [4,4,4]`. + +The sum of the final array is `4 + 4 + 4 = 12` which is greater than or equal to `k = 11`. +The total number of operations performed is `3 + 2 = 5`. + +**Example 2:** + +**Input:** k = 1 + +**Output:** 0 + +**Explanation:** + +The sum of the original array is already greater than or equal to `1`, so no operations are needed. + +**Constraints:** + +* 1 <= k <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/Solution.kt b/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/Solution.kt new file mode 100644 index 000000000..02fc4b027 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/Solution.kt @@ -0,0 +1,41 @@ +package g3001_3100.s3092_most_frequent_ids + +// #Medium #Array #Hash_Table #Heap_Priority_Queue #Ordered_Set +// #2024_04_18_Time_758_ms_(100.00%)_Space_61.1_MB_(98.25%) + +import kotlin.math.max + +class Solution { + fun mostFrequentIDs(nums: IntArray, freq: IntArray): LongArray { + var max = Int.MIN_VALUE + val n = nums.size + for (num in nums) { + max = max(max.toDouble(), num.toDouble()).toInt() + } + val bins = LongArray(max + 1) + var mostFrequentID = 0 + var maxCount: Long = 0 + val ans = LongArray(n) + for (i in 0 until n) { + bins[nums[i]] += freq[i].toLong() + if (freq[i] > 0) { + if (bins[nums[i]] > maxCount) { + maxCount = bins[nums[i]] + mostFrequentID = nums[i] + } + } else { + if (nums[i] == mostFrequentID) { + maxCount = bins[nums[i]] + for (j in 0..max) { + if (bins[j] > maxCount) { + maxCount = bins[j] + mostFrequentID = j + } + } + } + } + ans[i] = maxCount + } + return ans + } +} diff --git a/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/readme.md b/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/readme.md new file mode 100644 index 000000000..2dbfdc08b --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3092_most_frequent_ids/readme.md @@ -0,0 +1,43 @@ +3092\. Most Frequent IDs + +Medium + +The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, `nums` and `freq`, of equal length `n`. Each element in `nums` represents an ID, and the corresponding element in `freq` indicates how many times that ID should be added to or removed from the collection at each step. + +* **Addition of IDs:** If `freq[i]` is positive, it means `freq[i]` IDs with the value `nums[i]` are added to the collection at step `i`. +* **Removal of IDs:** If `freq[i]` is negative, it means `-freq[i]` IDs with the value `nums[i]` are removed from the collection at step `i`. + +Return an array `ans` of length `n`, where `ans[i]` represents the **count** of the _most frequent ID_ in the collection after the ith step. If the collection is empty at any step, `ans[i]` should be 0 for that step. + +**Example 1:** + +**Input:** nums = [2,3,2,1], freq = [3,2,-3,1] + +**Output:** [3,3,2,2] + +**Explanation:** + +After step 0, we have 3 IDs with the value of 2. So `ans[0] = 3`. +After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So `ans[1] = 3`. +After step 2, we have 2 IDs with the value of 3. So `ans[2] = 2`. +After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So `ans[3] = 2`. + +**Example 2:** + +**Input:** nums = [5,5,3], freq = [2,-2,1] + +**Output:** [2,0,1] + +**Explanation:** + +After step 0, we have 2 IDs with the value of 5. So `ans[0] = 2`. +After step 1, there are no IDs. So `ans[1] = 0`. +After step 2, we have 1 ID with the value of 3. So `ans[2] = 1`. + +**Constraints:** + +* 1 <= nums.length == freq.length <= 105 +* 1 <= nums[i] <= 105 +* -105 <= freq[i] <= 105 +* `freq[i] != 0` +* The input is generated such that the occurrences of an ID will not be negative in any step. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/Solution.kt b/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/Solution.kt new file mode 100644 index 000000000..142b4a94a --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/Solution.kt @@ -0,0 +1,65 @@ +package g3001_3100.s3093_longest_common_suffix_queries + +// #Hard #Array #String #Trie #2024_04_18_Time_860_ms_(89.29%)_Space_171.4_MB_(67.86%) + +class Solution { + fun stringIndices(wc: Array, wq: Array): IntArray { + var minLength = wc[0].length + var minIndex = 0 + val n = wc.size + val m = wq.size + for (i in 0 until n) { + if (minLength > wc[i].length) { + minLength = wc[i].length + minIndex = i + } + } + val root = Trie(minIndex) + for (i in 0 until n) { + var curr: Trie? = root + for (j in wc[i].length - 1 downTo 0) { + val ch = wc[i][j] + if (curr!!.has(ch)) { + val next = curr.get(ch) + if (wc[next!!.index].length > wc[i].length) { + next.index = i + } + curr = next + } else { + curr.put(ch, i) + curr = curr.get(ch) + } + } + } + val ans = IntArray(m) + for (i in 0 until m) { + var curr: Trie? = root + for (j in wq[i].length - 1 downTo 0) { + val ch = wq[i][j] + if (curr!!.has(ch)) { + curr = curr.get(ch) + } else { + break + } + } + ans[i] = curr!!.index + } + return ans + } + + private class Trie(var index: Int) { + var ch: Array = arrayOfNulls(26) + + fun get(ch: Char): Trie? { + return this.ch[ch.code - 'a'.code] + } + + fun has(ch: Char): Boolean { + return this.ch[ch.code - 'a'.code] != null + } + + fun put(ch: Char, index: Int) { + this.ch[ch.code - 'a'.code] = Trie(index) + } + } +} diff --git a/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/readme.md b/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/readme.md new file mode 100644 index 000000000..f0aa2c2e2 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries/readme.md @@ -0,0 +1,47 @@ +3093\. Longest Common Suffix Queries + +Hard + +You are given two arrays of strings `wordsContainer` and `wordsQuery`. + +For each `wordsQuery[i]`, you need to find a string from `wordsContainer` that has the **longest common suffix** with `wordsQuery[i]`. If there are two or more strings in `wordsContainer` that share the longest common suffix, find the string that is the **smallest** in length. If there are two or more such strings that have the **same** smallest length, find the one that occurred **earlier** in `wordsContainer`. + +Return _an array of integers_ `ans`_, where_ `ans[i]` _is the index of the string in_ `wordsContainer` _that has the **longest common suffix** with_ `wordsQuery[i]`_._ + +**Example 1:** + +**Input:** wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"] + +**Output:** [1,1,1] + +**Explanation:** + +Let's look at each `wordsQuery[i]` separately: + +* For `wordsQuery[0] = "cd"`, strings from `wordsContainer` that share the longest common suffix `"cd"` are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3. +* For `wordsQuery[1] = "bcd"`, strings from `wordsContainer` that share the longest common suffix `"bcd"` are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3. +* For `wordsQuery[2] = "xyz"`, there is no string from `wordsContainer` that shares a common suffix. Hence the longest common suffix is `""`, that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3. + +**Example 2:** + +**Input:** wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"] + +**Output:** [2,0,2] + +**Explanation:** + +Let's look at each `wordsQuery[i]` separately: + +* For `wordsQuery[0] = "gh"`, strings from `wordsContainer` that share the longest common suffix `"gh"` are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6. +* For `wordsQuery[1] = "acbfgh"`, only the string at index 0 shares the longest common suffix `"fgh"`. Hence it is the answer, even though the string at index 2 is shorter. +* For `wordsQuery[2] = "acbfegh"`, strings from `wordsContainer` that share the longest common suffix `"gh"` are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6. + +**Constraints:** + +* 1 <= wordsContainer.length, wordsQuery.length <= 104 +* 1 <= wordsContainer[i].length <= 5 * 103 +* 1 <= wordsQuery[i].length <= 5 * 103 +* `wordsContainer[i]` consists only of lowercase English letters. +* `wordsQuery[i]` consists only of lowercase English letters. +* Sum of `wordsContainer[i].length` is at most 5 * 105. +* Sum of `wordsQuery[i].length` is at most 5 * 105. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/Solution.kt b/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/Solution.kt new file mode 100644 index 000000000..6717f166d --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/Solution.kt @@ -0,0 +1,24 @@ +package g3001_3100.s3095_shortest_subarray_with_or_at_least_k_i + +// #Easy #Array #Bit_Manipulation #Sliding_Window +// #2024_04_18_Time_161_ms_(95.65%)_Space_35.3_MB_(71.74%) + +import kotlin.math.min + +class Solution { + fun minimumSubarrayLength(nums: IntArray, k: Int): Int { + val n = nums.size + var maxL = n + 1 + var `val`: Int + for (i in 0 until n) { + `val` = 0 + for (j in i until n) { + `val` = `val` or nums[j] + if (`val` >= k) { + maxL = min(maxL, (j - i + 1)) + } + } + } + return if (maxL == n + 1) -1 else maxL + } +} diff --git a/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/readme.md b/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/readme.md new file mode 100644 index 000000000..0ac9a855d --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/readme.md @@ -0,0 +1,45 @@ +3095\. Shortest Subarray With OR at Least K I + +Easy + +You are given an array `nums` of **non-negative** integers and an integer `k`. + +An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`. + +Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 2 + +**Output:** 1 + +**Explanation:** + +The subarray `[3]` has `OR` value of `3`. Hence, we return `1`. + +**Example 2:** + +**Input:** nums = [2,1,8], k = 10 + +**Output:** 3 + +**Explanation:** + +The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`. + +**Example 3:** + +**Input:** nums = [1,2], k = 0 + +**Output:** 1 + +**Explanation:** + +The subarray `[1]` has `OR` value of `1`. Hence, we return `1`. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `0 <= nums[i] <= 50` +* `0 <= k < 64` \ No newline at end of file diff --git a/src/test/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.kt new file mode 100644 index 000000000..7e2d51c3c --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun sumOfPower() { + assertThat(Solution().sumOfPower(intArrayOf(2, 3, 3), 5), equalTo(4)) + } + + @Test + fun sumOfPower2() { + assertThat(Solution().sumOfPower(intArrayOf(1, 2, 3), 7), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.kt new file mode 100644 index 000000000..cf3ab96d5 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isSubstringPresent() { + assertThat(Solution().isSubstringPresent("leetcode"), equalTo(true)) + } + + @Test + fun isSubstringPresent2() { + assertThat(Solution().isSubstringPresent("abcba"), equalTo(true)) + } + + @Test + fun isSubstringPresent3() { + assertThat(Solution().isSubstringPresent("abcd"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.kt new file mode 100644 index 000000000..c48e6d0b7 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countSubstrings() { + assertThat(Solution().countSubstrings("abada", 'a'), equalTo(6L)) + } + + @Test + fun countSubstrings2() { + assertThat(Solution().countSubstrings("zzz", 'z'), equalTo(6L)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.kt new file mode 100644 index 000000000..f6488ef1b --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3001_3100.s3085_minimum_deletions_to_make_string_k_special + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumDeletions() { + assertThat(Solution().minimumDeletions("aabcaba", 0), equalTo(3)) + } + + @Test + fun minimumDeletions2() { + assertThat(Solution().minimumDeletions("dabdcbdcdcd", 2), equalTo(2)) + } + + @Test + fun minimumDeletions3() { + assertThat(Solution().minimumDeletions("aaabaaa", 2), equalTo(1)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.kt new file mode 100644 index 000000000..e1e3c9f1f --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3001_3100.s3086_minimum_moves_to_pick_k_ones + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumMoves() { + assertThat( + Solution().minimumMoves(intArrayOf(1, 1, 0, 0, 0, 1, 1, 0, 0, 1), 3, 1), + equalTo(3L) + ) + } + + @Test + fun minimumMoves2() { + assertThat(Solution().minimumMoves(intArrayOf(0, 0, 0, 0), 2, 3), equalTo(4L)) + } + + @Test + fun minimumMoves3() { + assertThat(Solution().minimumMoves(intArrayOf(1, 0, 1, 0, 1), 3, 0), equalTo(4L)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/SolutionTest.kt new file mode 100644 index 000000000..5fea60a44 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3090_maximum_length_substring_with_two_occurrences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumLengthSubstring() { + assertThat(Solution().maximumLengthSubstring("bcbbbcba"), equalTo(4)) + } + + @Test + fun maximumLengthSubstring2() { + assertThat(Solution().maximumLengthSubstring("aaaa"), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/SolutionTest.kt new file mode 100644 index 000000000..4dae86f76 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minOperations() { + assertThat(Solution().minOperations(11), equalTo(5)) + } + + @Test + fun minOperations2() { + assertThat(Solution().minOperations(1), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3092_most_frequent_ids/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3092_most_frequent_ids/SolutionTest.kt new file mode 100644 index 000000000..0c0892f1f --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3092_most_frequent_ids/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3001_3100.s3092_most_frequent_ids + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun mostFrequentIDs() { + assertThat( + Solution().mostFrequentIDs(intArrayOf(2, 3, 2, 1), intArrayOf(3, 2, -3, 1)), + equalTo(longArrayOf(3, 3, 2, 2)) + ) + } + + @Test + fun mostFrequentIDs2() { + assertThat( + Solution().mostFrequentIDs(intArrayOf(5, 5, 3), intArrayOf(2, -2, 1)), + equalTo(longArrayOf(2, 0, 1)) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3093_longest_common_suffix_queries/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3093_longest_common_suffix_queries/SolutionTest.kt new file mode 100644 index 000000000..d18468740 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3093_longest_common_suffix_queries/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3001_3100.s3093_longest_common_suffix_queries + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun stringIndices() { + assertThat( + Solution() + .stringIndices( + arrayOf("abcd", "bcd", "xbcd"), + arrayOf("cd", "bcd", "xyz") + ), + equalTo(intArrayOf(1, 1, 1)) + ) + } + + @Test + fun stringIndices2() { + assertThat( + Solution() + .stringIndices( + arrayOf("abcdefgh", "poiuygh", "ghghgh"), + arrayOf("gh", "acbfgh", "acbfegh") + ), + equalTo(intArrayOf(2, 0, 2)) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/SolutionTest.kt new file mode 100644 index 000000000..b9131afa6 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3001_3100.s3095_shortest_subarray_with_or_at_least_k_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumSubarrayLength() { + assertThat(Solution().minimumSubarrayLength(intArrayOf(1, 2, 3), 2), equalTo(1)) + } + + @Test + fun minimumSubarrayLength2() { + assertThat(Solution().minimumSubarrayLength(intArrayOf(2, 1, 8), 10), equalTo(3)) + } + + @Test + fun minimumSubarrayLength3() { + assertThat(Solution().minimumSubarrayLength(intArrayOf(1, 2), 0), equalTo(1)) + } +}