id
stringlengths
13
13
content
stringlengths
774
4.21k
test
dict
labels
dict
LeetCode/3263
# Divide an Array Into Subarrays With Minimum Cost I You are given an array of integers `nums` of length `n`. The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`. You need to divide `nums` into `3` **disjoint contiguous** subarrays. Return *the **minimum** possible **sum** of the cost of these subarrays*.   **Example 1:** ``` **Input:** nums = [1,2,3,12] **Output:** 6 **Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6. The other possible ways to form 3 subarrays are: - [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15. - [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16. ``` **Example 2:** ``` **Input:** nums = [5,4,3] **Output:** 12 **Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12. It can be shown that 12 is the minimum cost achievable. ``` **Example 3:** ``` **Input:** nums = [10,3,1,1] **Output:** 12 **Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12. It can be shown that 12 is the minimum cost achievable. ```   **Constraints:** * `3 <= n <= 50` * `1 <= nums[i] <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 2, 3, 12]]) == 6\nassert my_solution.minimumCost(*[[5, 4, 3]]) == 12\nassert my_solution.minimumCost(*[[10, 3, 1, 1]]) == 12\nassert my_solution.minimumCost(*[[1, 1, 1]]) == 3\nassert my_solution.minimumCost(*[[1, 1, 2]]) == 4\nassert my_solution.minimumCost(*[[1, 1, 3]]) == 5\nassert my_solution.minimumCost(*[[1, 1, 4]]) == 6\nassert my_solution.minimumCost(*[[1, 1, 5]]) == 7\nassert my_solution.minimumCost(*[[1, 2, 1]]) == 4\nassert my_solution.minimumCost(*[[1, 2, 2]]) == 5\nassert my_solution.minimumCost(*[[1, 2, 3]]) == 6\nassert my_solution.minimumCost(*[[1, 2, 4]]) == 7\nassert my_solution.minimumCost(*[[1, 2, 5]]) == 8\nassert my_solution.minimumCost(*[[1, 3, 1]]) == 5\nassert my_solution.minimumCost(*[[1, 3, 2]]) == 6\nassert my_solution.minimumCost(*[[1, 3, 3]]) == 7\nassert my_solution.minimumCost(*[[1, 3, 4]]) == 8\nassert my_solution.minimumCost(*[[1, 3, 5]]) == 9\nassert my_solution.minimumCost(*[[1, 4, 1]]) == 6\nassert my_solution.minimumCost(*[[1, 4, 2]]) == 7\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3263", "questionFrontendId": "3010", "questionTitle": "Divide an Array Into Subarrays With Minimum Cost I", "stats": { "totalAccepted": "2.6K", "totalSubmission": "3.6K", "totalAcceptedRaw": 2567, "totalSubmissionRaw": 3605, "acRate": "71.2%" } }
LeetCode/3291
# Find if Array Can Be Sorted You are given a **0-indexed** array of **positive** integers `nums`. In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of set bits. You are allowed to do this operation **any** number of times (**including zero**). Return `true` *if you can sort the array, else return* `false`.   **Example 1:** ``` **Input:** nums = [8,4,2,30,15] **Output:** true **Explanation:** Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110". We can sort the array using 4 operations: - Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15]. - Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15]. - Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15]. - Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30]. The array has become sorted, hence we return true. Note that there may be other sequences of operations which also sort the array. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4,5] **Output:** true **Explanation:** The array is already sorted, hence we return true. ``` **Example 3:** ``` **Input:** nums = [3,16,8,4,2] **Output:** false **Explanation:** It can be shown that it is not possible to sort the input array using any number of operations. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 28` Please make sure your answer follows the type signature below: ```python3 class Solution: def canSortArray(self, nums: List[int]) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canSortArray(*[[8, 4, 2, 30, 15]]) == True\nassert my_solution.canSortArray(*[[1, 2, 3, 4, 5]]) == True\nassert my_solution.canSortArray(*[[3, 16, 8, 4, 2]]) == False\nassert my_solution.canSortArray(*[[1]]) == True\nassert my_solution.canSortArray(*[[4]]) == True\nassert my_solution.canSortArray(*[[7]]) == True\nassert my_solution.canSortArray(*[[10]]) == True\nassert my_solution.canSortArray(*[[18]]) == True\nassert my_solution.canSortArray(*[[30]]) == True\nassert my_solution.canSortArray(*[[1, 2]]) == True\nassert my_solution.canSortArray(*[[2, 17]]) == True\nassert my_solution.canSortArray(*[[20, 16]]) == False\nassert my_solution.canSortArray(*[[21, 17]]) == False\nassert my_solution.canSortArray(*[[24, 12]]) == True\nassert my_solution.canSortArray(*[[26, 10]]) == False\nassert my_solution.canSortArray(*[[128, 128]]) == True\nassert my_solution.canSortArray(*[[1, 2, 3]]) == True\nassert my_solution.canSortArray(*[[1, 256, 64]]) == True\nassert my_solution.canSortArray(*[[2, 28, 9]]) == False\nassert my_solution.canSortArray(*[[6, 6, 192]]) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3291", "questionFrontendId": "3011", "questionTitle": "Find if Array Can Be Sorted", "stats": { "totalAccepted": "2.3K", "totalSubmission": "4.8K", "totalAcceptedRaw": 2349, "totalSubmissionRaw": 4798, "acRate": "49.0%" } }
LeetCode/3244
# Minimize Length of Array Using Operations You are given a **0-indexed** integer array `nums` containing **positive** integers. Your task is to **minimize** the length of `nums` by performing the following operations **any** number of times (including zero): * Select **two** **distinct** indices `i` and `j` from `nums`, such that `nums[i] > 0` and `nums[j] > 0`. * Insert the result of `nums[i] % nums[j]` at the end of `nums`. * Delete the elements at indices `i` and `j` from `nums`. Return *an integer denoting the **minimum** **length** of* `nums` *after performing the operation any number of times.*   **Example 1:** ``` **Input:** nums = [1,4,3,1] **Output:** 1 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1. nums becomes [1,1,3]. Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2. nums becomes [1,1]. Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0. nums becomes [0]. The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length. ``` **Example 2:** ``` **Input:** nums = [5,5,5,10,5] **Output:** 2 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3. nums becomes [5,5,5,5]. Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3. nums becomes [5,5,0]. Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1. nums becomes [0,0]. The length of nums cannot be reduced further. Hence, the answer is 2. It can be shown that 2 is the minimum achievable length. ``` **Example 3:** ``` **Input:** nums = [2,3,4] **Output:** 1 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2. nums becomes [2,3]. Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0. nums becomes [1]. The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumArrayLength(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumArrayLength(*[[1, 4, 3, 1]]) == 1\nassert my_solution.minimumArrayLength(*[[5, 5, 5, 10, 5]]) == 2\nassert my_solution.minimumArrayLength(*[[2, 3, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[1]]) == 1\nassert my_solution.minimumArrayLength(*[[3]]) == 1\nassert my_solution.minimumArrayLength(*[[6]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[3, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[5, 3]]) == 1\nassert my_solution.minimumArrayLength(*[[6, 9]]) == 1\nassert my_solution.minimumArrayLength(*[[8, 2]]) == 1\nassert my_solution.minimumArrayLength(*[[9, 9]]) == 1\nassert my_solution.minimumArrayLength(*[[9, 10]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 2, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 1, 1]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 7, 10]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 9, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[3, 2, 1]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3244", "questionFrontendId": "3012", "questionTitle": "Minimize Length of Array Using Operations", "stats": { "totalAccepted": "1.5K", "totalSubmission": "5.4K", "totalAcceptedRaw": 1523, "totalSubmissionRaw": 5406, "acRate": "28.2%" } }
LeetCode/3260
# Divide an Array Into Subarrays With Minimum Cost II You are given a **0-indexed** array of integers `nums` of length `n`, and two **positive** integers `k` and `dist`. The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`. You need to divide `nums` into `k` **disjoint contiguous** subarrays, such that the difference between the starting index of the **second** subarray and the starting index of the `kth` subarray should be **less than or equal to** `dist`. In other words, if you divide `nums` into the subarrays `nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)]`, then `ik-1 - i1 <= dist`. Return *the **minimum** possible sum of the cost of these* *subarrays*.   **Example 1:** ``` **Input:** nums = [1,3,2,6,4,2], k = 3, dist = 3 **Output:** 5 **Explanation:** The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5. It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5. ``` **Example 2:** ``` **Input:** nums = [10,1,2,2,2,1], k = 4, dist = 3 **Output:** 15 **Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15. The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist. It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15. ``` **Example 3:** ``` **Input:** nums = [10,8,18,9], k = 3, dist = 1 **Output:** 36 **Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36. The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist. It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36. ```   **Constraints:** * `3 <= n <= 105` * `1 <= nums[i] <= 109` * `3 <= k <= n` * `k - 2 <= dist <= n - 2` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, nums: List[int], k: int, dist: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 3, 2, 6, 4, 2], 3, 3]) == 5\nassert my_solution.minimumCost(*[[10, 1, 2, 2, 2, 1], 4, 3]) == 15\nassert my_solution.minimumCost(*[[10, 8, 18, 9], 3, 1]) == 36\nassert my_solution.minimumCost(*[[1, 1, 1], 3, 1]) == 3\nassert my_solution.minimumCost(*[[1, 1, 3], 3, 1]) == 5\nassert my_solution.minimumCost(*[[1, 2, 2], 3, 1]) == 5\nassert my_solution.minimumCost(*[[1, 2, 5], 3, 1]) == 8\nassert my_solution.minimumCost(*[[1, 4, 4], 3, 1]) == 9\nassert my_solution.minimumCost(*[[2, 2, 1], 3, 1]) == 5\nassert my_solution.minimumCost(*[[2, 3, 2], 3, 1]) == 7\nassert my_solution.minimumCost(*[[2, 5, 4], 3, 1]) == 11\nassert my_solution.minimumCost(*[[3, 1, 2], 3, 1]) == 6\nassert my_solution.minimumCost(*[[3, 1, 3], 3, 1]) == 7\nassert my_solution.minimumCost(*[[3, 2, 2], 3, 1]) == 7\nassert my_solution.minimumCost(*[[3, 3, 2], 3, 1]) == 8\nassert my_solution.minimumCost(*[[3, 4, 1], 3, 1]) == 8\nassert my_solution.minimumCost(*[[3, 5, 3], 3, 1]) == 11\nassert my_solution.minimumCost(*[[4, 1, 4], 3, 1]) == 9\nassert my_solution.minimumCost(*[[4, 1, 5], 3, 1]) == 10\nassert my_solution.minimumCost(*[[4, 2, 1], 3, 1]) == 7\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3260", "questionFrontendId": "3013", "questionTitle": "Divide an Array Into Subarrays With Minimum Cost II", "stats": { "totalAccepted": "1K", "totalSubmission": "2.6K", "totalAcceptedRaw": 1021, "totalSubmissionRaw": 2604, "acRate": "39.2%" } }
LeetCode/3242
# Count Elements With Maximum Frequency You are given an array `nums` consisting of **positive** integers. Return *the **total frequencies** of elements in*`nums` *such that those elements all have the **maximum** frequency*. The **frequency** of an element is the number of occurrences of that element in the array.   **Example 1:** ``` **Input:** nums = [1,2,2,3,1,4] **Output:** 4 **Explanation:** The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array. So the number of elements in the array with maximum frequency is 4. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 5 **Explanation:** All elements of the array have a frequency of 1 which is the maximum. So the number of elements in the array with maximum frequency is 5. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxFrequencyElements(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxFrequencyElements(*[[1, 2, 2, 3, 1, 4]]) == 4\nassert my_solution.maxFrequencyElements(*[[1, 2, 3, 4, 5]]) == 5\nassert my_solution.maxFrequencyElements(*[[15]]) == 1\nassert my_solution.maxFrequencyElements(*[[10, 12, 11, 9, 6, 19, 11]]) == 2\nassert my_solution.maxFrequencyElements(*[[2, 12, 17, 18, 11]]) == 5\nassert my_solution.maxFrequencyElements(*[[19, 19, 19, 20, 19, 8, 19]]) == 5\nassert my_solution.maxFrequencyElements(*[[1, 1, 1, 1]]) == 4\nassert my_solution.maxFrequencyElements(*[[10, 1, 12, 10, 10, 19, 10]]) == 4\nassert my_solution.maxFrequencyElements(*[[1, 1, 1, 20, 6, 1]]) == 4\nassert my_solution.maxFrequencyElements(*[[17, 17]]) == 2\nassert my_solution.maxFrequencyElements(*[[6, 13, 15, 15, 11, 6, 7, 12, 4, 11]]) == 6\nassert my_solution.maxFrequencyElements(*[[1, 2]]) == 2\nassert my_solution.maxFrequencyElements(*[[14, 14, 17]]) == 2\nassert my_solution.maxFrequencyElements(*[[17, 17, 2, 12, 20, 17, 12]]) == 3\nassert my_solution.maxFrequencyElements(*[[3, 9, 11, 11, 20]]) == 2\nassert my_solution.maxFrequencyElements(*[[8, 15, 8, 11, 8, 13, 12, 11, 8]]) == 4\nassert my_solution.maxFrequencyElements(*[[17, 8, 17, 19, 17, 13, 17, 17, 17, 5]]) == 6\nassert my_solution.maxFrequencyElements(*[[11]]) == 1\nassert my_solution.maxFrequencyElements(*[[5]]) == 1\nassert my_solution.maxFrequencyElements(*[[4, 4, 10]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3242", "questionFrontendId": "3005", "questionTitle": "Count Elements With Maximum Frequency", "stats": { "totalAccepted": "5.1K", "totalSubmission": "6.6K", "totalAcceptedRaw": 5130, "totalSubmissionRaw": 6572, "acRate": "78.1%" } }
LeetCode/3245
# Find Beautiful Indices in the Given Array I You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`. An index `i` is **beautiful** if: * `0 <= i <= s.length - a.length` * `s[i..(i + a.length - 1)] == a` * There exists an index `j` such that: + `0 <= j <= s.length - b.length` + `s[j..(j + b.length - 1)] == b` + `|j - i| <= k` Return *the array that contains beautiful indices in **sorted order from smallest to largest***.   **Example 1:** ``` **Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 **Output:** [16,33] **Explanation:** There are 2 beautiful indices: [16,33]. - The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15. - The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result. ``` **Example 2:** ``` **Input:** s = "abcd", a = "a", b = "a", k = 4 **Output:** [0] **Explanation:** There is 1 beautiful index: [0]. - The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result. ```   **Constraints:** * `1 <= k <= s.length <= 105` * `1 <= a.length, b.length <= 10` * `s`, `a`, and `b` contain only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulIndices(*['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]\nassert my_solution.beautifulIndices(*['abcd', 'a', 'a', 4]) == [0]\nassert my_solution.beautifulIndices(*['sqgrt', 'rt', 'sq', 3]) == [3]\nassert my_solution.beautifulIndices(*['mquz', 'tklr', 'caz', 4]) == []\nassert my_solution.beautifulIndices(*['wl', 'xjigt', 'wl', 2]) == []\nassert my_solution.beautifulIndices(*['bavgoc', 'ba', 'c', 6]) == [0]\nassert my_solution.beautifulIndices(*['xpcp', 'yxnod', 'xpc', 4]) == []\nassert my_solution.beautifulIndices(*['lahhnlwx', 'hhnlw', 'ty', 6]) == []\nassert my_solution.beautifulIndices(*['dexgscgecd', 'gscge', 'd', 6]) == [3]\nassert my_solution.beautifulIndices(*['vjrao', 'vjr', 'yxpsw', 5]) == []\nassert my_solution.beautifulIndices(*['oo', 'swhup', 'o', 1]) == []\nassert my_solution.beautifulIndices(*['bxlzgxc', 'ducf', 'xlzgx', 3]) == []\nassert my_solution.beautifulIndices(*['wetlgztzm', 'box', 'wetl', 4]) == []\nassert my_solution.beautifulIndices(*['ocmm', 'm', 'oc', 3]) == [2, 3]\nassert my_solution.beautifulIndices(*['goxmox', 'gibs', 'ox', 6]) == []\nassert my_solution.beautifulIndices(*['kzlrqzldvy', 'zl', 'tfsr', 9]) == []\nassert my_solution.beautifulIndices(*['qhd', 'hd', 'od', 1]) == []\nassert my_solution.beautifulIndices(*['bozpeh', 'bozp', 'vrjn', 2]) == []\nassert my_solution.beautifulIndices(*['ggfsg', 'gfsg', 'g', 4]) == [1]\nassert my_solution.beautifulIndices(*['fape', 'vq', 'ap', 4]) == []\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3245", "questionFrontendId": "3006", "questionTitle": "Find Beautiful Indices in the Given Array I", "stats": { "totalAccepted": "4.1K", "totalSubmission": "9.5K", "totalAcceptedRaw": 4101, "totalSubmissionRaw": 9521, "acRate": "43.1%" } }
LeetCode/3240
# Maximum Number That Sum of the Prices Is Less Than or Equal to K You are given an integer `k` and an integer `x`. Consider `s` is the **1-indexed** binary representation of an integer `num`. The **price** of a number `num` is the number of `i`'s such that `i % x == 0` and `s[i]` is a **set bit**. Return *the **greatest** integer* `num` *such that the sum of **prices** of all numbers from* `1` *to* `num` *is less than or equal to* `k`*.* **Note**: * In the binary representation of a number **set bit** is a bit of value `1`. * The binary representation of a number will be indexed from right to left. For example, if `s == 11100`, `s[4] == 1` and `s[2] == 0`.   **Example 1:** ``` **Input:** k = 9, x = 1 **Output:** 6 **Explanation:** The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively. Since x is equal to 1, the price of each number is the number of its set bits. The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9. So the answer is 6. ``` **Example 2:** ``` **Input:** k = 7, x = 2 **Output:** 9 **Explanation:** Since x is equal to 2, we should just check eventh bits. The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2. The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2. The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2. Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0. The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2. The sum of the prices of the first 9 numbers is 6. Because the sum of the prices of the first 10 numbers is 8, the answer is 9. ```   **Constraints:** * `1 <= k <= 1015` * `1 <= x <= 8` Please make sure your answer follows the type signature below: ```python3 class Solution: def findMaximumNumber(self, k: int, x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMaximumNumber(*[9, 1]) == 6\nassert my_solution.findMaximumNumber(*[7, 2]) == 9\nassert my_solution.findMaximumNumber(*[19, 6]) == 50\nassert my_solution.findMaximumNumber(*[57, 4]) == 120\nassert my_solution.findMaximumNumber(*[58, 5]) == 121\nassert my_solution.findMaximumNumber(*[60, 8]) == 187\nassert my_solution.findMaximumNumber(*[72, 5]) == 151\nassert my_solution.findMaximumNumber(*[81, 6]) == 176\nassert my_solution.findMaximumNumber(*[83, 1]) == 33\nassert my_solution.findMaximumNumber(*[83, 7]) == 210\nassert my_solution.findMaximumNumber(*[116, 5]) == 243\nassert my_solution.findMaximumNumber(*[157, 6]) == 316\nassert my_solution.findMaximumNumber(*[201, 3]) == 212\nassert my_solution.findMaximumNumber(*[268, 6]) == 555\nassert my_solution.findMaximumNumber(*[281, 5]) == 531\nassert my_solution.findMaximumNumber(*[283, 3]) == 274\nassert my_solution.findMaximumNumber(*[309, 4]) == 364\nassert my_solution.findMaximumNumber(*[363, 7]) == 746\nassert my_solution.findMaximumNumber(*[409, 2]) == 220\nassert my_solution.findMaximumNumber(*[456, 7]) == 967\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3240", "questionFrontendId": "3007", "questionTitle": "Maximum Number That Sum of the Prices Is Less Than or Equal to K", "stats": { "totalAccepted": "2.6K", "totalSubmission": "6.2K", "totalAcceptedRaw": 2567, "totalSubmissionRaw": 6194, "acRate": "41.4%" } }
LeetCode/3303
# Find Beautiful Indices in the Given Array II You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`. An index `i` is **beautiful** if: * `0 <= i <= s.length - a.length` * `s[i..(i + a.length - 1)] == a` * There exists an index `j` such that: + `0 <= j <= s.length - b.length` + `s[j..(j + b.length - 1)] == b` + `|j - i| <= k` Return *the array that contains beautiful indices in **sorted order from smallest to largest***.   **Example 1:** ``` **Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 **Output:** [16,33] **Explanation:** There are 2 beautiful indices: [16,33]. - The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15. - The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result. ``` **Example 2:** ``` **Input:** s = "abcd", a = "a", b = "a", k = 4 **Output:** [0] **Explanation:** There is 1 beautiful index: [0]. - The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result. ```   **Constraints:** * `1 <= k <= s.length <= 5 * 105` * `1 <= a.length, b.length <= 5 * 105` * `s`, `a`, and `b` contain only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulIndices(*['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]\nassert my_solution.beautifulIndices(*['abcd', 'a', 'a', 4]) == [0]\nassert my_solution.beautifulIndices(*['a', 'a', 'a', 1]) == [0]\nassert my_solution.beautifulIndices(*['aba', 'a', 'a', 1]) == [0, 2]\nassert my_solution.beautifulIndices(*['nvnvt', 'eq', 'nv', 1]) == []\nassert my_solution.beautifulIndices(*['npearbvede', 'myqpb', 'pearb', 9]) == []\nassert my_solution.beautifulIndices(*['vatevavakz', 'va', 'lbda', 1]) == []\nassert my_solution.beautifulIndices(*['ithhi', 't', 'hhi', 1]) == [1]\nassert my_solution.beautifulIndices(*['osuv', 'osuv', 'wrn', 1]) == []\nassert my_solution.beautifulIndices(*['dc', 'dreec', 'dc', 2]) == []\nassert my_solution.beautifulIndices(*['jajrfw', 'rf', 'j', 3]) == [3]\nassert my_solution.beautifulIndices(*['zcvx', 'kfdvv', 'tru', 1]) == []\nassert my_solution.beautifulIndices(*['wltmqbxt', 'mqbxt', 'lt', 7]) == [3]\nassert my_solution.beautifulIndices(*['gggsytwgzg', 'sytwg', 'g', 4]) == [3]\nassert my_solution.beautifulIndices(*['diive', 'viw', 'lqqdn', 4]) == []\nassert my_solution.beautifulIndices(*['ss', 'omkdt', 's', 1]) == []\nassert my_solution.beautifulIndices(*['hfzoxcm', 'hfzo', 'ipelr', 1]) == []\nassert my_solution.beautifulIndices(*['xllimtmil', 'imt', 'iwqx', 5]) == []\nassert my_solution.beautifulIndices(*['vdyl', 'i', 'ir', 4]) == []\nassert my_solution.beautifulIndices(*['ouwpaz', 'mxre', 'pa', 5]) == []\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3303", "questionFrontendId": "3008", "questionTitle": "Find Beautiful Indices in the Given Array II", "stats": { "totalAccepted": "2.7K", "totalSubmission": "9.7K", "totalAcceptedRaw": 2698, "totalSubmissionRaw": 9738, "acRate": "27.7%" } }
LeetCode/3251
# Maximum Area of Longest Diagonal Rectangle You are given a 2D **0-indexed** integer array `dimensions`. For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`. Return *the **area** of the rectangle having the **longest** diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the **maximum** area.*   **Example 1:** ``` **Input:** dimensions = [[9,3],[8,6]] **Output:** 48 **Explanation:** For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487. For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10. So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48. ``` **Example 2:** ``` **Input:** dimensions = [[3,4],[4,3]] **Output:** 12 **Explanation:** Length of diagonal is the same for both which is 5, so maximum area = 12. ```   **Constraints:** * `1 <= dimensions.length <= 100` * `dimensions[i].length == 2` * `1 <= dimensions[i][0], dimensions[i][1] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.areaOfMaxDiagonal(*[[[9, 3], [8, 6]]]) == 48\nassert my_solution.areaOfMaxDiagonal(*[[[3, 4], [4, 3]]]) == 12\nassert my_solution.areaOfMaxDiagonal(*[[[4, 10], [4, 9], [9, 3], [10, 8]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[2, 6], [5, 1], [3, 10], [8, 4]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[3, 7], [2, 10], [3, 4], [9, 9], [5, 10]]]) == 81\nassert my_solution.areaOfMaxDiagonal(*[[[10, 4]]]) == 40\nassert my_solution.areaOfMaxDiagonal(*[[[9, 9], [1, 8], [10, 5], [2, 8], [6, 3], [7, 1]]]) == 81\nassert my_solution.areaOfMaxDiagonal(*[[[10, 3], [5, 9], [8, 3]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[2, 7], [3, 2], [3, 3], [10, 4], [5, 3], [8, 10], [8, 8], [4, 7]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[1, 10], [3, 10], [4, 4], [2, 6], [6, 3], [6, 4], [9, 1], [6, 1], [2, 3]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[4, 7], [10, 10], [3, 7], [9, 1], [5, 7], [3, 9], [10, 4], [4, 8]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[1, 1], [6, 8], [6, 9], [7, 2], [6, 8], [1, 3], [3, 1], [1, 9]]]) == 54\nassert my_solution.areaOfMaxDiagonal(*[[[6, 6], [1, 3], [8, 10], [10, 1], [3, 10], [7, 7], [10, 8]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[6, 5], [8, 6], [2, 10], [8, 1], [9, 2], [3, 5], [3, 5]]]) == 20\nassert my_solution.areaOfMaxDiagonal(*[[[5, 1], [4, 9], [9, 1], [5, 8], [2, 9], [3, 2], [10, 10], [5, 2]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[8, 3], [9, 10], [7, 7], [6, 5], [6, 9], [9, 10], [5, 10]]]) == 90\nassert my_solution.areaOfMaxDiagonal(*[[[6, 10], [8, 6], [10, 1], [7, 10], [10, 10], [9, 5]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[9, 5], [9, 2], [2, 2], [8, 9], [5, 7], [8, 10], [3, 5]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[3, 9], [9, 5]]]) == 45\nassert my_solution.areaOfMaxDiagonal(*[[[10, 10], [5, 5], [3, 2], [2, 6], [3, 1], [10, 7], [4, 8], [7, 9], [9, 9], [1, 2]]]) == 100\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3251", "questionFrontendId": "3000", "questionTitle": "Maximum Area of Longest Diagonal Rectangle", "stats": { "totalAccepted": "4.7K", "totalSubmission": "10.8K", "totalAcceptedRaw": 4667, "totalSubmissionRaw": 10779, "acRate": "43.3%" } }
LeetCode/3228
# Maximum Size of a Set After Removals You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`. You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`. Return *the **maximum** possible size of the set* `s`.   **Example 1:** ``` **Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1] **Output:** 2 **Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. It can be shown that 2 is the maximum possible size of the set s after the removals. ``` **Example 2:** ``` **Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] **Output:** 5 **Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. It can be shown that 5 is the maximum possible size of the set s after the removals. ``` **Example 3:** ``` **Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] **Output:** 6 **Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. It can be shown that 6 is the maximum possible size of the set s after the removals. ```   **Constraints:** * `n == nums1.length == nums2.length` * `1 <= n <= 2 * 104` * `n` is even. * `1 <= nums1[i], nums2[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSetSize(*[[1, 2, 1, 2], [1, 1, 1, 1]]) == 2\nassert my_solution.maximumSetSize(*[[1, 2, 3, 4, 5, 6], [2, 3, 2, 3, 2, 3]]) == 5\nassert my_solution.maximumSetSize(*[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]]) == 6\nassert my_solution.maximumSetSize(*[[1, 2, 1, 1], [1, 2, 3, 4]]) == 4\nassert my_solution.maximumSetSize(*[[1, 1, 1, 1], [12, 23, 41, 9]]) == 3\nassert my_solution.maximumSetSize(*[[12, 23, 41, 9], [1, 1, 1, 1]]) == 3\nassert my_solution.maximumSetSize(*[[9, 8, 4, 7], [5, 5, 9, 5]]) == 4\nassert my_solution.maximumSetSize(*[[8, 9], [4, 3]]) == 2\nassert my_solution.maximumSetSize(*[[7, 1], [6, 10]]) == 2\nassert my_solution.maximumSetSize(*[[10, 3], [5, 6]]) == 2\nassert my_solution.maximumSetSize(*[[3, 6], [6, 6]]) == 2\nassert my_solution.maximumSetSize(*[[5, 1], [6, 6]]) == 2\nassert my_solution.maximumSetSize(*[[10, 7], [8, 4]]) == 2\nassert my_solution.maximumSetSize(*[[10, 8, 7, 9], [7, 9, 9, 5]]) == 4\nassert my_solution.maximumSetSize(*[[1, 10, 6, 5], [3, 7, 10, 10]]) == 4\nassert my_solution.maximumSetSize(*[[5, 2, 8, 6], [7, 4, 1, 1]]) == 4\nassert my_solution.maximumSetSize(*[[2, 4, 1, 4], [10, 2, 4, 10]]) == 4\nassert my_solution.maximumSetSize(*[[5, 7], [3, 1]]) == 2\nassert my_solution.maximumSetSize(*[[1, 10, 1, 2], [9, 5, 8, 5]]) == 4\nassert my_solution.maximumSetSize(*[[9, 4], [5, 7]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3228", "questionFrontendId": "3002", "questionTitle": "Maximum Size of a Set After Removals", "stats": { "totalAccepted": "3.5K", "totalSubmission": "6.9K", "totalAcceptedRaw": 3537, "totalSubmissionRaw": 6917, "acRate": "51.1%" } }
LeetCode/3233
# Maximize the Number of Partitions After Operations You are given a **0-indexed** string `s` and an integer `k`. You are to perform the following partitioning operations until `s` is **empty**: * Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters. * **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order. **Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter. Return *an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change.*   **Example 1:** ``` **Input:** s = "accca", k = 2 **Output:** 3 **Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'. s becomes "acbca". The operations can now be performed as follows until s becomes empty: - Choose the longest prefix containing at most 2 distinct characters, "acbca". - Delete the prefix, and s becomes "bca". The number of partitions is now 1. - Choose the longest prefix containing at most 2 distinct characters, "bca". - Delete the prefix, and s becomes "a". The number of partitions is now 2. - Choose the longest prefix containing at most 2 distinct characters, "a". - Delete the prefix, and s becomes empty. The number of partitions is now 3. Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions. ``` **Example 2:** ``` **Input:** s = "aabaab", k = 3 **Output:** 1 **Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is. The operations can now be performed as follows until s becomes empty: - Choose the longest prefix containing at most 3 distinct characters, "aabaab". - Delete the prefix, and s becomes empty. The number of partitions becomes 1. Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition. ``` **Example 3:** ``` **Input:** s = "xxyz", k = 1 **Output:** 4 **Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'. s becomes "xayz". The operations can now be performed as follows until s becomes empty: - Choose the longest prefix containing at most 1 distinct character, "xayz". - Delete the prefix, and s becomes "ayz". The number of partitions is now 1. - Choose the longest prefix containing at most 1 distinct character, "ayz". - Delete the prefix, and s becomes "yz". The number of partitions is now 2. - Choose the longest prefix containing at most 1 distinct character, "yz". - Delete the prefix, and s becomes "z". The number of partitions is now 3. - Choose the longest prefix containing at most 1 distinct character, "z". - Delete the prefix, and s becomes empty. The number of partitions is now 4. Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions. ```   **Constraints:** * `1 <= s.length <= 104` * `s` consists only of lowercase English letters. * `1 <= k <= 26` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxPartitionsAfterOperations(self, s: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxPartitionsAfterOperations(*['accca', 2]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['aabaab', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['xxyz', 1]) == 4\nassert my_solution.maxPartitionsAfterOperations(*['c', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['c', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['h', 17]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['p', 13]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ab', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ba', 1]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['ba', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ca', 1]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['fh', 8]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['abb', 1]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['aca', 2]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['acb', 2]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['acb', 4]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['bab', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['cba', 1]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['cbb', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['cca', 5]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3233", "questionFrontendId": "3003", "questionTitle": "Maximize the Number of Partitions After Operations", "stats": { "totalAccepted": "1.3K", "totalSubmission": "4K", "totalAcceptedRaw": 1261, "totalSubmissionRaw": 4041, "acRate": "31.2%" } }
LeetCode/3236
# Smallest Missing Integer Greater Than Sequential Prefix Sum You are given a **0-indexed** array of integers `nums`. A prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**. Return *the **smallest** integer* `x` *missing from* `nums` *such that* `x` *is greater than or equal to the sum of the **longest** sequential prefix.*   **Example 1:** ``` **Input:** nums = [1,2,3,2,5] **Output:** 6 **Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ``` **Example 2:** ``` **Input:** nums = [3,4,5,1,12,14,13] **Output:** 15 **Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ```   **Constraints:** * `1 <= nums.length <= 50` * `1 <= nums[i] <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def missingInteger(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.missingInteger(*[[1, 2, 3, 2, 5]]) == 6\nassert my_solution.missingInteger(*[[3, 4, 5, 1, 12, 14, 13]]) == 15\nassert my_solution.missingInteger(*[[29, 30, 31, 32, 33, 34, 35, 36, 37]]) == 297\nassert my_solution.missingInteger(*[[19, 20, 21, 22]]) == 82\nassert my_solution.missingInteger(*[[18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 9]]) == 253\nassert my_solution.missingInteger(*[[4, 5, 6, 7, 8, 8, 9, 4, 3, 2, 7]]) == 30\nassert my_solution.missingInteger(*[[38]]) == 39\nassert my_solution.missingInteger(*[[1]]) == 2\nassert my_solution.missingInteger(*[[11, 12, 13]]) == 36\nassert my_solution.missingInteger(*[[47, 48, 49, 5, 3]]) == 144\nassert my_solution.missingInteger(*[[23, 24, 25, 4, 5, 1]]) == 72\nassert my_solution.missingInteger(*[[8, 9, 10, 10, 7, 8]]) == 27\nassert my_solution.missingInteger(*[[31, 32, 33, 34, 10, 8, 7, 9, 7, 9, 9, 5, 10, 1]]) == 130\nassert my_solution.missingInteger(*[[17, 18, 19, 20, 21, 22, 3, 7, 10, 10]]) == 117\nassert my_solution.missingInteger(*[[6, 7, 8, 9, 10, 8, 6, 7, 4, 1]]) == 40\nassert my_solution.missingInteger(*[[46, 8, 2, 4, 1, 4, 10, 2, 4, 10, 2, 5, 7, 3, 1]]) == 47\nassert my_solution.missingInteger(*[[37, 1, 2, 9, 5, 8, 5, 2, 9, 4]]) == 38\nassert my_solution.missingInteger(*[[31, 32, 33, 34, 1]]) == 130\nassert my_solution.missingInteger(*[[45, 46, 47, 48, 49, 10, 8, 1, 7, 4, 10, 10, 6, 6, 2]]) == 235\nassert my_solution.missingInteger(*[[13, 10, 7, 5, 7, 10, 6, 10, 2]]) == 14\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3236", "questionFrontendId": "2996", "questionTitle": "Smallest Missing Integer Greater Than Sequential Prefix Sum", "stats": { "totalAccepted": "2.7K", "totalSubmission": "7.5K", "totalAcceptedRaw": 2718, "totalSubmissionRaw": 7537, "acRate": "36.1%" } }
LeetCode/3249
# Minimum Number of Operations to Make Array XOR Equal to K You are given a **0-indexed** integer array `nums` and a positive integer `k`. You can apply the following operation on the array **any** number of times: * Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa. Return *the **minimum** number of operations required to make the bitwise* `XOR` *of **all** elements of the final array equal to* `k`. **Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number `(101)2` you can flip the fourth bit and obtain `(1101)2`.   **Example 1:** ``` **Input:** nums = [2,1,3,4], k = 1 **Output:** 2 **Explanation:** We can do the following operations: - Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. ``` **Example 2:** ``` **Input:** nums = [2,0,2,0], k = 0 **Output:** 0 **Explanation:** The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. ```   **Constraints:** * `1 <= nums.length <= 105` * `0 <= nums[i] <= 106` * `0 <= k <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def minOperations(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[2, 1, 3, 4], 1]) == 2\nassert my_solution.minOperations(*[[2, 0, 2, 0], 0]) == 0\nassert my_solution.minOperations(*[[4], 7]) == 2\nassert my_solution.minOperations(*[[3, 13, 9, 8, 5, 18, 11, 10], 13]) == 2\nassert my_solution.minOperations(*[[9, 7, 9, 14, 8, 6], 12]) == 3\nassert my_solution.minOperations(*[[13, 9, 10, 16, 11, 8, 1], 17]) == 3\nassert my_solution.minOperations(*[[12, 14], 1]) == 2\nassert my_solution.minOperations(*[[18, 18], 20]) == 2\nassert my_solution.minOperations(*[[3, 5, 1, 1], 19]) == 3\nassert my_solution.minOperations(*[[7, 0, 0, 0], 8]) == 4\nassert my_solution.minOperations(*[[13, 15, 19, 18, 2, 9, 18, 11, 0, 7], 6]) == 1\nassert my_solution.minOperations(*[[9, 15, 19, 15, 10, 15, 14, 0, 2, 5], 20]) == 1\nassert my_solution.minOperations(*[[19, 4, 19, 6, 3, 19, 14, 4, 16, 12], 4]) == 0\nassert my_solution.minOperations(*[[2, 10, 5, 5, 12, 3, 14, 6, 11, 14], 3]) == 2\nassert my_solution.minOperations(*[[11, 20], 10]) == 3\nassert my_solution.minOperations(*[[10, 12, 5, 3, 16, 0], 1]) == 2\nassert my_solution.minOperations(*[[0, 4, 4, 7, 14, 13], 1]) == 2\nassert my_solution.minOperations(*[[16, 2, 20, 13, 15, 20, 13], 16]) == 3\nassert my_solution.minOperations(*[[19, 11, 11, 0, 16, 2, 2, 0, 9], 4]) == 3\nassert my_solution.minOperations(*[[10, 17, 19, 8, 15], 19]) == 3\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3249", "questionFrontendId": "2997", "questionTitle": "Minimum Number of Operations to Make Array XOR Equal to K", "stats": { "totalAccepted": "2.5K", "totalSubmission": "3K", "totalAcceptedRaw": 2485, "totalSubmissionRaw": 2957, "acRate": "84.0%" } }
LeetCode/3239
# Minimum Number of Operations to Make X and Y Equal You are given two positive integers `x` and `y`. In one operation, you can do one of the four following operations: 1. Divide `x` by `11` if `x` is a multiple of `11`. 2. Divide `x` by `5` if `x` is a multiple of `5`. 3. Decrement `x` by `1`. 4. Increment `x` by `1`. Return *the **minimum** number of operations required to make* `x` *and* `y` equal.   **Example 1:** ``` **Input:** x = 26, y = 1 **Output:** 3 **Explanation:** We can make 26 equal to 1 by applying the following operations: 1. Decrement x by 1 2. Divide x by 5 3. Divide x by 5 It can be shown that 3 is the minimum number of operations required to make 26 equal to 1. ``` **Example 2:** ``` **Input:** x = 54, y = 2 **Output:** 4 **Explanation:** We can make 54 equal to 2 by applying the following operations: 1. Increment x by 1 2. Divide x by 11 3. Divide x by 5 4. Increment x by 1 It can be shown that 4 is the minimum number of operations required to make 54 equal to 2. ``` **Example 3:** ``` **Input:** x = 25, y = 30 **Output:** 5 **Explanation:** We can make 25 equal to 30 by applying the following operations: 1. Increment x by 1 2. Increment x by 1 3. Increment x by 1 4. Increment x by 1 5. Increment x by 1 It can be shown that 5 is the minimum number of operations required to make 25 equal to 30. ```   **Constraints:** * `1 <= x, y <= 104` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumOperationsToMakeEqual(*[26, 1]) == 3\nassert my_solution.minimumOperationsToMakeEqual(*[54, 2]) == 4\nassert my_solution.minimumOperationsToMakeEqual(*[25, 30]) == 5\nassert my_solution.minimumOperationsToMakeEqual(*[1, 1]) == 0\nassert my_solution.minimumOperationsToMakeEqual(*[1, 2]) == 1\nassert my_solution.minimumOperationsToMakeEqual(*[1, 3]) == 2\nassert my_solution.minimumOperationsToMakeEqual(*[1, 4]) == 3\nassert my_solution.minimumOperationsToMakeEqual(*[1, 5]) == 4\nassert my_solution.minimumOperationsToMakeEqual(*[1, 6]) == 5\nassert my_solution.minimumOperationsToMakeEqual(*[1, 7]) == 6\nassert my_solution.minimumOperationsToMakeEqual(*[1, 8]) == 7\nassert my_solution.minimumOperationsToMakeEqual(*[1, 9]) == 8\nassert my_solution.minimumOperationsToMakeEqual(*[1, 10]) == 9\nassert my_solution.minimumOperationsToMakeEqual(*[1, 11]) == 10\nassert my_solution.minimumOperationsToMakeEqual(*[1, 12]) == 11\nassert my_solution.minimumOperationsToMakeEqual(*[1, 13]) == 12\nassert my_solution.minimumOperationsToMakeEqual(*[1, 14]) == 13\nassert my_solution.minimumOperationsToMakeEqual(*[1, 15]) == 14\nassert my_solution.minimumOperationsToMakeEqual(*[1, 16]) == 15\nassert my_solution.minimumOperationsToMakeEqual(*[1, 17]) == 16\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3239", "questionFrontendId": "2998", "questionTitle": "Minimum Number of Operations to Make X and Y Equal", "stats": { "totalAccepted": "2.6K", "totalSubmission": "5.7K", "totalAcceptedRaw": 2596, "totalSubmissionRaw": 5655, "acRate": "45.9%" } }
LeetCode/3243
# Count the Number of Powerful Integers You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer. A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`. Return *the **total** number of powerful integers in the range* `[start..finish]`. A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including** `0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not.   **Example 1:** ``` **Input:** start = 1, finish = 6000, limit = 4, s = "124" **Output:** 5 **Explanation:** The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4. It can be shown that there are only 5 powerful integers in this range. ``` **Example 2:** ``` **Input:** start = 15, finish = 215, limit = 6, s = "10" **Output:** 2 **Explanation:** The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix. It can be shown that there are only 2 powerful integers in this range. ``` **Example 3:** ``` **Input:** start = 1000, finish = 2000, limit = 4, s = "3000" **Output:** 0 **Explanation:** All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range. ```   **Constraints:** * `1 <= start <= finish <= 1015` * `1 <= limit <= 9` * `1 <= s.length <= floor(log10(finish)) + 1` * `s` only consists of numeric digits which are at most `limit`. * `s` does not have leading zeros. Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfPowerfulInt(*[1, 6000, 4, '124']) == 5\nassert my_solution.numberOfPowerfulInt(*[15, 215, 6, '10']) == 2\nassert my_solution.numberOfPowerfulInt(*[1000, 2000, 4, '3000']) == 0\nassert my_solution.numberOfPowerfulInt(*[141, 148, 9, '9']) == 0\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '17']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '27']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '41']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '47']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '61']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '66']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '71']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '72']) == 9\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '20']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '24']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '32']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '33']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '40']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '41']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '42']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '43']) == 8\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3243", "questionFrontendId": "2999", "questionTitle": "Count the Number of Powerful Integers", "stats": { "totalAccepted": "1.7K", "totalSubmission": "4.5K", "totalAcceptedRaw": 1748, "totalSubmissionRaw": 4535, "acRate": "38.5%" } }
LeetCode/3246
# Check if Bitwise OR Has Trailing Zeros You are given an array of **positive** integers `nums`. You have to check if it is possible to select **two or more** elements in the array such that the bitwise `OR` of the selected elements has **at least** one trailing zero in its binary representation. For example, the binary representation of `5`, which is `"101"`, does not have any trailing zeros, whereas the binary representation of `4`, which is `"100"`, has two trailing zeros. Return `true` *if it is possible to select two or more elements whose bitwise* `OR` *has trailing zeros, return* `false` *otherwise*.   **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** true **Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero. ``` **Example 2:** ``` **Input:** nums = [2,4,8,16] **Output:** true **Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero. Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16). ``` **Example 3:** ``` **Input:** nums = [1,3,5,7,9] **Output:** false **Explanation:** There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR. ```   **Constraints:** * `2 <= nums.length <= 100` * `1 <= nums[i] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def hasTrailingZeros(self, nums: List[int]) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.hasTrailingZeros(*[[1, 2, 3, 4, 5]]) == True\nassert my_solution.hasTrailingZeros(*[[2, 4, 8, 16]]) == True\nassert my_solution.hasTrailingZeros(*[[1, 3, 5, 7, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 2]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 7]]) == False\nassert my_solution.hasTrailingZeros(*[[2, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[3, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[3, 4]]) == False\nassert my_solution.hasTrailingZeros(*[[3, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[4, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[4, 8]]) == True\nassert my_solution.hasTrailingZeros(*[[4, 32]]) == True\nassert my_solution.hasTrailingZeros(*[[5, 6]]) == False\nassert my_solution.hasTrailingZeros(*[[6, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[6, 8]]) == True\nassert my_solution.hasTrailingZeros(*[[7, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[7, 10]]) == False\nassert my_solution.hasTrailingZeros(*[[8, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[8, 4]]) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3246", "questionFrontendId": "2980", "questionTitle": "Check if Bitwise OR Has Trailing Zeros", "stats": { "totalAccepted": "4.3K", "totalSubmission": "5.7K", "totalAcceptedRaw": 4315, "totalSubmissionRaw": 5721, "acRate": "75.4%" } }
LeetCode/3267
# Find Longest Special Substring That Occurs Thrice I You are given a string `s` that consists of lowercase English letters. A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special. Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*. A **substring** is a contiguous **non-empty** sequence of characters within a string.   **Example 1:** ``` **Input:** s = "aaaa" **Output:** 2 **Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**". It can be shown that the maximum length achievable is 2. ``` **Example 2:** ``` **Input:** s = "abcdef" **Output:** -1 **Explanation:** There exists no special substring which occurs at least thrice. Hence return -1. ``` **Example 3:** ``` **Input:** s = "abcaba" **Output:** 1 **Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**". It can be shown that the maximum length achievable is 1. ```   **Constraints:** * `3 <= s.length <= 50` * `s` consists of only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumLength(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumLength(*['aaaa']) == 2\nassert my_solution.maximumLength(*['abcdef']) == -1\nassert my_solution.maximumLength(*['abcaba']) == 1\nassert my_solution.maximumLength(*['abcccccdddd']) == 3\nassert my_solution.maximumLength(*['aaa']) == 1\nassert my_solution.maximumLength(*['acc']) == -1\nassert my_solution.maximumLength(*['cab']) == -1\nassert my_solution.maximumLength(*['cad']) == -1\nassert my_solution.maximumLength(*['cbc']) == -1\nassert my_solution.maximumLength(*['ccc']) == 1\nassert my_solution.maximumLength(*['dca']) == -1\nassert my_solution.maximumLength(*['ddd']) == 1\nassert my_solution.maximumLength(*['fff']) == 1\nassert my_solution.maximumLength(*['ggg']) == 1\nassert my_solution.maximumLength(*['hhh']) == 1\nassert my_solution.maximumLength(*['kkk']) == 1\nassert my_solution.maximumLength(*['lll']) == 1\nassert my_solution.maximumLength(*['ooo']) == 1\nassert my_solution.maximumLength(*['ppp']) == 1\nassert my_solution.maximumLength(*['qqq']) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3267", "questionFrontendId": "2981", "questionTitle": "Find Longest Special Substring That Occurs Thrice I", "stats": { "totalAccepted": "3.5K", "totalSubmission": "7.4K", "totalAcceptedRaw": 3541, "totalSubmissionRaw": 7356, "acRate": "48.1%" } }
LeetCode/3266
# Find Longest Special Substring That Occurs Thrice II You are given a string `s` that consists of lowercase English letters. A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special. Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*. A **substring** is a contiguous **non-empty** sequence of characters within a string.   **Example 1:** ``` **Input:** s = "aaaa" **Output:** 2 **Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**". It can be shown that the maximum length achievable is 2. ``` **Example 2:** ``` **Input:** s = "abcdef" **Output:** -1 **Explanation:** There exists no special substring which occurs at least thrice. Hence return -1. ``` **Example 3:** ``` **Input:** s = "abcaba" **Output:** 1 **Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**". It can be shown that the maximum length achievable is 1. ```   **Constraints:** * `3 <= s.length <= 5 * 105` * `s` consists of only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumLength(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumLength(*['aaaa']) == 2\nassert my_solution.maximumLength(*['abcdef']) == -1\nassert my_solution.maximumLength(*['abcaba']) == 1\nassert my_solution.maximumLength(*['abcccccdddd']) == 3\nassert my_solution.maximumLength(*['acd']) == -1\nassert my_solution.maximumLength(*['bad']) == -1\nassert my_solution.maximumLength(*['bbc']) == -1\nassert my_solution.maximumLength(*['ccc']) == 1\nassert my_solution.maximumLength(*['cda']) == -1\nassert my_solution.maximumLength(*['dab']) == -1\nassert my_solution.maximumLength(*['ddd']) == 1\nassert my_solution.maximumLength(*['eee']) == 1\nassert my_solution.maximumLength(*['fff']) == 1\nassert my_solution.maximumLength(*['hhh']) == 1\nassert my_solution.maximumLength(*['jjj']) == 1\nassert my_solution.maximumLength(*['kkk']) == 1\nassert my_solution.maximumLength(*['lll']) == 1\nassert my_solution.maximumLength(*['mmm']) == 1\nassert my_solution.maximumLength(*['nnn']) == 1\nassert my_solution.maximumLength(*['ooo']) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3266", "questionFrontendId": "2982", "questionTitle": "Find Longest Special Substring That Occurs Thrice II", "stats": { "totalAccepted": "3.9K", "totalSubmission": "10.1K", "totalAcceptedRaw": 3897, "totalSubmissionRaw": 10118, "acRate": "38.5%" } }
LeetCode/3203
# Palindrome Rearrangement Queries You are given a **0-indexed** string `s` having an **even** length `n`. You are also given a **0-indexed** 2D integer array, `queries`, where `queries[i] = [ai, bi, ci, di]`. For each query `i`, you are allowed to perform the following operations: * Rearrange the characters within the **substring** `s[ai:bi]`, where `0 <= ai <= bi < n / 2`. * Rearrange the characters within the **substring** `s[ci:di]`, where `n / 2 <= ci <= di < n`. For each query, your task is to determine whether it is possible to make `s` a **palindrome** by performing the operations. Each query is answered **independently** of the others. Return *a **0-indexed** array* `answer`*, where* `answer[i] == true` *if it is possible to make* `s` *a palindrome by performing operations specified by the* `ith` *query, and* `false` *otherwise.* * A **substring** is a contiguous sequence of characters within a string. * `s[x:y]` represents the substring consisting of characters from the index `x` to index `y` in `s`, **both inclusive**.   **Example 1:** ``` **Input:** s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]] **Output:** [true,true] **Explanation:** In this example, there are two queries: In the first query: - a0 = 1, b0 = 1, c0 = 3, d0 = 5. - So, you are allowed to rearrange s[1:1] => abcabc and s[3:5] => abcabc. - To make s a palindrome, s[3:5] can be rearranged to become => abccba. - Now, s is a palindrome. So, answer[0] = true. In the second query: - a1 = 0, b1 = 2, c1 = 5, d1 = 5. - So, you are allowed to rearrange s[0:2] => abcabc and s[5:5] => abcabc. - To make s a palindrome, s[0:2] can be rearranged to become => cbaabc. - Now, s is a palindrome. So, answer[1] = true. ``` **Example 2:** ``` **Input:** s = "abbcdecbba", queries = [[0,2,7,9]] **Output:** [false] **Explanation:** In this example, there is only one query. a0 = 0, b0 = 2, c0 = 7, d0 = 9. So, you are allowed to rearrange s[0:2] => abbcdecbba and s[7:9] => abbcdecbba. It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome. So, answer[0] = false. ``` **Example 3:** ``` **Input:** s = "acbcab", queries = [[1,2,4,5]] **Output:** [true] **Explanation:** In this example, there is only one query. a0 = 1, b0 = 2, c0 = 4, d0 = 5. So, you are allowed to rearrange s[1:2] => acbcab and s[4:5] => acbcab. To make s a palindrome s[1:2] can be rearranged to become abccab. Then, s[4:5] can be rearranged to become abccba. Now, s is a palindrome. So, answer[0] = true. ```   **Constraints:** * `2 <= n == s.length <= 105` * `1 <= queries.length <= 105` * `queries[i].length == 4` * `ai == queries[i][0], bi == queries[i][1]` * `ci == queries[i][2], di == queries[i][3]` * `0 <= ai <= bi < n / 2` * `n / 2 <= ci <= di < n` * `n` is even. * `s` consists of only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canMakePalindromeQueries(*['abcabc', [[1, 1, 3, 5], [0, 2, 5, 5]]]) == [True, True]\nassert my_solution.canMakePalindromeQueries(*['abbcdecbba', [[0, 2, 7, 9]]]) == [False]\nassert my_solution.canMakePalindromeQueries(*['acbcab', [[1, 2, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bb', [[0, 0, 1, 1]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['dd', [[0, 0, 1, 1]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bdbd', [[0, 0, 2, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['eeee', [[0, 1, 2, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bbccbb', [[0, 1, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bcdbdc', [[1, 2, 3, 3]]]) == [False]\nassert my_solution.canMakePalindromeQueries(*['cababc', [[1, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['cbbbbc', [[1, 1, 5, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['cdbdbc', [[1, 2, 3, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ceacea', [[0, 2, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['daeaed', [[0, 2, 3, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ddaadd', [[0, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ddedde', [[0, 2, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ecbbce', [[0, 1, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['eczecz', [[0, 0, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['mpepem', [[0, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bccacacb', [[3, 3, 4, 7]]]) == [True]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3203", "questionFrontendId": "2983", "questionTitle": "Palindrome Rearrangement Queries", "stats": { "totalAccepted": "1.2K", "totalSubmission": "3.9K", "totalAcceptedRaw": 1243, "totalSubmissionRaw": 3947, "acRate": "31.5%" } }
LeetCode/3226
# Minimum Number Game You are given a **0-indexed** integer array `nums` of **even** length and there is also an empty array `arr`. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows: * Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same. * Now, first Bob will append the removed element in the array `arr`, and then Alice does the same. * The game continues until `nums` becomes empty. Return *the resulting array* `arr`.   **Example 1:** ``` **Input:** nums = [5,4,2,3] **Output:** [3,2,5,4] **Explanation:** In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2]. At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4]. ``` **Example 2:** ``` **Input:** nums = [2,5] **Output:** [5,2] **Explanation:** In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2]. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 100` * `nums.length % 2 == 0` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberGame(self, nums: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberGame(*[[5, 4, 2, 3]]) == [3, 2, 5, 4]\nassert my_solution.numberGame(*[[2, 5]]) == [5, 2]\nassert my_solution.numberGame(*[[4, 4, 3, 8]]) == [4, 3, 8, 4]\nassert my_solution.numberGame(*[[2, 5, 3, 8]]) == [3, 2, 8, 5]\nassert my_solution.numberGame(*[[2, 7, 9, 6, 4, 6]]) == [4, 2, 6, 6, 9, 7]\nassert my_solution.numberGame(*[[18, 26, 37, 46, 13, 33, 39, 1, 37, 16]]) == [13, 1, 18, 16, 33, 26, 37, 37, 46, 39]\nassert my_solution.numberGame(*[[17, 2, 4, 11, 14, 18]]) == [4, 2, 14, 11, 18, 17]\nassert my_solution.numberGame(*[[20, 30, 12, 3, 11, 17, 32, 12]]) == [11, 3, 12, 12, 20, 17, 32, 30]\nassert my_solution.numberGame(*[[9, 32, 6, 11, 11, 39, 18, 29, 44, 29]]) == [9, 6, 11, 11, 29, 18, 32, 29, 44, 39]\nassert my_solution.numberGame(*[[7, 2, 3, 4]]) == [3, 2, 7, 4]\nassert my_solution.numberGame(*[[8, 7, 1, 3]]) == [3, 1, 8, 7]\nassert my_solution.numberGame(*[[2, 6, 6, 6]]) == [6, 2, 6, 6]\nassert my_solution.numberGame(*[[1, 2]]) == [2, 1]\nassert my_solution.numberGame(*[[4, 1, 1, 3]]) == [1, 1, 4, 3]\nassert my_solution.numberGame(*[[13, 12, 18, 11, 15, 28, 26, 2]]) == [11, 2, 13, 12, 18, 15, 28, 26]\nassert my_solution.numberGame(*[[14, 30, 29, 3, 23, 21, 26, 23]]) == [14, 3, 23, 21, 26, 23, 30, 29]\nassert my_solution.numberGame(*[[1, 1]]) == [1, 1]\nassert my_solution.numberGame(*[[2, 1]]) == [2, 1]\nassert my_solution.numberGame(*[[12, 1, 28, 23, 2, 31, 11, 26]]) == [2, 1, 12, 11, 26, 23, 31, 28]\nassert my_solution.numberGame(*[[21, 11, 37, 1, 40, 50, 49, 45, 28, 47]]) == [11, 1, 28, 21, 40, 37, 47, 45, 50, 49]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3226", "questionFrontendId": "2974", "questionTitle": "Minimum Number Game", "stats": { "totalAccepted": "5.8K", "totalSubmission": "6.7K", "totalAcceptedRaw": 5847, "totalSubmissionRaw": 6669, "acRate": "87.7%" } }
LeetCode/3235
# Minimum Cost to Convert String I You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English letters. You are also given two **0-indexed** character arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of changing the character `original[i]` to the character `changed[i]`. You start with the string `source`. In one operation, you can pick a character `x` from the string and change it to the character `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`. Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations. If it is impossible to convert* `source` *to* `target`, *return* `-1`. **Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.   **Example 1:** ``` **Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] **Output:** 28 **Explanation:** To convert the string "abcd" to string "acbe": - Change value at index 1 from 'b' to 'c' at a cost of 5. - Change value at index 2 from 'c' to 'e' at a cost of 1. - Change value at index 2 from 'e' to 'b' at a cost of 2. - Change value at index 3 from 'd' to 'e' at a cost of 20. The total cost incurred is 5 + 1 + 2 + 20 = 28. It can be shown that this is the minimum possible cost. ``` **Example 2:** ``` **Input:** source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2] **Output:** 12 **Explanation:** To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred. ``` **Example 3:** ``` **Input:** source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000] **Output:** -1 **Explanation:** It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'. ```   **Constraints:** * `1 <= source.length == target.length <= 105` * `source`, `target` consist of lowercase English letters. * `1 <= cost.length == original.length == changed.length <= 2000` * `original[i]`, `changed[i]` are lowercase English letters. * `1 <= cost[i] <= 106` * `original[i] != changed[i]` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*['abcd', 'acbe', ['a', 'b', 'c', 'c', 'e', 'd'], ['b', 'c', 'b', 'e', 'b', 'e'], [2, 5, 5, 1, 2, 20]]) == 28\nassert my_solution.minimumCost(*['aaaa', 'bbbb', ['a', 'c'], ['c', 'b'], [1, 2]]) == 12\nassert my_solution.minimumCost(*['abcd', 'abce', ['a'], ['e'], [10000]]) == -1\nassert my_solution.minimumCost(*['aaaabadaaa', 'dbdadddbad', ['c', 'a', 'c', 'a', 'a', 'b', 'b', 'b', 'd', 'd', 'c'], ['a', 'c', 'b', 'd', 'b', 'c', 'a', 'd', 'c', 'b', 'd'], [7, 8, 11, 9, 7, 6, 4, 6, 9, 5, 9]]) == 56\nassert my_solution.minimumCost(*['aaadbdcdac', 'cdbabaddba', ['a', 'c', 'b', 'd', 'b', 'a', 'c'], ['c', 'a', 'd', 'b', 'c', 'b', 'd'], [7, 2, 1, 3, 6, 1, 7]]) == 39\nassert my_solution.minimumCost(*['aababdaacb', 'bcdcdcbdcb', ['a', 'd', 'd', 'a', 'c', 'b', 'c', 'a', 'c', 'd', 'b', 'b'], ['b', 'c', 'b', 'd', 'a', 'a', 'b', 'c', 'd', 'a', 'c', 'd'], [11, 4, 3, 2, 7, 11, 7, 6, 9, 2, 1, 7]]) == 42\nassert my_solution.minimumCost(*['aababdbddc', 'adcbbbcdba', ['a', 'd', 'b', 'a', 'd', 'c', 'd', 'b'], ['b', 'a', 'd', 'c', 'c', 'a', 'b', 'a'], [10, 6, 8, 3, 6, 10, 8, 6]]) == 72\nassert my_solution.minimumCost(*['aabbcabbdb', 'acddbabbdd', ['c', 'd', 'c', 'a', 'd', 'c', 'a', 'd', 'b', 'a', 'b'], ['d', 'b', 'a', 'c', 'c', 'b', 'b', 'a', 'd', 'd', 'c'], [5, 3, 8, 10, 9, 7, 8, 7, 5, 1, 10]]) == 32\nassert my_solution.minimumCost(*['aabbddccbc', 'abbbaabaca', ['a', 'b', 'c', 'b', 'a', 'd'], ['d', 'c', 'b', 'd', 'b', 'b'], [3, 8, 7, 6, 7, 10]]) == -1\nassert my_solution.minimumCost(*['aabdbaabaa', 'bdaacabcab', ['b', 'd', 'd', 'a', 'c', 'c', 'a', 'd', 'a', 'b'], ['c', 'c', 'b', 'd', 'b', 'd', 'b', 'a', 'c', 'a'], [9, 1, 7, 9, 2, 1, 3, 8, 8, 2]]) == 43\nassert my_solution.minimumCost(*['aacacaaccd', 'dadaacaabd', ['c', 'c', 'a', 'a', 'd', 'b', 'd', 'd'], ['b', 'd', 'd', 'b', 'b', 'c', 'c', 'a'], [7, 8, 9, 11, 4, 6, 9, 10]]) == 77\nassert my_solution.minimumCost(*['aacbabbacc', 'adbdbcbdaa', ['c', 'b', 'a', 'b', 'a', 'c', 'd', 'c', 'd'], ['b', 'c', 'b', 'd', 'd', 'a', 'b', 'd', 'c'], [2, 6, 7, 4, 7, 4, 3, 5, 6]]) == 41\nassert my_solution.minimumCost(*['aacbbabdad', 'ddadcababd', ['d', 'b', 'c', 'a', 'b', 'c', 'd', 'c', 'b', 'a', 'a'], ['c', 'd', 'd', 'b', 'c', 'b', 'b', 'a', 'a', 'c', 'd'], [7, 10, 4, 2, 7, 4, 4, 4, 6, 2, 8]]) == 45\nassert my_solution.minimumCost(*['aacbbbbcab', 'cdacdcddac', ['b', 'd', 'c', 'c', 'b', 'a'], ['c', 'c', 'b', 'a', 'a', 'd'], [4, 7, 9, 11, 3, 4]]) == 67\nassert my_solution.minimumCost(*['aacbcabcad', 'bbcadddcdd', ['b', 'a', 'd', 'a', 'b', 'c', 'a', 'd', 'd', 'b'], ['d', 'b', 'b', 'd', 'c', 'a', 'c', 'c', 'a', 'a'], [7, 7, 9, 8, 6, 3, 8, 2, 1, 5]]) == 53\nassert my_solution.minimumCost(*['aacbdbcdca', 'bbbdbcaacd', ['a', 'c', 'b', 'd', 'd', 'a', 'c', 'd'], ['c', 'b', 'c', 'c', 'b', 'd', 'd', 'a'], [9, 5, 4, 1, 2, 4, 7, 1]]) == 47\nassert my_solution.minimumCost(*['aadbbcdbbd', 'badddbdbac', ['c', 'd', 'c', 'd', 'b', 'a'], ['b', 'b', 'a', 'a', 'a', 'd'], [11, 4, 7, 8, 5, 2]]) == -1\nassert my_solution.minimumCost(*['aadbccbddd', 'cacdbabadc', ['d', 'b', 'c', 'd', 'a', 'a', 'c', 'b'], ['c', 'c', 'b', 'b', 'b', 'd', 'a', 'a'], [5, 8, 7, 2, 4, 7, 1, 5]]) == 46\nassert my_solution.minimumCost(*['aadbddcabd', 'bdcdccbada', ['d', 'a', 'a', 'b', 'd', 'b'], ['b', 'c', 'd', 'c', 'a', 'd'], [6, 10, 5, 8, 11, 4]]) == -1\nassert my_solution.minimumCost(*['aaddadccad', 'cbaaadbcba', ['c', 'a', 'a', 'd', 'c', 'c', 'b', 'b', 'a', 'd'], ['a', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'b', 'b'], [1, 10, 2, 8, 9, 1, 9, 10, 5, 1]]) == 44\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3235", "questionFrontendId": "2976", "questionTitle": "Minimum Cost to Convert String I", "stats": { "totalAccepted": "3K", "totalSubmission": "6.7K", "totalAcceptedRaw": 3023, "totalSubmissionRaw": 6678, "acRate": "45.3%" } }
LeetCode/3238
# Minimum Cost to Convert String II You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English characters. You are also given two **0-indexed** string arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of converting the string `original[i]` to the string `changed[i]`. You start with the string `source`. In one operation, you can pick a **substring** `x` from the string, and change it to `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`. You are allowed to do **any** number of operations, but any pair of operations must satisfy **either** of these two conditions: * The substrings picked in the operations are `source[a..b]` and `source[c..d]` with either `b < c` **or** `d < a`. In other words, the indices picked in both operations are **disjoint**. * The substrings picked in the operations are `source[a..b]` and `source[c..d]` with `a == c` **and** `b == d`. In other words, the indices picked in both operations are **identical**. Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations*. *If it is impossible to convert* `source` *to* `target`, *return* `-1`. **Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.   **Example 1:** ``` **Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] **Output:** 28 **Explanation:** To convert "abcd" to "acbe", do the following operations: - Change substring source[1..1] from "b" to "c" at a cost of 5. - Change substring source[2..2] from "c" to "e" at a cost of 1. - Change substring source[2..2] from "e" to "b" at a cost of 2. - Change substring source[3..3] from "d" to "e" at a cost of 20. The total cost incurred is 5 + 1 + 2 + 20 = 28. It can be shown that this is the minimum possible cost. ``` **Example 2:** ``` **Input:** source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5] **Output:** 9 **Explanation:** To convert "abcdefgh" to "acdeeghh", do the following operations: - Change substring source[1..3] from "bcd" to "cde" at a cost of 1. - Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation. - Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation. The total cost incurred is 1 + 3 + 5 = 9. It can be shown that this is the minimum possible cost. ``` **Example 3:** ``` **Input:** source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578] **Output:** -1 **Explanation:** It is impossible to convert "abcdefgh" to "addddddd". If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation. If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation. ```   **Constraints:** * `1 <= source.length == target.length <= 1000` * `source`, `target` consist only of lowercase English characters. * `1 <= cost.length == original.length == changed.length <= 100` * `1 <= original[i].length == changed[i].length <= source.length` * `original[i]`, `changed[i]` consist only of lowercase English characters. * `original[i] != changed[i]` * `1 <= cost[i] <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*['abcd', 'acbe', ['a', 'b', 'c', 'c', 'e', 'd'], ['b', 'c', 'b', 'e', 'b', 'e'], [2, 5, 5, 1, 2, 20]]) == 28\nassert my_solution.minimumCost(*['abcdefgh', 'acdeeghh', ['bcd', 'fgh', 'thh'], ['cde', 'thh', 'ghh'], [1, 3, 5]]) == 9\nassert my_solution.minimumCost(*['abcdefgh', 'addddddd', ['bcd', 'defgh'], ['ddd', 'ddddd'], [100, 1578]]) == -1\nassert my_solution.minimumCost(*['a', 'b', ['a'], ['b'], [1]]) == 1\nassert my_solution.minimumCost(*['a', 'c', ['a', 'b', 'a', 'a'], ['b', 'c', 'c', 'c'], [1, 2, 10, 1]]) == 1\nassert my_solution.minimumCost(*['a', 'd', ['a'], ['b'], [1]]) == -1\nassert my_solution.minimumCost(*['ajhpd', 'djjdc', ['hpd', 'iyk', 'qzd', 'hpi', 'aic', 'znh', 'cea', 'fug', 'wir', 'kwu', 'yjo', 'rzi', 'a', 'n', 'f', 'q', 'u', 'w', 'x', 'i', 'x', 's', 'o', 'u'], ['iyk', 'qzd', 'hpi', 'aic', 'znh', 'cea', 'fug', 'wir', 'kwu', 'yjo', 'rzi', 'jdc', 'n', 'f', 'q', 'u', 'w', 'x', 'i', 'x', 's', 'o', 'u', 'd'], [80257, 95140, 96349, 89449, 81714, 5859, 96734, 96109, 41211, 99975, 57611, 32644, 82896, 22164, 99889, 98061, 95403, 90922, 64031, 94558, 58418, 99717, 96588, 88286]]) == 1264348\nassert my_solution.minimumCost(*['bzshh', 'mlosr', ['shh', 'wbs', 'hup', 'sab', 'csp', 'tel', 'mhq', 'ezp', 'eap', 'fqb', 'iea', 'cej', 'b', 'v', 'g', 'e', 'd', 'x', 'q', 'v', 'g', 'x', 'u', 'm', 'u', 'q', 'z', 'q', 'n', 'p'], ['wbs', 'hup', 'sab', 'csp', 'tel', 'mhq', 'ezp', 'eap', 'fqb', 'iea', 'cej', 'osr', 'v', 'g', 'e', 'd', 'x', 'q', 'v', 'g', 'x', 'u', 'm', 'u', 'q', 'm', 'q', 'n', 'p', 'l'], [69565, 82190, 75322, 85502, 89675, 98424, 86521, 85852, 32285, 99465, 82356, 97775, 30173, 88276, 82158, 40971, 75361, 65284, 89814, 68219, 44777, 95082, 99781, 99072, 74513, 49667, 99719, 93132, 99203, 54171]]) == 1589277\nassert my_solution.minimumCost(*['fjybg', 'apyyt', ['bg', 'xr', 'cc', 'ip', 'vq', 'po', 'ym', 'rh', 'vw', 'lf', 'lo', 'ee', 'qv', 'yr', 'f', 'w', 'i', 'u', 'g', 'a', 'e', 'f', 's', 'r', 'p', 'j', 'o', 'g', 'i', 'u'], ['xr', 'cc', 'ip', 'vq', 'po', 'ym', 'rh', 'vw', 'lf', 'lo', 'ee', 'qv', 'yr', 'yt', 'w', 'i', 'u', 'g', 'a', 'e', 'f', 's', 'r', 'p', 'a', 'o', 'g', 'i', 'u', 'p'], [97733, 90086, 87125, 85361, 75644, 46301, 21616, 79538, 52507, 95884, 79353, 61127, 58665, 96031, 95035, 12116, 41158, 91096, 47819, 88522, 25493, 80186, 66981, 87597, 56691, 86820, 89031, 99954, 41271, 39699]]) == 1628332\nassert my_solution.minimumCost(*['htkdz', 'oaqaw', ['kdz', 'yyv', 'cde', 'oks', 'fzu', 'hkm', 'dmb', 'arh', 'lix', 'eij', 'ksv', 't', 'u', 'f', 'w', 'b', 'u', 'v', 'h', 'o', 'b', 'o', 'p', 'z', 'h', 'w', 't', 'p', 'x', 'y'], ['yyv', 'cde', 'oks', 'fzu', 'hkm', 'dmb', 'arh', 'lix', 'eij', 'ksv', 'qaw', 'u', 'f', 'w', 'b', 'u', 'v', 'h', 'o', 'b', 'o', 'p', 'z', 'a', 'w', 't', 'p', 'x', 'y', 'o'], [90243, 86765, 84893, 80924, 85915, 42672, 99995, 99429, 88069, 84925, 71184, 54929, 83245, 72750, 87238, 30151, 58657, 94445, 98330, 90683, 83980, 96513, 75536, 95212, 79301, 74556, 94836, 94781, 76273, 86147]]) == 1278928\nassert my_solution.minimumCost(*['iktgh', 'srwcg', ['h', 'e', 'y', 'g', 'q', 'y', 't', 'n', 'r', 'e', 'i', 'x', 'iktg', 'xwgv', 'ddrp', 'saxt', 'rvdq', 'moiy', 'loln', 'bkgj', 'jjgi', 'vatf'], ['e', 'y', 'g', 'q', 'y', 't', 'n', 'r', 'e', 'i', 'x', 'g', 'xwgv', 'ddrp', 'saxt', 'rvdq', 'moiy', 'loln', 'bkgj', 'jjgi', 'vatf', 'srwc'], [70839, 75691, 55903, 82637, 97906, 86576, 92197, 74464, 86638, 61531, 80041, 52732, 96361, 39766, 74988, 59857, 69068, 89990, 74293, 82838, 37650, 26885]]) == 854129\nassert my_solution.minimumCost(*['imbin', 'dmhjv', ['bin', 'pwo', 'fwt', 'xwi', 'xal', 'uqt', 'lmp', 'erq', 'kac', 'dgv', 'qgh', 'rei', 'nbx', 'i', 'u', 'b', 'v', 'c', 'q', 'p', 'f', 'q', 'v', 't', 'n', 'b'], ['pwo', 'fwt', 'xwi', 'xal', 'uqt', 'lmp', 'erq', 'kac', 'dgv', 'qgh', 'rei', 'nbx', 'hjv', 'u', 'b', 'v', 'c', 'q', 'p', 'f', 'q', 'v', 't', 'n', 'b', 'd'], [47307, 30907, 64949, 35735, 84284, 83424, 69858, 92113, 51405, 69242, 97014, 91471, 78165, 92733, 79709, 99573, 78055, 20529, 85549, 90496, 60896, 75354, 50630, 49094, 41380, 46980]]) == 1115296\nassert my_solution.minimumCost(*['jegbx', 'ezhfc', ['egbx', 'hrbf', 'twne', 'snjd', 'ysrf', 'qzqg', 'rcll', 'ekvz', 'inpr', 'frxs', 'xcww', 'unsw', 'vdug', 'ycvs', 'j', 'v', 'j', 'y', 'n', 'q', 'w', 'a', 'z', 'g', 'b', 'd'], ['hrbf', 'twne', 'snjd', 'ysrf', 'qzqg', 'rcll', 'ekvz', 'inpr', 'frxs', 'xcww', 'unsw', 'vdug', 'ycvs', 'zhfc', 'v', 'j', 'y', 'n', 'q', 'w', 'a', 'z', 'g', 'b', 'd', 'e'], [50682, 89150, 91153, 85032, 97960, 96862, 81138, 86570, 77628, 45200, 44955, 70845, 99254, 80325, 91331, 95349, 84374, 94177, 53994, 94284, 79531, 92353, 60384, 100000, 93152, 19787]]) == 1868790\nassert my_solution.minimumCost(*['jpyjj', 'jqnfp', ['j', 'i', 'q', 'u', 'y', 'w', 'd', 'a', 'h', 's', 'i', 'y', 'w', 'pyj', 'qng', 'lrn', 'nrm', 'tvn', 'fei', 'fpj', 'qlw', 'lrb', 'ufu', 'kll', 'nqp'], ['i', 'q', 'u', 'y', 'w', 'd', 'a', 'h', 's', 'i', 'y', 'w', 'p', 'qng', 'lrn', 'nrm', 'tvn', 'fei', 'fpj', 'qlw', 'lrb', 'ufu', 'kll', 'nqp', 'qnf'], [62657, 90954, 55348, 88767, 87756, 55487, 49700, 51801, 94877, 81661, 99027, 91814, 62872, 25235, 62153, 96875, 12009, 85321, 68993, 75866, 72888, 96411, 78568, 83975, 60456]]) == 1131062\nassert my_solution.minimumCost(*['nialx', 'qvqfl', ['x', 'r', 'a', 'x', 'c', 'w', 's', 'a', 'n', 'e', 'q', 'p', 'v', 'k', 'o', 'ial', 'qzu', 'owr', 'kyq', 'ukk', 'gpq', 'jdp', 'dus', 'eng', 'btu', 'cbp'], ['r', 'a', 'x', 'c', 'w', 's', 'a', 'l', 'e', 'q', 'p', 'v', 'k', 'o', 'q', 'qzu', 'owr', 'kyq', 'ukk', 'gpq', 'jdp', 'dus', 'eng', 'btu', 'cbp', 'vqf'], [64196, 95812, 96987, 40860, 41507, 99365, 99208, 53062, 44440, 65136, 95625, 86166, 61798, 84228, 92555, 97678, 97576, 19742, 92989, 98167, 68457, 82411, 39923, 81778, 87792, 7523]]) == 1096682\nassert my_solution.minimumCost(*['pagpe', 'xacng', ['gpe', 'owt', 'wyv', 'eba', 'xgp', 'uny', 'ibc', 'usb', 'mzj', 'wdo', 'lyc', 'eof', 'oci', 'p', 'e', 'p', 'u', 'h', 'w', 'i', 'l'], ['owt', 'wyv', 'eba', 'xgp', 'uny', 'ibc', 'usb', 'mzj', 'wdo', 'lyc', 'eof', 'oci', 'cng', 'e', 'p', 'u', 'h', 'w', 'i', 'l', 'x'], [56193, 92982, 90717, 67407, 91949, 77752, 88841, 43278, 51149, 43646, 99585, 41038, 84989, 57688, 64474, 96532, 77511, 37031, 90895, 62831, 87342]]) == 1381668\nassert my_solution.minimumCost(*['aaabbebbbhbbbbebaaeh', 'hhbebebbahhhehhbbhee', ['a', 'b', 'b', 'b', 'e', 'a', 'h'], ['b', 'e', 'a', 'h', 'h', 'h', 'e'], [9, 8, 5, 9, 3, 7, 9]]) == 99\nassert my_solution.minimumCost(*['abbbeebebehbbhhhbeab', 'aehebehebaeaebbaahhb', ['b', 'b', 'e', 'e', 'h', 'h', 'h', 'b', 'e', 'a'], ['e', 'h', 'b', 'a', 'e', 'b', 'a', 'a', 'h', 'h'], [10, 2, 9, 10, 7, 8, 10, 10, 6, 9]]) == 118\nassert my_solution.minimumCost(*['abebbeeeahhbahaehaab', 'eebhheeahaahbaebaaea', ['a', 'b', 'e', 'a', 'h', 'a', 'e', 'b'], ['e', 'h', 'a', 'h', 'a', 'b', 'b', 'a'], [6, 8, 5, 10, 10, 10, 10, 8]]) == 149\nassert my_solution.minimumCost(*['aeaaebhbhehbeehbehea', 'babehheaaeebeebahhba', ['a', 'e', 'a', 'e', 'b', 'h', 'b', 'h', 'h', 'e'], ['b', 'a', 'e', 'h', 'h', 'e', 'a', 'a', 'b', 'b'], [8, 6, 3, 8, 7, 9, 9, 10, 10, 5]]) == 109\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3238", "questionFrontendId": "2977", "questionTitle": "Minimum Cost to Convert String II", "stats": { "totalAccepted": "2K", "totalSubmission": "6.9K", "totalAcceptedRaw": 1993, "totalSubmissionRaw": 6946, "acRate": "28.7%" } }
LeetCode/3252
# Count the Number of Incremovable Subarrays I You are given a **0-indexed** array of **positive** integers `nums`. A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing. Return *the total number of **incremovable** subarrays of* `nums`. **Note** that an empty array is considered strictly increasing. A **subarray** is a contiguous non-empty sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 10 **Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray. ``` **Example 2:** ``` **Input:** nums = [6,5,7,8] **Output:** 7 **Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums. ``` **Example 3:** ``` **Input:** nums = [8,7,6,6] **Output:** 3 **Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing. ```   **Constraints:** * `1 <= nums.length <= 50` * `1 <= nums[i] <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.incremovableSubarrayCount(*[[1, 2, 3, 4]]) == 10\nassert my_solution.incremovableSubarrayCount(*[[6, 5, 7, 8]]) == 7\nassert my_solution.incremovableSubarrayCount(*[[8, 7, 6, 6]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[2]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[3]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[5]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[6]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[7]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[8]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[9]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[10]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[1, 2]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1, 4]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1, 8]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[2, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 4]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 8]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 10]]) == 3\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3252", "questionFrontendId": "2970", "questionTitle": "Count the Number of Incremovable Subarrays I", "stats": { "totalAccepted": "2.4K", "totalSubmission": "4K", "totalAcceptedRaw": 2420, "totalSubmissionRaw": 4010, "acRate": "60.3%" } }
LeetCode/3262
# Find Polygon With the Largest Perimeter You are given an array of **positive** integers `nums` of length `n`. A **polygon** is a closed plane figure that has at least `3` sides. The **longest side** of a polygon is **smaller** than the sum of its other sides. Conversely, if you have `k` (`k >= 3`) **positive** real numbers `a1`, `a2`, `a3`, ..., `ak` where `a1 <= a2 <= a3 <= ... <= ak` **and** `a1 + a2 + a3 + ... + ak-1 > ak`, then there **always** exists a polygon with `k` sides whose lengths are `a1`, `a2`, `a3`, ..., `ak`. The **perimeter** of a polygon is the sum of lengths of its sides. Return *the **largest** possible **perimeter** of a **polygon** whose sides can be formed from* `nums`, *or* `-1` *if it is not possible to create a polygon*.   **Example 1:** ``` **Input:** nums = [5,5,5] **Output:** 15 **Explanation:** The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. ``` **Example 2:** ``` **Input:** nums = [1,12,1,2,5,50,3] **Output:** 12 **Explanation:** The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. ``` **Example 3:** ``` **Input:** nums = [5,5,50] **Output:** -1 **Explanation:** There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5. ```   **Constraints:** * `3 <= n <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def largestPerimeter(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.largestPerimeter(*[[5, 5, 5]]) == 15\nassert my_solution.largestPerimeter(*[[1, 12, 1, 2, 5, 50, 3]]) == 12\nassert my_solution.largestPerimeter(*[[5, 5, 50]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 1]]) == 3\nassert my_solution.largestPerimeter(*[[1, 1, 2]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 3]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 2]]) == 5\nassert my_solution.largestPerimeter(*[[1, 2, 3]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 2]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 3]]) == 7\nassert my_solution.largestPerimeter(*[[1, 3, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 4, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 4, 2]]) == -1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3262", "questionFrontendId": "2971", "questionTitle": "Find Polygon With the Largest Perimeter", "stats": { "totalAccepted": "2.8K", "totalSubmission": "4.1K", "totalAcceptedRaw": 2813, "totalSubmissionRaw": 4118, "acRate": "68.3%" } }
LeetCode/3248
# Count the Number of Incremovable Subarrays II You are given a **0-indexed** array of **positive** integers `nums`. A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing. Return *the total number of **incremovable** subarrays of* `nums`. **Note** that an empty array is considered strictly increasing. A **subarray** is a contiguous non-empty sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 10 **Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray. ``` **Example 2:** ``` **Input:** nums = [6,5,7,8] **Output:** 7 **Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums. ``` **Example 3:** ``` **Input:** nums = [8,7,6,6] **Output:** 3 **Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.incremovableSubarrayCount(*[[1, 2, 3, 4]]) == 10\nassert my_solution.incremovableSubarrayCount(*[[6, 5, 7, 8]]) == 7\nassert my_solution.incremovableSubarrayCount(*[[8, 7, 6, 6]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[2]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[3]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[5]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[6]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[7]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[8]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[9]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[10]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[7, 3]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[8, 5]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[8, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[9, 9]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[5, 5, 6]]) == 5\nassert my_solution.incremovableSubarrayCount(*[[6, 7, 5]]) == 4\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3248", "questionFrontendId": "2972", "questionTitle": "Count the Number of Incremovable Subarrays II", "stats": { "totalAccepted": "2K", "totalSubmission": "3.8K", "totalAcceptedRaw": 1974, "totalSubmissionRaw": 3774, "acRate": "52.3%" } }
LeetCode/3227
# Find Missing and Repeated Values You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range `[1, n2]`. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`. Return *a **0-indexed** integer array* `ans` *of size* `2` *where* `ans[0]` *equals to* `a` *and* `ans[1]` *equals to* `b`*.*   **Example 1:** ``` **Input:** grid = [[1,3],[2,2]] **Output:** [2,4] **Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4]. ``` **Example 2:** ``` **Input:** grid = [[9,1,7],[8,9,2],[3,4,6]] **Output:** [9,5] **Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5]. ```   **Constraints:** * `2 <= n == grid.length == grid[i].length <= 50` * `1 <= grid[i][j] <= n * n` * For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members. * For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members. * For all `x` that `1 <= x <= n * n` except two of them there is exatly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`. Please make sure your answer follows the type signature below: ```python3 class Solution: def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 3], [2, 2]]]) == [2, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[9, 1, 7], [8, 9, 2], [3, 4, 6]]]) == [9, 5]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 1], [3, 2]]]) == [1, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 1], [3, 4]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [1, 3]]]) == [1, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [1, 4]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [3, 3]]]) == [3, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 1]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 2]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 4]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [1, 3]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [2, 1]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [3, 1]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [3, 4]]]) == [4, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [4, 2]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 1], [4, 2]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 1], [4, 4]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 2], [3, 4]]]) == [2, 1]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 2], [4, 1]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 3], [2, 1]]]) == [2, 4]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3227", "questionFrontendId": "2965", "questionTitle": "Find Missing and Repeated Values", "stats": { "totalAccepted": "5.2K", "totalSubmission": "6.6K", "totalAcceptedRaw": 5216, "totalSubmissionRaw": 6569, "acRate": "79.4%" } }
LeetCode/3241
# Divide Array Into Arrays With Max Difference You are given an integer array `nums` of size `n` and a positive integer `k`. Divide the array into one or more arrays of size `3` satisfying the following conditions: * **Each** element of `nums` should be in **exactly** one array. * The difference between **any** two elements in one array is less than or equal to `k`. Return *a* **2D** *array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them.*   **Example 1:** ``` **Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2 **Output:** [[1,1,3],[3,4,5],[7,8,9]] **Explanation:** We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9]. The difference between any two elements in each array is less than or equal to 2. Note that the order of elements is not important. ``` **Example 2:** ``` **Input:** nums = [1,3,3,2,7,3], k = 3 **Output:** [] **Explanation:** It is not possible to divide the array satisfying all the conditions. ```   **Constraints:** * `n == nums.length` * `1 <= n <= 105` * `n` is a multiple of `3`. * `1 <= nums[i] <= 105` * `1 <= k <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def divideArray(self, nums: List[int], k: int) -> List[List[int]]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.divideArray(*[[1, 3, 4, 8, 7, 9, 3, 5, 1], 2]) == [[1, 1, 3], [3, 4, 5], [7, 8, 9]]\nassert my_solution.divideArray(*[[1, 3, 3, 2, 7, 3], 3]) == []\nassert my_solution.divideArray(*[[4, 2, 9, 8, 2, 12, 7, 12, 10, 5, 8, 5, 5, 7, 9, 2, 5, 11], 14]) == [[2, 2, 2], [4, 5, 5], [5, 5, 7], [7, 8, 8], [9, 9, 10], [11, 12, 12]]\nassert my_solution.divideArray(*[[33, 26, 4, 18, 16, 24, 24, 15, 8, 18, 34, 20, 24, 16, 3], 16]) == [[3, 4, 8], [15, 16, 16], [18, 18, 20], [24, 24, 24], [26, 33, 34]]\nassert my_solution.divideArray(*[[6, 1, 8, 8, 5, 8, 5, 9, 8, 9, 5, 8, 3, 4, 6], 7]) == [[1, 3, 4], [5, 5, 5], [6, 6, 8], [8, 8, 8], [8, 9, 9]]\nassert my_solution.divideArray(*[[20, 21, 34, 3, 19, 2, 23, 32, 20, 17, 14, 13, 19, 20, 6], 15]) == [[2, 3, 6], [13, 14, 17], [19, 19, 20], [20, 20, 21], [23, 32, 34]]\nassert my_solution.divideArray(*[[6, 10, 5, 12, 7, 11, 6, 6, 12, 12, 11, 7], 2]) == [[5, 6, 6], [6, 7, 7], [10, 11, 11], [12, 12, 12]]\nassert my_solution.divideArray(*[[12, 15, 26, 7, 10, 13, 15, 5, 27, 16, 14, 15], 18]) == [[5, 7, 10], [12, 13, 14], [15, 15, 15], [16, 26, 27]]\nassert my_solution.divideArray(*[[12, 7, 13, 10, 7, 19, 11, 23, 3, 3, 7, 9], 16]) == [[3, 3, 7], [7, 7, 9], [10, 11, 12], [13, 19, 23]]\nassert my_solution.divideArray(*[[19, 3, 23, 4, 8, 1, 1, 3, 26], 7]) == [[1, 1, 3], [3, 4, 8], [19, 23, 26]]\nassert my_solution.divideArray(*[[11, 13, 24, 11, 9, 23, 16, 19, 13], 8]) == [[9, 11, 11], [13, 13, 16], [19, 23, 24]]\nassert my_solution.divideArray(*[[6, 12, 21, 12, 6, 12, 25, 20, 15, 22, 11, 19, 8, 4, 18, 26, 17, 18, 12, 5, 8], 11]) == [[4, 5, 6], [6, 8, 8], [11, 12, 12], [12, 12, 15], [17, 18, 18], [19, 20, 21], [22, 25, 26]]\nassert my_solution.divideArray(*[[15, 17, 14, 3, 25, 15, 11, 25, 15, 16, 12, 18], 10]) == [[3, 11, 12], [14, 15, 15], [15, 16, 17], [18, 25, 25]]\nassert my_solution.divideArray(*[[16, 20, 16, 19, 20, 13, 14, 20, 14], 10]) == [[13, 14, 14], [16, 16, 19], [20, 20, 20]]\nassert my_solution.divideArray(*[[2, 13, 15, 14, 18, 15, 3, 13, 2], 1]) == []\nassert my_solution.divideArray(*[[1, 14, 20, 7, 17, 2, 14, 1, 8], 11]) == [[1, 1, 2], [7, 8, 14], [14, 17, 20]]\nassert my_solution.divideArray(*[[8, 12, 19, 8, 9, 19, 9, 19, 9, 8, 6, 9, 6, 6, 12], 3]) == [[6, 6, 6], [8, 8, 8], [9, 9, 9], [9, 12, 12], [19, 19, 19]]\nassert my_solution.divideArray(*[[18, 16, 17, 19, 12, 25, 11, 27, 11, 32, 32, 17], 20]) == [[11, 11, 12], [16, 17, 17], [18, 19, 25], [27, 32, 32]]\nassert my_solution.divideArray(*[[21, 11, 24, 20, 17, 13, 7, 20, 20, 16, 24, 20, 12, 17, 16, 15, 7, 7, 18, 15, 20], 6]) == [[7, 7, 7], [11, 12, 13], [15, 15, 16], [16, 17, 17], [18, 20, 20], [20, 20, 20], [21, 24, 24]]\nassert my_solution.divideArray(*[[6, 7, 7, 6, 7, 6], 13]) == [[6, 6, 6], [7, 7, 7]]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3241", "questionFrontendId": "2966", "questionTitle": "Divide Array Into Arrays With Max Difference", "stats": { "totalAccepted": "4.7K", "totalSubmission": "7K", "totalAcceptedRaw": 4670, "totalSubmissionRaw": 7035, "acRate": "66.4%" } }
LeetCode/3229
# Minimum Cost to Make Array Equalindromic You are given a **0-indexed** integer array `nums` having length `n`. You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**: * Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`. * Add `|nums[i] - x|` to the total cost. * Change the value of `nums[i]` to `x`. A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers. An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than `109`. Return *an integer denoting the **minimum** possible total cost to make* `nums` ***equalindromic** by performing any number of special moves.*   **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 6 **Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6. It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost. ``` **Example 2:** ``` **Input:** nums = [10,12,13,14,15] **Output:** 11 **Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11. It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost. ``` **Example 3:** ``` **Input:** nums = [22,33,22,33,22] **Output:** 22 **Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22. It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost. ```   **Constraints:** * `1 <= n <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 2, 3, 4, 5]]) == 6\nassert my_solution.minimumCost(*[[10, 12, 13, 14, 15]]) == 11\nassert my_solution.minimumCost(*[[22, 33, 22, 33, 22]]) == 22\nassert my_solution.minimumCost(*[[1]]) == 0\nassert my_solution.minimumCost(*[[2]]) == 0\nassert my_solution.minimumCost(*[[3]]) == 0\nassert my_solution.minimumCost(*[[4]]) == 0\nassert my_solution.minimumCost(*[[5]]) == 0\nassert my_solution.minimumCost(*[[2, 1]]) == 1\nassert my_solution.minimumCost(*[[3, 1]]) == 2\nassert my_solution.minimumCost(*[[3, 2]]) == 1\nassert my_solution.minimumCost(*[[4, 1]]) == 3\nassert my_solution.minimumCost(*[[4, 2]]) == 2\nassert my_solution.minimumCost(*[[4, 3]]) == 1\nassert my_solution.minimumCost(*[[5, 1]]) == 4\nassert my_solution.minimumCost(*[[5, 2]]) == 3\nassert my_solution.minimumCost(*[[5, 3]]) == 2\nassert my_solution.minimumCost(*[[5, 4]]) == 1\nassert my_solution.minimumCost(*[[3, 2, 1]]) == 2\nassert my_solution.minimumCost(*[[4, 2, 1]]) == 3\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3229", "questionFrontendId": "2967", "questionTitle": "Minimum Cost to Make Array Equalindromic", "stats": { "totalAccepted": "3.6K", "totalSubmission": "16.1K", "totalAcceptedRaw": 3637, "totalSubmissionRaw": 16137, "acRate": "22.5%" } }
LeetCode/3196
# Apply Operations to Maximize Frequency Score You are given a **0-indexed** integer array `nums` and an integer `k`. You can perform the following operation on the array **at most** `k` times: * Choose any index `i` from the array and **increase** or **decrease** `nums[i]` by `1`. The score of the final array is the **frequency** of the most frequent element in the array. Return *the **maximum** score you can achieve*. The frequency of an element is the number of occurences of that element in the array.   **Example 1:** ``` **Input:** nums = [1,2,6,4], k = 3 **Output:** 3 **Explanation:** We can do the following operations on the array: - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2]. The element 2 is the most frequent in the final array so our score is 3. It can be shown that we cannot achieve a better score. ``` **Example 2:** ``` **Input:** nums = [1,4,4,2,4], k = 0 **Output:** 3 **Explanation:** We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `0 <= k <= 1014` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxFrequencyScore(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxFrequencyScore(*[[1, 2, 6, 4], 3]) == 3\nassert my_solution.maxFrequencyScore(*[[1, 4, 4, 2, 4], 0]) == 3\nassert my_solution.maxFrequencyScore(*[[3, 20, 13, 2, 3, 15, 24, 19, 8, 13, 19, 20, 21], 45]) == 10\nassert my_solution.maxFrequencyScore(*[[13, 22, 29, 21, 13, 17, 5, 2, 27, 6, 10, 4, 23, 29, 27], 117]) == 14\nassert my_solution.maxFrequencyScore(*[[27, 8, 30, 3, 13, 28, 7, 14, 21, 19, 24, 28, 29, 1, 14, 22, 6], 23]) == 8\nassert my_solution.maxFrequencyScore(*[[10, 11, 3], 1]) == 2\nassert my_solution.maxFrequencyScore(*[[10, 19, 26, 18, 27, 18], 9]) == 4\nassert my_solution.maxFrequencyScore(*[[17, 24, 10, 23, 22, 15, 25, 2, 13, 24, 22, 25, 25, 21], 52]) == 13\nassert my_solution.maxFrequencyScore(*[[28, 6, 22, 10], 12]) == 2\nassert my_solution.maxFrequencyScore(*[[17, 17, 25, 14, 29, 28, 20, 14, 16, 22, 4, 28, 2, 5, 3, 11, 6, 20, 17], 76]) == 14\nassert my_solution.maxFrequencyScore(*[[23, 10, 18, 21, 16, 23, 14], 2]) == 3\nassert my_solution.maxFrequencyScore(*[[5, 13, 7], 8]) == 3\nassert my_solution.maxFrequencyScore(*[[6, 29, 3, 19, 10, 6, 20, 26, 1, 30, 11, 25, 29, 12, 29, 14, 15, 16, 5], 64]) == 12\nassert my_solution.maxFrequencyScore(*[[10, 26, 21, 18, 30, 25, 1], 8]) == 3\nassert my_solution.maxFrequencyScore(*[[29, 10, 26, 1, 2, 2, 17, 7, 5, 16, 24, 27, 7, 7, 26, 26, 24], 3]) == 5\nassert my_solution.maxFrequencyScore(*[[11, 16, 6, 12, 3, 8, 5, 29, 9, 15, 7, 9, 14, 6, 11, 14, 12, 23, 22, 14], 79]) == 19\nassert my_solution.maxFrequencyScore(*[[5, 17, 15, 14, 27, 11, 22, 6, 4], 26]) == 6\nassert my_solution.maxFrequencyScore(*[[13, 22, 17], 4]) == 2\nassert my_solution.maxFrequencyScore(*[[24, 6, 14, 6, 30, 9, 6, 11, 21, 10, 12, 27, 1], 90]) == 13\nassert my_solution.maxFrequencyScore(*[[19, 5, 2, 23, 16, 22, 3, 2, 5, 20, 17, 3, 22, 1], 15]) == 7\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3196", "questionFrontendId": "2968", "questionTitle": "Apply Operations to Maximize Frequency Score", "stats": { "totalAccepted": "2.2K", "totalSubmission": "4.6K", "totalAcceptedRaw": 2167, "totalSubmissionRaw": 4564, "acRate": "47.5%" } }
LeetCode/3220
# Count Tested Devices After Test Operations You are given a **0-indexed** integer array `batteryPercentages` having length `n`, denoting the battery percentages of `n` **0-indexed** devices. Your task is to test each device `i` **in order** from `0` to `n - 1`, by performing the following test operations: * If `batteryPercentages[i]` is **greater** than `0`: + **Increment** the count of tested devices. + **Decrease** the battery percentage of all devices with indices `j` in the range `[i + 1, n - 1]` by `1`, ensuring their battery percentage **never goes below** `0`, i.e, `batteryPercentages[j] = max(0, batteryPercentages[j] - 1)`. + Move to the next device. * Otherwise, move to the next device without performing any test. Return *an integer denoting the number of devices that will be tested after performing the test operations in order.*   **Example 1:** ``` **Input:** batteryPercentages = [1,1,2,1,3] **Output:** 3 **Explanation:** Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2]. At device 1, batteryPercentages[1] == 0, so we move to the next device without testing. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1]. At device 3, batteryPercentages[3] == 0, so we move to the next device without testing. At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same. So, the answer is 3. ``` **Example 2:** ``` **Input:** batteryPercentages = [0,1,2] **Output:** 2 **Explanation:** Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] == 0, so we move to the next device without testing. At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1]. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same. So, the answer is 2. ```   **Constraints:** * `1 <= n == batteryPercentages.length <= 100` * `0 <= batteryPercentages[i] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def countTestedDevices(self, batteryPercentages: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countTestedDevices(*[[1, 1, 2, 1, 3]]) == 3\nassert my_solution.countTestedDevices(*[[0, 1, 2]]) == 2\nassert my_solution.countTestedDevices(*[[0]]) == 0\nassert my_solution.countTestedDevices(*[[1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 0]]) == 0\nassert my_solution.countTestedDevices(*[[0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 2]]) == 1\nassert my_solution.countTestedDevices(*[[1, 0]]) == 1\nassert my_solution.countTestedDevices(*[[1, 2]]) == 2\nassert my_solution.countTestedDevices(*[[2, 1]]) == 1\nassert my_solution.countTestedDevices(*[[2, 2]]) == 2\nassert my_solution.countTestedDevices(*[[0, 0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 0, 2]]) == 1\nassert my_solution.countTestedDevices(*[[1, 1, 0]]) == 1\nassert my_solution.countTestedDevices(*[[1, 2, 0]]) == 2\nassert my_solution.countTestedDevices(*[[1, 3, 1]]) == 2\nassert my_solution.countTestedDevices(*[[2, 0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[2, 2, 0]]) == 2\nassert my_solution.countTestedDevices(*[[2, 2, 2]]) == 2\nassert my_solution.countTestedDevices(*[[3, 0, 3]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3220", "questionFrontendId": "2960", "questionTitle": "Count Tested Devices After Test Operations", "stats": { "totalAccepted": "5.8K", "totalSubmission": "6.8K", "totalAcceptedRaw": 5796, "totalSubmissionRaw": 6847, "acRate": "84.7%" } }
LeetCode/3234
# Double Modular Exponentiation You are given a **0-indexed** 2D array `variables` where `variables[i] = [ai, bi, ci, mi]`, and an integer `target`. An index `i` is **good** if the following formula holds: * `0 <= i < variables.length` * `((aibi % 10)ci) % mi == target` Return *an array consisting of **good** indices in **any order***.   **Example 1:** ``` **Input:** variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 **Output:** [0,2] **Explanation:** For each index i in the variables array: 1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2. 2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0. 3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2. Therefore we return [0,2] as the answer. ``` **Example 2:** ``` **Input:** variables = [[39,3,1000,1000]], target = 17 **Output:** [] **Explanation:** For each index i in the variables array: 1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1. Therefore we return [] as the answer. ```   **Constraints:** * `1 <= variables.length <= 100` * `variables[i] == [ai, bi, ci, mi]` * `1 <= ai, bi, ci, mi <= 103` * `0 <= target <= 103` Please make sure your answer follows the type signature below: ```python3 class Solution: def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.getGoodIndices(*[[[2, 3, 3, 10], [3, 3, 3, 1], [6, 1, 1, 4]], 2]) == [0, 2]\nassert my_solution.getGoodIndices(*[[[39, 3, 1000, 1000]], 17]) == []\nassert my_solution.getGoodIndices(*[[[3, 2, 4, 2], [3, 3, 1, 3], [2, 2, 2, 4], [4, 4, 2, 3], [2, 4, 1, 3]], 4]) == []\nassert my_solution.getGoodIndices(*[[[9, 2, 8, 5], [7, 8, 8, 8], [8, 9, 6, 1], [8, 6, 2, 2], [3, 6, 3, 1]], 9]) == []\nassert my_solution.getGoodIndices(*[[[2, 2, 3, 2], [1, 3, 3, 2], [3, 2, 2, 3], [3, 1, 2, 3], [1, 2, 3, 1], [2, 2, 2, 2], [2, 1, 3, 1], [3, 2, 2, 2], [2, 1, 3, 1], [3, 3, 1, 3]], 0]) == [0, 2, 3, 4, 5, 6, 8]\nassert my_solution.getGoodIndices(*[[[1, 3, 2, 3], [4, 2, 3, 3], [4, 1, 4, 4], [4, 2, 3, 1], [4, 2, 1, 1], [1, 2, 4, 1], [1, 1, 4, 2], [1, 4, 4, 3], [1, 2, 2, 3]], 2]) == []\nassert my_solution.getGoodIndices(*[[[5, 4, 1, 3], [2, 5, 5, 1], [5, 3, 4, 1]], 5]) == []\nassert my_solution.getGoodIndices(*[[[4, 7, 6, 7], [7, 6, 6, 4], [6, 8, 2, 3], [8, 3, 5, 8]], 4]) == []\nassert my_solution.getGoodIndices(*[[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], 0]) == [0, 1, 2, 3, 4, 5, 6]\nassert my_solution.getGoodIndices(*[[[3, 5, 1, 2], [3, 2, 5, 2], [4, 4, 3, 2], [3, 2, 5, 3], [1, 5, 1, 4]], 1]) == [0, 1, 4]\nassert my_solution.getGoodIndices(*[[[1, 2, 1, 1], [2, 2, 2, 2], [1, 1, 1, 2], [1, 2, 2, 2]], 2]) == []\nassert my_solution.getGoodIndices(*[[[3, 3, 5, 6], [8, 2, 9, 2], [1, 4, 6, 1], [6, 4, 7, 7]], 8]) == []\nassert my_solution.getGoodIndices(*[[[3, 5, 4, 3], [1, 3, 3, 1], [3, 3, 5, 5], [4, 5, 5, 5], [5, 1, 4, 3], [2, 5, 3, 4]], 7]) == []\nassert my_solution.getGoodIndices(*[[[9, 7, 2, 7], [9, 1, 8, 1], [9, 3, 5, 6], [6, 1, 8, 4], [9, 6, 2, 3]], 8]) == []\nassert my_solution.getGoodIndices(*[[[10, 6, 8, 7], [3, 6, 1, 8]], 5]) == []\nassert my_solution.getGoodIndices(*[[[4, 6, 5, 2], [2, 6, 4, 6], [4, 6, 3, 6], [2, 2, 6, 5], [6, 5, 5, 2]], 2]) == []\nassert my_solution.getGoodIndices(*[[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], 1]) == []\nassert my_solution.getGoodIndices(*[[[5, 6, 5, 1], [4, 3, 1, 6], [5, 4, 4, 2]], 4]) == [1]\nassert my_solution.getGoodIndices(*[[[5, 1, 2, 4], [4, 5, 5, 5], [5, 9, 7, 4], [7, 9, 6, 3], [1, 8, 6, 1], [1, 1, 9, 9], [3, 7, 6, 5], [2, 6, 2, 6]], 1]) == [0, 2, 3, 5]\nassert my_solution.getGoodIndices(*[[[1, 3, 2, 5], [5, 4, 1, 2], [2, 2, 3, 2], [4, 2, 5, 4], [1, 5, 4, 1], [2, 2, 5, 2], [3, 3, 2, 1], [2, 5, 4, 3], [2, 1, 5, 1]], 4]) == []\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3234", "questionFrontendId": "2961", "questionTitle": "Double Modular Exponentiation", "stats": { "totalAccepted": "4.6K", "totalSubmission": "9.7K", "totalAcceptedRaw": 4597, "totalSubmissionRaw": 9672, "acRate": "47.5%" } }
LeetCode/3213
# Count Subarrays Where Max Element Appears at Least K Times You are given an integer array `nums` and a **positive** integer `k`. Return *the number of subarrays where the **maximum** element of* `nums` *appears **at least*** `k` *times in that subarray.* A **subarray** is a contiguous sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [1,3,2,3,3], k = 2 **Output:** 6 **Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. ``` **Example 2:** ``` **Input:** nums = [1,4,2,1], k = 3 **Output:** 0 **Explanation:** No subarray contains the element 4 at least 3 times. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 106` * `1 <= k <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def countSubarrays(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSubarrays(*[[1, 3, 2, 3, 3], 2]) == 6\nassert my_solution.countSubarrays(*[[1, 4, 2, 1], 3]) == 0\nassert my_solution.countSubarrays(*[[61, 23, 38, 23, 56, 40, 82, 56, 82, 82, 82, 70, 8, 69, 8, 7, 19, 14, 58, 42, 82, 10, 82, 78, 15, 82], 2]) == 224\nassert my_solution.countSubarrays(*[[37, 20, 38, 66, 34, 38, 9, 41, 1, 14, 25, 63, 8, 12, 66, 66, 60, 12, 35, 27, 16, 38, 12, 66, 38, 36, 59, 54, 66, 54, 66, 48, 59, 66, 34, 11, 50, 66, 42, 51, 53, 66, 31, 24, 66, 44, 66, 1, 66, 66, 29, 54], 5]) == 594\nassert my_solution.countSubarrays(*[[28, 5, 58, 91, 24, 91, 53, 9, 48, 85, 16, 70, 91, 91, 47, 91, 61, 4, 54, 61, 49], 1]) == 187\nassert my_solution.countSubarrays(*[[43, 105, 105, 88, 19, 82, 95, 32, 80, 37, 49, 105, 25, 105, 46, 54, 45, 84, 105, 88, 26, 20, 49, 54, 31, 105, 8, 103, 37, 32, 105, 105, 97, 27, 105, 89, 105, 47, 25, 87, 29, 105, 105, 105, 24, 105, 105, 48, 19, 91, 96, 71], 7]) == 454\nassert my_solution.countSubarrays(*[[107, 101, 180, 137, 191, 148, 83, 15, 188, 22, 100, 124, 69, 94, 191, 181, 171, 64, 136, 96, 91, 191, 107, 191, 191, 191, 107, 191, 191, 11, 140, 33, 4, 110, 83, 5, 86, 33, 42, 186, 191, 6, 42, 61, 94, 129, 191, 119, 191, 134, 43, 182, 191, 187, 63, 116, 172, 118, 50, 141, 124, 191, 125, 145, 191, 34, 191, 191], 9]) == 548\nassert my_solution.countSubarrays(*[[41, 121, 92, 15, 24, 59, 45, 110, 97, 132, 75, 72, 31, 38, 103, 37, 132, 91, 132, 132, 105, 24], 3]) == 61\nassert my_solution.countSubarrays(*[[21, 11, 13, 15, 16, 21, 8, 9, 6, 21], 2]) == 10\nassert my_solution.countSubarrays(*[[31, 18, 36, 166, 166, 166, 135, 166, 166, 12, 102], 3]) == 31\nassert my_solution.countSubarrays(*[[2, 2, 2, 2, 1, 3, 3, 2, 2, 1, 1, 3, 1, 1, 2, 3, 2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 3], 5]) == 31\nassert my_solution.countSubarrays(*[[3, 2, 4, 4, 3, 4, 3, 1, 1, 1, 1, 3, 2, 1, 2, 1, 3, 4, 4, 1, 2, 4, 1, 1, 2, 3, 3, 3, 4, 4, 4, 1, 3, 1, 4, 1, 4, 4, 4, 2, 2, 3, 4, 3, 3, 2, 2, 2, 1, 2, 4, 2, 2, 4, 4, 1, 3, 2, 3, 2, 4, 4, 4, 2, 3, 4, 2, 4, 1, 4, 1, 4, 1, 4, 4, 3, 4, 2, 4, 3, 3, 2, 3, 3, 2, 3, 4, 2, 1, 1, 1, 2, 3], 23]) == 473\nassert my_solution.countSubarrays(*[[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 3, 3, 2, 3, 2, 1, 1, 2, 2, 1, 3, 2, 3, 1, 2, 1, 3, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 3], 8]) == 148\nassert my_solution.countSubarrays(*[[54, 161, 161, 161, 161, 31, 74, 51, 87, 19, 161, 116, 108, 149, 6, 19, 155, 101, 161, 161, 154, 161, 78, 132, 62, 156, 112, 51, 161, 42, 92, 151, 142, 17, 110, 85], 4]) == 279\nassert my_solution.countSubarrays(*[[97, 102, 144, 55, 144, 128, 16, 93, 144, 9, 144, 15, 144, 144, 32, 68, 144, 60, 94, 56, 103, 5, 41, 27, 48, 144, 12, 86, 129, 144, 144, 99, 93, 96, 144, 73, 106, 76, 107, 144, 53, 21, 144, 144, 98, 32, 85, 97, 71, 127, 144, 9, 144, 144, 133, 125, 144, 135, 52, 144, 144, 46, 134, 23, 23, 144, 79], 1]) == 2163\nassert my_solution.countSubarrays(*[[17, 17, 15, 9, 14, 11, 15, 1, 6, 2, 1, 17, 3, 17, 11, 12, 9, 11, 2, 4, 15, 17, 3, 17, 8, 6, 7, 12, 3, 16, 2, 9, 14, 17, 17, 17, 3, 7, 8, 9, 8, 17, 17, 17, 4, 2, 12, 17, 7, 17, 17, 16, 17, 17, 8, 12, 11, 3, 10, 4, 10, 17, 14, 7, 5, 17, 12, 10, 17, 13, 5, 17, 8, 14, 9, 17, 17, 17, 7, 16, 10, 13, 17, 15, 1, 14, 6, 8, 11, 3], 15]) == 1055\nassert my_solution.countSubarrays(*[[17, 12, 16, 17, 7, 1, 12, 6, 17, 5, 17, 13, 16, 16, 17, 14, 17, 6, 17, 17, 17, 17, 16, 17, 14, 8, 14, 1, 12, 13, 17, 17, 14, 8, 14, 5, 16, 17, 17], 5]) == 404\nassert my_solution.countSubarrays(*[[98, 59, 98, 32, 45, 15, 98, 98, 98, 65, 98, 10, 98, 89, 87, 51, 42, 58, 76, 23, 85, 98, 98, 35, 18, 65, 39, 88, 56, 62, 10, 32, 8, 16, 32, 98, 6, 39, 14, 24, 98, 95, 68, 98, 77, 47, 98, 23, 69, 98, 49, 98, 7, 11, 92, 98, 27, 25, 85, 98, 45, 30, 50, 62, 46, 1, 79, 58, 69, 15, 59, 57, 85, 19, 98, 95, 98, 67, 52, 98, 59, 8, 98, 98, 98, 73, 86, 20, 98, 96, 21, 98, 79, 97, 52, 22, 98, 86], 12]) == 1168\nassert my_solution.countSubarrays(*[[6, 50, 118, 27, 133, 133, 3, 121, 133, 72, 117, 133, 91, 57, 107, 93, 66, 122, 133, 6, 133, 122, 81, 20, 133, 133, 121, 133, 46, 25, 133, 133, 133, 17, 8, 49, 133, 116, 40, 133, 67, 9, 133, 133, 133, 133, 109, 41, 127, 13, 39, 133, 133, 133, 122, 58, 8, 125, 33, 62], 12]) == 538\nassert my_solution.countSubarrays(*[[94, 34, 112, 106, 112, 13, 12, 112, 112, 21, 48, 71, 112, 104, 112, 29, 99, 58, 23, 11, 49, 112, 20, 86], 4]) == 105\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3213", "questionFrontendId": "2962", "questionTitle": "Count Subarrays Where Max Element Appears at Least K Times", "stats": { "totalAccepted": "5K", "totalSubmission": "10.6K", "totalAcceptedRaw": 4961, "totalSubmissionRaw": 10606, "acRate": "46.8%" } }
LeetCode/3212
# Count the Number of Good Partitions You are given a **0-indexed** array `nums` consisting of **positive** integers. A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number. Return *the **total number** of good partitions of* `nums`. Since the answer may be large, return it **modulo** `109 + 7`.   **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 8 **Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]). ``` **Example 2:** ``` **Input:** nums = [1,1,1,1] **Output:** 1 **Explanation:** The only possible good partition is: ([1,1,1,1]). ``` **Example 3:** ``` **Input:** nums = [1,2,1,3] **Output:** 2 **Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]). ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfGoodPartitions(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfGoodPartitions(*[[1, 2, 3, 4]]) == 8\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 1]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1, 2, 1, 3]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[100000]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1000000000]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 3, 2]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 9, 7]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 5, 9, 2]]) == 8\nassert my_solution.numberOfGoodPartitions(*[[1, 4, 1, 7, 5]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 5, 1, 5, 6]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1, 5, 1, 10, 8]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 6, 8, 1, 5]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1, 6, 9, 4, 10]]) == 16\nassert my_solution.numberOfGoodPartitions(*[[1, 7, 1, 6, 8]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 9, 1, 1, 7]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[2, 1, 6, 7, 5]]) == 16\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 2, 6, 9]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 2, 8, 8]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 9, 2, 6]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3212", "questionFrontendId": "2963", "questionTitle": "Count the Number of Good Partitions", "stats": { "totalAccepted": "3.4K", "totalSubmission": "6.8K", "totalAcceptedRaw": 3378, "totalSubmissionRaw": 6754, "acRate": "50.0%" } }
LeetCode/3206
# Find Common Elements Between Two Arrays You are given two **0-indexed** integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively. Consider calculating the following values: * The number of indices `i` such that `0 <= i < n` and `nums1[i]` occurs **at least** once in `nums2`. * The number of indices `i` such that `0 <= i < m` and `nums2[i]` occurs **at least** once in `nums1`. Return *an integer array* `answer` *of size* `2` *containing the two values **in the above order***.   **Example 1:** ``` **Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] **Output:** [3,4] **Explanation:** We calculate the values as follows: - The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3. - The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4. ``` **Example 2:** ``` **Input:** nums1 = [3,4,2,3], nums2 = [1,5] **Output:** [0,0] **Explanation:** There are no common elements between the two arrays, so the two values will be 0. ```   **Constraints:** * `n == nums1.length` * `m == nums2.length` * `1 <= n, m <= 100` * `1 <= nums1[i], nums2[i] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findIntersectionValues(*[[4, 3, 2, 3, 1], [2, 2, 5, 2, 3, 6]]) == [3, 4]\nassert my_solution.findIntersectionValues(*[[3, 4, 2, 3], [1, 5]]) == [0, 0]\nassert my_solution.findIntersectionValues(*[[24, 28, 7, 27, 7, 27, 9, 24, 9, 10], [12, 29, 9, 7, 5]]) == [4, 2]\nassert my_solution.findIntersectionValues(*[[10, 30, 16, 18], [23, 30, 30, 6, 10, 26, 9, 27, 6, 16, 18, 10, 27, 2, 20, 7, 16]]) == [4, 7]\nassert my_solution.findIntersectionValues(*[[7, 23, 27, 20, 21, 29, 7, 27, 27, 18, 7, 6, 20, 10], [27, 27, 28, 24, 20, 4, 6, 17, 9, 29, 20, 14, 20]]) == [7, 7]\nassert my_solution.findIntersectionValues(*[[15, 30, 6, 6], [15, 4, 16, 10, 7, 23, 24, 3, 4, 6, 14, 8, 18, 1, 29, 27, 2, 17]]) == [3, 2]\nassert my_solution.findIntersectionValues(*[[24, 7, 8, 6, 22, 28, 22, 28, 7, 19], [3, 7, 28, 7, 3, 3]]) == [4, 3]\nassert my_solution.findIntersectionValues(*[[23, 4, 26, 17, 23, 13], [24, 17, 20, 16, 1, 13, 17, 28, 17]]) == [2, 4]\nassert my_solution.findIntersectionValues(*[[5, 8, 18, 27, 16, 29, 27, 12, 1, 29, 16, 27, 22, 19, 14, 12, 11, 25], [24, 8, 16]]) == [3, 2]\nassert my_solution.findIntersectionValues(*[[29, 17, 30, 17, 15, 30, 11, 2, 24, 28, 28, 30, 30, 27, 30, 2, 30, 9, 1, 7], [12, 12, 11, 21, 2, 28, 5, 24, 12, 17, 24, 29, 22, 19, 11, 17, 1, 23]]) == [10, 10]\nassert my_solution.findIntersectionValues(*[[4, 27, 12, 16, 16, 21, 26, 7, 19, 21, 24, 26, 12, 24, 22, 12, 16], [1, 25, 8, 27, 23, 27, 27, 24]]) == [3, 4]\nassert my_solution.findIntersectionValues(*[[27, 19, 20, 16, 24, 27, 27, 24], [30, 21, 21, 6, 17, 16]]) == [1, 1]\nassert my_solution.findIntersectionValues(*[[3, 19, 21, 5, 24, 26, 22, 22, 5], [23, 26, 20, 14, 30, 9, 10, 24, 19, 22, 19, 6, 3, 20, 22, 22, 5, 24, 24]]) == [8, 11]\nassert my_solution.findIntersectionValues(*[[13, 13, 29, 12], [29, 29, 13, 7, 30, 22]]) == [3, 3]\nassert my_solution.findIntersectionValues(*[[30, 4, 16, 14, 14, 14, 20, 15, 20, 30, 6, 10, 14], [30, 16, 20, 2, 18, 10, 5, 6, 30, 20, 22, 18, 14, 23, 15]]) == [12, 9]\nassert my_solution.findIntersectionValues(*[[22, 1, 22, 4, 11, 22, 4, 20, 11, 29, 11, 11, 4, 26, 20, 12, 20, 8, 26, 17], [4, 17, 7, 15]]) == [4, 2]\nassert my_solution.findIntersectionValues(*[[30, 15, 16, 15, 11, 16, 26, 15, 21], [22, 25, 27, 2, 26, 20, 18, 15, 26, 20, 16]]) == [6, 4]\nassert my_solution.findIntersectionValues(*[[5, 6], [13, 12, 8, 5, 19, 13, 27]]) == [1, 1]\nassert my_solution.findIntersectionValues(*[[27, 28, 15, 20, 5, 13, 28, 29, 24, 29, 20, 15, 5, 20, 20, 25, 9, 20, 24, 20], [16, 20, 13, 24, 11]]) == [9, 3]\nassert my_solution.findIntersectionValues(*[[25, 7, 18], [28, 1, 14, 22, 24, 8, 25, 17]]) == [1, 1]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3206", "questionFrontendId": "2956", "questionTitle": "Find Common Elements Between Two Arrays", "stats": { "totalAccepted": "3.6K", "totalSubmission": "4.5K", "totalAcceptedRaw": 3641, "totalSubmissionRaw": 4520, "acRate": "80.6%" } }
LeetCode/3230
# Remove Adjacent Almost-Equal Characters You are given a **0-indexed** string `word`. In one operation, you can pick any index `i` of `word` and change `word[i]` to any lowercase English letter. Return *the **minimum** number of operations needed to remove all adjacent **almost-equal** characters from* `word`. Two characters `a` and `b` are **almost-equal** if `a == b` or `a` and `b` are adjacent in the alphabet.   **Example 1:** ``` **Input:** word = "aaaaa" **Output:** 2 **Explanation:** We can change word into "a**c**a**c**a" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2. ``` **Example 2:** ``` **Input:** word = "abddez" **Output:** 2 **Explanation:** We can change word into "**y**bd**o**ez" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2. ``` **Example 3:** ``` **Input:** word = "zyxyxyz" **Output:** 3 **Explanation:** We can change word into "z**a**x**a**x**a**z" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3. ```   **Constraints:** * `1 <= word.length <= 100` * `word` consists only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def removeAlmostEqualCharacters(self, word: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.removeAlmostEqualCharacters(*['aaaaa']) == 2\nassert my_solution.removeAlmostEqualCharacters(*['abddez']) == 2\nassert my_solution.removeAlmostEqualCharacters(*['zyxyxyz']) == 3\nassert my_solution.removeAlmostEqualCharacters(*['a']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['b']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['c']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['aa']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ab']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ac']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['ba']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['bb']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['bc']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ca']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['cb']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['cc']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aaa']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aab']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aac']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aba']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['abb']) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3230", "questionFrontendId": "2957", "questionTitle": "Remove Adjacent Almost-Equal Characters", "stats": { "totalAccepted": "3.3K", "totalSubmission": "5.6K", "totalAcceptedRaw": 3259, "totalSubmissionRaw": 5561, "acRate": "58.6%" } }
LeetCode/3225
# Length of Longest Subarray With at Most K Frequency You are given an integer array `nums` and an integer `k`. The **frequency** of an element `x` is the number of times it occurs in an array. An array is called **good** if the frequency of each element in this array is **less than or equal** to `k`. Return *the length of the **longest** **good** subarray of* `nums`*.* A **subarray** is a contiguous non-empty sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [1,2,3,1,2,3,1,2], k = 2 **Output:** 6 **Explanation:** The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good. It can be shown that there are no good subarrays with length more than 6. ``` **Example 2:** ``` **Input:** nums = [1,2,1,2,1,2,1,2], k = 1 **Output:** 2 **Explanation:** The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good. It can be shown that there are no good subarrays with length more than 2. ``` **Example 3:** ``` **Input:** nums = [5,5,5,5,5,5,5], k = 4 **Output:** 4 **Explanation:** The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray. It can be shown that there are no good subarrays with length more than 4. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `1 <= k <= nums.length` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxSubarrayLength(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSubarrayLength(*[[1, 2, 3, 1, 2, 3, 1, 2], 2]) == 6\nassert my_solution.maxSubarrayLength(*[[1, 2, 1, 2, 1, 2, 1, 2], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[5, 5, 5, 5, 5, 5, 5], 4]) == 4\nassert my_solution.maxSubarrayLength(*[[1], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[2], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[3], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[4], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[5], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[6], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[7], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[8], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[9], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[10], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[1, 11], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[2, 11], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[3, 5], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[4, 6], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[5, 8], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[6, 7], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[7, 9], 2]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3225", "questionFrontendId": "2958", "questionTitle": "Length of Longest Subarray With at Most K Frequency", "stats": { "totalAccepted": "3.4K", "totalSubmission": "7.1K", "totalAcceptedRaw": 3372, "totalSubmissionRaw": 7115, "acRate": "47.4%" } }
LeetCode/3221
# Find the Peaks You are given a **0-indexed** array `mountain`. Your task is to find all the **peaks** in the `mountain` array. Return *an array that consists of* indices *of **peaks** in the given array in **any order**.* **Notes:** * A **peak** is defined as an element that is **strictly greater** than its neighboring elements. * The first and last elements of the array are **not** a peak.   **Example 1:** ``` **Input:** mountain = [2,4,4] **Output:** [] **Explanation:** mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array. mountain[1] also can not be a peak because it is not strictly greater than mountain[2]. So the answer is []. ``` **Example 2:** ``` **Input:** mountain = [1,4,3,8,5] **Output:** [1,3] **Explanation:** mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array. mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1]. But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3]. ```   **Constraints:** * `3 <= mountain.length <= 100` * `1 <= mountain[i] <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def findPeaks(self, mountain: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findPeaks(*[[2, 4, 4]]) == []\nassert my_solution.findPeaks(*[[1, 4, 3, 8, 5]]) == [1, 3]\nassert my_solution.findPeaks(*[[1, 1, 1]]) == []\nassert my_solution.findPeaks(*[[1, 1, 3]]) == []\nassert my_solution.findPeaks(*[[1, 1, 5]]) == []\nassert my_solution.findPeaks(*[[1, 2, 5]]) == []\nassert my_solution.findPeaks(*[[1, 4, 1]]) == [1]\nassert my_solution.findPeaks(*[[1, 4, 3]]) == [1]\nassert my_solution.findPeaks(*[[1, 5, 5]]) == []\nassert my_solution.findPeaks(*[[1, 6, 4]]) == [1]\nassert my_solution.findPeaks(*[[2, 1, 1]]) == []\nassert my_solution.findPeaks(*[[2, 1, 2]]) == []\nassert my_solution.findPeaks(*[[2, 2, 3]]) == []\nassert my_solution.findPeaks(*[[2, 2, 5]]) == []\nassert my_solution.findPeaks(*[[2, 3, 2]]) == [1]\nassert my_solution.findPeaks(*[[2, 3, 6]]) == []\nassert my_solution.findPeaks(*[[2, 4, 3]]) == [1]\nassert my_solution.findPeaks(*[[2, 4, 5]]) == []\nassert my_solution.findPeaks(*[[2, 6, 4]]) == [1]\nassert my_solution.findPeaks(*[[3, 3, 3]]) == []\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3221", "questionFrontendId": "2951", "questionTitle": "Find the Peaks", "stats": { "totalAccepted": "6.3K", "totalSubmission": "8K", "totalAcceptedRaw": 6316, "totalSubmissionRaw": 8009, "acRate": "78.9%" } }
LeetCode/3231
# Minimum Number of Coins to be Added You are given a **0-indexed** integer array `coins`, representing the values of the coins available, and an integer `target`. An integer `x` is **obtainable** if there exists a subsequence of `coins` that sums to `x`. Return *the **minimum** number of coins **of any value** that need to be added to the array so that every integer in the range* `[1, target]` *is **obtainable***. A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements.   **Example 1:** ``` **Input:** coins = [1,4,10], target = 19 **Output:** 2 **Explanation:** We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array. ``` **Example 2:** ``` **Input:** coins = [1,4,10,5,7,19], target = 19 **Output:** 1 **Explanation:** We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array. ``` **Example 3:** ``` **Input:** coins = [1,1,1], target = 20 **Output:** 3 **Explanation:** We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16]. It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array. ```   **Constraints:** * `1 <= target <= 105` * `1 <= coins.length <= 105` * `1 <= coins[i] <= target` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumAddedCoins(self, coins: List[int], target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumAddedCoins(*[[1, 4, 10], 19]) == 2\nassert my_solution.minimumAddedCoins(*[[1, 4, 10, 5, 7, 19], 19]) == 1\nassert my_solution.minimumAddedCoins(*[[1, 1, 1], 20]) == 3\nassert my_solution.minimumAddedCoins(*[[1], 100000]) == 16\nassert my_solution.minimumAddedCoins(*[[100000], 100000]) == 17\nassert my_solution.minimumAddedCoins(*[[2], 5]) == 2\nassert my_solution.minimumAddedCoins(*[[5, 6, 7], 10]) == 3\nassert my_solution.minimumAddedCoins(*[[5, 6, 7], 15]) == 3\nassert my_solution.minimumAddedCoins(*[[4, 11, 13, 15, 7, 5, 12, 11, 5, 9], 34]) == 2\nassert my_solution.minimumAddedCoins(*[[8, 12, 9], 27]) == 3\nassert my_solution.minimumAddedCoins(*[[2, 13, 7, 1, 11], 35]) == 1\nassert my_solution.minimumAddedCoins(*[[10, 3, 5, 11, 6], 27]) == 2\nassert my_solution.minimumAddedCoins(*[[6, 6, 6, 15, 4], 31]) == 2\nassert my_solution.minimumAddedCoins(*[[6, 15, 6], 22]) == 3\nassert my_solution.minimumAddedCoins(*[[8, 14, 15, 4, 14, 15, 8, 10, 8], 42]) == 2\nassert my_solution.minimumAddedCoins(*[[9, 14, 14, 9, 14, 5, 12, 10, 11], 17]) == 3\nassert my_solution.minimumAddedCoins(*[[14, 5, 13, 3, 7, 10, 10, 10], 32]) == 2\nassert my_solution.minimumAddedCoins(*[[8, 6, 7, 12], 26]) == 3\nassert my_solution.minimumAddedCoins(*[[15, 1, 12], 43]) == 4\nassert my_solution.minimumAddedCoins(*[[4, 1, 4, 10], 16]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3231", "questionFrontendId": "2952", "questionTitle": "Minimum Number of Coins to be Added", "stats": { "totalAccepted": "4.1K", "totalSubmission": "8.2K", "totalAcceptedRaw": 4143, "totalSubmissionRaw": 8235, "acRate": "50.3%" } }
LeetCode/3223
# Count Complete Substrings You are given a string `word` and an integer `k`. A substring `s` of `word` is **complete** if: * Each character in `s` occurs **exactly** `k` times. * The difference between two adjacent characters is **at most** `2`. That is, for any two adjacent characters `c1` and `c2` in `s`, the absolute difference in their positions in the alphabet is **at most** `2`. Return *the number of **complete** substrings of* `word`. A **substring** is a **non-empty** contiguous sequence of characters in a string.   **Example 1:** ``` **Input:** word = "igigee", k = 2 **Output:** 3 **Explanation:** The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: **igig**ee, igig**ee**, **igigee**. ``` **Example 2:** ``` **Input:** word = "aaabbbccc", k = 3 **Output:** 6 **Explanation:** The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: **aaa**bbbccc, aaa**bbb**ccc, aaabbb**ccc**, **aaabbb**ccc, aaa**bbbccc**, **aaabbbccc**. ```   **Constraints:** * `1 <= word.length <= 105` * `word` consists only of lowercase English letters. * `1 <= k <= word.length` Please make sure your answer follows the type signature below: ```python3 class Solution: def countCompleteSubstrings(self, word: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countCompleteSubstrings(*['igigee', 2]) == 3\nassert my_solution.countCompleteSubstrings(*['aaabbbccc', 3]) == 6\nassert my_solution.countCompleteSubstrings(*['a', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['b', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['c', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['aa', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['ab', 2]) == 0\nassert my_solution.countCompleteSubstrings(*['ac', 2]) == 0\nassert my_solution.countCompleteSubstrings(*['ba', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['bb', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['bc', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['ca', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['cb', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['cc', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['aaa', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['aab', 3]) == 0\nassert my_solution.countCompleteSubstrings(*['aac', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['aba', 3]) == 0\nassert my_solution.countCompleteSubstrings(*['abb', 1]) == 4\nassert my_solution.countCompleteSubstrings(*['abc', 3]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3223", "questionFrontendId": "2953", "questionTitle": "Count Complete Substrings", "stats": { "totalAccepted": "3.4K", "totalSubmission": "10.2K", "totalAcceptedRaw": 3433, "totalSubmissionRaw": 10180, "acRate": "33.7%" } }
LeetCode/3224
# Count the Number of Infection Sequences You are given an integer `n` and a **0-indexed**integer array `sick` which is **sorted** in **increasing** order. There are `n` children standing in a queue with positions `0` to `n - 1` assigned to them. The array `sick` contains the positions of the children who are infected with an infectious disease. An infected child at position `i` can spread the disease to either of its immediate neighboring children at positions `i - 1` and `i + 1` **if** they exist and are currently not infected. **At most one** child who was previously not infected can get infected with the disease in one second. It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An **infection sequence** is the sequential order of positions in which **all** of the non-infected children get infected with the disease. Return *the total number of possible infection sequences*. Since the answer may be large, return it modulo `109 + 7`. **Note** that an infection sequence **does not** contain positions of children who were already infected with the disease in the beginning.   **Example 1:** ``` **Input:** n = 5, sick = [0,4] **Output:** 4 **Explanation:** Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences: - The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first. Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected. Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3]. - The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first. Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected. Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2]. - The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4]. - The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4]. ``` **Example 2:** ``` **Input:** n = 4, sick = [1] **Output:** 3 **Explanation:** Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences: - The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3]. - The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3]. - The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3]. ```   **Constraints:** * `2 <= n <= 105` * `1 <= sick.length <= n - 1` * `0 <= sick[i] <= n - 1` * `sick` is sorted in increasing order. Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfSequence(self, n: int, sick: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfSequence(*[5, [0, 4]]) == 4\nassert my_solution.numberOfSequence(*[4, [1]]) == 3\nassert my_solution.numberOfSequence(*[2, [0]]) == 1\nassert my_solution.numberOfSequence(*[5, [0]]) == 1\nassert my_solution.numberOfSequence(*[100, [0]]) == 1\nassert my_solution.numberOfSequence(*[2, [1]]) == 1\nassert my_solution.numberOfSequence(*[5, [1]]) == 4\nassert my_solution.numberOfSequence(*[5, [2]]) == 6\nassert my_solution.numberOfSequence(*[5, [3]]) == 4\nassert my_solution.numberOfSequence(*[5, [4]]) == 1\nassert my_solution.numberOfSequence(*[5, [0, 1]]) == 1\nassert my_solution.numberOfSequence(*[3, [0, 2]]) == 1\nassert my_solution.numberOfSequence(*[5, [0, 2]]) == 3\nassert my_solution.numberOfSequence(*[5, [0, 3]]) == 6\nassert my_solution.numberOfSequence(*[5, [1, 2]]) == 3\nassert my_solution.numberOfSequence(*[5, [1, 3]]) == 6\nassert my_solution.numberOfSequence(*[5, [1, 4]]) == 6\nassert my_solution.numberOfSequence(*[5, [2, 3]]) == 3\nassert my_solution.numberOfSequence(*[5, [2, 4]]) == 3\nassert my_solution.numberOfSequence(*[5, [3, 4]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3224", "questionFrontendId": "2954", "questionTitle": "Count the Number of Infection Sequences", "stats": { "totalAccepted": "1.3K", "totalSubmission": "3.3K", "totalAcceptedRaw": 1335, "totalSubmissionRaw": 3325, "acRate": "40.2%" } }
LeetCode/3210
# Count Beautiful Substrings I You are given a string `s` and a positive integer `k`. Let `vowels` and `consonants` be the number of vowels and consonants in a string. A string is **beautiful** if: * `vowels == consonants`. * `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`. Return *the number of **non-empty beautiful substrings** in the given string* `s`. A **substring** is a contiguous sequence of characters in a string. **Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. **Consonant letters** in English are every letter except vowels.   **Example 1:** ``` **Input:** s = "baeyh", k = 2 **Output:** 2 **Explanation:** There are 2 beautiful substrings in the given string. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]). You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]). You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string. ``` **Example 2:** ``` **Input:** s = "abba", k = 1 **Output:** 3 **Explanation:** There are 3 beautiful substrings in the given string. - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]). It can be shown that there are only 3 beautiful substrings in the given string. ``` **Example 3:** ``` **Input:** s = "bcdf", k = 1 **Output:** 0 **Explanation:** There are no beautiful substrings in the given string. ```   **Constraints:** * `1 <= s.length <= 1000` * `1 <= k <= 1000` * `s` consists of only English lowercase letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def beautifulSubstrings(self, s: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulSubstrings(*['baeyh', 2]) == 2\nassert my_solution.beautifulSubstrings(*['abba', 1]) == 3\nassert my_solution.beautifulSubstrings(*['bcdf', 1]) == 0\nassert my_solution.beautifulSubstrings(*['ihroyeeb', 5]) == 0\nassert my_solution.beautifulSubstrings(*['uzuxpzou', 3]) == 1\nassert my_solution.beautifulSubstrings(*['ouuoeqd', 2]) == 1\nassert my_solution.beautifulSubstrings(*['eeebjoxxujuaeoqibd', 8]) == 4\nassert my_solution.beautifulSubstrings(*['ilougekqlovegioemdvu', 4]) == 21\nassert my_solution.beautifulSubstrings(*['tqaewreikaztwpfwnef', 8]) == 3\nassert my_solution.beautifulSubstrings(*['oykiuhsafgfjumnzb', 7]) == 0\nassert my_solution.beautifulSubstrings(*['ifvsa', 3]) == 0\nassert my_solution.beautifulSubstrings(*['svzauyuevujektj', 5]) == 3\nassert my_solution.beautifulSubstrings(*['urahjig', 2]) == 2\nassert my_solution.beautifulSubstrings(*['ime', 2]) == 0\nassert my_solution.beautifulSubstrings(*['oacghieut', 5]) == 0\nassert my_solution.beautifulSubstrings(*['aoluu', 3]) == 0\nassert my_solution.beautifulSubstrings(*['ioaoiciiuoziout', 1]) == 5\nassert my_solution.beautifulSubstrings(*['ouafupsuhid', 6]) == 0\nassert my_solution.beautifulSubstrings(*['ox', 2]) == 0\nassert my_solution.beautifulSubstrings(*['tlaiwoauazutusiaaui', 10]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3210", "questionFrontendId": "2947", "questionTitle": "Count Beautiful Substrings I", "stats": { "totalAccepted": "4.2K", "totalSubmission": "7.1K", "totalAcceptedRaw": 4176, "totalSubmissionRaw": 7130, "acRate": "58.6%" } }
LeetCode/3219
# Make Lexicographically Smallest Array by Swapping Elements You are given a **0-indexed** array of **positive** integers `nums` and a **positive** integer `limit`. In one operation, you can choose any two indices `i` and `j` and swap `nums[i]` and `nums[j]` **if** `|nums[i] - nums[j]| <= limit`. Return *the **lexicographically smallest array** that can be obtained by performing the operation any number of times*. An array `a` is lexicographically smaller than an array `b` if in the first position where `a` and `b` differ, array `a` has an element that is less than the corresponding element in `b`. For example, the array `[2,10,3]` is lexicographically smaller than the array `[10,2,3]` because they differ at index `0` and `2 < 10`.   **Example 1:** ``` **Input:** nums = [1,5,3,9,8], limit = 2 **Output:** [1,3,5,8,9] **Explanation:** Apply the operation 2 times: - Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8] - Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9] We cannot obtain a lexicographically smaller array by applying any more operations. Note that it may be possible to get the same result by doing different operations. ``` **Example 2:** ``` **Input:** nums = [1,7,6,18,2,1], limit = 3 **Output:** [1,6,7,18,1,2] **Explanation:** Apply the operation 3 times: - Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1] - Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1] - Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2] We cannot obtain a lexicographically smaller array by applying any more operations. ``` **Example 3:** ``` **Input:** nums = [1,7,28,19,10], limit = 3 **Output:** [1,7,28,19,10] **Explanation:** [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `1 <= limit <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.lexicographicallySmallestArray(*[[1, 5, 3, 9, 8], 2]) == [1, 3, 5, 8, 9]\nassert my_solution.lexicographicallySmallestArray(*[[1, 7, 6, 18, 2, 1], 3]) == [1, 6, 7, 18, 1, 2]\nassert my_solution.lexicographicallySmallestArray(*[[1, 7, 28, 19, 10], 3]) == [1, 7, 28, 19, 10]\nassert my_solution.lexicographicallySmallestArray(*[[1000000000], 1]) == [1000000000]\nassert my_solution.lexicographicallySmallestArray(*[[1, 60, 34, 84, 62, 56, 39, 76, 49, 38], 4]) == [1, 56, 34, 84, 60, 62, 38, 76, 49, 39]\nassert my_solution.lexicographicallySmallestArray(*[[1, 81, 10, 79, 36, 2, 87, 12, 20, 77], 7]) == [1, 77, 10, 79, 36, 2, 81, 12, 20, 87]\nassert my_solution.lexicographicallySmallestArray(*[[2, 71, 5, 87, 11, 15, 70, 70, 14, 38], 14]) == [2, 70, 5, 87, 11, 14, 70, 71, 15, 38]\nassert my_solution.lexicographicallySmallestArray(*[[4, 3, 23, 84, 34, 88, 44, 44, 18, 15], 3]) == [3, 4, 23, 84, 34, 88, 44, 44, 15, 18]\nassert my_solution.lexicographicallySmallestArray(*[[4, 34, 29, 73, 51, 11, 8, 53, 98, 47], 10]) == [4, 29, 34, 73, 47, 8, 11, 51, 98, 53]\nassert my_solution.lexicographicallySmallestArray(*[[4, 52, 38, 59, 71, 27, 31, 83, 88, 10], 14]) == [4, 27, 31, 38, 52, 59, 71, 83, 88, 10]\nassert my_solution.lexicographicallySmallestArray(*[[4, 68, 8, 10, 70, 62, 27, 5, 42, 61], 11]) == [4, 61, 5, 8, 62, 68, 27, 10, 42, 70]\nassert my_solution.lexicographicallySmallestArray(*[[5, 9, 35, 60, 73, 91, 61, 57, 87, 76], 11]) == [5, 9, 35, 57, 73, 76, 60, 61, 87, 91]\nassert my_solution.lexicographicallySmallestArray(*[[5, 15, 68, 47, 49, 67, 9, 6, 35, 14], 4]) == [5, 14, 67, 47, 49, 68, 6, 9, 35, 15]\nassert my_solution.lexicographicallySmallestArray(*[[5, 16, 43, 15, 66, 21, 58, 74, 55, 66], 9]) == [5, 15, 43, 16, 55, 21, 58, 66, 66, 74]\nassert my_solution.lexicographicallySmallestArray(*[[5, 30, 92, 4, 31, 2, 17, 39, 15, 7], 3]) == [2, 30, 92, 4, 31, 5, 15, 39, 17, 7]\nassert my_solution.lexicographicallySmallestArray(*[[5, 38, 68, 80, 64, 79, 50, 5, 8, 95], 7]) == [5, 38, 64, 79, 68, 80, 50, 5, 8, 95]\nassert my_solution.lexicographicallySmallestArray(*[[5, 100, 44, 45, 16, 30, 14, 65, 83, 64], 15]) == [5, 100, 14, 16, 30, 44, 45, 64, 83, 65]\nassert my_solution.lexicographicallySmallestArray(*[[6, 57, 100, 67, 4, 63, 47, 59, 21, 66], 8]) == [4, 57, 100, 59, 6, 63, 47, 66, 21, 67]\nassert my_solution.lexicographicallySmallestArray(*[[6, 70, 90, 1, 33, 81, 60, 80, 68, 44], 7]) == [1, 68, 90, 6, 33, 80, 60, 81, 70, 44]\nassert my_solution.lexicographicallySmallestArray(*[[6, 74, 74, 74, 30, 70, 91, 74, 76, 41], 1]) == [6, 74, 74, 74, 30, 70, 91, 74, 76, 41]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3219", "questionFrontendId": "2948", "questionTitle": "Make Lexicographically Smallest Array by Swapping Elements", "stats": { "totalAccepted": "3.2K", "totalSubmission": "7.4K", "totalAcceptedRaw": 3223, "totalSubmissionRaw": 7361, "acRate": "43.8%" } }
LeetCode/3208
# Count Beautiful Substrings II You are given a string `s` and a positive integer `k`. Let `vowels` and `consonants` be the number of vowels and consonants in a string. A string is **beautiful** if: * `vowels == consonants`. * `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`. Return *the number of **non-empty beautiful substrings** in the given string* `s`. A **substring** is a contiguous sequence of characters in a string. **Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. **Consonant letters** in English are every letter except vowels.   **Example 1:** ``` **Input:** s = "baeyh", k = 2 **Output:** 2 **Explanation:** There are 2 beautiful substrings in the given string. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]). You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]). You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string. ``` **Example 2:** ``` **Input:** s = "abba", k = 1 **Output:** 3 **Explanation:** There are 3 beautiful substrings in the given string. - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]). It can be shown that there are only 3 beautiful substrings in the given string. ``` **Example 3:** ``` **Input:** s = "bcdf", k = 1 **Output:** 0 **Explanation:** There are no beautiful substrings in the given string. ```   **Constraints:** * `1 <= s.length <= 5 * 104` * `1 <= k <= 1000` * `s` consists of only English lowercase letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def beautifulSubstrings(self, s: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulSubstrings(*['baeyh', 2]) == 2\nassert my_solution.beautifulSubstrings(*['abba', 1]) == 3\nassert my_solution.beautifulSubstrings(*['bcdf', 1]) == 0\nassert my_solution.beautifulSubstrings(*['ihroyeeb', 5]) == 0\nassert my_solution.beautifulSubstrings(*['uzuxpzou', 3]) == 1\nassert my_solution.beautifulSubstrings(*['ouuoeqd', 2]) == 1\nassert my_solution.beautifulSubstrings(*['eeebjoxxujuaeoqibd', 8]) == 4\nassert my_solution.beautifulSubstrings(*['ilougekqlovegioemdvu', 4]) == 21\nassert my_solution.beautifulSubstrings(*['tqaewreikaztwpfwnef', 8]) == 3\nassert my_solution.beautifulSubstrings(*['oykiuhsafgfjumnzb', 7]) == 0\nassert my_solution.beautifulSubstrings(*['ifvsa', 3]) == 0\nassert my_solution.beautifulSubstrings(*['svzauyuevujektj', 5]) == 3\nassert my_solution.beautifulSubstrings(*['urahjig', 2]) == 2\nassert my_solution.beautifulSubstrings(*['ime', 2]) == 0\nassert my_solution.beautifulSubstrings(*['oacghieut', 5]) == 0\nassert my_solution.beautifulSubstrings(*['aoluu', 3]) == 0\nassert my_solution.beautifulSubstrings(*['ioaoiciiuoziout', 1]) == 5\nassert my_solution.beautifulSubstrings(*['ouafupsuhid', 6]) == 0\nassert my_solution.beautifulSubstrings(*['ox', 2]) == 0\nassert my_solution.beautifulSubstrings(*['tlaiwoauazutusiaaui', 10]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3208", "questionFrontendId": "2949", "questionTitle": "Count Beautiful Substrings II", "stats": { "totalAccepted": "2.1K", "totalSubmission": "6.4K", "totalAcceptedRaw": 2066, "totalSubmissionRaw": 6390, "acRate": "32.3%" } }
LeetCode/3194
# Find Words Containing Character You are given a **0-indexed** array of strings `words` and a character `x`. Return *an **array of indices** representing the words that contain the character* `x`. **Note** that the returned array may be in **any** order.   **Example 1:** ``` **Input:** words = ["leet","code"], x = "e" **Output:** [0,1] **Explanation:** "e" occurs in both words: "l**ee**t", and "cod**e**". Hence, we return indices 0 and 1. ``` **Example 2:** ``` **Input:** words = ["abc","bcd","aaaa","cbc"], x = "a" **Output:** [0,2] **Explanation:** "a" occurs in "**a**bc", and "**aaaa**". Hence, we return indices 0 and 2. ``` **Example 3:** ``` **Input:** words = ["abc","bcd","aaaa","cbc"], x = "z" **Output:** [] **Explanation:** "z" does not occur in any of the words. Hence, we return an empty array. ```   **Constraints:** * `1 <= words.length <= 50` * `1 <= words[i].length <= 50` * `x` is a lowercase English letter. * `words[i]` consists only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def findWordsContaining(self, words: List[str], x: str) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findWordsContaining(*[['leet', 'code'], 'e']) == [0, 1]\nassert my_solution.findWordsContaining(*[['abc', 'bcd', 'aaaa', 'cbc'], 'a']) == [0, 2]\nassert my_solution.findWordsContaining(*[['abc', 'bcd', 'aaaa', 'cbc'], 'z']) == []\nassert my_solution.findWordsContaining(*[['sgtkshnss', 'm', 'ryvbkyvuz', 'ezittyjwgb', 'wudlwg'], 'x']) == []\nassert my_solution.findWordsContaining(*[['lkwnhpbj', 'tlohm', 'juazsb', 'f', 'rq'], 'v']) == []\nassert my_solution.findWordsContaining(*[['aaa', 'imvtfjmxr', 'wbzfoovjnf', 'hqwrwmi'], 'c']) == []\nassert my_solution.findWordsContaining(*[['utyeachht', 'bgpkcs', 'skeecqvvvw', 'nccrd'], 'i']) == []\nassert my_solution.findWordsContaining(*[['alcpxexztg', 'r'], 'h']) == []\nassert my_solution.findWordsContaining(*[['ekcpg', 'pdknua', 'fot', 'janppw', 'ofomkfvx'], 'g']) == [0]\nassert my_solution.findWordsContaining(*[['dq', 'rlvopu'], 'd']) == [0]\nassert my_solution.findWordsContaining(*[['wzppkd', 'jxvk', 'zaztizmwuv', 'hvcdtobr'], 'b']) == [3]\nassert my_solution.findWordsContaining(*[['y', 'hs', 'qznrkpi'], 'v']) == []\nassert my_solution.findWordsContaining(*[['pze', 'yojczsb', 'mjvyr', 'i', 'xsygks'], 'q']) == []\nassert my_solution.findWordsContaining(*[['qsgtjagcu', 'm'], 'e']) == []\nassert my_solution.findWordsContaining(*[['kidtwmw', 'ogh', 'trdedlh', 'wwbtlindg', 'naoylytpof', 'ujcbzwzkm', 'doamcoxdv'], 'o']) == [1, 4, 6]\nassert my_solution.findWordsContaining(*[['tsmeupctki'], 't']) == [0]\nassert my_solution.findWordsContaining(*[['dqxlbljmpf', 'uvdzfoiqg', 'jsnbnx', 'fbedae', 'nodewb', 'o', 'ivepktj'], 'g']) == [1]\nassert my_solution.findWordsContaining(*[['fjlmmecm', 'sautsoorhl', 'n', 'hsyco', 'amlukrpjpv', 'rmhdnj', 'g'], 'e']) == [0]\nassert my_solution.findWordsContaining(*[['khjchmeciv', 'vgx', 'xghr', 'bbufgegu', 'qyfxu'], 'r']) == [2]\nassert my_solution.findWordsContaining(*[['jhtcugtcpl', 'bvhlgmmla', 'ntfkwzite', 'imbtzafaj', 'sdl', 't'], 'm']) == [1, 3]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3194", "questionFrontendId": "2942", "questionTitle": "Find Words Containing Character", "stats": { "totalAccepted": "4.8K", "totalSubmission": "5.4K", "totalAcceptedRaw": 4826, "totalSubmissionRaw": 5440, "acRate": "88.7%" } }
LeetCode/3209
# Minimum Number of Coins for Fruits You are at a fruit market with different types of exotic fruits on display. You are given a **1-indexed** array `prices`, where `prices[i]` denotes the number of coins needed to purchase the `ith` fruit. The fruit market has the following offer: * If you purchase the `ith` fruit at `prices[i]` coins, you can get the next `i` fruits for free. **Note** that even if you **can** take fruit `j` for free, you can still purchase it for `prices[j]` coins to receive a new offer. Return *the **minimum** number of coins needed to acquire all the fruits*.   **Example 1:** ``` **Input:** prices = [3,1,2] **Output:** 4 **Explanation:** You can acquire the fruits as follows: - Purchase the 1st fruit with 3 coins, you are allowed to take the 2nd fruit for free. - Purchase the 2nd fruit with 1 coin, you are allowed to take the 3rd fruit for free. - Take the 3rd fruit for free. Note that even though you were allowed to take the 2nd fruit for free, you purchased it because it is more optimal. It can be proven that 4 is the minimum number of coins needed to acquire all the fruits. ``` **Example 2:** ``` **Input:** prices = [1,10,1,1] **Output:** 2 **Explanation:** You can acquire the fruits as follows: - Purchase the 1st fruit with 1 coin, you are allowed to take the 2nd fruit for free. - Take the 2nd fruit for free. - Purchase the 3rd fruit for 1 coin, you are allowed to take the 4th fruit for free. - Take the 4th fruit for free. It can be proven that 2 is the minimum number of coins needed to acquire all the fruits. ```   **Constraints:** * `1 <= prices.length <= 1000` * `1 <= prices[i] <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCoins(self, prices: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCoins(*[[3, 1, 2]]) == 4\nassert my_solution.minimumCoins(*[[1, 10, 1, 1]]) == 2\nassert my_solution.minimumCoins(*[[26, 18, 6, 12, 49, 7, 45, 45]]) == 39\nassert my_solution.minimumCoins(*[[27, 17, 29, 45, 3, 39, 42, 26]]) == 47\nassert my_solution.minimumCoins(*[[14, 37, 37, 38, 24, 15, 12]]) == 63\nassert my_solution.minimumCoins(*[[1, 37, 19, 38, 11, 42, 18, 33, 6, 37, 15, 48, 23, 12, 41, 18, 27, 32]]) == 37\nassert my_solution.minimumCoins(*[[38, 23, 27, 32, 47, 45, 48, 24, 39, 26, 37, 42, 24, 45, 27, 26, 15, 16, 26, 6]]) == 132\nassert my_solution.minimumCoins(*[[45, 44, 5, 9, 22, 14, 29, 14, 21, 13, 45, 10, 2, 16, 14, 30, 26, 1, 49]]) == 66\nassert my_solution.minimumCoins(*[[37, 42, 6, 50, 50, 38, 30, 38, 1, 13, 25, 39, 18, 1, 35, 32, 12]]) == 74\nassert my_solution.minimumCoins(*[[17, 32, 11, 25, 22]]) == 28\nassert my_solution.minimumCoins(*[[18, 10, 1, 11, 6, 30, 19, 24, 1, 18, 37, 29, 28, 27, 38]]) == 26\nassert my_solution.minimumCoins(*[[3, 10, 25, 47, 49, 10, 49]]) == 38\nassert my_solution.minimumCoins(*[[46, 7, 15]]) == 53\nassert my_solution.minimumCoins(*[[16, 45, 25, 5, 18, 19, 25, 13, 33]]) == 59\nassert my_solution.minimumCoins(*[[21, 16, 7, 10, 30]]) == 28\nassert my_solution.minimumCoins(*[[21, 22, 29, 37, 23, 15, 39, 9, 19, 10, 6, 9, 33, 28, 43]]) == 71\nassert my_solution.minimumCoins(*[[37, 16, 42, 47, 16, 31, 39, 8, 26, 50, 33]]) == 77\nassert my_solution.minimumCoins(*[[32, 4]]) == 32\nassert my_solution.minimumCoins(*[[31, 9, 2, 36, 4, 45, 28, 28, 12, 22, 44, 17, 10, 48, 15, 22, 7, 14, 41]]) == 56\nassert my_solution.minimumCoins(*[[1, 31, 9, 36, 44, 2, 23]]) == 12\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3209", "questionFrontendId": "2944", "questionTitle": "Minimum Number of Coins for Fruits", "stats": { "totalAccepted": "3.5K", "totalSubmission": "5.9K", "totalAcceptedRaw": 3538, "totalSubmissionRaw": 5909, "acRate": "59.9%" } }
LeetCode/3211
# Find Maximum Non-decreasing Array Length You are given a **0-indexed** integer array `nums`. You can perform any number of operations, where each operation involves selecting a **subarray** of the array and replacing it with the **sum** of its elements. For example, if the given array is `[1,3,5,6]` and you select subarray `[3,5]` the array will convert to `[1,8,6]`. Return *the* ***maximum*** *length of a* ***non-decreasing*** *array that can be made after applying operations.* A **subarray** is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [5,2,2] **Output:** 1 **Explanation:** This array with length 3 is not non-decreasing. We have two ways to make the array length two. First, choosing subarray [2,2] converts the array to [5,4]. Second, choosing subarray [5,2] converts the array to [7,2]. In these two ways the array is not non-decreasing. And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing. So the answer is 1. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** 4 **Explanation:** The array is non-decreasing. So the answer is 4. ``` **Example 3:** ``` **Input:** nums = [4,3,2,6] **Output:** 3 **Explanation:** Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing. Because the given array is not non-decreasing, the maximum possible answer is 3. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def findMaximumLength(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMaximumLength(*[[5, 2, 2]]) == 1\nassert my_solution.findMaximumLength(*[[1, 2, 3, 4]]) == 4\nassert my_solution.findMaximumLength(*[[4, 3, 2, 6]]) == 3\nassert my_solution.findMaximumLength(*[[32]]) == 1\nassert my_solution.findMaximumLength(*[[38]]) == 1\nassert my_solution.findMaximumLength(*[[60]]) == 1\nassert my_solution.findMaximumLength(*[[79]]) == 1\nassert my_solution.findMaximumLength(*[[85]]) == 1\nassert my_solution.findMaximumLength(*[[170]]) == 1\nassert my_solution.findMaximumLength(*[[198]]) == 1\nassert my_solution.findMaximumLength(*[[220]]) == 1\nassert my_solution.findMaximumLength(*[[318]]) == 1\nassert my_solution.findMaximumLength(*[[350]]) == 1\nassert my_solution.findMaximumLength(*[[381]]) == 1\nassert my_solution.findMaximumLength(*[[413]]) == 1\nassert my_solution.findMaximumLength(*[[426]]) == 1\nassert my_solution.findMaximumLength(*[[429]]) == 1\nassert my_solution.findMaximumLength(*[[431]]) == 1\nassert my_solution.findMaximumLength(*[[445]]) == 1\nassert my_solution.findMaximumLength(*[[488]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3211", "questionFrontendId": "2945", "questionTitle": "Find Maximum Non-decreasing Array Length", "stats": { "totalAccepted": "1.1K", "totalSubmission": "4.1K", "totalAcceptedRaw": 1054, "totalSubmissionRaw": 4133, "acRate": "25.5%" } }
LeetCode/3207
# Make Three Strings Equal You are given three strings `s1`, `s2`, and `s3`. You have to perform the following operation on these three strings **as many times** as you want. In one operation you can choose one of these three strings such that its length is at least `2` and delete the **rightmost** character of it. Return *the **minimum** number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return* `-1`*.*   **Example 1:** ``` **Input:** s1 = "abc", s2 = "abb", s3 = "ab" **Output:** 2 **Explanation:** Performing operations on s1 and s2 once will lead to three equal strings. It can be shown that there is no way to make them equal with less than two operations. ``` **Example 2:** ``` **Input:** s1 = "dac", s2 = "bac", s3 = "cac" **Output:** -1 **Explanation:** Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1. ```   **Constraints:** * `1 <= s1.length, s2.length, s3.length <= 100` * `s1`, `s2` and `s3` consist only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMinimumOperations(*['abc', 'abb', 'ab']) == 2\nassert my_solution.findMinimumOperations(*['dac', 'bac', 'cac']) == -1\nassert my_solution.findMinimumOperations(*['a', 'a', 'a']) == 0\nassert my_solution.findMinimumOperations(*['kui', 'm', 'v']) == -1\nassert my_solution.findMinimumOperations(*['a', 'aabc', 'a']) == 3\nassert my_solution.findMinimumOperations(*['cc', 'cccb', 'c']) == 4\nassert my_solution.findMinimumOperations(*['luso', 'lu', 'lu']) == 2\nassert my_solution.findMinimumOperations(*['xx', 'phe', 'xie']) == -1\nassert my_solution.findMinimumOperations(*['gzd', 'bcju', 'db']) == -1\nassert my_solution.findMinimumOperations(*['cbba', 'cbaa', 'c']) == 6\nassert my_solution.findMinimumOperations(*['k', 'kfb', 'krcnf']) == 6\nassert my_solution.findMinimumOperations(*['oby', 'obz', 'obf']) == 3\nassert my_solution.findMinimumOperations(*['b', 'aba', 'aaccaa']) == -1\nassert my_solution.findMinimumOperations(*['a', 'accabb', 'aaa']) == 7\nassert my_solution.findMinimumOperations(*['b', 'bccaaba', 'ba']) == 7\nassert my_solution.findMinimumOperations(*['b', 'bacccab', 'cc']) == -1\nassert my_solution.findMinimumOperations(*['ca', 'cccabb', 'cb']) == 7\nassert my_solution.findMinimumOperations(*['ccb', 'ccba', 'ccb']) == 1\nassert my_solution.findMinimumOperations(*['mbooi', 'pdq', 'br']) == -1\nassert my_solution.findMinimumOperations(*['xxfzj', 'faho', 'c']) == -1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3207", "questionFrontendId": "2937", "questionTitle": "Make Three Strings Equal", "stats": { "totalAccepted": "5.9K", "totalSubmission": "12.7K", "totalAcceptedRaw": 5885, "totalSubmissionRaw": 12732, "acRate": "46.2%" } }
LeetCode/3195
# Separate Black and White Balls There are `n` balls on a table, each ball has a color black or white. You are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively. In each step, you can choose two adjacent balls and swap them. Return *the **minimum** number of steps to group all the black balls to the right and all the white balls to the left*.   **Example 1:** ``` **Input:** s = "101" **Output:** 1 **Explanation:** We can group all the black balls to the right in the following way: - Swap s[0] and s[1], s = "011". Initially, 1s are not grouped together, requiring at least 1 step to group them to the right. ``` **Example 2:** ``` **Input:** s = "100" **Output:** 2 **Explanation:** We can group all the black balls to the right in the following way: - Swap s[0] and s[1], s = "010". - Swap s[1] and s[2], s = "001". It can be proven that the minimum number of steps needed is 2. ``` **Example 3:** ``` **Input:** s = "0111" **Output:** 0 **Explanation:** All the black balls are already grouped to the right. ```   **Constraints:** * `1 <= n == s.length <= 105` * `s[i]` is either `'0'` or `'1'`. Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumSteps(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSteps(*['101']) == 1\nassert my_solution.minimumSteps(*['100']) == 2\nassert my_solution.minimumSteps(*['0111']) == 0\nassert my_solution.minimumSteps(*['11000111']) == 6\nassert my_solution.minimumSteps(*['01010001']) == 7\nassert my_solution.minimumSteps(*['0100101']) == 4\nassert my_solution.minimumSteps(*['111111111100100010']) == 65\nassert my_solution.minimumSteps(*['10100000110010011010']) == 44\nassert my_solution.minimumSteps(*['1101110000111011110']) == 42\nassert my_solution.minimumSteps(*['01000010111010001']) == 29\nassert my_solution.minimumSteps(*['11110']) == 4\nassert my_solution.minimumSteps(*['010001001011010']) == 21\nassert my_solution.minimumSteps(*['0011011']) == 2\nassert my_solution.minimumSteps(*['001']) == 0\nassert my_solution.minimumSteps(*['000100100']) == 6\nassert my_solution.minimumSteps(*['00110']) == 2\nassert my_solution.minimumSteps(*['001110001110001']) == 27\nassert my_solution.minimumSteps(*['10000000001']) == 9\nassert my_solution.minimumSteps(*['01']) == 0\nassert my_solution.minimumSteps(*['0100011100001100100']) == 45\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3195", "questionFrontendId": "2938", "questionTitle": "Separate Black and White Balls", "stats": { "totalAccepted": "5.7K", "totalSubmission": "10.6K", "totalAcceptedRaw": 5664, "totalSubmissionRaw": 10560, "acRate": "53.6%" } }
LeetCode/3192
# Maximum Xor Product Given three integers `a`, `b`, and `n`, return *the **maximum value** of* `(a XOR x) * (b XOR x)` *where* `0 <= x < 2n`. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that `XOR` is the bitwise XOR operation.   **Example 1:** ``` **Input:** a = 12, b = 5, n = 4 **Output:** 98 **Explanation:** For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98. It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n. ``` **Example 2:** ``` **Input:** a = 6, b = 7 , n = 5 **Output:** 930 **Explanation:** For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930. It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n. ``` **Example 3:** ``` **Input:** a = 1, b = 6, n = 3 **Output:** 12 **Explanation:** For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12. It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n. ```   **Constraints:** * `0 <= a, b < 250` * `0 <= n <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumXorProduct(self, a: int, b: int, n: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumXorProduct(*[12, 5, 4]) == 98\nassert my_solution.maximumXorProduct(*[6, 7, 5]) == 930\nassert my_solution.maximumXorProduct(*[1, 6, 3]) == 12\nassert my_solution.maximumXorProduct(*[0, 0, 1]) == 1\nassert my_solution.maximumXorProduct(*[0, 1, 6]) == 3906\nassert my_solution.maximumXorProduct(*[0, 2, 7]) == 15875\nassert my_solution.maximumXorProduct(*[0, 3, 1]) == 2\nassert my_solution.maximumXorProduct(*[0, 4, 0]) == 0\nassert my_solution.maximumXorProduct(*[0, 5, 6]) == 3658\nassert my_solution.maximumXorProduct(*[0, 6, 1]) == 7\nassert my_solution.maximumXorProduct(*[0, 7, 2]) == 12\nassert my_solution.maximumXorProduct(*[0, 8, 5]) == 713\nassert my_solution.maximumXorProduct(*[0, 9, 2]) == 30\nassert my_solution.maximumXorProduct(*[0, 10, 7]) == 14875\nassert my_solution.maximumXorProduct(*[0, 11, 4]) == 84\nassert my_solution.maximumXorProduct(*[0, 12, 2]) == 45\nassert my_solution.maximumXorProduct(*[0, 13, 2]) == 42\nassert my_solution.maximumXorProduct(*[0, 14, 0]) == 0\nassert my_solution.maximumXorProduct(*[0, 15, 6]) == 3080\nassert my_solution.maximumXorProduct(*[1, 0, 3]) == 42\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3192", "questionFrontendId": "2939", "questionTitle": "Maximum Xor Product", "stats": { "totalAccepted": "3.4K", "totalSubmission": "11.2K", "totalAcceptedRaw": 3379, "totalSubmissionRaw": 11169, "acRate": "30.3%" } }
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
36
Edit dataset card