id
stringlengths
13
13
content
stringlengths
774
4.21k
test
dict
labels
dict
LeetCode/3024
# String Transformation You are given two strings `s` and `t` of equal length `n`. You can perform the following operation on the string `s`: * Remove a **suffix** of `s` of length `l` where `0 < l < n` and append it at the start of `s`. For example, let `s = 'abcd'` then in one operation you can remove the suffix `'cd'` and append it in front of `s` making `s = 'cdab'`. You are also given an integer `k`. Return *the number of ways in which* `s` *can be transformed into* `t` *in **exactly*** `k` *operations.* Since the answer can be large, return it **modulo** `109 + 7`.   **Example 1:** ``` **Input:** s = "abcd", t = "cdab", k = 2 **Output:** 2 **Explanation:** First way: In first operation, choose suffix from index = 3, so resulting s = "dabc". In second operation, choose suffix from index = 3, so resulting s = "cdab". Second way: In first operation, choose suffix from index = 1, so resulting s = "bcda". In second operation, choose suffix from index = 1, so resulting s = "cdab". ``` **Example 2:** ``` **Input:** s = "ababab", t = "ababab", k = 1 **Output:** 2 **Explanation:** First way: Choose suffix from index = 2, so resulting s = "ababab". Second way: Choose suffix from index = 4, so resulting s = "ababab". ```   **Constraints:** * `2 <= s.length <= 5 * 105` * `1 <= k <= 1015` * `s.length == t.length` * `s` and `t` consist of only lowercase English alphabets. Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfWays(self, s: str, t: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfWays(*['abcd', 'cdab', 2]) == 2\nassert my_solution.numberOfWays(*['ababab', 'ababab', 1]) == 2\nassert my_solution.numberOfWays(*['goxoq', 'dfqgl', 244326024901249]) == 0\nassert my_solution.numberOfWays(*['ceoceo', 'eoceoc', 4]) == 208\nassert my_solution.numberOfWays(*['ib', 'ib', 10]) == 1\nassert my_solution.numberOfWays(*['ttttttt', 'ttttttt', 5]) == 7776\nassert my_solution.numberOfWays(*['aaaa', 'aaaa', 8]) == 6561\nassert my_solution.numberOfWays(*['meplrmeplr', 'eplrmeplrm', 7]) == 956594\nassert my_solution.numberOfWays(*['dsmn', 'smnd', 3]) == 7\nassert my_solution.numberOfWays(*['jjj', 'jjj', 10]) == 1024\nassert my_solution.numberOfWays(*['rrrrr', 'rrrrr', 1]) == 4\nassert my_solution.numberOfWays(*['fefe', 'fefe', 9]) == 9841\nassert my_solution.numberOfWays(*['pfly', 'wvqr', 840550364246523]) == 0\nassert my_solution.numberOfWays(*['ltjwwltjww', 'jwwltjwwlt', 1]) == 2\nassert my_solution.numberOfWays(*['mb', 'mb', 3]) == 0\nassert my_solution.numberOfWays(*['jjjjjjjjjj', 'jjjjjjjjjj', 3]) == 729\nassert my_solution.numberOfWays(*['oqytlmi', 'lmioqyt', 8]) == 239945\nassert my_solution.numberOfWays(*['hpcg', 'pcgh', 5]) == 61\nassert my_solution.numberOfWays(*['bqbqbqbqbq', 'bqbqbqbqbq', 9]) == 193710244\nassert my_solution.numberOfWays(*['ccccccccc', 'ccccccccc', 7]) == 2097152\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3024", "questionFrontendId": "2851", "questionTitle": "String Transformation", "stats": { "totalAccepted": "1.5K", "totalSubmission": "3.5K", "totalAcceptedRaw": 1493, "totalSubmissionRaw": 3522, "acRate": "42.4%" } }
LeetCode/2998
# Count Symmetric Integers You are given two positive integers `low` and `high`. An integer `x` consisting of `2 * n` digits is **symmetric** if the sum of the first `n` digits of `x` is equal to the sum of the last `n` digits of `x`. Numbers with an odd number of digits are never symmetric. Return *the **number of symmetric** integers in the range* `[low, high]`.   **Example 1:** ``` **Input:** low = 1, high = 100 **Output:** 9 **Explanation:** There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99. ``` **Example 2:** ``` **Input:** low = 1200, high = 1230 **Output:** 4 **Explanation:** There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230. ```   **Constraints:** * `1 <= low <= high <= 104` Please make sure your answer follows the type signature below: ```python3 class Solution: def countSymmetricIntegers(self, low: int, high: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSymmetricIntegers(*[1, 100]) == 9\nassert my_solution.countSymmetricIntegers(*[1200, 1230]) == 4\nassert my_solution.countSymmetricIntegers(*[1, 1]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 2]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 3]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 4]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 5]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 6]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 7]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 8]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 9]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 10]) == 0\nassert my_solution.countSymmetricIntegers(*[1, 11]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 12]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 13]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 14]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 15]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 16]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 17]) == 1\nassert my_solution.countSymmetricIntegers(*[1, 18]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2998", "questionFrontendId": "2843", "questionTitle": " Count Symmetric Integers", "stats": { "totalAccepted": "7.7K", "totalSubmission": "10.9K", "totalAcceptedRaw": 7725, "totalSubmissionRaw": 10853, "acRate": "71.2%" } }
LeetCode/3046
# Minimum Operations to Make a Special Number You are given a **0-indexed** string `num` representing a non-negative integer. In one operation, you can pick any digit of `num` and delete it. Note that if you delete all the digits of `num`, `num` becomes `0`. Return *the **minimum number of operations** required to make* `num` *special*. An integer `x` is considered **special** if it is divisible by `25`.   **Example 1:** ``` **Input:** num = "2245047" **Output:** 2 **Explanation:** Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number. ``` **Example 2:** ``` **Input:** num = "2908305" **Output:** 3 **Explanation:** Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number. ``` **Example 3:** ``` **Input:** num = "10" **Output:** 1 **Explanation:** Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number. ```   **Constraints:** * `1 <= num.length <= 100` * `num` only consists of digits `'0'` through `'9'`. * `num` does not contain any leading zeros. Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumOperations(self, num: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumOperations(*['2245047']) == 2\nassert my_solution.minimumOperations(*['2908305']) == 3\nassert my_solution.minimumOperations(*['10']) == 1\nassert my_solution.minimumOperations(*['1']) == 1\nassert my_solution.minimumOperations(*['2']) == 1\nassert my_solution.minimumOperations(*['3']) == 1\nassert my_solution.minimumOperations(*['4']) == 1\nassert my_solution.minimumOperations(*['5']) == 1\nassert my_solution.minimumOperations(*['6']) == 1\nassert my_solution.minimumOperations(*['7']) == 1\nassert my_solution.minimumOperations(*['8']) == 1\nassert my_solution.minimumOperations(*['9']) == 1\nassert my_solution.minimumOperations(*['11']) == 2\nassert my_solution.minimumOperations(*['12']) == 2\nassert my_solution.minimumOperations(*['13']) == 2\nassert my_solution.minimumOperations(*['14']) == 2\nassert my_solution.minimumOperations(*['15']) == 2\nassert my_solution.minimumOperations(*['16']) == 2\nassert my_solution.minimumOperations(*['17']) == 2\nassert my_solution.minimumOperations(*['18']) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3046", "questionFrontendId": "2844", "questionTitle": "Minimum Operations to Make a Special Number", "stats": { "totalAccepted": "5.5K", "totalSubmission": "13.3K", "totalAcceptedRaw": 5519, "totalSubmissionRaw": 13348, "acRate": "41.3%" } }
LeetCode/2915
# Count of Interesting Subarrays You are given a **0-indexed** integer array `nums`, an integer `modulo`, and an integer `k`. Your task is to find the count of subarrays that are **interesting**. A **subarray** `nums[l..r]` is **interesting** if the following condition holds: * Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`. Return *an integer denoting the count of interesting subarrays.* **Note:** A subarray is *a contiguous non-empty sequence of elements within an array*.   **Example 1:** ``` **Input:** nums = [3,2,4], modulo = 2, k = 1 **Output:** 3 **Explanation:** In this example the interesting subarrays are: The subarray nums[0..0] which is [3]. - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..1] which is [3,2]. - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..2] which is [3,2,4]. - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 3. ``` **Example 2:** ``` **Input:** nums = [3,1,9,6], modulo = 3, k = 0 **Output:** 2 **Explanation:** In this example the interesting subarrays are: The subarray nums[0..3] which is [3,1,9,6]. - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. - Hence, cnt = 3 and cnt % modulo == k. The subarray nums[1..1] which is [1]. - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 0 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 2. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `1 <= modulo <= 109` * `0 <= k < modulo` Please make sure your answer follows the type signature below: ```python3 class Solution: def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countInterestingSubarrays(*[[3, 2, 4], 2, 1]) == 3\nassert my_solution.countInterestingSubarrays(*[[3, 1, 9, 6], 3, 0]) == 2\nassert my_solution.countInterestingSubarrays(*[[11, 12, 21, 31], 10, 1]) == 5\nassert my_solution.countInterestingSubarrays(*[[2, 4], 7, 2]) == 0\nassert my_solution.countInterestingSubarrays(*[[2, 7], 7, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[2, 45], 13, 2]) == 0\nassert my_solution.countInterestingSubarrays(*[[3, 3], 5, 3]) == 0\nassert my_solution.countInterestingSubarrays(*[[3, 4], 8, 3]) == 0\nassert my_solution.countInterestingSubarrays(*[[4, 5], 1, 0]) == 3\nassert my_solution.countInterestingSubarrays(*[[5, 1], 6, 1]) == 2\nassert my_solution.countInterestingSubarrays(*[[7, 2], 7, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[7, 4], 7, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[8, 8], 4, 0]) == 0\nassert my_solution.countInterestingSubarrays(*[[9, 2], 2, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[18, 43], 3, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[19, 67], 47, 19]) == 0\nassert my_solution.countInterestingSubarrays(*[[20, 8], 41, 8]) == 0\nassert my_solution.countInterestingSubarrays(*[[26, 5], 21, 5]) == 0\nassert my_solution.countInterestingSubarrays(*[[81, 36], 4, 0]) == 1\nassert my_solution.countInterestingSubarrays(*[[2, 1, 5], 9, 1]) == 4\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2915", "questionFrontendId": "2845", "questionTitle": "Count of Interesting Subarrays", "stats": { "totalAccepted": "4.1K", "totalSubmission": "11.7K", "totalAcceptedRaw": 4117, "totalSubmissionRaw": 11655, "acRate": "35.3%" } }
LeetCode/2999
# Check if Strings Can be Made Equal With Operations I You are given two strings `s1` and `s2`, both of length `4`, consisting of **lowercase** English letters. You can apply the following operation on any of the two strings **any** number of times: * Choose any two indices `i` and `j` such that `j - i = 2`, then **swap** the two characters at those indices in the string. Return `true` *if you can make the strings* `s1` *and* `s2` *equal, and* `false` *otherwise*.   **Example 1:** ``` **Input:** s1 = "abcd", s2 = "cdab" **Output:** true **Explanation:** We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2. ``` **Example 2:** ``` **Input:** s1 = "abcd", s2 = "dacb" **Output:** false **Explanation:** It is not possible to make the two strings equal. ```   **Constraints:** * `s1.length == s2.length == 4` * `s1` and `s2` consist only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def canBeEqual(self, s1: str, s2: str) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canBeEqual(*['abcd', 'cdab']) == True\nassert my_solution.canBeEqual(*['abcd', 'dacb']) == False\nassert my_solution.canBeEqual(*['bnxw', 'bwxn']) == True\nassert my_solution.canBeEqual(*['gudo', 'ogdu']) == False\nassert my_solution.canBeEqual(*['xsvc', 'vcxs']) == True\nassert my_solution.canBeEqual(*['hvsz', 'hzsv']) == True\nassert my_solution.canBeEqual(*['zzon', 'zozn']) == False\nassert my_solution.canBeEqual(*['zrmq', 'mrzq']) == True\nassert my_solution.canBeEqual(*['cmpr', 'rmcp']) == False\nassert my_solution.canBeEqual(*['qnde', 'flsi']) == False\nassert my_solution.canBeEqual(*['kina', 'kina']) == True\nassert my_solution.canBeEqual(*['vofo', 'oofv']) == False\nassert my_solution.canBeEqual(*['riti', 'riti']) == True\nassert my_solution.canBeEqual(*['ifjz', 'jzfi']) == False\nassert my_solution.canBeEqual(*['hazw', 'pfmp']) == False\nassert my_solution.canBeEqual(*['goze', 'gezo']) == True\nassert my_solution.canBeEqual(*['gcdm', 'dmgc']) == True\nassert my_solution.canBeEqual(*['fymg', 'famj']) == False\nassert my_solution.canBeEqual(*['seeo', 'vfvm']) == False\nassert my_solution.canBeEqual(*['kvne', 'nekv']) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2999", "questionFrontendId": "2839", "questionTitle": "Check if Strings Can be Made Equal With Operations I", "stats": { "totalAccepted": "4.6K", "totalSubmission": "7.1K", "totalAcceptedRaw": 4637, "totalSubmissionRaw": 7123, "acRate": "65.1%" } }
LeetCode/2978
# Check if Strings Can be Made Equal With Operations II You are given two strings `s1` and `s2`, both of length `n`, consisting of **lowercase** English letters. You can apply the following operation on **any** of the two strings **any** number of times: * Choose any two indices `i` and `j` such that `i < j` and the difference `j - i` is **even**, then **swap** the two characters at those indices in the string. Return `true` *if you can make the strings* `s1` *and* `s2` *equal, and*`false` *otherwise*.   **Example 1:** ``` **Input:** s1 = "abcdba", s2 = "cabdab" **Output:** true **Explanation:** We can apply the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba". - Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa". - Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2. ``` **Example 2:** ``` **Input:** s1 = "abe", s2 = "bea" **Output:** false **Explanation:** It is not possible to make the two strings equal. ```   **Constraints:** * `n == s1.length == s2.length` * `1 <= n <= 105` * `s1` and `s2` consist only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def checkStrings(self, s1: str, s2: str) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.checkStrings(*['abcdba', 'cabdab']) == True\nassert my_solution.checkStrings(*['abe', 'bea']) == False\nassert my_solution.checkStrings(*['ublnlasppynwgx', 'ganplbuylnswpx']) == True\nassert my_solution.checkStrings(*['aavizsxpqhxztrwi', 'zvisqatzpaxhixwr']) == False\nassert my_solution.checkStrings(*['jghn', 'jghn']) == True\nassert my_solution.checkStrings(*['slmqqdbrwyvm', 'qyldmmwsrqvb']) == False\nassert my_solution.checkStrings(*['pqtsprqmvi', 'qrvqpitmps']) == True\nassert my_solution.checkStrings(*['shvqocguj', 'vqsghujco']) == False\nassert my_solution.checkStrings(*['usvpwcehhvlg', 'ehuvvshcwplg']) == True\nassert my_solution.checkStrings(*['ppmfd', 'pfdpm']) == True\nassert my_solution.checkStrings(*['ktjpralqanofuuqsyal', 'qjlornpasktfuyluaqa']) == False\nassert my_solution.checkStrings(*['epjtzubboiallzd', 'dboilpzzjteualb']) == True\nassert my_solution.checkStrings(*['mgflranpdjdkrh', 'fpcgobmkdxbzyl']) == False\nassert my_solution.checkStrings(*['jh', 'fy']) == False\nassert my_solution.checkStrings(*['c', 'c']) == True\nassert my_solution.checkStrings(*['kfqofkvsoiiirznw', 'hosthwbinxrsikkf']) == False\nassert my_solution.checkStrings(*['ntuuwwh', 'jyjwmdf']) == False\nassert my_solution.checkStrings(*['oziiqbotydegrm', 'ytizriobogqmed']) == True\nassert my_solution.checkStrings(*['ztmuzn', 'muztzn']) == True\nassert my_solution.checkStrings(*['lcgcm', 'brdxe']) == False\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2978", "questionFrontendId": "2840", "questionTitle": "Check if Strings Can be Made Equal With Operations II", "stats": { "totalAccepted": "4K", "totalSubmission": "6.6K", "totalAcceptedRaw": 4011, "totalSubmissionRaw": 6562, "acRate": "61.1%" } }
LeetCode/2954
# Maximum Sum of Almost Unique Subarray You are given an integer array `nums` and two positive integers `m` and `k`. Return *the **maximum sum** out of all **almost unique** subarrays of length* `k` *of* `nums`. If no such subarray exists, return `0`. A subarray of `nums` is **almost unique** if it contains at least `m` distinct elements. A subarray is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [2,6,7,3,1,7], m = 3, k = 4 **Output:** 18 **Explanation:** There are 3 almost unique subarrays of size k = 4. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18. ``` **Example 2:** ``` **Input:** nums = [5,9,9,2,4,5,4], m = 1, k = 3 **Output:** 23 **Explanation:** There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23. ``` **Example 3:** ``` **Input:** nums = [1,2,1,2,1,2,1], m = 3, k = 3 **Output:** 0 **Explanation:** There are no subarrays of size k = 3 that contain at least m = 3 distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0. ```   **Constraints:** * `1 <= nums.length <= 2 * 104` * `1 <= m <= k <= nums.length` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxSum(self, nums: List[int], m: int, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSum(*[[2, 6, 7, 3, 1, 7], 3, 4]) == 18\nassert my_solution.maxSum(*[[5, 9, 9, 2, 4, 5, 4], 1, 3]) == 23\nassert my_solution.maxSum(*[[1, 2, 1, 2, 1, 2, 1], 3, 3]) == 0\nassert my_solution.maxSum(*[[1], 1, 1]) == 1\nassert my_solution.maxSum(*[[1, 1], 2, 2]) == 0\nassert my_solution.maxSum(*[[1, 1, 1], 1, 1]) == 1\nassert my_solution.maxSum(*[[1, 1, 1, 1], 1, 1]) == 1\nassert my_solution.maxSum(*[[1, 1, 1, 2], 2, 4]) == 5\nassert my_solution.maxSum(*[[1, 1, 1, 3], 2, 2]) == 4\nassert my_solution.maxSum(*[[1, 1, 1, 4], 2, 4]) == 7\nassert my_solution.maxSum(*[[1, 1, 2], 1, 1]) == 2\nassert my_solution.maxSum(*[[1, 1, 2, 1], 2, 2]) == 3\nassert my_solution.maxSum(*[[1, 1, 2, 2], 1, 3]) == 5\nassert my_solution.maxSum(*[[1, 1, 2, 3], 1, 1]) == 3\nassert my_solution.maxSum(*[[1, 1, 2, 4], 1, 1]) == 4\nassert my_solution.maxSum(*[[1, 1, 3], 1, 2]) == 4\nassert my_solution.maxSum(*[[1, 1, 3, 1], 2, 4]) == 6\nassert my_solution.maxSum(*[[1, 1, 3, 2], 1, 2]) == 5\nassert my_solution.maxSum(*[[1, 1, 3, 3], 1, 1]) == 3\nassert my_solution.maxSum(*[[1, 1, 3, 4], 1, 1]) == 4\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2954", "questionFrontendId": "2841", "questionTitle": "Maximum Sum of Almost Unique Subarray", "stats": { "totalAccepted": "4.4K", "totalSubmission": "9.9K", "totalAcceptedRaw": 4425, "totalSubmissionRaw": 9946, "acRate": "44.5%" } }
LeetCode/3057
# Count K-Subsequences of a String With Maximum Beauty You are given a string `s` and an integer `k`. A **k-subsequence** is a **subsequence** of `s`, having length `k`, and all its characters are **unique**, **i.e**., every character occurs once. Let `f(c)` denote the number of times the character `c` occurs in `s`. The **beauty** of a **k-subsequence** is the **sum** of `f(c)` for every character `c` in the k-subsequence. For example, consider `s = "abbbdd"` and `k = 2`: * `f('a') = 1`, `f('b') = 3`, `f('d') = 2` * Some k-subsequences of `s` are: + `"**ab**bbdd"` -> `"ab"` having a beauty of `f('a') + f('b') = 4` + `"**a**bbb**d**d"` -> `"ad"` having a beauty of `f('a') + f('d') = 3` + `"a**b**bb**d**d"` -> `"bd"` having a beauty of `f('b') + f('d') = 5` Return *an integer denoting the number of k-subsequences* *whose **beauty** is the **maximum** among all **k-subsequences***. Since the answer may be too large, return it modulo `109 + 7`. A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. **Notes** * `f(c)` is the number of times a character `c` occurs in `s`, not a k-subsequence. * Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string.   **Example 1:** ``` **Input:** s = "bcca", k = 2 **Output:** 4 **Explanation:** From s we have f('a') = 1, f('b') = 1, and f('c') = 2. The k-subsequences of s are: **bc**ca having a beauty of f('b') + f('c') = 3 **b**c**c**a having a beauty of f('b') + f('c') = 3 **b**cc**a** having a beauty of f('b') + f('a') = 2 b**c**c**a**having a beauty of f('c') + f('a') = 3 bc**ca** having a beauty of f('c') + f('a') = 3 There are 4 k-subsequences that have the maximum beauty, 3. Hence, the answer is 4. ``` **Example 2:** ``` **Input:** s = "abbcd", k = 4 **Output:** 2 **Explanation:** From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1. The k-subsequences of s are: **ab**b**cd** having a beauty of f('a') + f('b') + f('c') + f('d') = 5 **a**b**bcd** having a beauty of f('a') + f('b') + f('c') + f('d') = 5 There are 2 k-subsequences that have the maximum beauty, 5. Hence, the answer is 2. ```   **Constraints:** * `1 <= s.length <= 2 * 105` * `1 <= k <= s.length` * `s` consists only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countKSubsequencesWithMaxBeauty(*['bcca', 2]) == 4\nassert my_solution.countKSubsequencesWithMaxBeauty(*['abbcd', 4]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['am', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['az', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['ci', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['dd', 2]) == 0\nassert my_solution.countKSubsequencesWithMaxBeauty(*['di', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['dw', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['ef', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['gq', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['hj', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['hx', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['ii', 2]) == 0\nassert my_solution.countKSubsequencesWithMaxBeauty(*['il', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['jb', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['kx', 1]) == 2\nassert my_solution.countKSubsequencesWithMaxBeauty(*['qh', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['qk', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['qr', 2]) == 1\nassert my_solution.countKSubsequencesWithMaxBeauty(*['rg', 2]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3057", "questionFrontendId": "2842", "questionTitle": "Count K-Subsequences of a String With Maximum Beauty", "stats": { "totalAccepted": "2.7K", "totalSubmission": "9.3K", "totalAcceptedRaw": 2658, "totalSubmissionRaw": 9265, "acRate": "28.7%" } }
LeetCode/3019
# Furthest Point From Origin You are given a string `moves` of length `n` consisting only of characters `'L'`, `'R'`, and `'_'`. The string represents your movement on a number line starting from the origin `0`. In the `ith` move, you can choose one of the following directions: * move to the left if `moves[i] = 'L'` or `moves[i] = '_'` * move to the right if `moves[i] = 'R'` or `moves[i] = '_'` Return *the **distance from the origin** of the **furthest** point you can get to after* `n` *moves*.   **Example 1:** ``` **Input:** moves = "L_RL__R" **Output:** 3 **Explanation:** The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR". ``` **Example 2:** ``` **Input:** moves = "_R__LL_" **Output:** 5 **Explanation:** The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL". ``` **Example 3:** ``` **Input:** moves = "_______" **Output:** 7 **Explanation:** The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR". ```   **Constraints:** * `1 <= moves.length == n <= 50` * `moves` consists only of characters `'L'`, `'R'` and `'_'`. Please make sure your answer follows the type signature below: ```python3 class Solution: def furthestDistanceFromOrigin(self, moves: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.furthestDistanceFromOrigin(*['L_RL__R']) == 3\nassert my_solution.furthestDistanceFromOrigin(*['_R__LL_']) == 5\nassert my_solution.furthestDistanceFromOrigin(*['_______']) == 7\nassert my_solution.furthestDistanceFromOrigin(*['L']) == 1\nassert my_solution.furthestDistanceFromOrigin(*['R']) == 1\nassert my_solution.furthestDistanceFromOrigin(*['_']) == 1\nassert my_solution.furthestDistanceFromOrigin(*['LL']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['LR']) == 0\nassert my_solution.furthestDistanceFromOrigin(*['L_']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['RL']) == 0\nassert my_solution.furthestDistanceFromOrigin(*['RR']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['R_']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['_L']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['_R']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['__']) == 2\nassert my_solution.furthestDistanceFromOrigin(*['LLL']) == 3\nassert my_solution.furthestDistanceFromOrigin(*['LLR']) == 1\nassert my_solution.furthestDistanceFromOrigin(*['LL_']) == 3\nassert my_solution.furthestDistanceFromOrigin(*['LRL']) == 1\nassert my_solution.furthestDistanceFromOrigin(*['LRR']) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3019", "questionFrontendId": "2833", "questionTitle": "Furthest Point From Origin", "stats": { "totalAccepted": "8.2K", "totalSubmission": "10.4K", "totalAcceptedRaw": 8173, "totalSubmissionRaw": 10353, "acRate": "78.9%" } }
LeetCode/3026
# Find the Minimum Possible Sum of a Beautiful Array You are given positive integers `n` and `target`. An array `nums` is **beautiful** if it meets the following conditions: * `nums.length == n`. * `nums` consists of pairwise **distinct** **positive** integers. * There doesn't exist two **distinct** indices, `i` and `j`, in the range `[0, n - 1]`, such that `nums[i] + nums[j] == target`. Return *the **minimum** possible sum that a beautiful array could have modulo* `109 + 7`.   **Example 1:** ``` **Input:** n = 2, target = 3 **Output:** 4 **Explanation:** We can see that nums = [1,3] is beautiful. - The array nums has length n = 2. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 4 is the minimum possible sum that a beautiful array could have. ``` **Example 2:** ``` **Input:** n = 3, target = 3 **Output:** 8 **Explanation:** We can see that nums = [1,3,4] is beautiful. - The array nums has length n = 3. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 8 is the minimum possible sum that a beautiful array could have. ``` **Example 3:** ``` **Input:** n = 1, target = 1 **Output:** 1 **Explanation:** We can see, that nums = [1] is beautiful. ```   **Constraints:** * `1 <= n <= 109` * `1 <= target <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumPossibleSum(self, n: int, target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumPossibleSum(*[2, 3]) == 4\nassert my_solution.minimumPossibleSum(*[3, 3]) == 8\nassert my_solution.minimumPossibleSum(*[1, 1]) == 1\nassert my_solution.minimumPossibleSum(*[16, 6]) == 162\nassert my_solution.minimumPossibleSum(*[16, 32]) == 136\nassert my_solution.minimumPossibleSum(*[13, 50]) == 91\nassert my_solution.minimumPossibleSum(*[36, 21]) == 926\nassert my_solution.minimumPossibleSum(*[40, 17]) == 1076\nassert my_solution.minimumPossibleSum(*[37, 46]) == 1011\nassert my_solution.minimumPossibleSum(*[33, 7]) == 651\nassert my_solution.minimumPossibleSum(*[42, 46]) == 1321\nassert my_solution.minimumPossibleSum(*[46, 29]) == 1529\nassert my_solution.minimumPossibleSum(*[9, 43]) == 45\nassert my_solution.minimumPossibleSum(*[30, 31]) == 690\nassert my_solution.minimumPossibleSum(*[14, 47]) == 105\nassert my_solution.minimumPossibleSum(*[5, 3]) == 19\nassert my_solution.minimumPossibleSum(*[41, 23]) == 1191\nassert my_solution.minimumPossibleSum(*[17, 13]) == 219\nassert my_solution.minimumPossibleSum(*[9, 13]) == 63\nassert my_solution.minimumPossibleSum(*[29, 18]) == 595\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3026", "questionFrontendId": "2834", "questionTitle": "Find the Minimum Possible Sum of a Beautiful Array", "stats": { "totalAccepted": "6.4K", "totalSubmission": "15.6K", "totalAcceptedRaw": 6372, "totalSubmissionRaw": 15619, "acRate": "40.8%" } }
LeetCode/3025
# Minimum Operations to Form Subsequence With Target Sum You are given a **0-indexed** array `nums` consisting of **non-negative** powers of `2`, and an integer `target`. In one operation, you must apply the following changes to the array: * Choose any element of the array `nums[i]` such that `nums[i] > 1`. * Remove `nums[i]` from the array. * Add **two** occurrences of `nums[i] / 2` to the **end** of `nums`. Return the ***minimum number of operations** you need to perform so that* `nums` *contains a **subsequence** whose elements sum to* `target`. If it is impossible to obtain such a subsequence, return `-1`. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.   **Example 1:** ``` **Input:** nums = [1,2,8], target = 7 **Output:** 1 **Explanation:** In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4]. At this stage, nums contains the subsequence [1,2,4] which sums up to 7. It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7. ``` **Example 2:** ``` **Input:** nums = [1,32,1,2], target = 12 **Output:** 2 **Explanation:** In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16]. In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8] At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12. It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12. ``` **Example 3:** ``` **Input:** nums = [1,32,1], target = 35 **Output:** -1 **Explanation:** It can be shown that no sequence of operations results in a subsequence that sums up to 35. ```   **Constraints:** * `1 <= nums.length <= 1000` * `1 <= nums[i] <= 230` * `nums` consists only of non-negative powers of two. * `1 <= target < 231` Please make sure your answer follows the type signature below: ```python3 class Solution: def minOperations(self, nums: List[int], target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[1, 2, 8], 7]) == 1\nassert my_solution.minOperations(*[[1, 32, 1, 2], 12]) == 2\nassert my_solution.minOperations(*[[1, 32, 1], 35]) == -1\nassert my_solution.minOperations(*[[1], 1]) == 0\nassert my_solution.minOperations(*[[16, 128, 32], 1]) == 4\nassert my_solution.minOperations(*[[1, 1], 2]) == 0\nassert my_solution.minOperations(*[[64, 128, 128], 2]) == 5\nassert my_solution.minOperations(*[[2], 2]) == 0\nassert my_solution.minOperations(*[[32, 256, 4], 2]) == 1\nassert my_solution.minOperations(*[[1, 1, 1], 3]) == 0\nassert my_solution.minOperations(*[[1, 256, 16, 128], 3]) == 3\nassert my_solution.minOperations(*[[1, 2], 3]) == 0\nassert my_solution.minOperations(*[[16, 16, 4], 3]) == 2\nassert my_solution.minOperations(*[[1, 1, 1, 1], 4]) == 0\nassert my_solution.minOperations(*[[128, 1, 128, 1, 64], 4]) == 4\nassert my_solution.minOperations(*[[2, 1, 1], 4]) == 0\nassert my_solution.minOperations(*[[8, 2, 64, 32], 4]) == 1\nassert my_solution.minOperations(*[[16, 128, 8, 1, 1], 4]) == 1\nassert my_solution.minOperations(*[[1, 2, 1], 4]) == 0\nassert my_solution.minOperations(*[[128, 8, 8, 2], 4]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3025", "questionFrontendId": "2835", "questionTitle": "Minimum Operations to Form Subsequence With Target Sum", "stats": { "totalAccepted": "3.7K", "totalSubmission": "10.8K", "totalAcceptedRaw": 3709, "totalSubmissionRaw": 10750, "acRate": "34.5%" } }
LeetCode/3032
# Maximize Value of Function in a Ball Passing Game You are given a **0-indexed** integer array `receiver` of length `n` and an integer `k`. There are `n` players having a **unique id** in the range `[0, n - 1]` who will play a ball passing game, and `receiver[i]` is the id of the player who receives passes from the player with id `i`. Players can pass to themselves, **i.e.** `receiver[i]` may be equal to `i`. You must choose one of the `n` players as the starting player for the game, and the ball will be passed **exactly** `k` times starting from the chosen player. For a chosen starting player having id `x`, we define a function `f(x)` that denotes the **sum** of `x` and the **ids** of all players who receive the ball during the `k` passes, **including repetitions**. In other words, `f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver(k)[x]`. Your task is to choose a starting player having id `x` that **maximizes** the value of `f(x)`. Return *an integer denoting the **maximum** value of the function.* **Note:** `receiver` may contain duplicates.   **Example 1:** | Pass Number | Sender ID | Receiver ID | x + Receiver IDs | | --- | --- | --- | --- | | | | | 2 | | 1 | 2 | 1 | 3 | | 2 | 1 | 0 | 3 | | 3 | 0 | 2 | 5 | | 4 | 2 | 1 | 6 | ``` **Input:** receiver = [2,0,1], k = 4 **Output:** 6 **Explanation:** The table above shows a simulation of the game starting with the player having id x = 2. From the table, f(2) is equal to 6. It can be shown that 6 is the maximum achievable value of the function. Hence, the output is 6. ``` **Example 2:** | Pass Number | Sender ID | Receiver ID | x + Receiver IDs | | --- | --- | --- | --- | | | | | 4 | | 1 | 4 | 3 | 7 | | 2 | 3 | 2 | 9 | | 3 | 2 | 1 | 10 | ``` **Input:** receiver = [1,1,1,2,3], k = 3 **Output:** 10 **Explanation:** The table above shows a simulation of the game starting with the player having id x = 4. From the table, f(4) is equal to 10. It can be shown that 10 is the maximum achievable value of the function. Hence, the output is 10. ```   **Constraints:** * `1 <= receiver.length == n <= 105` * `0 <= receiver[i] <= n - 1` * `1 <= k <= 1010` Please make sure your answer follows the type signature below: ```python3 class Solution: def getMaxFunctionValue(self, receiver: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.getMaxFunctionValue(*[[2, 0, 1], 4]) == 6\nassert my_solution.getMaxFunctionValue(*[[1, 1, 1, 2, 3], 3]) == 10\nassert my_solution.getMaxFunctionValue(*[[0], 1]) == 0\nassert my_solution.getMaxFunctionValue(*[[0], 2]) == 0\nassert my_solution.getMaxFunctionValue(*[[0], 3]) == 0\nassert my_solution.getMaxFunctionValue(*[[0], 100]) == 0\nassert my_solution.getMaxFunctionValue(*[[0, 0], 1]) == 1\nassert my_solution.getMaxFunctionValue(*[[0, 0], 7]) == 1\nassert my_solution.getMaxFunctionValue(*[[0, 0], 10]) == 1\nassert my_solution.getMaxFunctionValue(*[[0, 0], 13]) == 1\nassert my_solution.getMaxFunctionValue(*[[0, 0], 16]) == 1\nassert my_solution.getMaxFunctionValue(*[[0, 1], 1]) == 2\nassert my_solution.getMaxFunctionValue(*[[0, 1], 3]) == 4\nassert my_solution.getMaxFunctionValue(*[[0, 1], 5]) == 6\nassert my_solution.getMaxFunctionValue(*[[0, 1], 8]) == 9\nassert my_solution.getMaxFunctionValue(*[[0, 1], 13]) == 14\nassert my_solution.getMaxFunctionValue(*[[0, 1], 14]) == 15\nassert my_solution.getMaxFunctionValue(*[[0, 1], 15]) == 16\nassert my_solution.getMaxFunctionValue(*[[1, 0], 5]) == 3\nassert my_solution.getMaxFunctionValue(*[[1, 0], 6]) == 4\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3032", "questionFrontendId": "2836", "questionTitle": "Maximize Value of Function in a Ball Passing Game", "stats": { "totalAccepted": "2.5K", "totalSubmission": "6.3K", "totalAcceptedRaw": 2453, "totalSubmissionRaw": 6282, "acRate": "39.0%" } }
LeetCode/2977
# Check if a String Is an Acronym of Words Given an array of strings `words` and a string `s`, determine if `s` is an **acronym** of words. The string `s` is considered an acronym of `words` if it can be formed by concatenating the **first** character of each string in `words` **in order**. For example, `"ab"` can be formed from `["apple", "banana"]`, but it can't be formed from `["bear", "aardvark"]`. Return `true` *if* `s` *is an acronym of* `words`*, and* `false` *otherwise.*   **Example 1:** ``` **Input:** words = ["alice","bob","charlie"], s = "abc" **Output:** true **Explanation:** The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. ``` **Example 2:** ``` **Input:** words = ["an","apple"], s = "a" **Output:** false **Explanation:** The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym. ``` **Example 3:** ``` **Input:** words = ["never","gonna","give","up","on","you"], s = "ngguoy" **Output:** true **Explanation:** By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym. ```   **Constraints:** * `1 <= words.length <= 100` * `1 <= words[i].length <= 10` * `1 <= s.length <= 100` * `words[i]` and `s` consist of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def isAcronym(self, words: List[str], s: str) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.isAcronym(*[['alice', 'bob', 'charlie'], 'abc']) == True\nassert my_solution.isAcronym(*[['an', 'apple'], 'a']) == False\nassert my_solution.isAcronym(*[['never', 'gonna', 'give', 'up', 'on', 'you'], 'ngguoy']) == True\nassert my_solution.isAcronym(*[['ad', 'uadhrwxki'], 'au']) == True\nassert my_solution.isAcronym(*[['afkc', 'icxufam'], 'ai']) == True\nassert my_solution.isAcronym(*[['afqcpzsx', 'icenu'], 'yi']) == False\nassert my_solution.isAcronym(*[['ahbibag', 'aoximesw'], 'aa']) == True\nassert my_solution.isAcronym(*[['auqoc', 'koioxa'], 'ak']) == True\nassert my_solution.isAcronym(*[['b', 'x'], 'bx']) == True\nassert my_solution.isAcronym(*[['bpctc', 'kaqquqbpmw'], 'bk']) == True\nassert my_solution.isAcronym(*[['c', 'df'], 'bd']) == False\nassert my_solution.isAcronym(*[['c', 'evlvvhrsqa'], 'ce']) == True\nassert my_solution.isAcronym(*[['cfsrsyt', 'md'], 'cm']) == True\nassert my_solution.isAcronym(*[['ddnlfpvy', 'exs'], 'de']) == True\nassert my_solution.isAcronym(*[['deacf', 'hldiauk'], 'dh']) == True\nassert my_solution.isAcronym(*[['dllcn', 'tnzrnzypg'], 'dt']) == True\nassert my_solution.isAcronym(*[['dmekslxlpo', 'wqdgxqwdk'], 'dw']) == True\nassert my_solution.isAcronym(*[['dv', 'g'], 'sg']) == False\nassert my_solution.isAcronym(*[['dvn', 'acafe'], 'dp']) == False\nassert my_solution.isAcronym(*[['dwrvgkxdtm', 'wy'], 'hw']) == False\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2977", "questionFrontendId": "2828", "questionTitle": "Check if a String Is an Acronym of Words", "stats": { "totalAccepted": "29.8K", "totalSubmission": "35K", "totalAcceptedRaw": 29846, "totalSubmissionRaw": 34967, "acRate": "85.4%" } }
LeetCode/2811
# Determine the Minimum Sum of a k-avoiding Array You are given two integers, `n` and `k`. An array of **distinct** positive integers is called a **k-avoiding** array if there does not exist any pair of distinct elements that sum to `k`. Return *the **minimum** possible sum of a k-avoiding array of length* `n`.   **Example 1:** ``` **Input:** n = 5, k = 4 **Output:** 18 **Explanation:** Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18. It can be proven that there is no k-avoiding array with a sum less than 18. ``` **Example 2:** ``` **Input:** n = 2, k = 6 **Output:** 3 **Explanation:** We can construct the array [1,2], which has a sum of 3. It can be proven that there is no k-avoiding array with a sum less than 3. ```   **Constraints:** * `1 <= n, k <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumSum(self, n: int, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSum(*[5, 4]) == 18\nassert my_solution.minimumSum(*[2, 6]) == 3\nassert my_solution.minimumSum(*[1, 1]) == 1\nassert my_solution.minimumSum(*[1, 2]) == 1\nassert my_solution.minimumSum(*[1, 3]) == 1\nassert my_solution.minimumSum(*[1, 4]) == 1\nassert my_solution.minimumSum(*[1, 5]) == 1\nassert my_solution.minimumSum(*[1, 6]) == 1\nassert my_solution.minimumSum(*[1, 7]) == 1\nassert my_solution.minimumSum(*[1, 8]) == 1\nassert my_solution.minimumSum(*[1, 9]) == 1\nassert my_solution.minimumSum(*[1, 10]) == 1\nassert my_solution.minimumSum(*[1, 11]) == 1\nassert my_solution.minimumSum(*[1, 12]) == 1\nassert my_solution.minimumSum(*[1, 13]) == 1\nassert my_solution.minimumSum(*[1, 14]) == 1\nassert my_solution.minimumSum(*[1, 15]) == 1\nassert my_solution.minimumSum(*[1, 16]) == 1\nassert my_solution.minimumSum(*[1, 17]) == 1\nassert my_solution.minimumSum(*[1, 18]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2811", "questionFrontendId": "2829", "questionTitle": "Determine the Minimum Sum of a k-avoiding Array", "stats": { "totalAccepted": "6.7K", "totalSubmission": "11.3K", "totalAcceptedRaw": 6723, "totalSubmissionRaw": 11308, "acRate": "59.5%" } }
LeetCode/2979
# Maximize the Profit as the Salesman You are given an integer `n` representing the number of houses on a number line, numbered from `0` to `n - 1`. Additionally, you are given a 2D integer array `offers` where `offers[i] = [starti, endi, goldi]`, indicating that `ith` buyer wants to buy all the houses from `starti` to `endi` for `goldi` amount of gold. As a salesman, your goal is to **maximize** your earnings by strategically selecting and selling houses to buyers. Return *the maximum amount of gold you can earn*. **Note** that different buyers can't buy the same house, and some houses may remain unsold.   **Example 1:** ``` **Input:** n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]] **Output:** 3 **Explanation:** There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,0] to 1st buyer for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds. It can be proven that 3 is the maximum amount of gold we can achieve. ``` **Example 2:** ``` **Input:** n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]] **Output:** 10 **Explanation:** There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,2] to 2nd buyer for 10 golds. It can be proven that 10 is the maximum amount of gold we can achieve. ```   **Constraints:** * `1 <= n <= 105` * `1 <= offers.length <= 105` * `offers[i].length == 3` * `0 <= starti <= endi <= n - 1` * `1 <= goldi <= 103` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximizeTheProfit(self, n: int, offers: List[List[int]]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximizeTheProfit(*[5, [[0, 0, 1], [0, 2, 2], [1, 3, 2]]]) == 3\nassert my_solution.maximizeTheProfit(*[5, [[0, 0, 1], [0, 2, 10], [1, 3, 2]]]) == 10\nassert my_solution.maximizeTheProfit(*[4, [[1, 3, 10], [1, 3, 3], [0, 0, 1], [0, 0, 7]]]) == 17\nassert my_solution.maximizeTheProfit(*[4, [[0, 0, 6], [1, 2, 8], [0, 3, 7], [2, 2, 5], [0, 1, 5], [2, 3, 2], [0, 2, 8], [2, 3, 10], [0, 3, 2]]]) == 16\nassert my_solution.maximizeTheProfit(*[15, [[5, 5, 10], [2, 6, 6], [8, 11, 5], [7, 11, 9], [2, 4, 1], [3, 8, 5], [0, 6, 9], [0, 10, 5], [5, 10, 8], [4, 5, 1]]]) == 20\nassert my_solution.maximizeTheProfit(*[10, [[1, 6, 1], [0, 1, 10], [3, 6, 2], [0, 5, 10], [0, 0, 3], [0, 0, 4], [1, 1, 4], [0, 6, 7], [4, 4, 1]]]) == 12\nassert my_solution.maximizeTheProfit(*[11, [[7, 8, 6], [6, 6, 4], [4, 6, 9], [6, 7, 4], [5, 5, 8], [1, 5, 9], [7, 7, 8], [1, 2, 5], [0, 2, 9], [1, 3, 8], [0, 2, 7], [2, 2, 8]]]) == 29\nassert my_solution.maximizeTheProfit(*[3, [[0, 0, 6], [0, 1, 8], [1, 2, 1], [0, 1, 4], [0, 1, 2], [0, 0, 7], [0, 0, 6], [0, 0, 5]]]) == 8\nassert my_solution.maximizeTheProfit(*[4, [[0, 1, 9], [1, 1, 4]]]) == 9\nassert my_solution.maximizeTheProfit(*[11, [[1, 10, 6], [1, 10, 5], [0, 2, 7], [0, 0, 8], [8, 10, 7]]]) == 15\nassert my_solution.maximizeTheProfit(*[3, [[0, 1, 8], [1, 1, 6], [2, 2, 7], [0, 2, 6], [0, 2, 2], [0, 0, 6], [0, 0, 9], [0, 1, 4]]]) == 22\nassert my_solution.maximizeTheProfit(*[6, [[0, 2, 4]]]) == 4\nassert my_solution.maximizeTheProfit(*[10, [[5, 9, 3], [1, 5, 8], [0, 0, 6], [5, 8, 10]]]) == 16\nassert my_solution.maximizeTheProfit(*[5, [[1, 1, 3], [1, 1, 3], [0, 0, 8], [1, 3, 8], [0, 2, 1], [3, 3, 9], [0, 0, 7], [0, 2, 3], [0, 0, 5], [0, 3, 10], [1, 3, 10], [4, 4, 6], [0, 1, 1], [2, 4, 10]]]) == 26\nassert my_solution.maximizeTheProfit(*[13, [[2, 2, 5], [1, 8, 10], [2, 3, 3]]]) == 10\nassert my_solution.maximizeTheProfit(*[2, [[1, 1, 8], [1, 1, 8], [1, 1, 10], [1, 1, 7], [0, 0, 7], [0, 0, 3], [0, 1, 8], [0, 0, 4], [0, 0, 4], [0, 0, 7], [0, 0, 10], [0, 1, 4], [1, 1, 1], [0, 1, 5]]]) == 20\nassert my_solution.maximizeTheProfit(*[3, [[0, 1, 7], [1, 1, 3], [0, 0, 2], [1, 1, 6], [0, 0, 10], [1, 1, 7], [0, 2, 3], [0, 1, 2], [0, 0, 7]]]) == 17\nassert my_solution.maximizeTheProfit(*[5, [[0, 0, 5], [1, 3, 9], [0, 2, 2], [1, 1, 6], [1, 2, 10], [0, 2, 10], [1, 1, 3]]]) == 15\nassert my_solution.maximizeTheProfit(*[10, [[0, 1, 9], [5, 6, 10], [1, 3, 8], [1, 9, 7], [7, 8, 1], [2, 7, 1], [0, 8, 7], [1, 6, 6], [1, 4, 4], [0, 5, 4], [0, 0, 3], [0, 8, 6]]]) == 22\nassert my_solution.maximizeTheProfit(*[4, [[0, 0, 1], [0, 0, 10], [0, 2, 1], [0, 0, 6], [0, 3, 10], [0, 1, 5], [1, 2, 10], [0, 0, 2], [3, 3, 1], [0, 0, 9], [0, 1, 2], [0, 0, 4], [1, 3, 5], [1, 1, 1]]]) == 21\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2979", "questionFrontendId": "2830", "questionTitle": "Maximize the Profit as the Salesman", "stats": { "totalAccepted": "5.8K", "totalSubmission": "13.7K", "totalAcceptedRaw": 5837, "totalSubmissionRaw": 13747, "acRate": "42.5%" } }
LeetCode/2832
# Find the Longest Equal Subarray You are given a **0-indexed** integer array `nums` and an integer `k`. A subarray is called **equal** if all of its elements are equal. Note that the empty subarray is an **equal** subarray. Return *the length of the **longest** possible equal subarray after deleting **at most*** `k` *elements from* `nums`. A **subarray** is a contiguous, possibly empty sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [1,3,2,3,1,3], k = 3 **Output:** 3 **Explanation:** It's optimal to delete the elements at index 2 and index 4. After deleting them, nums becomes equal to [1, 3, 3, 3]. The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created. ``` **Example 2:** ``` **Input:** nums = [1,1,2,2,1,1], k = 2 **Output:** 4 **Explanation:** It's optimal to delete the elements at index 2 and index 3. After deleting them, nums becomes equal to [1, 1, 1, 1]. The array itself is an equal subarray, so the answer is 4. It can be proven that no longer equal subarrays can be created. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= nums.length` * `0 <= k <= nums.length` Please make sure your answer follows the type signature below: ```python3 class Solution: def longestEqualSubarray(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.longestEqualSubarray(*[[1, 3, 2, 3, 1, 3], 3]) == 3\nassert my_solution.longestEqualSubarray(*[[1, 1, 2, 2, 1, 1], 2]) == 4\nassert my_solution.longestEqualSubarray(*[[1], 0]) == 1\nassert my_solution.longestEqualSubarray(*[[1], 1]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 1], 1]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 2], 1]) == 2\nassert my_solution.longestEqualSubarray(*[[1, 1], 0]) == 2\nassert my_solution.longestEqualSubarray(*[[2, 1], 0]) == 1\nassert my_solution.longestEqualSubarray(*[[1, 2], 1]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 2], 2]) == 2\nassert my_solution.longestEqualSubarray(*[[2, 3, 2], 1]) == 2\nassert my_solution.longestEqualSubarray(*[[3, 2, 2], 1]) == 2\nassert my_solution.longestEqualSubarray(*[[3, 1, 1], 2]) == 2\nassert my_solution.longestEqualSubarray(*[[1, 2, 3], 2]) == 1\nassert my_solution.longestEqualSubarray(*[[1, 2, 3], 3]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 3, 1], 2]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 2, 1], 1]) == 2\nassert my_solution.longestEqualSubarray(*[[1, 3, 2], 3]) == 1\nassert my_solution.longestEqualSubarray(*[[2, 3, 3], 3]) == 2\nassert my_solution.longestEqualSubarray(*[[3, 2, 3], 3]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2832", "questionFrontendId": "2831", "questionTitle": "Find the Longest Equal Subarray", "stats": { "totalAccepted": "4.9K", "totalSubmission": "11.9K", "totalAcceptedRaw": 4881, "totalSubmissionRaw": 11886, "acRate": "41.1%" } }
LeetCode/2917
# Count Pairs Whose Sum is Less than Target Given a **0-indexed** integer array `nums` of length `n` and an integer `target`, return *the number of pairs* `(i, j)` *where* `0 <= i < j < n` *and* `nums[i] + nums[j] < target`.   **Example 1:** ``` **Input:** nums = [-1,1,2,3,1], target = 2 **Output:** 3 **Explanation:** There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target. ``` **Example 2:** ``` **Input:** nums = [-6,2,5,-2,-7,-1,3], target = -2 **Output:** 10 **Explanation:** There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target ```   **Constraints:** * `1 <= nums.length == n <= 50` * `-50 <= nums[i], target <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def countPairs(self, nums: List[int], target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countPairs(*[[-1, 1, 2, 3, 1], 2]) == 3\nassert my_solution.countPairs(*[[-6, 2, 5, -2, -7, -1, 3], -2]) == 10\nassert my_solution.countPairs(*[[9, -5, -5, 5, -5, -4, -6, 6, -6], 3]) == 27\nassert my_solution.countPairs(*[[-8, -5, 5, -4, 10], 2]) == 6\nassert my_solution.countPairs(*[[-5, 0, -7, -1, 9, 8, -9, 9], -14]) == 1\nassert my_solution.countPairs(*[[6, -1, 7, 4, 2, 3], 8]) == 8\nassert my_solution.countPairs(*[[2, 8, 2, 8, 7], 10]) == 3\nassert my_solution.countPairs(*[[-6, 1, 1, -1, -10, -7, 1, -5, -4, 0], -15]) == 2\nassert my_solution.countPairs(*[[10, -2, -1, 7, 8, 5, 3, -4, -9], -10]) == 2\nassert my_solution.countPairs(*[[3, 8, -3, 4, 10, -6], 1]) == 4\nassert my_solution.countPairs(*[[-4, -6, -7, 8], -13]) == 0\nassert my_solution.countPairs(*[[-4, 0, 10, 8, -2], 0]) == 3\nassert my_solution.countPairs(*[[-8, -5, -3, 1, -7], -6]) == 7\nassert my_solution.countPairs(*[[4, -8, -2, 5, 2, -9, 6, 5, -4], -4]) == 9\nassert my_solution.countPairs(*[[-1, -5, 4, 4, -10], -6]) == 2\nassert my_solution.countPairs(*[[-5, 4, -6, -5, -10, -1, 10, 3], 6]) == 24\nassert my_solution.countPairs(*[[-9, 6, -4, 10, 1, 8], 11]) == 11\nassert my_solution.countPairs(*[[-10, -6, -8, -9, 6, 6, -6, -6, -3], -2]) == 25\nassert my_solution.countPairs(*[[-7, 7, 6, -9, -4, 10, 8, -8, 2, 2], -1]) == 17\nassert my_solution.countPairs(*[[0, -1, 0, -6, -9], -9]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2917", "questionFrontendId": "2824", "questionTitle": "Count Pairs Whose Sum is Less than Target", "stats": { "totalAccepted": "31.1K", "totalSubmission": "35.7K", "totalAcceptedRaw": 31062, "totalSubmissionRaw": 35741, "acRate": "86.9%" } }
LeetCode/3018
# Make String a Subsequence Using Cyclic Increments You are given two **0-indexed** strings `str1` and `str2`. In an operation, you select a **set** of indices in `str1`, and for each index `i` in the set, increment `str1[i]` to the next character **cyclically**. That is `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`. Return `true` *if it is possible to make* `str2` *a subsequence of* `str1` *by performing the operation **at most once***, *and* `false` *otherwise*. **Note:** A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.   **Example 1:** ``` **Input:** str1 = "abc", str2 = "ad" **Output:** true **Explanation:** Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned. ``` **Example 2:** ``` **Input:** str1 = "zc", str2 = "ad" **Output:** true **Explanation:** Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned. ``` **Example 3:** ``` **Input:** str1 = "ab", str2 = "d" **Output:** false **Explanation:** In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned. ```   **Constraints:** * `1 <= str1.length <= 105` * `1 <= str2.length <= 105` * `str1` and `str2` consist of only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def canMakeSubsequence(self, str1: str, str2: str) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canMakeSubsequence(*['abc', 'ad']) == True\nassert my_solution.canMakeSubsequence(*['zc', 'ad']) == True\nassert my_solution.canMakeSubsequence(*['ab', 'd']) == False\nassert my_solution.canMakeSubsequence(*['a', 'd']) == False\nassert my_solution.canMakeSubsequence(*['b', 'c']) == True\nassert my_solution.canMakeSubsequence(*['b', 'v']) == False\nassert my_solution.canMakeSubsequence(*['c', 'b']) == False\nassert my_solution.canMakeSubsequence(*['c', 'k']) == False\nassert my_solution.canMakeSubsequence(*['c', 'm']) == False\nassert my_solution.canMakeSubsequence(*['d', 'h']) == False\nassert my_solution.canMakeSubsequence(*['d', 'm']) == False\nassert my_solution.canMakeSubsequence(*['d', 'x']) == False\nassert my_solution.canMakeSubsequence(*['f', 'f']) == True\nassert my_solution.canMakeSubsequence(*['f', 'g']) == True\nassert my_solution.canMakeSubsequence(*['f', 's']) == False\nassert my_solution.canMakeSubsequence(*['g', 'g']) == True\nassert my_solution.canMakeSubsequence(*['h', 'i']) == True\nassert my_solution.canMakeSubsequence(*['i', 'e']) == False\nassert my_solution.canMakeSubsequence(*['i', 'j']) == True\nassert my_solution.canMakeSubsequence(*['j', 'j']) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3018", "questionFrontendId": "2825", "questionTitle": "Make String a Subsequence Using Cyclic Increments", "stats": { "totalAccepted": "4.2K", "totalSubmission": "7.6K", "totalAcceptedRaw": 4195, "totalSubmissionRaw": 7638, "acRate": "54.9%" } }
LeetCode/2904
# Sorting Three Groups You are given a **0-indexed** integer array `nums` of length `n`. The numbers from `0` to `n - 1` are divided into three groups numbered from `1` to `3`, where number `i` belongs to group `nums[i]`. Notice that some groups may be **empty**. You are allowed to perform this operation any number of times: * Pick number `x` and change its group. More formally, change `nums[x]` to any number from `1` to `3`. A new array `res` is constructed using the following procedure: 1. Sort the numbers in each group independently. 2. Append the elements of groups `1`, `2`, and `3` to `res` **in this order**. Array `nums` is called a **beautiful array** if the constructed array `res` is sorted in **non-decreasing** order. Return *the **minimum** number of operations to make* `nums` *a **beautiful array***.   **Example 1:** ``` **Input:** nums = [2,1,3,2,1] **Output:** 3 **Explanation:** It's optimal to perform three operations: 1. change nums[0] to 1. 2. change nums[2] to 1. 3. change nums[3] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than three operations. ``` **Example 2:** ``` **Input:** nums = [1,3,2,1,3,3] **Output:** 2 **Explanation:** It's optimal to perform two operations: 1. change nums[1] to 1. 2. change nums[2] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than two operations. ``` **Example 3:** ``` **Input:** nums = [2,2,2,2,3,3] **Output:** 0 **Explanation:** It's optimal to not perform operations. After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 3` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumOperations(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumOperations(*[[2, 1, 3, 2, 1]]) == 3\nassert my_solution.minimumOperations(*[[1, 3, 2, 1, 3, 3]]) == 2\nassert my_solution.minimumOperations(*[[2, 2, 2, 2, 3, 3]]) == 0\nassert my_solution.minimumOperations(*[[1]]) == 0\nassert my_solution.minimumOperations(*[[2]]) == 0\nassert my_solution.minimumOperations(*[[3]]) == 0\nassert my_solution.minimumOperations(*[[1, 2]]) == 0\nassert my_solution.minimumOperations(*[[2, 2]]) == 0\nassert my_solution.minimumOperations(*[[3, 2]]) == 1\nassert my_solution.minimumOperations(*[[1, 3]]) == 0\nassert my_solution.minimumOperations(*[[2, 3]]) == 0\nassert my_solution.minimumOperations(*[[3, 3]]) == 0\nassert my_solution.minimumOperations(*[[1, 1, 2]]) == 0\nassert my_solution.minimumOperations(*[[2, 1, 2]]) == 1\nassert my_solution.minimumOperations(*[[3, 1, 2]]) == 1\nassert my_solution.minimumOperations(*[[1, 2, 2]]) == 0\nassert my_solution.minimumOperations(*[[2, 2, 2]]) == 0\nassert my_solution.minimumOperations(*[[3, 2, 2]]) == 1\nassert my_solution.minimumOperations(*[[1, 3, 2]]) == 1\nassert my_solution.minimumOperations(*[[2, 3, 2]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2904", "questionFrontendId": "2826", "questionTitle": "Sorting Three Groups", "stats": { "totalAccepted": "3.8K", "totalSubmission": "7.2K", "totalAcceptedRaw": 3831, "totalSubmissionRaw": 7183, "acRate": "53.3%" } }
LeetCode/3017
# Number of Beautiful Integers in the Range You are given positive integers `low`, `high`, and `k`. A number is **beautiful** if it meets both of the following conditions: * The count of even digits in the number is equal to the count of odd digits. * The number is divisible by `k`. Return *the number of beautiful integers in the range* `[low, high]`.   **Example 1:** ``` **Input:** low = 10, high = 20, k = 3 **Output:** 2 **Explanation:** There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range. ``` **Example 2:** ``` **Input:** low = 1, high = 10, k = 1 **Output:** 1 **Explanation:** There is 1 beautiful integer in the given range: [10]. - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range. ``` **Example 3:** ``` **Input:** low = 5, high = 5, k = 2 **Output:** 0 **Explanation:** There are 0 beautiful integers in the given range. - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits. ```   **Constraints:** * `0 < low <= high <= 109` * `0 < k <= 20` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfBeautifulIntegers(*[10, 20, 3]) == 2\nassert my_solution.numberOfBeautifulIntegers(*[1, 10, 1]) == 1\nassert my_solution.numberOfBeautifulIntegers(*[5, 5, 2]) == 0\nassert my_solution.numberOfBeautifulIntegers(*[3, 31, 16]) == 1\nassert my_solution.numberOfBeautifulIntegers(*[25, 31, 11]) == 0\nassert my_solution.numberOfBeautifulIntegers(*[9, 25, 4]) == 2\nassert my_solution.numberOfBeautifulIntegers(*[58, 72, 16]) == 0\nassert my_solution.numberOfBeautifulIntegers(*[5, 79, 12]) == 3\nassert my_solution.numberOfBeautifulIntegers(*[26, 74, 7]) == 4\nassert my_solution.numberOfBeautifulIntegers(*[36, 65, 7]) == 3\nassert my_solution.numberOfBeautifulIntegers(*[12, 84, 8]) == 4\nassert my_solution.numberOfBeautifulIntegers(*[13, 91, 13]) == 3\nassert my_solution.numberOfBeautifulIntegers(*[8, 18, 4]) == 2\nassert my_solution.numberOfBeautifulIntegers(*[22, 59, 6]) == 3\nassert my_solution.numberOfBeautifulIntegers(*[15, 27, 9]) == 2\nassert my_solution.numberOfBeautifulIntegers(*[4, 9, 19]) == 0\nassert my_solution.numberOfBeautifulIntegers(*[14, 81, 17]) == 1\nassert my_solution.numberOfBeautifulIntegers(*[12, 33, 18]) == 1\nassert my_solution.numberOfBeautifulIntegers(*[10, 17, 8]) == 1\nassert my_solution.numberOfBeautifulIntegers(*[42, 58, 3]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3017", "questionFrontendId": "2827", "questionTitle": "Number of Beautiful Integers in the Range", "stats": { "totalAccepted": "2.6K", "totalSubmission": "7.6K", "totalAcceptedRaw": 2579, "totalSubmissionRaw": 7589, "acRate": "34.0%" } }
LeetCode/2902
# Max Pair Sum in an Array You are given a **0-indexed** integer array `nums`. You have to find the **maximum** sum of a pair of numbers from `nums` such that the maximum **digit** in both numbers are equal. Return *the maximum sum or* `-1` *if no such pair exists*.   **Example 1:** ``` **Input:** nums = [51,71,17,24,42] **Output:** 88 **Explanation:** For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66. It can be shown that there are no other pairs with equal maximum digits, so the answer is 88. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** -1 **Explanation:** No pair exists in nums with equal maximum digits. ```   **Constraints:** * `2 <= nums.length <= 100` * `1 <= nums[i] <= 104` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxSum(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSum(*[[51, 71, 17, 24, 42]]) == 88\nassert my_solution.maxSum(*[[1, 2, 3, 4]]) == -1\nassert my_solution.maxSum(*[[31, 25, 72, 79, 74]]) == 146\nassert my_solution.maxSum(*[[84, 91, 18, 59, 27, 9, 81, 33, 17, 58]]) == 165\nassert my_solution.maxSum(*[[8, 75, 28, 35, 21, 13, 21]]) == 42\nassert my_solution.maxSum(*[[35, 52, 74, 92, 25, 65, 77, 1, 73, 32]]) == 151\nassert my_solution.maxSum(*[[68, 8, 100, 84, 80, 14, 88]]) == 172\nassert my_solution.maxSum(*[[53, 98, 69, 64, 40, 60, 23]]) == 167\nassert my_solution.maxSum(*[[21, 76]]) == -1\nassert my_solution.maxSum(*[[99, 63, 23, 70, 18, 64]]) == 127\nassert my_solution.maxSum(*[[21, 21, 78]]) == 42\nassert my_solution.maxSum(*[[58, 88, 58, 99, 26, 92]]) == 191\nassert my_solution.maxSum(*[[10, 24, 25, 20, 92, 73, 63, 51]]) == 76\nassert my_solution.maxSum(*[[87, 6, 17, 32, 14, 42, 46, 65, 43, 9]]) == 111\nassert my_solution.maxSum(*[[96, 46, 85, 19, 29]]) == 125\nassert my_solution.maxSum(*[[5, 24]]) == -1\nassert my_solution.maxSum(*[[26, 76, 24, 96, 82, 97, 97, 72, 35]]) == 194\nassert my_solution.maxSum(*[[77, 82, 30, 94]]) == -1\nassert my_solution.maxSum(*[[76, 94, 51, 82, 3, 89, 52, 96]]) == 190\nassert my_solution.maxSum(*[[27, 59, 57, 97, 6, 46, 88, 41, 52, 46]]) == 156\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2902", "questionFrontendId": "2815", "questionTitle": "Max Pair Sum in an Array", "stats": { "totalAccepted": "8K", "totalSubmission": "11.8K", "totalAcceptedRaw": 8044, "totalSubmissionRaw": 11816, "acRate": "68.1%" } }
LeetCode/3000
# Minimum Absolute Difference Between Elements With Constraint You are given a **0-indexed** integer array `nums` and an integer `x`. Find the **minimum absolute difference** between two elements in the array that are at least `x` indices apart. In other words, find two indices `i` and `j` such that `abs(i - j) >= x` and `abs(nums[i] - nums[j])` is minimized. Return *an integer denoting the **minimum** absolute difference between two elements that are at least* `x` *indices apart*.   **Example 1:** ``` **Input:** nums = [4,3,2,4], x = 2 **Output:** 0 **Explanation:** We can select nums[0] = 4 and nums[3] = 4. They are at least 2 indices apart, and their absolute difference is the minimum, 0. It can be shown that 0 is the optimal answer. ``` **Example 2:** ``` **Input:** nums = [5,3,2,10,15], x = 1 **Output:** 1 **Explanation:** We can select nums[1] = 3 and nums[2] = 2. They are at least 1 index apart, and their absolute difference is the minimum, 1. It can be shown that 1 is the optimal answer. ``` **Example 3:** ``` **Input:** nums = [1,2,3,4], x = 3 **Output:** 3 **Explanation:** We can select nums[0] = 1 and nums[3] = 4. They are at least 3 indices apart, and their absolute difference is the minimum, 3. It can be shown that 3 is the optimal answer. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `0 <= x < nums.length` Please make sure your answer follows the type signature below: ```python3 class Solution: def minAbsoluteDifference(self, nums: List[int], x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minAbsoluteDifference(*[[4, 3, 2, 4], 2]) == 0\nassert my_solution.minAbsoluteDifference(*[[5, 3, 2, 10, 15], 1]) == 1\nassert my_solution.minAbsoluteDifference(*[[1, 2, 3, 4], 3]) == 3\nassert my_solution.minAbsoluteDifference(*[[1, 67], 1]) == 66\nassert my_solution.minAbsoluteDifference(*[[7, 398], 1]) == 391\nassert my_solution.minAbsoluteDifference(*[[12, 141], 1]) == 129\nassert my_solution.minAbsoluteDifference(*[[21, 75], 1]) == 54\nassert my_solution.minAbsoluteDifference(*[[22, 147], 1]) == 125\nassert my_solution.minAbsoluteDifference(*[[25, 197], 1]) == 172\nassert my_solution.minAbsoluteDifference(*[[27, 275], 1]) == 248\nassert my_solution.minAbsoluteDifference(*[[37, 192], 1]) == 155\nassert my_solution.minAbsoluteDifference(*[[41, 163], 1]) == 122\nassert my_solution.minAbsoluteDifference(*[[45, 49], 1]) == 4\nassert my_solution.minAbsoluteDifference(*[[48, 195], 1]) == 147\nassert my_solution.minAbsoluteDifference(*[[68, 68], 1]) == 0\nassert my_solution.minAbsoluteDifference(*[[71, 4], 1]) == 67\nassert my_solution.minAbsoluteDifference(*[[72, 169], 1]) == 97\nassert my_solution.minAbsoluteDifference(*[[74, 62], 1]) == 12\nassert my_solution.minAbsoluteDifference(*[[75, 1], 1]) == 74\nassert my_solution.minAbsoluteDifference(*[[76, 49], 1]) == 27\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3000", "questionFrontendId": "2817", "questionTitle": "Minimum Absolute Difference Between Elements With Constraint", "stats": { "totalAccepted": "5.1K", "totalSubmission": "16K", "totalAcceptedRaw": 5075, "totalSubmissionRaw": 15974, "acRate": "31.8%" } }
LeetCode/3001
# Apply Operations to Maximize Score You are given an array `nums` of `n` positive integers and an integer `k`. Initially, you start with a score of `1`. You have to maximize your score by applying the following operation at most `k` times: * Choose any **non-empty** subarray `nums[l, ..., r]` that you haven't chosen previously. * Choose an element `x` of `nums[l, ..., r]` with the highest **prime score**. If multiple such elements exist, choose the one with the smallest index. * Multiply your score by `x`. Here, `nums[l, ..., r]` denotes the subarray of `nums` starting at index `l` and ending at the index `r`, both ends being inclusive. The **prime score** of an integer `x` is equal to the number of distinct prime factors of `x`. For example, the prime score of `300` is `3` since `300 = 2 * 2 * 3 * 5 * 5`. Return *the **maximum possible score** after applying at most* `k` *operations*. Since the answer may be large, return it modulo `109 + 7`.   **Example 1:** ``` **Input:** nums = [8,3,9,3,8], k = 2 **Output:** 81 **Explanation:** To get a score of 81, we can apply the following operations: - Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81. It can be proven that 81 is the highest score one can obtain. ``` **Example 2:** ``` **Input:** nums = [19,12,14,6,10,18], k = 3 **Output:** 4788 **Explanation:** To get a score of 4788, we can apply the following operations: - Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19. - Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788. It can be proven that 4788 is the highest score one can obtain. ```   **Constraints:** * `1 <= nums.length == n <= 105` * `1 <= nums[i] <= 105` * `1 <= k <= min(n * (n + 1) / 2, 109)` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumScore(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumScore(*[[8, 3, 9, 3, 8], 2]) == 81\nassert my_solution.maximumScore(*[[19, 12, 14, 6, 10, 18], 3]) == 4788\nassert my_solution.maximumScore(*[[3289, 2832, 14858, 22011], 6]) == 256720975\nassert my_solution.maximumScore(*[[1, 7, 11, 1, 5], 14]) == 823751938\nassert my_solution.maximumScore(*[[1, 1, 2, 18, 1, 9, 3, 1], 32]) == 346264255\nassert my_solution.maximumScore(*[[1, 1, 1], 2]) == 1\nassert my_solution.maximumScore(*[[1, 2, 1, 12, 1, 3], 3]) == 1728\nassert my_solution.maximumScore(*[[12, 5, 1, 6, 9, 1, 17, 14], 12]) == 62996359\nassert my_solution.maximumScore(*[[1], 1]) == 1\nassert my_solution.maximumScore(*[[1, 10, 15, 1, 3], 13]) == 499978741\nassert my_solution.maximumScore(*[[6, 1, 13, 10, 1, 17, 6], 27]) == 630596200\nassert my_solution.maximumScore(*[[1, 14], 1]) == 14\nassert my_solution.maximumScore(*[[2, 1, 14, 5, 18, 1, 8, 5], 34]) == 799392504\nassert my_solution.maximumScore(*[[5, 12, 11, 15, 10, 18], 18]) == 557423913\nassert my_solution.maximumScore(*[[4, 1], 3]) == 16\nassert my_solution.maximumScore(*[[1, 2, 5, 1, 10, 1, 1], 20]) == 600000014\nassert my_solution.maximumScore(*[[13, 16, 12, 15, 12, 1, 13, 1, 18, 1], 46]) == 912532739\nassert my_solution.maximumScore(*[[10, 11], 3]) == 1100\nassert my_solution.maximumScore(*[[15, 16, 12, 1, 10, 14], 19]) == 311972352\nassert my_solution.maximumScore(*[[14, 12, 5, 2, 14], 6]) == 7529536\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "3001", "questionFrontendId": "2818", "questionTitle": "Apply Operations to Maximize Score", "stats": { "totalAccepted": "3.8K", "totalSubmission": "8.2K", "totalAcceptedRaw": 3825, "totalSubmissionRaw": 8220, "acRate": "46.5%" } }
LeetCode/2886
# Faulty Keyboard Your laptop keyboard is faulty, and whenever you type a character `'i'` on it, it reverses the string that you have written. Typing other characters works as expected. You are given a **0-indexed** string `s`, and you type each character of `s` using your faulty keyboard. Return *the final string that will be present on your laptop screen.*   **Example 1:** ``` **Input:** s = "string" **Output:** "rtsng" **Explanation:** After typing first character, the text on the screen is "s". After the second character, the text is "st". After the third character, the text is "str". Since the fourth character is an 'i', the text gets reversed and becomes "rts". After the fifth character, the text is "rtsn". After the sixth character, the text is "rtsng". Therefore, we return "rtsng". ``` **Example 2:** ``` **Input:** s = "poiinter" **Output:** "ponter" **Explanation:** After the first character, the text on the screen is "p". After the second character, the text is "po". Since the third character you type is an 'i', the text gets reversed and becomes "op". Since the fourth character you type is an 'i', the text gets reversed and becomes "po". After the fifth character, the text is "pon". After the sixth character, the text is "pont". After the seventh character, the text is "ponte". After the eighth character, the text is "ponter". Therefore, we return "ponter". ```   **Constraints:** * `1 <= s.length <= 100` * `s` consists of lowercase English letters. * `s[0] != 'i'` Please make sure your answer follows the type signature below: ```python3 class Solution: def finalString(self, s: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.finalString(*['string']) == rtsng\nassert my_solution.finalString(*['poiinter']) == ponter\nassert my_solution.finalString(*['goci']) == cog\nassert my_solution.finalString(*['ksi']) == sk\nassert my_solution.finalString(*['fii']) == f\nassert my_solution.finalString(*['qskyviiiii']) == vyksq\nassert my_solution.finalString(*['pft']) == pft\nassert my_solution.finalString(*['viwif']) == wvf\nassert my_solution.finalString(*['wiie']) == we\nassert my_solution.finalString(*['kiis']) == ks\nassert my_solution.finalString(*['xihbosxitx']) == xsobhxtx\nassert my_solution.finalString(*['uwioili']) == lwuo\nassert my_solution.finalString(*['aapziai']) == aaapz\nassert my_solution.finalString(*['pviist']) == pvst\nassert my_solution.finalString(*['miiuiei']) == emu\nassert my_solution.finalString(*['diiiiq']) == dq\nassert my_solution.finalString(*['eirov']) == erov\nassert my_solution.finalString(*['niiiiisiii']) == sn\nassert my_solution.finalString(*['siiuii']) == su\nassert my_solution.finalString(*['piijciivq']) == pjcvq\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2886", "questionFrontendId": "2810", "questionTitle": "Faulty Keyboard", "stats": { "totalAccepted": "9.1K", "totalSubmission": "11.1K", "totalAcceptedRaw": 9146, "totalSubmissionRaw": 11110, "acRate": "82.3%" } }
LeetCode/2916
# Check if it is Possible to Split Array You are given an array `nums` of length `n` and an integer `m`. You need to determine if it is possible to split the array into `n` **non-empty** arrays by performing a series of steps. In each step, you can select an existing array (which may be the result of previous steps) with a length of **at least two** and split it into **two** subarrays, if, **for each** resulting subarray, **at least** one of the following holds: * The length of the subarray is one, or * The sum of elements of the subarray is **greater than or equal** to `m`. Return `true` *if you can split the given array into* `n` *arrays, otherwise return* `false`. **Note:** A subarray is *a contiguous non-empty sequence of elements within an array*.   **Example 1:** ``` **Input:** nums = [2, 2, 1], m = 4 **Output:** true **Explanation:** We can split the array into [2, 2] and [1] in the first step. Then, in the second step, we can split [2, 2] into [2] and [2]. As a result, the answer is true. ``` **Example 2:** ``` **Input:** nums = [2, 1, 3], m = 5 **Output:** false **Explanation:** We can try splitting the array in two different ways: the first way is to have [2, 1] and [3], and the second way is to have [2] and [1, 3]. However, both of these ways are not valid. So, the answer is false. ``` **Example 3:** ``` **Input:** nums = [2, 3, 3, 2, 3], m = 6 **Output:** true **Explanation:** We can split the array into [2, 3, 3, 2] and [3] in the first step. Then, in the second step, we can split [2, 3, 3, 2] into [2, 3, 3] and [2]. Then, in the third step, we can split [2, 3, 3] into [2] and [3, 3]. And in the last step we can split [3, 3] into [3] and [3]. As a result, the answer is true. ```   **Constraints:** * `1 <= n == nums.length <= 100` * `1 <= nums[i] <= 100` * `1 <= m <= 200` Please make sure your answer follows the type signature below: ```python3 class Solution: def canSplitArray(self, nums: List[int], m: int) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canSplitArray(*[[2, 2, 1], 4]) == True\nassert my_solution.canSplitArray(*[[2, 1, 3], 5]) == False\nassert my_solution.canSplitArray(*[[2, 3, 3, 2, 3], 6]) == True\nassert my_solution.canSplitArray(*[[1], 1]) == True\nassert my_solution.canSplitArray(*[[2], 1]) == True\nassert my_solution.canSplitArray(*[[3], 1]) == True\nassert my_solution.canSplitArray(*[[1], 2]) == True\nassert my_solution.canSplitArray(*[[2], 2]) == True\nassert my_solution.canSplitArray(*[[3], 2]) == True\nassert my_solution.canSplitArray(*[[3], 3]) == True\nassert my_solution.canSplitArray(*[[7], 5]) == True\nassert my_solution.canSplitArray(*[[4], 7]) == True\nassert my_solution.canSplitArray(*[[9], 7]) == True\nassert my_solution.canSplitArray(*[[2], 8]) == True\nassert my_solution.canSplitArray(*[[4], 8]) == True\nassert my_solution.canSplitArray(*[[10], 11]) == True\nassert my_solution.canSplitArray(*[[6], 12]) == True\nassert my_solution.canSplitArray(*[[2], 14]) == True\nassert my_solution.canSplitArray(*[[3], 18]) == True\nassert my_solution.canSplitArray(*[[9, 7], 1]) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2916", "questionFrontendId": "2811", "questionTitle": "Check if it is Possible to Split Array", "stats": { "totalAccepted": "7K", "totalSubmission": "21.6K", "totalAcceptedRaw": 7044, "totalSubmissionRaw": 21582, "acRate": "32.6%" } }
LeetCode/2894
# Maximum Elegance of a K-Length Subsequence You are given a **0-indexed** 2D integer array `items` of length `n` and an integer `k`. `items[i] = [profiti, categoryi]`, where `profiti` and `categoryi` denote the profit and category of the `ith` item respectively. Let's define the **elegance** of a **subsequence** of `items` as `total_profit + distinct_categories2`, where `total_profit` is the sum of all profits in the subsequence, and `distinct_categories` is the number of **distinct** categories from all the categories in the selected subsequence. Your task is to find the **maximum elegance** from all subsequences of size `k` in `items`. Return *an integer denoting the maximum elegance of a subsequence of* `items` *with size exactly* `k`. **Note:** A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order.   **Example 1:** ``` **Input:** items = [[3,2],[5,1],[10,1]], k = 2 **Output:** 17 **Explanation:** In this example, we have to select a subsequence of size 2. We can select items[0] = [3,2] and items[2] = [10,1]. The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1]. Hence, the elegance is 13 + 22 = 17, and we can show that it is the maximum achievable elegance. ``` **Example 2:** ``` **Input:** items = [[3,1],[3,1],[2,2],[5,3]], k = 3 **Output:** 19 **Explanation:** In this example, we have to select a subsequence of size 3. We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3]. The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3]. Hence, the elegance is 10 + 32 = 19, and we can show that it is the maximum achievable elegance. ``` **Example 3:** ``` **Input:** items = [[1,1],[2,1],[3,1]], k = 3 **Output:** 7 **Explanation:** In this example, we have to select a subsequence of size 3. We should select all the items. The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1]. Hence, the maximum elegance is 6 + 12 = 7. ```   **Constraints:** * `1 <= items.length == n <= 105` * `items[i].length == 2` * `items[i][0] == profiti` * `items[i][1] == categoryi` * `1 <= profiti <= 109` * `1 <= categoryi <= n` * `1 <= k <= n` Please make sure your answer follows the type signature below: ```python3 class Solution: def findMaximumElegance(self, items: List[List[int]], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMaximumElegance(*[[[3, 2], [5, 1], [10, 1]], 2]) == 17\nassert my_solution.findMaximumElegance(*[[[3, 1], [3, 1], [2, 2], [5, 3]], 3]) == 19\nassert my_solution.findMaximumElegance(*[[[1, 1], [2, 1], [3, 1]], 3]) == 7\nassert my_solution.findMaximumElegance(*[[[1, 1], [1, 2]], 2]) == 6\nassert my_solution.findMaximumElegance(*[[[1, 1], [4, 1]], 1]) == 5\nassert my_solution.findMaximumElegance(*[[[1, 1], [4, 1]], 2]) == 6\nassert my_solution.findMaximumElegance(*[[[1, 1], [6, 1]], 1]) == 7\nassert my_solution.findMaximumElegance(*[[[1, 1], [6, 1]], 2]) == 8\nassert my_solution.findMaximumElegance(*[[[1, 1], [8, 2]], 2]) == 13\nassert my_solution.findMaximumElegance(*[[[1, 2], [6, 2]], 1]) == 7\nassert my_solution.findMaximumElegance(*[[[1, 2], [10, 1]], 1]) == 11\nassert my_solution.findMaximumElegance(*[[[2, 1], [6, 1]], 2]) == 9\nassert my_solution.findMaximumElegance(*[[[2, 1], [7, 1]], 2]) == 10\nassert my_solution.findMaximumElegance(*[[[2, 1], [9, 2]], 1]) == 10\nassert my_solution.findMaximumElegance(*[[[2, 2], [2, 2]], 1]) == 3\nassert my_solution.findMaximumElegance(*[[[2, 2], [2, 2]], 2]) == 5\nassert my_solution.findMaximumElegance(*[[[2, 2], [3, 1]], 2]) == 9\nassert my_solution.findMaximumElegance(*[[[2, 2], [6, 1]], 2]) == 12\nassert my_solution.findMaximumElegance(*[[[3, 1], [1, 2]], 2]) == 8\nassert my_solution.findMaximumElegance(*[[[3, 1], [9, 1]], 2]) == 13\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2894", "questionFrontendId": "2813", "questionTitle": "Maximum Elegance of a K-Length Subsequence", "stats": { "totalAccepted": "2.4K", "totalSubmission": "5.9K", "totalAcceptedRaw": 2415, "totalSubmissionRaw": 5872, "acRate": "41.1%" } }
LeetCode/2955
# Account Balance After Rounded Purchase Initially, you have a bank account balance of `100` dollars. You are given an integer `purchaseAmount` representing the amount you will spend on a purchase in dollars. At the store where you will make the purchase, the purchase amount is rounded to the **nearest multiple** of `10`. In other words, you pay a **non-negative** amount, `roundedAmount`, such that `roundedAmount` is a multiple of `10` and `abs(roundedAmount - purchaseAmount)` is **minimized**. If there is more than one nearest multiple of `10`, the **largest multiple** is chosen. Return *an integer denoting your account balance after making a purchase worth* `purchaseAmount` *dollars from the store.* **Note:** `0` is considered to be a multiple of `10` in this problem.   **Example 1:** ``` **Input:** purchaseAmount = 9 **Output:** 90 **Explanation:** In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90. ``` **Example 2:** ``` **Input:** purchaseAmount = 15 **Output:** 80 **Explanation:** In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen. Hence, your account balance becomes 100 - 20 = 80. ```   **Constraints:** * `0 <= purchaseAmount <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.accountBalanceAfterPurchase(*[9]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[15]) == 80\nassert my_solution.accountBalanceAfterPurchase(*[10]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[11]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[12]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[13]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[14]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[16]) == 80\nassert my_solution.accountBalanceAfterPurchase(*[17]) == 80\nassert my_solution.accountBalanceAfterPurchase(*[18]) == 80\nassert my_solution.accountBalanceAfterPurchase(*[19]) == 80\nassert my_solution.accountBalanceAfterPurchase(*[1]) == 100\nassert my_solution.accountBalanceAfterPurchase(*[2]) == 100\nassert my_solution.accountBalanceAfterPurchase(*[3]) == 100\nassert my_solution.accountBalanceAfterPurchase(*[4]) == 100\nassert my_solution.accountBalanceAfterPurchase(*[5]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[6]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[7]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[8]) == 90\nassert my_solution.accountBalanceAfterPurchase(*[20]) == 80\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2955", "questionFrontendId": "2806", "questionTitle": "Account Balance After Rounded Purchase", "stats": { "totalAccepted": "4.7K", "totalSubmission": "7.5K", "totalAcceptedRaw": 4681, "totalSubmissionRaw": 7508, "acRate": "62.3%" } }
LeetCode/2920
# Minimum Seconds to Equalize a Circular Array You are given a **0-indexed** array `nums` containing `n` integers. At each second, you perform the following operation on the array: * For every index `i` in the range `[0, n - 1]`, replace `nums[i]` with either `nums[i]`, `nums[(i - 1 + n) % n]`, or `nums[(i + 1) % n]`. **Note** that all the elements get replaced simultaneously. Return *the **minimum** number of seconds needed to make all elements in the array* `nums` *equal*.   **Example 1:** ``` **Input:** nums = [1,2,1,2] **Output:** 1 **Explanation:** We can equalize the array in 1 second in the following way: - At 1st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2]. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array. ``` **Example 2:** ``` **Input:** nums = [2,1,3,3,2] **Output:** 2 **Explanation:** We can equalize the array in 2 seconds in the following way: - At 1st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3]. - At 2nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3]. It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array. ``` **Example 3:** ``` **Input:** nums = [5,5,5,5] **Output:** 0 **Explanation:** We don't need to perform any operations as all elements in the initial array are the same. ```   **Constraints:** * `1 <= n == nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumSeconds(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSeconds(*[[1, 2, 1, 2]]) == 1\nassert my_solution.minimumSeconds(*[[2, 1, 3, 3, 2]]) == 2\nassert my_solution.minimumSeconds(*[[5, 5, 5, 5]]) == 0\nassert my_solution.minimumSeconds(*[[4, 18]]) == 1\nassert my_solution.minimumSeconds(*[[11, 7]]) == 1\nassert my_solution.minimumSeconds(*[[14, 2]]) == 1\nassert my_solution.minimumSeconds(*[[14, 9]]) == 1\nassert my_solution.minimumSeconds(*[[20, 1]]) == 1\nassert my_solution.minimumSeconds(*[[17, 15]]) == 1\nassert my_solution.minimumSeconds(*[[11, 13]]) == 1\nassert my_solution.minimumSeconds(*[[7, 13]]) == 1\nassert my_solution.minimumSeconds(*[[18, 17]]) == 1\nassert my_solution.minimumSeconds(*[[15, 17]]) == 1\nassert my_solution.minimumSeconds(*[[13, 8]]) == 1\nassert my_solution.minimumSeconds(*[[12, 16]]) == 1\nassert my_solution.minimumSeconds(*[[12, 8]]) == 1\nassert my_solution.minimumSeconds(*[[18, 16]]) == 1\nassert my_solution.minimumSeconds(*[[16, 16]]) == 0\nassert my_solution.minimumSeconds(*[[6, 12]]) == 1\nassert my_solution.minimumSeconds(*[[9, 6]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2920", "questionFrontendId": "2808", "questionTitle": "Minimum Seconds to Equalize a Circular Array", "stats": { "totalAccepted": "3.2K", "totalSubmission": "8.7K", "totalAcceptedRaw": 3192, "totalSubmissionRaw": 8676, "acRate": "36.8%" } }
LeetCode/2952
# Minimum Time to Make Array Sum At Most x You are given two **0-indexed** integer arrays `nums1` and `nums2` of equal length. Every second, for all indices `0 <= i < nums1.length`, value of `nums1[i]` is incremented by `nums2[i]`. **After** this is done, you can do the following operation: * Choose an index `0 <= i < nums1.length` and make `nums1[i] = 0`. You are also given an integer `x`. Return *the **minimum** time in which you can make the sum of all elements of* `nums1` *to be **less than or equal** to* `x`, *or* `-1` *if this is not possible.*   **Example 1:** ``` **Input:** nums1 = [1,2,3], nums2 = [1,2,3], x = 4 **Output:** 3 **Explanation:** For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6]. For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9]. For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0]. Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3. ``` **Example 2:** ``` **Input:** nums1 = [1,2,3], nums2 = [3,3,3], x = 4 **Output:** -1 **Explanation:** It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed. ```   **Constraints:** * `1 <= nums1.length <= 103` * `1 <= nums1[i] <= 103` * `0 <= nums2[i] <= 103` * `nums1.length == nums2.length` * `0 <= x <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumTime(self, nums1: List[int], nums2: List[int], x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumTime(*[[1, 2, 3], [1, 2, 3], 4]) == 3\nassert my_solution.minimumTime(*[[1, 2, 3], [3, 3, 3], 4]) == -1\nassert my_solution.minimumTime(*[[4, 4, 9, 10], [4, 4, 1, 3], 16]) == 4\nassert my_solution.minimumTime(*[[5, 3], [3, 2], 4]) == 2\nassert my_solution.minimumTime(*[[4, 5, 3, 2, 3, 9, 5, 7, 10, 4], [4, 4, 0, 4, 1, 2, 4, 0, 4, 0], 47]) == -1\nassert my_solution.minimumTime(*[[7, 9, 8, 5, 8, 3], [0, 1, 4, 2, 3, 1], 37]) == 2\nassert my_solution.minimumTime(*[[8, 2, 3], [1, 4, 2], 13]) == 0\nassert my_solution.minimumTime(*[[4, 7, 2, 3, 4, 3, 10, 8], [3, 4, 0, 1, 1, 0, 2, 2], 36]) == 4\nassert my_solution.minimumTime(*[[2, 10, 10, 4, 6, 3], [1, 0, 0, 1, 3, 1], 35]) == 0\nassert my_solution.minimumTime(*[[9, 5, 3], [4, 1, 3], 17]) == 0\nassert my_solution.minimumTime(*[[1, 7, 9, 4, 8, 8, 1], [2, 2, 3, 2, 0, 1, 0], 20]) == 6\nassert my_solution.minimumTime(*[[9, 2, 8, 3, 1, 9, 7, 6], [0, 3, 4, 1, 3, 4, 2, 1], 40]) == 8\nassert my_solution.minimumTime(*[[10], [3], 10]) == 0\nassert my_solution.minimumTime(*[[7, 6, 8, 2, 8, 9, 3, 3], [2, 2, 4, 0, 0, 2, 2, 3], 45]) == 5\nassert my_solution.minimumTime(*[[4, 9, 5, 2, 3], [4, 2, 0, 4, 0], 18]) == 3\nassert my_solution.minimumTime(*[[2, 10, 2, 7, 8, 9, 7, 6, 6], [4, 2, 1, 4, 3, 2, 4, 4, 4], 55]) == -1\nassert my_solution.minimumTime(*[[6, 8, 10, 7, 10, 9], [4, 2, 0, 4, 4, 2], 38]) == 5\nassert my_solution.minimumTime(*[[9, 2, 8, 5, 8, 3, 5, 2, 2], [4, 3, 4, 2, 0, 1, 4, 4, 2], 41]) == -1\nassert my_solution.minimumTime(*[[5, 3, 2, 3, 10, 4, 7, 9, 1, 10], [2, 0, 2, 0, 3, 3, 4, 4, 0, 1], 30]) == -1\nassert my_solution.minimumTime(*[[2, 3, 5], [0, 0, 1], 8]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2952", "questionFrontendId": "2809", "questionTitle": "Minimum Time to Make Array Sum At Most x", "stats": { "totalAccepted": "10.8K", "totalSubmission": "17.4K", "totalAcceptedRaw": 10761, "totalSubmissionRaw": 17396, "acRate": "61.9%" } }
LeetCode/2876
# Number of Employees Who Met the Target There are `n` employees in a company, numbered from `0` to `n - 1`. Each employee `i` has worked for `hours[i]` hours in the company. The company requires each employee to work for **at least** `target` hours. You are given a **0-indexed** array of non-negative integers `hours` of length `n` and a non-negative integer `target`. Return *the integer denoting the number of employees who worked at least* `target` *hours*.   **Example 1:** ``` **Input:** hours = [0,1,2,3,4], target = 2 **Output:** 3 **Explanation:** The company wants each employee to work for at least 2 hours. - Employee 0 worked for 0 hours and didn't meet the target. - Employee 1 worked for 1 hours and didn't meet the target. - Employee 2 worked for 2 hours and met the target. - Employee 3 worked for 3 hours and met the target. - Employee 4 worked for 4 hours and met the target. There are 3 employees who met the target. ``` **Example 2:** ``` **Input:** hours = [5,1,4,2,2], target = 6 **Output:** 0 **Explanation:** The company wants each employee to work for at least 6 hours. There are 0 employees who met the target. ```   **Constraints:** * `1 <= n == hours.length <= 50` * `0 <= hours[i], target <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[0, 1, 2, 3, 4], 2]) == 3\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[5, 1, 4, 2, 2], 6]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[98], 5]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[19], 13]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[70], 13]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[26], 14]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[2], 16]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[77], 19]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[6], 21]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[27], 21]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[36], 22]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[42], 25]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[70], 27]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[2], 28]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[14], 31]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[45], 34]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[44], 35]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[11], 39]) == 0\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[71], 39]) == 1\nassert my_solution.numberOfEmployeesWhoMetTarget(*[[91], 45]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2876", "questionFrontendId": "2798", "questionTitle": "Number of Employees Who Met the Target", "stats": { "totalAccepted": "10.2K", "totalSubmission": "11.9K", "totalAcceptedRaw": 10171, "totalSubmissionRaw": 11950, "acRate": "85.1%" } }
LeetCode/2856
# Count Complete Subarrays in an Array You are given an array `nums` consisting of **positive** integers. We call a subarray of an array **complete** if the following condition is satisfied: * The number of **distinct** elements in the subarray is equal to the number of distinct elements in the whole array. Return *the number of **complete** subarrays*. A **subarray** is a contiguous non-empty part of an array.   **Example 1:** ``` **Input:** nums = [1,3,1,2,2] **Output:** 4 **Explanation:** The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2]. ``` **Example 2:** ``` **Input:** nums = [5,5,5,5] **Output:** 10 **Explanation:** The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10. ```   **Constraints:** * `1 <= nums.length <= 1000` * `1 <= nums[i] <= 2000` Please make sure your answer follows the type signature below: ```python3 class Solution: def countCompleteSubarrays(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countCompleteSubarrays(*[[1, 3, 1, 2, 2]]) == 4\nassert my_solution.countCompleteSubarrays(*[[5, 5, 5, 5]]) == 10\nassert my_solution.countCompleteSubarrays(*[[459, 459, 962, 1579, 1435, 756, 1872, 1597]]) == 2\nassert my_solution.countCompleteSubarrays(*[[1786, 1786, 1786, 114]]) == 3\nassert my_solution.countCompleteSubarrays(*[[1632, 1632, 528, 359, 1671, 1632, 511, 1087, 424, 1684]]) == 3\nassert my_solution.countCompleteSubarrays(*[[1430, 12, 1430, 1075, 1722]]) == 2\nassert my_solution.countCompleteSubarrays(*[[1917, 1917, 608, 608, 1313, 751, 558, 1561, 608]]) == 4\nassert my_solution.countCompleteSubarrays(*[[254, 1690, 1690, 1068, 1779]]) == 1\nassert my_solution.countCompleteSubarrays(*[[1116]]) == 1\nassert my_solution.countCompleteSubarrays(*[[1677, 1677, 1352, 1219, 1666, 1677, 1892, 1892, 319]]) == 3\nassert my_solution.countCompleteSubarrays(*[[1386, 1997, 1997, 574, 574, 1360, 989]]) == 1\nassert my_solution.countCompleteSubarrays(*[[50, 48, 1118, 540, 1248, 1984, 1698, 41, 1984, 186]]) == 1\nassert my_solution.countCompleteSubarrays(*[[273, 524, 40, 1323, 1323]]) == 2\nassert my_solution.countCompleteSubarrays(*[[246, 376, 828, 191, 1942, 210]]) == 1\nassert my_solution.countCompleteSubarrays(*[[463, 1497, 1676, 127, 1379, 17, 1075, 190]]) == 1\nassert my_solution.countCompleteSubarrays(*[[765, 1370]]) == 1\nassert my_solution.countCompleteSubarrays(*[[1110, 804, 1110, 839, 728, 839]]) == 4\nassert my_solution.countCompleteSubarrays(*[[1001]]) == 1\nassert my_solution.countCompleteSubarrays(*[[665, 867, 954, 1411, 728, 1006, 372, 1411]]) == 2\nassert my_solution.countCompleteSubarrays(*[[1213, 1203, 1277, 369, 1277]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2856", "questionFrontendId": "2799", "questionTitle": "Count Complete Subarrays in an Array", "stats": { "totalAccepted": "7.4K", "totalSubmission": "12.6K", "totalAcceptedRaw": 7372, "totalSubmissionRaw": 12634, "acRate": "58.4%" } }
LeetCode/2877
# Shortest String That Contains Three Strings Given three strings `a`, `b`, and `c`, your task is to find a string that has the **minimum** length and contains all three strings as **substrings**. If there are multiple such strings, return the**lexicographicallysmallest** one. Return *a string denoting the answer to the problem.* **Notes** * A string `a` is **lexicographically smaller** than a string `b` (of the same length) if in the first position where `a` and `b` differ, string `a` has a letter that appears **earlier** in the alphabet than the corresponding letter in `b`. * A **substring** is a contiguous sequence of characters within a string.   **Example 1:** ``` **Input:** a = "abc", b = "bca", c = "aaa" **Output:** "aaabca" **Explanation:** We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one. ``` **Example 2:** ``` **Input:** a = "ab", b = "ba", c = "aba" **Output:** "aba" **Explanation:** We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one. ```   **Constraints:** * `1 <= a.length, b.length, c.length <= 100` * `a`, `b`, `c` consist only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumString(self, a: str, b: str, c: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumString(*['abc', 'bca', 'aaa']) == aaabca\nassert my_solution.minimumString(*['ab', 'ba', 'aba']) == aba\nassert my_solution.minimumString(*['xyyyz', 'xzyz', 'zzz']) == xyyyzxzyzzz\nassert my_solution.minimumString(*['a', 'a', 'a']) == a\nassert my_solution.minimumString(*['a', 'a', 'b']) == ab\nassert my_solution.minimumString(*['a', 'c', 'a']) == ac\nassert my_solution.minimumString(*['a', 'b', 'b']) == ab\nassert my_solution.minimumString(*['a', 'a', 'c']) == ac\nassert my_solution.minimumString(*['a', 'c', 'b']) == abc\nassert my_solution.minimumString(*['c', 'c', 'a']) == ac\nassert my_solution.minimumString(*['k', 'e', 'a']) == aek\nassert my_solution.minimumString(*['a', 'b', 'a']) == ab\nassert my_solution.minimumString(*['b', 'b', 'a']) == ab\nassert my_solution.minimumString(*['b', 'b', 'b']) == b\nassert my_solution.minimumString(*['b', 'b', 'c']) == bc\nassert my_solution.minimumString(*['c', 'b', 'b']) == bc\nassert my_solution.minimumString(*['b', 'c', 'c']) == bc\nassert my_solution.minimumString(*['b', 'c', 'a']) == abc\nassert my_solution.minimumString(*['c', 'a', 'c']) == ac\nassert my_solution.minimumString(*['c', 'b', 'c']) == bc\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2877", "questionFrontendId": "2800", "questionTitle": "Shortest String That Contains Three Strings", "stats": { "totalAccepted": "4.6K", "totalSubmission": "13.2K", "totalAcceptedRaw": 4622, "totalSubmissionRaw": 13225, "acRate": "34.9%" } }
LeetCode/2921
# Count Stepping Numbers in Range Given two positive integers `low` and `high` represented as strings, find the count of **stepping numbers** in the inclusive range `[low, high]`. A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of **exactly** `1`. Return *an integer denoting the count of stepping numbers in the inclusive range* `[low, high]`*.* Since the answer may be very large, return it **modulo** `109 + 7`. **Note:** A stepping number should not have a leading zero.   **Example 1:** ``` **Input:** low = "1", high = "11" **Output:** 10 **Explanation:** The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10. ``` **Example 2:** ``` **Input:** low = "90", high = "101" **Output:** 2 **Explanation:** The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2. ```   **Constraints:** * `1 <= int(low) <= int(high) < 10100` * `1 <= low.length, high.length <= 100` * `low` and `high` consist of only digits. * `low` and `high` don't have any leading zeros. Please make sure your answer follows the type signature below: ```python3 class Solution: def countSteppingNumbers(self, low: str, high: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSteppingNumbers(*['1', '11']) == 10\nassert my_solution.countSteppingNumbers(*['90', '101']) == 2\nassert my_solution.countSteppingNumbers(*['2', '40']) == 14\nassert my_solution.countSteppingNumbers(*['26', '60']) == 6\nassert my_solution.countSteppingNumbers(*['40', '70']) == 6\nassert my_solution.countSteppingNumbers(*['46', '66']) == 3\nassert my_solution.countSteppingNumbers(*['58', '58']) == 0\nassert my_solution.countSteppingNumbers(*['23', '99']) == 14\nassert my_solution.countSteppingNumbers(*['44', '86']) == 7\nassert my_solution.countSteppingNumbers(*['20', '111']) == 16\nassert my_solution.countSteppingNumbers(*['70', '75']) == 0\nassert my_solution.countSteppingNumbers(*['37', '111']) == 12\nassert my_solution.countSteppingNumbers(*['17', '149']) == 18\nassert my_solution.countSteppingNumbers(*['21', '145']) == 18\nassert my_solution.countSteppingNumbers(*['47', '124']) == 12\nassert my_solution.countSteppingNumbers(*['81', '91']) == 2\nassert my_solution.countSteppingNumbers(*['18', '159']) == 18\nassert my_solution.countSteppingNumbers(*['85', '92']) == 2\nassert my_solution.countSteppingNumbers(*['66', '112']) == 7\nassert my_solution.countSteppingNumbers(*['84', '102']) == 4\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2921", "questionFrontendId": "2801", "questionTitle": "Count Stepping Numbers in Range", "stats": { "totalAccepted": "2.9K", "totalSubmission": "6.9K", "totalAcceptedRaw": 2947, "totalSubmissionRaw": 6888, "acRate": "42.8%" } }
LeetCode/2881
# Split Strings by Separator Given an array of strings `words` and a character `separator`, **split** each string in `words` by `separator`. Return *an array of strings containing the new strings formed after the splits, **excluding empty strings**.* **Notes** * `separator` is used to determine where the split should occur, but it is not included as part of the resulting strings. * A split may result in more than two strings. * The resulting strings must maintain the same order as they were initially given.   **Example 1:** ``` **Input:** words = ["one.two.three","four.five","six"], separator = "." **Output:** ["one","two","three","four","five","six"] **Explanation:** In this example we split as follows: "one.two.three" splits into "one", "two", "three" "four.five" splits into "four", "five" "six" splits into "six" Hence, the resulting array is ["one","two","three","four","five","six"]. ``` **Example 2:** ``` **Input:** words = ["$easy$","$problem$"], separator = "$" **Output:** ["easy","problem"] **Explanation:** In this example we split as follows: "$easy$" splits into "easy" (excluding empty strings) "$problem$" splits into "problem" (excluding empty strings) Hence, the resulting array is ["easy","problem"]. ``` **Example 3:** ``` **Input:** words = ["|||"], separator = "|" **Output:** [] **Explanation:** In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. ```   **Constraints:** * `1 <= words.length <= 100` * `1 <= words[i].length <= 20` * characters in `words[i]` are either lowercase English letters or characters from the string `".,|$#@"` (excluding the quotes) * `separator` is a character from the string `".,|$#@"` (excluding the quotes) Please make sure your answer follows the type signature below: ```python3 class Solution: def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.splitWordsBySeparator(*[['one.two.three', 'four.five', 'six'], '.']) == ['one', 'two', 'three', 'four', 'five', 'six']\nassert my_solution.splitWordsBySeparator(*[['$easy$', '$problem$'], '$']) == ['easy', 'problem']\nassert my_solution.splitWordsBySeparator(*[['|||'], '|']) == []\nassert my_solution.splitWordsBySeparator(*[['stars.bars.$'], '.']) == ['stars', 'bars', '$']\nassert my_solution.splitWordsBySeparator(*[['###x#i@f'], '#']) == ['x', 'i@f']\nassert my_solution.splitWordsBySeparator(*[['##q@t#'], '#']) == ['q@t']\nassert my_solution.splitWordsBySeparator(*[['#,'], '#']) == [',']\nassert my_solution.splitWordsBySeparator(*[['#@'], '@']) == ['#']\nassert my_solution.splitWordsBySeparator(*[['#a$f$nwgq#vw'], '$']) == ['#a', 'f', 'nwgq#vw']\nassert my_solution.splitWordsBySeparator(*[['#uyddd,trxiwfv'], ',']) == ['#uyddd', 'trxiwfv']\nassert my_solution.splitWordsBySeparator(*[['#x'], '$']) == ['#x']\nassert my_solution.splitWordsBySeparator(*[['#x#,'], ',']) == ['#x#']\nassert my_solution.splitWordsBySeparator(*[['#|'], '#']) == ['|']\nassert my_solution.splitWordsBySeparator(*[['#|a|b|##|#|#g#u|'], '|']) == ['#', 'a', 'b', '##', '#', '#g#u']\nassert my_solution.splitWordsBySeparator(*[['$$.o.$$.'], '$']) == ['.o.', '.']\nassert my_solution.splitWordsBySeparator(*[['$,,'], ',']) == ['$']\nassert my_solution.splitWordsBySeparator(*[['$j@@@'], '@']) == ['$j']\nassert my_solution.splitWordsBySeparator(*[[',$$'], '$']) == [',']\nassert my_solution.splitWordsBySeparator(*[[',,'], '|']) == [',,']\nassert my_solution.splitWordsBySeparator(*[[',,,@@n'], ',']) == ['@@n']\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2881", "questionFrontendId": "2788", "questionTitle": "Split Strings by Separator", "stats": { "totalAccepted": "23.7K", "totalSubmission": "29.4K", "totalAcceptedRaw": 23744, "totalSubmissionRaw": 29444, "acRate": "80.6%" } }
LeetCode/2872
# Largest Element in an Array after Merge Operations You are given a **0-indexed** array `nums` consisting of positive integers. You can do the following operation on the array **any** number of times: * Choose an integer `i` such that `0 <= i < nums.length - 1` and `nums[i] <= nums[i + 1]`. Replace the element `nums[i + 1]` with `nums[i] + nums[i + 1]` and delete the element `nums[i]` from the array. Return *the value of the **largest** element that you can possibly obtain in the final array.*   **Example 1:** ``` **Input:** nums = [2,3,7,9,3] **Output:** 21 **Explanation:** We can apply the following operations on the array: - Choose i = 0. The resulting array will be nums = [5,7,9,3]. - Choose i = 1. The resulting array will be nums = [5,16,3]. - Choose i = 0. The resulting array will be nums = [21,3]. The largest element in the final array is 21. It can be shown that we cannot obtain a larger element. ``` **Example 2:** ``` **Input:** nums = [5,3,3] **Output:** 11 **Explanation:** We can do the following operations on the array: - Choose i = 1. The resulting array will be nums = [5,6]. - Choose i = 0. The resulting array will be nums = [11]. There is only one element in the final array, which is 11. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxArrayValue(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxArrayValue(*[[2, 3, 7, 9, 3]]) == 21\nassert my_solution.maxArrayValue(*[[5, 3, 3]]) == 11\nassert my_solution.maxArrayValue(*[[77]]) == 77\nassert my_solution.maxArrayValue(*[[34, 95, 50, 12, 25, 100, 21, 3, 25, 16, 76, 73, 93, 46, 18]]) == 623\nassert my_solution.maxArrayValue(*[[40, 15, 35, 98, 77, 79, 24, 62, 53, 84, 97, 16, 30, 22, 49]]) == 781\nassert my_solution.maxArrayValue(*[[64, 35, 42, 19, 95, 8, 83, 89, 33, 21, 97, 11, 51, 93, 36, 34, 67, 53]]) == 878\nassert my_solution.maxArrayValue(*[[65, 68, 55, 6, 79, 30, 81, 25, 61, 2, 28, 59, 63, 15, 35, 8, 10, 83]]) == 773\nassert my_solution.maxArrayValue(*[[56]]) == 56\nassert my_solution.maxArrayValue(*[[100]]) == 100\nassert my_solution.maxArrayValue(*[[35, 23, 71, 38]]) == 129\nassert my_solution.maxArrayValue(*[[56, 67, 18, 81, 95, 41, 39, 56, 63, 70, 56, 31, 84, 46, 28, 38, 27, 56, 13, 10, 58, 16, 85, 21, 63, 8]]) == 1134\nassert my_solution.maxArrayValue(*[[72, 72]]) == 144\nassert my_solution.maxArrayValue(*[[16, 31, 55]]) == 102\nassert my_solution.maxArrayValue(*[[6, 65, 68, 7, 35, 19, 28]]) == 228\nassert my_solution.maxArrayValue(*[[38, 37, 88, 60, 93, 4, 5, 65, 74, 25, 59, 28, 86, 33, 28, 33, 93]]) == 849\nassert my_solution.maxArrayValue(*[[29, 9, 3, 55, 25, 38, 88, 39, 38, 73, 47, 57, 40, 56, 4, 52, 1, 44, 88, 20, 18, 8]]) == 786\nassert my_solution.maxArrayValue(*[[34, 92, 42, 24, 98, 87, 40, 82, 51, 67, 70, 75, 45, 57, 67]]) == 931\nassert my_solution.maxArrayValue(*[[31, 75, 44, 92, 13, 10, 3, 41, 47, 89, 5, 92, 17, 62, 65, 40, 43, 68, 30, 45, 85, 24, 40, 77, 80, 65]]) == 1218\nassert my_solution.maxArrayValue(*[[63, 58, 61, 58, 82, 48, 83, 24, 24, 61, 31, 16, 26, 50]]) == 685\nassert my_solution.maxArrayValue(*[[10, 82, 74, 54, 20, 43, 74, 95, 17, 28, 44, 74, 25, 19, 75, 2, 84, 99]]) == 919\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2872", "questionFrontendId": "2789", "questionTitle": "Largest Element in an Array after Merge Operations", "stats": { "totalAccepted": "6.5K", "totalSubmission": "13.1K", "totalAcceptedRaw": 6493, "totalSubmissionRaw": 13053, "acRate": "49.7%" } }
LeetCode/2919
# Maximum Number of Groups With Increasing Length You are given a **0-indexed** array `usageLimits` of length `n`. Your task is to create **groups** using numbers from `0` to `n - 1`, ensuring that each number, `i`, is used no more than `usageLimits[i]` times in total **across all groups**. You must also satisfy the following conditions: * Each group must consist of **distinct** numbers, meaning that no duplicate numbers are allowed within a single group. * Each group (except the first one) must have a length **strictly greater** than the previous group. Return *an integer denoting the **maximum** number of groups you can create while satisfying these conditions.*   **Example 1:** ``` **Input:** usageLimits = [1,2,5] **Output:** 3 **Explanation:** In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [2]. Group 2 contains the numbers [1,2]. Group 3 contains the numbers [0,1,2]. It can be shown that the maximum number of groups is 3. So, the output is 3. ``` **Example 2:** ``` **Input:** usageLimits = [2,1,2] **Output:** 2 **Explanation:** In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. Group 2 contains the numbers [1,2]. It can be shown that the maximum number of groups is 2. So, the output is 2. ``` **Example 3:** ``` **Input:** usageLimits = [1,1] **Output:** 1 **Explanation:** In this example, we can use both 0 and 1 at most once. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. It can be shown that the maximum number of groups is 1. So, the output is 1. ```   **Constraints:** * `1 <= usageLimits.length <= 105` * `1 <= usageLimits[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxIncreasingGroups(self, usageLimits: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxIncreasingGroups(*[[1, 2, 5]]) == 3\nassert my_solution.maxIncreasingGroups(*[[2, 1, 2]]) == 2\nassert my_solution.maxIncreasingGroups(*[[1, 1]]) == 1\nassert my_solution.maxIncreasingGroups(*[[1, 4]]) == 2\nassert my_solution.maxIncreasingGroups(*[[1, 5]]) == 2\nassert my_solution.maxIncreasingGroups(*[[1, 7]]) == 2\nassert my_solution.maxIncreasingGroups(*[[1, 8]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 1]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 2]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 3]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 4]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 5]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 7]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 8]]) == 2\nassert my_solution.maxIncreasingGroups(*[[2, 9]]) == 2\nassert my_solution.maxIncreasingGroups(*[[3, 1]]) == 2\nassert my_solution.maxIncreasingGroups(*[[3, 4]]) == 2\nassert my_solution.maxIncreasingGroups(*[[3, 7]]) == 2\nassert my_solution.maxIncreasingGroups(*[[3, 10]]) == 2\nassert my_solution.maxIncreasingGroups(*[[4, 1]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2919", "questionFrontendId": "2790", "questionTitle": "Maximum Number of Groups With Increasing Length", "stats": { "totalAccepted": "2.8K", "totalSubmission": "12.7K", "totalAcceptedRaw": 2801, "totalSubmissionRaw": 12691, "acRate": "22.1%" } }
LeetCode/2892
# Check if Array is Good You are given an integer array `nums`. We consider an array **good** if it is a permutation of an array `base[n]`. `base[n] = [1, 2, ..., n - 1, n, n]` (in other words, it is an array of length `n + 1` which contains `1` to `n - 1` exactly once, plus two occurrences of `n`). For example, `base[1] = [1, 1]` and `base[3] = [1, 2, 3, 3]`. Return `true` *if the given array is good, otherwise return*`false`. **Note:** A permutation of integers represents an arrangement of these numbers.   **Example 1:** ``` **Input:** nums = [2, 1, 3] **Output:** false **Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false. ``` **Example 2:** ``` **Input:** nums = [1, 3, 3, 2] **Output:** true **Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true. ``` **Example 3:** ``` **Input:** nums = [1, 1] **Output:** true **Explanation:** Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true. ``` **Example 4:** ``` **Input:** nums = [3, 4, 4, 1, 2, 1] **Output:** false **Explanation:** Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= num[i] <= 200` Please make sure your answer follows the type signature below: ```python3 class Solution: def isGood(self, nums: List[int]) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.isGood(*[[2, 1, 3]]) == False\nassert my_solution.isGood(*[[1, 3, 3, 2]]) == True\nassert my_solution.isGood(*[[1, 1]]) == True\nassert my_solution.isGood(*[[3, 4, 4, 1, 2, 1]]) == False\nassert my_solution.isGood(*[[1]]) == False\nassert my_solution.isGood(*[[2]]) == False\nassert my_solution.isGood(*[[3]]) == False\nassert my_solution.isGood(*[[4]]) == False\nassert my_solution.isGood(*[[5]]) == False\nassert my_solution.isGood(*[[6]]) == False\nassert my_solution.isGood(*[[8]]) == False\nassert my_solution.isGood(*[[9]]) == False\nassert my_solution.isGood(*[[10]]) == False\nassert my_solution.isGood(*[[12]]) == False\nassert my_solution.isGood(*[[13]]) == False\nassert my_solution.isGood(*[[14]]) == False\nassert my_solution.isGood(*[[15]]) == False\nassert my_solution.isGood(*[[82]]) == False\nassert my_solution.isGood(*[[1, 8]]) == False\nassert my_solution.isGood(*[[1, 13]]) == False\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2892", "questionFrontendId": "2784", "questionTitle": "Check if Array is Good", "stats": { "totalAccepted": "4.8K", "totalSubmission": "7.7K", "totalAcceptedRaw": 4778, "totalSubmissionRaw": 7743, "acRate": "61.7%" } }
LeetCode/2887
# Sort Vowels in a String Given a **0-indexed** string `s`, **permute** `s` to get a new string `t` such that: * All consonants remain in their original places. More formally, if there is an index `i` with `0 <= i < s.length` such that `s[i]` is a consonant, then `t[i] = s[i]`. * The vowels must be sorted in the **nondecreasing** order of their **ASCII** values. More formally, for pairs of indices `i`, `j` with `0 <= i < j < s.length` such that `s[i]` and `s[j]` are vowels, then `t[i]` must not have a higher ASCII value than `t[j]`. Return *the resulting string*. The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.   **Example 1:** ``` **Input:** s = "lEetcOde" **Output:** "lEOtcede" **Explanation:** 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. ``` **Example 2:** ``` **Input:** s = "lYmpH" **Output:** "lYmpH" **Explanation:** There are no vowels in s (all characters in s are consonants), so we return "lYmpH". ```   **Constraints:** * `1 <= s.length <= 105` * `s` consists only of letters of the English alphabet in **uppercase and lowercase**. Please make sure your answer follows the type signature below: ```python3 class Solution: def sortVowels(self, s: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sortVowels(*['lEetcOde']) == lEOtcede\nassert my_solution.sortVowels(*['lYmpH']) == lYmpH\nassert my_solution.sortVowels(*['mDVD']) == mDVD\nassert my_solution.sortVowels(*['xdX']) == xdX\nassert my_solution.sortVowels(*['xdE']) == xdE\nassert my_solution.sortVowels(*['RiQYo']) == RiQYo\nassert my_solution.sortVowels(*['LQRamBOHfq']) == LQROmBaHfq\nassert my_solution.sortVowels(*['UpjPbEnOj']) == EpjPbOnUj\nassert my_solution.sortVowels(*['ziF']) == ziF\nassert my_solution.sortVowels(*['WxkKdjhL']) == WxkKdjhL\nassert my_solution.sortVowels(*['uZcPmqAd']) == AZcPmqud\nassert my_solution.sortVowels(*['UjshJXjkjS']) == UjshJXjkjS\nassert my_solution.sortVowels(*['nElwWTQHJ']) == nElwWTQHJ\nassert my_solution.sortVowels(*['kwcJqvsgM']) == kwcJqvsgM\nassert my_solution.sortVowels(*['z']) == z\nassert my_solution.sortVowels(*['Pz']) == Pz\nassert my_solution.sortVowels(*['T']) == T\nassert my_solution.sortVowels(*['syRWvFi']) == syRWvFi\nassert my_solution.sortVowels(*['G']) == G\nassert my_solution.sortVowels(*['MuQYHVy']) == MuQYHVy\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2887", "questionFrontendId": "2785", "questionTitle": "Sort Vowels in a String", "stats": { "totalAccepted": "4.2K", "totalSubmission": "5.5K", "totalAcceptedRaw": 4201, "totalSubmissionRaw": 5452, "acRate": "77.1%" } }
LeetCode/2893
# Visit Array Positions to Maximize Score You are given a **0-indexed** integer array `nums` and a positive integer `x`. You are **initially** at position `0` in the array and you can visit other positions according to the following rules: * If you are currently in position `i`, then you can move to **any** position `j` such that `i < j`. * For each position `i` that you visit, you get a score of `nums[i]`. * If you move from a position `i` to a position `j` and the **parities** of `nums[i]` and `nums[j]` differ, then you lose a score of `x`. Return *the **maximum** total score you can get*. **Note** that initially you have `nums[0]` points.   **Example 1:** ``` **Input:** nums = [2,3,6,1,9,2], x = 5 **Output:** 13 **Explanation:** We can visit the following positions in the array: 0 -> 2 -> 3 -> 4. The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5. The total score will be: 2 + 6 + 1 + 9 - 5 = 13. ``` **Example 2:** ``` **Input:** nums = [2,4,6,8], x = 3 **Output:** 20 **Explanation:** All the integers in the array have the same parities, so we can visit all of them without losing any score. The total score is: 2 + 4 + 6 + 8 = 20. ```   **Constraints:** * `2 <= nums.length <= 105` * `1 <= nums[i], x <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxScore(self, nums: List[int], x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxScore(*[[2, 3, 6, 1, 9, 2], 5]) == 13\nassert my_solution.maxScore(*[[2, 4, 6, 8], 3]) == 20\nassert my_solution.maxScore(*[[38, 92, 23, 30, 25, 96, 6, 71, 78, 77, 33, 23, 71, 48, 87, 77, 53, 28, 6, 20, 90, 83, 42, 21, 64, 95, 84, 29, 22, 21, 33, 36, 53, 51, 85, 25, 80, 56, 71, 69, 5, 21, 4, 84, 28, 16, 65, 7], 52]) == 1545\nassert my_solution.maxScore(*[[18, 13, 60, 61, 57, 21, 10, 98, 51, 3, 13, 36, 72, 70, 68, 62, 52, 83, 63, 63, 53, 42, 59, 98, 95, 48, 22, 64, 94, 80, 14, 14], 2]) == 1633\nassert my_solution.maxScore(*[[90, 87, 79, 59, 91, 19, 96], 51]) == 419\nassert my_solution.maxScore(*[[96, 81, 48, 3, 60, 78, 74, 82, 14, 7, 87, 72, 42, 41, 80, 4, 92, 82, 59, 16, 19, 94, 70, 45, 83, 58, 2, 91, 11, 96, 17, 62, 79, 34, 44, 47, 89, 76, 85, 21, 5, 57, 35, 51], 24]) == 1952\nassert my_solution.maxScore(*[[99, 88, 98, 15, 34, 40, 29, 81, 2, 6, 12, 9, 82, 93, 5, 81, 84, 71, 83, 31, 12, 22, 9, 65, 56, 9, 68, 79, 39, 84, 50, 7, 25, 3, 49], 19]) == 1363\nassert my_solution.maxScore(*[[8, 50, 65, 85, 8, 73, 55, 50, 29, 95, 5, 68, 52, 79], 74]) == 470\nassert my_solution.maxScore(*[[45, 9, 20, 89, 18, 94, 12, 51, 38, 77, 100, 95, 46, 1, 76, 41, 8, 90, 82, 33, 92, 32, 76, 43, 6, 61, 85, 40, 63, 10, 74, 18, 44, 43, 17, 100, 17, 33, 100, 77, 97, 8, 99, 85, 88, 9, 63, 31, 32], 68]) == 1694\nassert my_solution.maxScore(*[[87, 23, 53, 57, 21, 60, 68, 84, 66, 49, 48, 61, 32, 95, 71, 11, 15, 61, 10, 86, 50, 53, 38, 20, 63], 92]) == 814\nassert my_solution.maxScore(*[[39, 47, 76, 64, 90, 17, 30, 57, 19, 40, 9, 76, 68, 33, 36, 61, 19, 93, 8, 1, 31, 2, 55, 70, 24, 85, 97, 40, 35, 93, 56, 67, 64, 67, 52, 2, 75, 13, 89, 97], 77]) == 1390\nassert my_solution.maxScore(*[[92, 87, 85, 27, 27, 10, 24, 94, 26, 78, 24, 61, 4, 46, 3, 76, 29, 65, 52, 61, 34, 67, 74, 61, 90, 40, 81, 60, 10, 98, 87, 57, 28, 77, 55, 33, 10, 91, 57, 72, 3, 72, 4, 39, 99], 70]) == 1551\nassert my_solution.maxScore(*[[20, 90, 68], 39]) == 178\nassert my_solution.maxScore(*[[43, 100, 72, 33, 45, 9, 51, 10, 22, 42, 7, 74, 41, 68, 100, 24, 20, 20, 79, 30, 99, 82], 1]) == 1060\nassert my_solution.maxScore(*[[100, 87, 29, 94, 56, 41, 53, 98, 34, 17, 52, 3, 54, 51, 22, 39, 37, 9, 76], 40]) == 670\nassert my_solution.maxScore(*[[55, 37, 87, 45, 96, 7, 66, 62, 91, 51, 33, 92, 65, 99], 81]) == 625\nassert my_solution.maxScore(*[[2, 75, 65, 43], 39]) == 146\nassert my_solution.maxScore(*[[74, 82, 80, 95, 72, 23, 49, 43, 76, 28, 87, 27, 58, 39, 7, 77, 26, 63, 56, 96, 77, 75, 82, 60, 90, 69, 83, 20, 13, 82, 16, 90, 40, 23, 36, 17, 77, 15, 18, 10], 40]) == 1571\nassert my_solution.maxScore(*[[75, 99, 45, 34, 63, 19, 71, 48, 73, 66, 2, 14, 76, 41, 92, 23, 6, 31, 49, 6, 70, 40, 69, 25, 97, 58, 20, 84, 21, 37, 100, 75, 73, 10, 59, 87, 30], 96]) == 1181\nassert my_solution.maxScore(*[[9, 58, 17, 54, 91, 90, 32, 6, 13, 67, 24, 80, 8, 56, 29, 66, 85, 38, 45, 13, 20, 73, 16, 98, 28, 56, 23, 2, 47, 85, 11, 97, 72, 2, 28, 52, 33], 90]) == 886\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2893", "questionFrontendId": "2786", "questionTitle": "Visit Array Positions to Maximize Score", "stats": { "totalAccepted": "3.8K", "totalSubmission": "9.5K", "totalAcceptedRaw": 3830, "totalSubmissionRaw": 9465, "acRate": "40.5%" } }
LeetCode/2882
# Ways to Express an Integer as Sum of Powers Given two **positive** integers `n` and `x`. Return *the number of ways* `n` *can be expressed as the sum of the* `xth` *power of **unique** positive integers, in other words, the number of sets of unique integers* `[n1, n2, ..., nk]` *where* `n = n1x + n2x + ... + nkx`*.* Since the result can be very large, return it modulo `109 + 7`. For example, if `n = 160` and `x = 3`, one way to express `n` is `n = 23 + 33 + 53`.   **Example 1:** ``` **Input:** n = 10, x = 2 **Output:** 1 **Explanation:** We can express n as the following: n = 32 + 12 = 10. It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers. ``` **Example 2:** ``` **Input:** n = 4, x = 1 **Output:** 2 **Explanation:** We can express n in the following ways: - n = 41 = 4. - n = 31 + 11 = 4. ```   **Constraints:** * `1 <= n <= 300` * `1 <= x <= 5` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfWays(self, n: int, x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfWays(*[10, 2]) == 1\nassert my_solution.numberOfWays(*[4, 1]) == 2\nassert my_solution.numberOfWays(*[1, 1]) == 1\nassert my_solution.numberOfWays(*[1, 2]) == 1\nassert my_solution.numberOfWays(*[1, 3]) == 1\nassert my_solution.numberOfWays(*[1, 4]) == 1\nassert my_solution.numberOfWays(*[1, 5]) == 1\nassert my_solution.numberOfWays(*[2, 1]) == 1\nassert my_solution.numberOfWays(*[2, 2]) == 0\nassert my_solution.numberOfWays(*[2, 3]) == 0\nassert my_solution.numberOfWays(*[2, 4]) == 0\nassert my_solution.numberOfWays(*[2, 5]) == 0\nassert my_solution.numberOfWays(*[3, 1]) == 2\nassert my_solution.numberOfWays(*[3, 2]) == 0\nassert my_solution.numberOfWays(*[3, 3]) == 0\nassert my_solution.numberOfWays(*[3, 4]) == 0\nassert my_solution.numberOfWays(*[3, 5]) == 0\nassert my_solution.numberOfWays(*[4, 2]) == 1\nassert my_solution.numberOfWays(*[4, 3]) == 0\nassert my_solution.numberOfWays(*[4, 4]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2882", "questionFrontendId": "2787", "questionTitle": "Ways to Express an Integer as Sum of Powers", "stats": { "totalAccepted": "3.3K", "totalSubmission": "7.5K", "totalAcceptedRaw": 3345, "totalSubmissionRaw": 7515, "acRate": "44.5%" } }
LeetCode/2844
# Sum of Squares of Special Elements You are given a **1-indexed** integer array `nums` of length `n`. An element `nums[i]` of `nums` is called **special** if `i` divides `n`, i.e. `n % i == 0`. Return *the **sum of the squares** of all **special** elements of* `nums`.   **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 21 **Explanation:** There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21. ``` **Example 2:** ``` **Input:** nums = [2,7,1,19,18,3] **Output:** 63 **Explanation:** There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63. ```   **Constraints:** * `1 <= nums.length == n <= 50` * `1 <= nums[i] <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def sumOfSquares(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumOfSquares(*[[1, 2, 3, 4]]) == 21\nassert my_solution.sumOfSquares(*[[2, 7, 1, 19, 18, 3]]) == 63\nassert my_solution.sumOfSquares(*[[1]]) == 1\nassert my_solution.sumOfSquares(*[[2]]) == 4\nassert my_solution.sumOfSquares(*[[3]]) == 9\nassert my_solution.sumOfSquares(*[[4]]) == 16\nassert my_solution.sumOfSquares(*[[5]]) == 25\nassert my_solution.sumOfSquares(*[[6]]) == 36\nassert my_solution.sumOfSquares(*[[7]]) == 49\nassert my_solution.sumOfSquares(*[[8]]) == 64\nassert my_solution.sumOfSquares(*[[9]]) == 81\nassert my_solution.sumOfSquares(*[[10]]) == 100\nassert my_solution.sumOfSquares(*[[11]]) == 121\nassert my_solution.sumOfSquares(*[[12]]) == 144\nassert my_solution.sumOfSquares(*[[13]]) == 169\nassert my_solution.sumOfSquares(*[[14]]) == 196\nassert my_solution.sumOfSquares(*[[15]]) == 225\nassert my_solution.sumOfSquares(*[[16]]) == 256\nassert my_solution.sumOfSquares(*[[17]]) == 289\nassert my_solution.sumOfSquares(*[[18]]) == 324\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2844", "questionFrontendId": "2778", "questionTitle": "Sum of Squares of Special Elements ", "stats": { "totalAccepted": "8.2K", "totalSubmission": "10.4K", "totalAcceptedRaw": 8223, "totalSubmissionRaw": 10400, "acRate": "79.1%" } }
LeetCode/2891
# Maximum Beauty of an Array After Applying Operation You are given a **0-indexed** array `nums` and a **non-negative** integer `k`. In one operation, you can do the following: * Choose an index `i` that **hasn't been chosen before** from the range `[0, nums.length - 1]`. * Replace `nums[i]` with any integer from the range `[nums[i] - k, nums[i] + k]`. The **beauty** of the array is the length of the longest subsequence consisting of equal elements. Return *the **maximum** possible beauty of the array* `nums` *after applying the operation any number of times.* **Note** that you can apply the operation to each index **only once**. A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.   **Example 1:** ``` **Input:** nums = [4,6,1,2], k = 2 **Output:** 3 **Explanation:** In this example, we apply the following operations: - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2]. - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4]. After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve. ``` **Example 2:** ``` **Input:** nums = [1,1,1,1], k = 10 **Output:** 4 **Explanation:** In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array). ```   **Constraints:** * `1 <= nums.length <= 105` * `0 <= nums[i], k <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumBeauty(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumBeauty(*[[4, 6, 1, 2], 2]) == 3\nassert my_solution.maximumBeauty(*[[1, 1, 1, 1], 10]) == 4\nassert my_solution.maximumBeauty(*[[12, 71], 10]) == 1\nassert my_solution.maximumBeauty(*[[27, 55], 1]) == 1\nassert my_solution.maximumBeauty(*[[52, 34], 21]) == 2\nassert my_solution.maximumBeauty(*[[76, 0], 16]) == 1\nassert my_solution.maximumBeauty(*[[56, 40], 26]) == 2\nassert my_solution.maximumBeauty(*[[49, 26], 12]) == 2\nassert my_solution.maximumBeauty(*[[69, 66], 14]) == 2\nassert my_solution.maximumBeauty(*[[64, 98], 12]) == 1\nassert my_solution.maximumBeauty(*[[83, 81], 7]) == 2\nassert my_solution.maximumBeauty(*[[44, 93], 15]) == 1\nassert my_solution.maximumBeauty(*[[44, 31], 26]) == 2\nassert my_solution.maximumBeauty(*[[70, 60], 15]) == 2\nassert my_solution.maximumBeauty(*[[60, 22], 11]) == 1\nassert my_solution.maximumBeauty(*[[33, 20], 1]) == 1\nassert my_solution.maximumBeauty(*[[64, 24], 4]) == 1\nassert my_solution.maximumBeauty(*[[59, 20], 28]) == 2\nassert my_solution.maximumBeauty(*[[10, 98], 27]) == 1\nassert my_solution.maximumBeauty(*[[54, 21], 20]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2891", "questionFrontendId": "2779", "questionTitle": "Maximum Beauty of an Array After Applying Operation", "stats": { "totalAccepted": "6.7K", "totalSubmission": "16.7K", "totalAcceptedRaw": 6655, "totalSubmissionRaw": 16677, "acRate": "39.9%" } }
LeetCode/2888
# Minimum Index of a Valid Split An element `x` of an integer array `arr` of length `m` is **dominant** if `freq(x) * 2 > m`, where `freq(x)` is the number of occurrences of `x` in `arr`. Note that this definition implies that `arr` can have **at most one** dominant element. You are given a **0-indexed** integer array `nums` of length `n` with one dominant element. You can split `nums` at an index `i` into two arrays `nums[0, ..., i]` and `nums[i + 1, ..., n - 1]`, but the split is only **valid** if: * `0 <= i < n - 1` * `nums[0, ..., i]`, and `nums[i + 1, ..., n - 1]` have the same dominant element. Here, `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j`, both ends being inclusive. Particularly, if `j < i` then `nums[i, ..., j]` denotes an empty subarray. Return *the **minimum** index of a **valid split***. If no valid split exists, return `-1`.   **Example 1:** ``` **Input:** nums = [1,2,2,2] **Output:** 2 **Explanation:** We can split the array at index 2 to obtain arrays [1,2,2] and [2]. In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3. In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1. Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split. It can be shown that index 2 is the minimum index of a valid split. ``` **Example 2:** ``` **Input:** nums = [2,1,3,1,1,1,7,1,2,1] **Output:** 4 **Explanation:** We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1]. In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split. It can be shown that index 4 is the minimum index of a valid split. ``` **Example 3:** ``` **Input:** nums = [3,3,3,3,7,2,2] **Output:** -1 **Explanation:** It can be shown that there is no valid split. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `nums` has exactly one dominant element. Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumIndex(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumIndex(*[[1, 2, 2, 2]]) == 2\nassert my_solution.minimumIndex(*[[2, 1, 3, 1, 1, 1, 7, 1, 2, 1]]) == 4\nassert my_solution.minimumIndex(*[[3, 3, 3, 3, 7, 2, 2]]) == -1\nassert my_solution.minimumIndex(*[[1]]) == -1\nassert my_solution.minimumIndex(*[[1, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 1, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 1, 2]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 1, 3]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 1, 4]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 2]]) == -1\nassert my_solution.minimumIndex(*[[1, 1, 2, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 3]]) == -1\nassert my_solution.minimumIndex(*[[1, 1, 3, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 1, 4]]) == -1\nassert my_solution.minimumIndex(*[[1, 1, 4, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 2, 1]]) == -1\nassert my_solution.minimumIndex(*[[1, 2, 1, 1]]) == 0\nassert my_solution.minimumIndex(*[[1, 2, 2]]) == -1\nassert my_solution.minimumIndex(*[[1, 3, 1]]) == -1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2888", "questionFrontendId": "2780", "questionTitle": "Minimum Index of a Valid Split", "stats": { "totalAccepted": "5K", "totalSubmission": "7.5K", "totalAcceptedRaw": 4951, "totalSubmissionRaw": 7546, "acRate": "65.6%" } }
LeetCode/2884
# Length of the Longest Valid Substring You are given a string `word` and an array of strings `forbidden`. A string is called **valid** if none of its substrings are present in `forbidden`. Return *the length of the **longest valid substring** of the string* `word`. A **substring** is a contiguous sequence of characters in a string, possibly empty.   **Example 1:** ``` **Input:** word = "cbaaaabc", forbidden = ["aaa","cb"] **Output:** 4 **Explanation:** There are 11 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" and "aabc". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "aaa" or "cb" as a substring. ``` **Example 2:** ``` **Input:** word = "leetcode", forbidden = ["de","le","e"] **Output:** 4 **Explanation:** There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "de", "le", or "e" as a substring. ```   **Constraints:** * `1 <= word.length <= 105` * `word` consists only of lowercase English letters. * `1 <= forbidden.length <= 105` * `1 <= forbidden[i].length <= 10` * `forbidden[i]` consists only of lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def longestValidSubstring(self, word: str, forbidden: List[str]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.longestValidSubstring(*['cbaaaabc', ['aaa', 'cb']]) == 4\nassert my_solution.longestValidSubstring(*['leetcode', ['de', 'le', 'e']]) == 4\nassert my_solution.longestValidSubstring(*['a', ['n']]) == 1\nassert my_solution.longestValidSubstring(*['a', ['s']]) == 1\nassert my_solution.longestValidSubstring(*['a', ['a']]) == 0\nassert my_solution.longestValidSubstring(*['b', ['g']]) == 1\nassert my_solution.longestValidSubstring(*['b', ['t']]) == 1\nassert my_solution.longestValidSubstring(*['b', ['b']]) == 0\nassert my_solution.longestValidSubstring(*['c', ['k']]) == 1\nassert my_solution.longestValidSubstring(*['c', ['s']]) == 1\nassert my_solution.longestValidSubstring(*['c', ['c']]) == 0\nassert my_solution.longestValidSubstring(*['d', ['h']]) == 1\nassert my_solution.longestValidSubstring(*['d', ['n']]) == 1\nassert my_solution.longestValidSubstring(*['d', ['d']]) == 0\nassert my_solution.longestValidSubstring(*['e', ['s']]) == 1\nassert my_solution.longestValidSubstring(*['e', ['e']]) == 0\nassert my_solution.longestValidSubstring(*['f', ['b']]) == 1\nassert my_solution.longestValidSubstring(*['f', ['s']]) == 1\nassert my_solution.longestValidSubstring(*['f', ['f']]) == 0\nassert my_solution.longestValidSubstring(*['g', ['r']]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2884", "questionFrontendId": "2781", "questionTitle": "Length of the Longest Valid Substring", "stats": { "totalAccepted": "4K", "totalSubmission": "10.3K", "totalAcceptedRaw": 4003, "totalSubmissionRaw": 10322, "acRate": "38.8%" } }
LeetCode/2812
# Find the Maximum Achievable Number You are given two integers, `num` and `t`. An integer `x` is called **achievable** if it can become equal to `num` after applying the following operation no more than `t` times: * Increase or decrease `x` by `1`, and simultaneously increase or decrease `num` by `1`. Return *the maximum possible achievable number*. It can be proven that there exists at least one achievable number.   **Example 1:** ``` **Input:** num = 4, t = 1 **Output:** 6 **Explanation:** The maximum achievable number is x = 6; it can become equal to num after performing this operation: 1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 6. ``` **Example 2:** ``` **Input:** num = 3, t = 2 **Output:** 7 **Explanation:** The maximum achievable number is x = 7; after performing these operations, x will equal num: 1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4. 2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 7. ```   **Constraints:** * `1 <= num, t <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def theMaximumAchievableX(self, num: int, t: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.theMaximumAchievableX(*[4, 1]) == 6\nassert my_solution.theMaximumAchievableX(*[3, 2]) == 7\nassert my_solution.theMaximumAchievableX(*[1, 1]) == 3\nassert my_solution.theMaximumAchievableX(*[1, 2]) == 5\nassert my_solution.theMaximumAchievableX(*[1, 3]) == 7\nassert my_solution.theMaximumAchievableX(*[1, 4]) == 9\nassert my_solution.theMaximumAchievableX(*[1, 5]) == 11\nassert my_solution.theMaximumAchievableX(*[1, 6]) == 13\nassert my_solution.theMaximumAchievableX(*[1, 7]) == 15\nassert my_solution.theMaximumAchievableX(*[1, 8]) == 17\nassert my_solution.theMaximumAchievableX(*[1, 9]) == 19\nassert my_solution.theMaximumAchievableX(*[1, 10]) == 21\nassert my_solution.theMaximumAchievableX(*[1, 11]) == 23\nassert my_solution.theMaximumAchievableX(*[1, 12]) == 25\nassert my_solution.theMaximumAchievableX(*[1, 13]) == 27\nassert my_solution.theMaximumAchievableX(*[1, 14]) == 29\nassert my_solution.theMaximumAchievableX(*[1, 15]) == 31\nassert my_solution.theMaximumAchievableX(*[1, 16]) == 33\nassert my_solution.theMaximumAchievableX(*[1, 17]) == 35\nassert my_solution.theMaximumAchievableX(*[1, 18]) == 37\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2812", "questionFrontendId": "2769", "questionTitle": "Find the Maximum Achievable Number", "stats": { "totalAccepted": "13.9K", "totalSubmission": "15.5K", "totalAcceptedRaw": 13921, "totalSubmissionRaw": 15516, "acRate": "89.7%" } }
LeetCode/2855
# Maximum Number of Jumps to Reach the Last Index You are given a **0-indexed** array `nums` of `n` integers and an integer `target`. You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that: * `0 <= i < j < n` * `-target <= nums[j] - nums[i] <= target` Return *the **maximum number of jumps** you can make to reach index* `n - 1`. If there is no way to reach index `n - 1`, return `-1`.   **Example 1:** ``` **Input:** nums = [1,3,6,4,1,2], target = 2 **Output:** 3 **Explanation:** To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 3. - Jump from index 3 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. ``` **Example 2:** ``` **Input:** nums = [1,3,6,4,1,2], target = 3 **Output:** 5 **Explanation:** To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 2. - Jump from index 2 to index 3. - Jump from index 3 to index 4. - Jump from index 4 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. ``` **Example 3:** ``` **Input:** nums = [1,3,6,4,1,2], target = 0 **Output:** -1 **Explanation:** It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. ```   **Constraints:** * `2 <= nums.length == n <= 1000` * `-109 <= nums[i] <= 109` * `0 <= target <= 2 * 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumJumps(self, nums: List[int], target: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumJumps(*[[1, 3, 6, 4, 1, 2], 2]) == 3\nassert my_solution.maximumJumps(*[[1, 3, 6, 4, 1, 2], 3]) == 5\nassert my_solution.maximumJumps(*[[1, 3, 6, 4, 1, 2], 0]) == -1\nassert my_solution.maximumJumps(*[[0, 1], 0]) == -1\nassert my_solution.maximumJumps(*[[0, 1], 1]) == 1\nassert my_solution.maximumJumps(*[[0, 1], 2]) == 1\nassert my_solution.maximumJumps(*[[1, 0], 0]) == -1\nassert my_solution.maximumJumps(*[[1, 0], 1]) == 1\nassert my_solution.maximumJumps(*[[1, 0], 2]) == 1\nassert my_solution.maximumJumps(*[[0, 1, 2], 0]) == -1\nassert my_solution.maximumJumps(*[[0, 1, 2], 1]) == 2\nassert my_solution.maximumJumps(*[[0, 1, 2], 2]) == 2\nassert my_solution.maximumJumps(*[[0, 1, 2], 3]) == 2\nassert my_solution.maximumJumps(*[[0, 2, 1], 0]) == -1\nassert my_solution.maximumJumps(*[[0, 2, 1], 1]) == 1\nassert my_solution.maximumJumps(*[[0, 2, 1], 2]) == 2\nassert my_solution.maximumJumps(*[[0, 2, 1], 3]) == 2\nassert my_solution.maximumJumps(*[[1, 0, 2], 0]) == -1\nassert my_solution.maximumJumps(*[[1, 0, 2], 1]) == 1\nassert my_solution.maximumJumps(*[[1, 0, 2], 2]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2855", "questionFrontendId": "2770", "questionTitle": "Maximum Number of Jumps to Reach the Last Index", "stats": { "totalAccepted": "6.8K", "totalSubmission": "19K", "totalAcceptedRaw": 6779, "totalSubmissionRaw": 18955, "acRate": "35.8%" } }
LeetCode/2869
# Longest Non-decreasing Subarray From Two Arrays You are given two **0-indexed** integer arrays `nums1` and `nums2` of length `n`. Let's define another **0-indexed** integer array, `nums3`, of length `n`. For each index `i` in the range `[0, n - 1]`, you can assign either `nums1[i]` or `nums2[i]` to `nums3[i]`. Your task is to maximize the length of the **longest non-decreasing subarray** in `nums3` by choosing its values optimally. Return *an integer representing the length of the **longest non-decreasing** subarray in* `nums3`. **Note:** A **subarray** is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums1 = [2,3,1], nums2 = [1,2,1] **Output:** 2 **Explanation:** One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. We can show that 2 is the maximum achievable length. ``` **Example 2:** ``` **Input:** nums1 = [1,3,2,1], nums2 = [2,2,3,4] **Output:** 4 **Explanation:** One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length. ``` **Example 3:** ``` **Input:** nums1 = [1,1], nums2 = [2,2] **Output:** 2 **Explanation:** One way to construct nums3 is: nums3 = [nums1[0], nums1[1]] => [1,1]. The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length. ```   **Constraints:** * `1 <= nums1.length == nums2.length == n <= 105` * `1 <= nums1[i], nums2[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxNonDecreasingLength(*[[2, 3, 1], [1, 2, 1]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 3, 2, 1], [2, 2, 3, 4]]) == 4\nassert my_solution.maxNonDecreasingLength(*[[1, 1], [2, 2]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1], [1]]) == 1\nassert my_solution.maxNonDecreasingLength(*[[1], [2]]) == 1\nassert my_solution.maxNonDecreasingLength(*[[1, 4], [4, 19]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 8], [10, 1]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 11], [9, 1]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 13], [18, 1]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 19], [12, 20]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[1, 19], [18, 9]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[2, 20], [1, 18]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 5], [13, 3]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 6], [10, 12]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 7], [8, 3]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 8], [15, 2]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 9], [11, 3]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 12], [7, 3]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 12], [20, 3]]) == 2\nassert my_solution.maxNonDecreasingLength(*[[3, 20], [5, 17]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2869", "questionFrontendId": "2771", "questionTitle": "Longest Non-decreasing Subarray From Two Arrays", "stats": { "totalAccepted": "5.7K", "totalSubmission": "16.3K", "totalAcceptedRaw": 5687, "totalSubmissionRaw": 16309, "acRate": "34.9%" } }
LeetCode/2878
# Apply Operations to Make All Array Elements Equal to Zero 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** subarray of size `k` from the array and **decrease** all its elements by `1`. Return `true` *if you can make all the array elements equal to* `0`*, or* `false` *otherwise*. A **subarray** is a contiguous non-empty part of an array.   **Example 1:** ``` **Input:** nums = [2,2,3,1,1,0], k = 3 **Output:** true **Explanation:** We can do the following operations: - Choose the subarray [2,2,3]. The resulting array will be nums = [**1**,**1**,**2**,1,1,0]. - Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,**1**,**0**,**0**,0]. - Choose the subarray [1,1,1]. The resulting array will be nums = [**0**,**0**,**0**,0,0,0]. ``` **Example 2:** ``` **Input:** nums = [1,3,1,1], k = 2 **Output:** false **Explanation:** It is not possible to make all the array elements equal to 0. ```   **Constraints:** * `1 <= k <= nums.length <= 105` * `0 <= nums[i] <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def checkArray(self, nums: List[int], k: int) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.checkArray(*[[2, 2, 3, 1, 1, 0], 3]) == True\nassert my_solution.checkArray(*[[1, 3, 1, 1], 2]) == False\nassert my_solution.checkArray(*[[60, 72, 87, 89, 63, 52, 64, 62, 31, 37, 57, 83, 98, 94, 92, 77, 94, 91, 87, 100, 91, 91, 50, 26], 4]) == True\nassert my_solution.checkArray(*[[63, 40, 30, 0, 72, 53], 1]) == True\nassert my_solution.checkArray(*[[59, 60, 99, 99, 99, 99, 99, 99, 99, 40, 39, 0], 9]) == True\nassert my_solution.checkArray(*[[24, 24, 14, 37, 31, 88, 94, 38, 94, 0, 100, 100, 4, 46, 5, 50, 0, 33, 22, 25, 0], 10]) == False\nassert my_solution.checkArray(*[[0, 0, 51, 67, 80, 98, 88, 75, 89, 83, 100, 70, 77, 82, 57, 100, 80, 69, 19, 17], 3]) == True\nassert my_solution.checkArray(*[[22], 1]) == True\nassert my_solution.checkArray(*[[22, 4, 1, 25, 68, 30, 97, 99, 100, 22, 20, 39, 85, 68, 3, 1, 1, 74], 4]) == False\nassert my_solution.checkArray(*[[8, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 43, 0], 25]) == True\nassert my_solution.checkArray(*[[27, 99, 7, 1, 94, 63, 84, 46, 76, 35, 97, 77, 19, 72, 3], 2]) == False\nassert my_solution.checkArray(*[[0, 0, 39, 84, 86, 94, 55, 10, 8, 0], 4]) == True\nassert my_solution.checkArray(*[[60, 78, 96, 97, 97, 97, 49, 7, 97, 97, 97, 99, 97, 97, 97, 97, 85, 97, 97, 97, 37, 5, 1], 20]) == False\nassert my_solution.checkArray(*[[12, 79, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 83, 16, 0], 24]) == True\nassert my_solution.checkArray(*[[0, 0, 33, 72, 86, 53, 14], 3]) == True\nassert my_solution.checkArray(*[[0, 0, 8, 64, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 81, 25], 25]) == True\nassert my_solution.checkArray(*[[84, 0, 68, 95, 95, 0, 25, 0, 7, 71, 4, 68, 23, 97, 80, 0], 1]) == True\nassert my_solution.checkArray(*[[48, 48, 48, 48, 48], 5]) == True\nassert my_solution.checkArray(*[[34, 34, 99, 93, 93, 26, 99, 100, 94, 94, 82, 86, 100, 100, 87, 100, 100, 100, 100, 100, 63, 100, 100, 66, 17, 10, 8, 7, 3, 1], 23]) == False\nassert my_solution.checkArray(*[[67, 98, 97, 99, 98, 97, 97, 96, 99, 99, 99, 42, 68, 18, 99, 44, 95, 79, 1, 16, 49, 1, 2, 2, 0], 16]) == False\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2878", "questionFrontendId": "2772", "questionTitle": "Apply Operations to Make All Array Elements Equal to Zero", "stats": { "totalAccepted": "5.6K", "totalSubmission": "15K", "totalAcceptedRaw": 5587, "totalSubmissionRaw": 15049, "acRate": "37.1%" } }
LeetCode/2870
# Longest Alternating Subarray You are given a **0-indexed** integer array `nums`. A subarray `s` of length `m` is called **alternating** if: * `m` is greater than `1`. * `s1 = s0 + 1`. * The **0-indexed** subarray `s` looks like `[s0, s1, s0, s1,...,s(m-1) % 2]`. In other words, `s1 - s0 = 1`, `s2 - s1 = -1`, `s3 - s2 = 1`, `s4 - s3 = -1`, and so on up to `s[m - 1] - s[m - 2] = (-1)m`. Return *the maximum length of all **alternating** subarrays present in* `nums` *or* `-1` *if no such subarray exists**.* A subarray is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [2,3,4,3,4] **Output:** 4 **Explanation:** The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4. ``` **Example 2:** ``` **Input:** nums = [4,5,6] **Output:** 2 **Explanation:** [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2. ```   **Constraints:** * `2 <= nums.length <= 100` * `1 <= nums[i] <= 104` Please make sure your answer follows the type signature below: ```python3 class Solution: def alternatingSubarray(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.alternatingSubarray(*[[2, 3, 4, 3, 4]]) == 4\nassert my_solution.alternatingSubarray(*[[4, 5, 6]]) == 2\nassert my_solution.alternatingSubarray(*[[31, 32, 31, 32, 33]]) == 4\nassert my_solution.alternatingSubarray(*[[21, 9, 5]]) == -1\nassert my_solution.alternatingSubarray(*[[42, 43, 44, 43, 44, 43, 44, 45, 46]]) == 6\nassert my_solution.alternatingSubarray(*[[13, 14, 15, 14]]) == 3\nassert my_solution.alternatingSubarray(*[[74, 75, 74, 75, 74, 75, 74, 75]]) == 8\nassert my_solution.alternatingSubarray(*[[77, 78, 79, 78, 79, 78, 79, 78, 79, 80]]) == 8\nassert my_solution.alternatingSubarray(*[[88, 42, 53]]) == -1\nassert my_solution.alternatingSubarray(*[[64, 65, 64, 65, 64, 65, 66, 65, 66, 65]]) == 6\nassert my_solution.alternatingSubarray(*[[99, 100, 99, 100]]) == 4\nassert my_solution.alternatingSubarray(*[[21, 22]]) == 2\nassert my_solution.alternatingSubarray(*[[23, 24, 23, 24, 25, 24, 25, 24, 25]]) == 6\nassert my_solution.alternatingSubarray(*[[20, 9, 15, 15]]) == -1\nassert my_solution.alternatingSubarray(*[[92, 93, 92, 93, 92]]) == 5\nassert my_solution.alternatingSubarray(*[[24, 25, 26]]) == 2\nassert my_solution.alternatingSubarray(*[[51, 52, 53, 52, 53, 52, 53, 54, 53]]) == 6\nassert my_solution.alternatingSubarray(*[[65, 66, 65, 66, 67, 68, 69]]) == 4\nassert my_solution.alternatingSubarray(*[[29, 2, 5, 24]]) == -1\nassert my_solution.alternatingSubarray(*[[26, 27, 26, 27, 28, 27, 28, 27, 28]]) == 6\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2870", "questionFrontendId": "2765", "questionTitle": "Longest Alternating Subarray", "stats": { "totalAccepted": "14.8K", "totalSubmission": "29.8K", "totalAcceptedRaw": 14784, "totalSubmissionRaw": 29841, "acRate": "49.5%" } }
LeetCode/2834
# Relocate Marbles You are given a **0-indexed** integer array `nums` representing the initial positions of some marbles. You are also given two **0-indexed** integer arrays `moveFrom` and `moveTo` of **equal** length. Throughout `moveFrom.length` steps, you will change the positions of the marbles. On the `ith` step, you will move **all** marbles at position `moveFrom[i]` to position `moveTo[i]`. After completing all the steps, return *the sorted list of **occupied** positions*. **Notes:** * We call a position **occupied** if there is at least one marble in that position. * There may be multiple marbles in a single position.   **Example 1:** ``` **Input:** nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] **Output:** [5,6,8,9] **Explanation:** Initially, the marbles are at positions 1,6,7,8. At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied. At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied. At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied. At the end, the final positions containing at least one marbles are [5,6,8,9]. ``` **Example 2:** ``` **Input:** nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] **Output:** [2] **Explanation:** Initially, the marbles are at positions [1,1,3,3]. At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3]. At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2]. Since 2 is the only occupied position, we return [2]. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= moveFrom.length <= 105` * `moveFrom.length == moveTo.length` * `1 <= nums[i], moveFrom[i], moveTo[i] <= 109` * The test cases are generated such that there is at least a marble in `moveFrom[i]` at the moment we want to apply the `ith` move. Please make sure your answer follows the type signature below: ```python3 class Solution: def relocateMarbles(self, nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.relocateMarbles(*[[1, 6, 7, 8], [1, 7, 2], [2, 9, 5]]) == [5, 6, 8, 9]\nassert my_solution.relocateMarbles(*[[1, 1, 3, 3], [1, 3], [2, 2]]) == [2]\nassert my_solution.relocateMarbles(*[[5, 7, 8, 15], [5, 7, 8, 9], [9, 15, 2, 7]]) == [2, 7, 15]\nassert my_solution.relocateMarbles(*[[4, 6, 6, 9, 18], [18, 6, 17, 4, 9, 19, 2], [23, 17, 20, 19, 11, 2, 20]]) == [11, 20, 23]\nassert my_solution.relocateMarbles(*[[3, 4], [4, 3, 1, 2, 2, 3, 2, 4, 1], [3, 1, 2, 2, 3, 2, 4, 1, 1]]) == [1]\nassert my_solution.relocateMarbles(*[[5, 13, 22, 23, 23, 33], [13, 5, 12], [1, 12, 13]]) == [1, 13, 22, 23, 33]\nassert my_solution.relocateMarbles(*[[21, 24, 35, 72, 77, 82, 82, 96, 97, 97], [82, 76, 3, 97], [76, 3, 52, 27]]) == [21, 24, 27, 35, 52, 72, 77, 96]\nassert my_solution.relocateMarbles(*[[4, 6, 17, 41, 46, 46, 52, 57], [4], [62]]) == [6, 17, 41, 46, 52, 57, 62]\nassert my_solution.relocateMarbles(*[[1, 4, 10, 24, 46, 55, 61, 63, 71], [10, 52, 1, 80, 63, 55, 4, 46, 71, 24], [52, 42, 80, 55, 50, 62, 60, 17, 46, 38]]) == [17, 38, 42, 46, 50, 60, 61, 62]\nassert my_solution.relocateMarbles(*[[8, 9, 16, 17, 23], [8, 5, 16, 2, 9], [5, 20, 2, 18, 22]]) == [17, 18, 20, 22, 23]\nassert my_solution.relocateMarbles(*[[12, 37, 46, 47, 49, 55, 59, 65, 71, 88], [88, 59, 71], [81, 39, 73]]) == [12, 37, 39, 46, 47, 49, 55, 65, 73, 81]\nassert my_solution.relocateMarbles(*[[2, 45, 45, 48, 51, 57, 67, 73, 78, 78], [78, 67, 45, 34, 51, 62, 48, 95, 2, 67], [34, 65, 62, 95, 62, 12, 85, 67, 79, 71]]) == [12, 57, 65, 71, 73, 79, 85]\nassert my_solution.relocateMarbles(*[[1, 2], [1, 2, 3], [2, 3, 2]]) == [2]\nassert my_solution.relocateMarbles(*[[7, 19, 28, 34, 36, 36, 47], [36, 33, 34, 28, 41, 19, 14, 47, 28, 40], [33, 41, 27, 47, 14, 40, 46, 28, 42, 16]]) == [7, 16, 27, 42, 46]\nassert my_solution.relocateMarbles(*[[1, 1, 1], [1], [7]]) == [7]\nassert my_solution.relocateMarbles(*[[1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) == [1]\nassert my_solution.relocateMarbles(*[[5, 9, 17, 20, 29, 29], [20, 5, 1, 29, 22, 21, 9, 36, 33, 1], [1, 22, 21, 36, 36, 15, 33, 1, 3, 15]]) == [3, 15, 17]\nassert my_solution.relocateMarbles(*[[1], [1, 1, 1, 1], [1, 1, 1, 1]]) == [1]\nassert my_solution.relocateMarbles(*[[27, 41, 50, 52, 57, 60, 65, 67, 70], [52, 67, 70, 50, 57, 27, 47], [45, 45, 61, 47, 21, 65, 60]]) == [21, 41, 45, 60, 61, 65]\nassert my_solution.relocateMarbles(*[[2, 3, 7], [2, 7, 8, 8, 3, 5, 1, 4], [8, 5, 8, 1, 4, 4, 4, 5]]) == [5]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2834", "questionFrontendId": "2766", "questionTitle": "Relocate Marbles", "stats": { "totalAccepted": "3.8K", "totalSubmission": "6.9K", "totalAcceptedRaw": 3816, "totalSubmissionRaw": 6860, "acRate": "55.6%" } }
LeetCode/2883
# Partition String Into Minimum Beautiful Substrings Given a binary string `s`, partition the string into one or more **substrings** such that each substring is **beautiful**. A string is **beautiful** if: * It doesn't contain leading zeros. * It's the **binary** representation of a number that is a power of `5`. Return *the **minimum** number of substrings in such partition.* If it is impossible to partition the string `s` into beautiful substrings, return `-1`. A **substring** is a contiguous sequence of characters in a string.   **Example 1:** ``` **Input:** s = "1011" **Output:** 2 **Explanation:** We can paritition the given string into ["101", "1"]. - The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into. ``` **Example 2:** ``` **Input:** s = "111" **Output:** 3 **Explanation:** We can paritition the given string into ["1", "1", "1"]. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into. ``` **Example 3:** ``` **Input:** s = "0" **Output:** -1 **Explanation:** We can not partition the given string into beautiful substrings. ```   **Constraints:** * `1 <= s.length <= 15` * `s[i]` is either `'0'` or `'1'`. Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumBeautifulSubstrings(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumBeautifulSubstrings(*['1011']) == 2\nassert my_solution.minimumBeautifulSubstrings(*['111']) == 3\nassert my_solution.minimumBeautifulSubstrings(*['0']) == -1\nassert my_solution.minimumBeautifulSubstrings(*['100111000110111']) == 4\nassert my_solution.minimumBeautifulSubstrings(*['100111000111']) == 3\nassert my_solution.minimumBeautifulSubstrings(*['1001110001111']) == 4\nassert my_solution.minimumBeautifulSubstrings(*['100111000111101']) == 4\nassert my_solution.minimumBeautifulSubstrings(*['10110011100011']) == 3\nassert my_solution.minimumBeautifulSubstrings(*['101101']) == 2\nassert my_solution.minimumBeautifulSubstrings(*['101101101']) == 3\nassert my_solution.minimumBeautifulSubstrings(*['101101101101101']) == 5\nassert my_solution.minimumBeautifulSubstrings(*['1011011011101']) == 5\nassert my_solution.minimumBeautifulSubstrings(*['10110110111011']) == 6\nassert my_solution.minimumBeautifulSubstrings(*['101101101111001']) == 5\nassert my_solution.minimumBeautifulSubstrings(*['1011011011111']) == 7\nassert my_solution.minimumBeautifulSubstrings(*['101101110011101']) == 5\nassert my_solution.minimumBeautifulSubstrings(*['10110111001111']) == 6\nassert my_solution.minimumBeautifulSubstrings(*['1011011101101']) == 5\nassert my_solution.minimumBeautifulSubstrings(*['101101110110111']) == 7\nassert my_solution.minimumBeautifulSubstrings(*['101101110111011']) == 7\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2883", "questionFrontendId": "2767", "questionTitle": "Partition String Into Minimum Beautiful Substrings", "stats": { "totalAccepted": "3.2K", "totalSubmission": "5.6K", "totalAcceptedRaw": 3180, "totalSubmissionRaw": 5615, "acRate": "56.6%" } }
LeetCode/2866
# Longest Even Odd Subarray With Threshold You are given a **0-indexed** integer array `nums` and an integer `threshold`. Find the length of the **longest subarray** of `nums` starting at index `l` and ending at index `r` `(0 <= l <= r < nums.length)` that satisfies the following conditions: * `nums[l] % 2 == 0` * For all indices `i` in the range `[l, r - 1]`, `nums[i] % 2 != nums[i + 1] % 2` * For all indices `i` in the range `[l, r]`, `nums[i] <= threshold` Return *an integer denoting the length of the longest such subarray.* **Note:** A **subarray** is a contiguous non-empty sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [3,2,5,4], threshold = 5 **Output:** 3 **Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. ``` **Example 2:** ``` **Input:** nums = [1,2], threshold = 2 **Output:** 1 **Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. It satisfies all the conditions and we can show that 1 is the maximum possible achievable length. ``` **Example 3:** ``` **Input:** nums = [2,3,4,5], threshold = 4 **Output:** 3 **Explanation:** In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 100` * `1 <= threshold <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.longestAlternatingSubarray(*[[3, 2, 5, 4], 5]) == 3\nassert my_solution.longestAlternatingSubarray(*[[1, 2], 2]) == 1\nassert my_solution.longestAlternatingSubarray(*[[2, 3, 4, 5], 4]) == 3\nassert my_solution.longestAlternatingSubarray(*[[1], 1]) == 0\nassert my_solution.longestAlternatingSubarray(*[[2], 2]) == 1\nassert my_solution.longestAlternatingSubarray(*[[3], 3]) == 0\nassert my_solution.longestAlternatingSubarray(*[[4], 1]) == 0\nassert my_solution.longestAlternatingSubarray(*[[5], 3]) == 0\nassert my_solution.longestAlternatingSubarray(*[[6], 5]) == 0\nassert my_solution.longestAlternatingSubarray(*[[7], 2]) == 0\nassert my_solution.longestAlternatingSubarray(*[[8], 1]) == 0\nassert my_solution.longestAlternatingSubarray(*[[9], 7]) == 0\nassert my_solution.longestAlternatingSubarray(*[[10], 7]) == 0\nassert my_solution.longestAlternatingSubarray(*[[1, 3], 16]) == 0\nassert my_solution.longestAlternatingSubarray(*[[1, 6], 2]) == 0\nassert my_solution.longestAlternatingSubarray(*[[1, 10], 6]) == 0\nassert my_solution.longestAlternatingSubarray(*[[1, 10], 7]) == 0\nassert my_solution.longestAlternatingSubarray(*[[2, 2], 18]) == 1\nassert my_solution.longestAlternatingSubarray(*[[2, 4], 7]) == 1\nassert my_solution.longestAlternatingSubarray(*[[2, 5], 2]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2866", "questionFrontendId": "2760", "questionTitle": "Longest Even Odd Subarray With Threshold", "stats": { "totalAccepted": "28.9K", "totalSubmission": "63.7K", "totalAcceptedRaw": 28941, "totalSubmissionRaw": 63719, "acRate": "45.4%" } }
LeetCode/2873
# Prime Pairs With Target Sum You are given an integer `n`. We say that two integers `x` and `y` form a prime number pair if: * `1 <= x <= y <= n` * `x + y == n` * `x` and `y` are prime numbers Return *the 2D sorted list of prime number pairs* `[xi, yi]`. The list should be sorted in **increasing** order of `xi`. If there are no prime number pairs at all, return *an empty array*. **Note:** A prime number is a natural number greater than `1` with only two factors, itself and `1`.   **Example 1:** ``` **Input:** n = 10 **Output:** [[3,7],[5,5]] **Explanation:** In this example, there are two prime pairs that satisfy the criteria. These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement. ``` **Example 2:** ``` **Input:** n = 2 **Output:** [] **Explanation:** We can show that there is no prime number pair that gives a sum of 2, so we return an empty array. ```   **Constraints:** * `1 <= n <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def findPrimePairs(self, n: int) -> List[List[int]]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findPrimePairs(*[10]) == [[3, 7], [5, 5]]\nassert my_solution.findPrimePairs(*[2]) == []\nassert my_solution.findPrimePairs(*[1]) == []\nassert my_solution.findPrimePairs(*[3]) == []\nassert my_solution.findPrimePairs(*[4]) == [[2, 2]]\nassert my_solution.findPrimePairs(*[5]) == [[2, 3]]\nassert my_solution.findPrimePairs(*[6]) == [[3, 3]]\nassert my_solution.findPrimePairs(*[7]) == [[2, 5]]\nassert my_solution.findPrimePairs(*[8]) == [[3, 5]]\nassert my_solution.findPrimePairs(*[9]) == [[2, 7]]\nassert my_solution.findPrimePairs(*[11]) == []\nassert my_solution.findPrimePairs(*[12]) == [[5, 7]]\nassert my_solution.findPrimePairs(*[13]) == [[2, 11]]\nassert my_solution.findPrimePairs(*[14]) == [[3, 11], [7, 7]]\nassert my_solution.findPrimePairs(*[15]) == [[2, 13]]\nassert my_solution.findPrimePairs(*[16]) == [[3, 13], [5, 11]]\nassert my_solution.findPrimePairs(*[17]) == []\nassert my_solution.findPrimePairs(*[18]) == [[5, 13], [7, 11]]\nassert my_solution.findPrimePairs(*[19]) == [[2, 17]]\nassert my_solution.findPrimePairs(*[20]) == [[3, 17], [7, 13]]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2873", "questionFrontendId": "2761", "questionTitle": "Prime Pairs With Target Sum", "stats": { "totalAccepted": "6.1K", "totalSubmission": "17.8K", "totalAcceptedRaw": 6135, "totalSubmissionRaw": 17830, "acRate": "34.4%" } }
LeetCode/2868
# Continuous Subarrays You are given a **0-indexed** integer array `nums`. A subarray of `nums` is called **continuous** if: * Let `i`, `i + 1`, ..., `j`be the indices in the subarray. Then, for each pair of indices `i <= i1, i2 <= j`, `0 <= |nums[i1] - nums[i2]| <= 2`. Return *the total number of **continuous** subarrays.* A subarray is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [5,4,2,4] **Output:** 8 **Explanation:** Continuous subarray of size 1: [5], [4], [2], [4]. Continuous subarray of size 2: [5,4], [4,2], [2,4]. Continuous subarray of size 3: [4,2,4]. Thereare no subarrys of size 4. Total continuous subarrays = 4 + 3 + 1 = 8. It can be shown that there are no more continuous subarrays. ```   **Example 2:** ``` **Input:** nums = [1,2,3] **Output:** 6 **Explanation:** Continuous subarray of size 1: [1], [2], [3]. Continuous subarray of size 2: [1,2], [2,3]. Continuous subarray of size 3: [1,2,3]. Total continuous subarrays = 3 + 2 + 1 = 6. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def continuousSubarrays(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.continuousSubarrays(*[[5, 4, 2, 4]]) == 8\nassert my_solution.continuousSubarrays(*[[1, 2, 3]]) == 6\nassert my_solution.continuousSubarrays(*[[31, 30, 31, 32]]) == 10\nassert my_solution.continuousSubarrays(*[[65, 66, 67, 66, 66, 65, 64, 65, 65, 64]]) == 43\nassert my_solution.continuousSubarrays(*[[42, 41, 42, 41, 41, 40, 39, 38]]) == 28\nassert my_solution.continuousSubarrays(*[[35, 35, 36, 37, 36, 37, 38, 37, 38]]) == 39\nassert my_solution.continuousSubarrays(*[[43, 44, 43, 44]]) == 10\nassert my_solution.continuousSubarrays(*[[14, 15, 15, 15, 16, 16, 16, 16, 15, 16]]) == 55\nassert my_solution.continuousSubarrays(*[[21]]) == 1\nassert my_solution.continuousSubarrays(*[[34, 34, 33, 34, 33, 33, 32, 31, 30, 31]]) == 39\nassert my_solution.continuousSubarrays(*[[58, 59, 59, 58, 59]]) == 15\nassert my_solution.continuousSubarrays(*[[10, 9, 8, 7, 8, 9, 9]]) == 24\nassert my_solution.continuousSubarrays(*[[65, 66, 65, 64, 63, 62, 62]]) == 20\nassert my_solution.continuousSubarrays(*[[65, 65, 64, 65, 66, 65]]) == 21\nassert my_solution.continuousSubarrays(*[[85, 84, 83, 84, 83, 82]]) == 20\nassert my_solution.continuousSubarrays(*[[60, 59, 60]]) == 6\nassert my_solution.continuousSubarrays(*[[96, 97, 98]]) == 6\nassert my_solution.continuousSubarrays(*[[21, 22, 23, 22, 23]]) == 15\nassert my_solution.continuousSubarrays(*[[76, 77, 77, 78, 77, 78, 78]]) == 28\nassert my_solution.continuousSubarrays(*[[27, 27, 27, 26, 26, 27, 27, 27, 27]]) == 45\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2868", "questionFrontendId": "2762", "questionTitle": "Continuous Subarrays", "stats": { "totalAccepted": "5K", "totalSubmission": "10.4K", "totalAcceptedRaw": 5024, "totalSubmissionRaw": 10377, "acRate": "48.4%" } }
LeetCode/2849
# Sum of Imbalance Numbers of All Subarrays The **imbalance number** of a **0-indexed** integer array `arr` of length `n` is defined as the number of indices in `sarr = sorted(arr)` such that: * `0 <= i < n - 1`, and * `sarr[i+1] - sarr[i] > 1` Here, `sorted(arr)` is the function that returns the sorted version of `arr`. Given a **0-indexed** integer array `nums`, return *the **sum of imbalance numbers** of all its **subarrays***. A **subarray** is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [2,3,1,4] **Output:** 3 **Explanation:** There are 3 subarrays with non-zeroimbalance numbers: - Subarray [3, 1] with an imbalance number of 1. - Subarray [3, 1, 4] with an imbalance number of 1. - Subarray [1, 4] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. ``` **Example 2:** ``` **Input:** nums = [1,3,3,3,5] **Output:** 8 **Explanation:** There are 7 subarrays with non-zero imbalance numbers: - Subarray [1, 3] with an imbalance number of 1. - Subarray [1, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. - Subarray [3, 3, 3, 5] with an imbalance number of 1. - Subarray [3, 3, 5] with an imbalance number of 1. - Subarray [3, 5] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. ```   **Constraints:** * `1 <= nums.length <= 1000` * `1 <= nums[i] <= nums.length` Please make sure your answer follows the type signature below: ```python3 class Solution: def sumImbalanceNumbers(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumImbalanceNumbers(*[[2, 3, 1, 4]]) == 3\nassert my_solution.sumImbalanceNumbers(*[[1, 3, 3, 3, 5]]) == 8\nassert my_solution.sumImbalanceNumbers(*[[1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 2]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[2, 1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[2, 2]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 1, 1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 1, 2]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 1, 3]]) == 2\nassert my_solution.sumImbalanceNumbers(*[[1, 2, 1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 2, 2]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 2, 3]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[1, 3, 1]]) == 3\nassert my_solution.sumImbalanceNumbers(*[[1, 3, 2]]) == 1\nassert my_solution.sumImbalanceNumbers(*[[1, 3, 3]]) == 2\nassert my_solution.sumImbalanceNumbers(*[[2, 1, 1]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[2, 1, 2]]) == 0\nassert my_solution.sumImbalanceNumbers(*[[2, 1, 3]]) == 1\nassert my_solution.sumImbalanceNumbers(*[[2, 2, 1]]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2849", "questionFrontendId": "2763", "questionTitle": "Sum of Imbalance Numbers of All Subarrays", "stats": { "totalAccepted": "2.8K", "totalSubmission": "4.5K", "totalAcceptedRaw": 2802, "totalSubmissionRaw": 4492, "acRate": "62.4%" } }
LeetCode/2831
# Number of Beautiful Pairs You are given a **0-indexed** integer array `nums`. A pair of indices `i`, `j` where `0 <= i < j < nums.length` is called beautiful if the **first digit** of `nums[i]` and the **last digit** of `nums[j]` are **coprime**. Return *the total number of beautiful pairs in* `nums`. Two integers `x` and `y` are **coprime** if there is no integer greater than 1 that divides both of them. In other words, `x` and `y` are coprime if `gcd(x, y) == 1`, where `gcd(x, y)` is the **greatest common divisor** of `x` and `y`.   **Example 1:** ``` **Input:** nums = [2,5,1,4] **Output:** 5 **Explanation:** There are 5 beautiful pairs in nums: When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1. When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1. When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1. When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1. When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1. Thus, we return 5. ``` **Example 2:** ``` **Input:** nums = [11,21,12] **Output:** 2 **Explanation:** There are 2 beautiful pairs: When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1. When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1. Thus, we return 2. ```   **Constraints:** * `2 <= nums.length <= 100` * `1 <= nums[i] <= 9999` * `nums[i] % 10 != 0` Please make sure your answer follows the type signature below: ```python3 class Solution: def countBeautifulPairs(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countBeautifulPairs(*[[2, 5, 1, 4]]) == 5\nassert my_solution.countBeautifulPairs(*[[11, 21, 12]]) == 2\nassert my_solution.countBeautifulPairs(*[[31, 25, 72, 79, 74]]) == 7\nassert my_solution.countBeautifulPairs(*[[84, 91, 18, 59, 27, 9, 81, 33, 17, 58]]) == 37\nassert my_solution.countBeautifulPairs(*[[8, 75, 28, 35, 21, 13, 21]]) == 19\nassert my_solution.countBeautifulPairs(*[[35, 52, 74, 92, 25, 65, 77, 1, 73, 32]]) == 37\nassert my_solution.countBeautifulPairs(*[[68, 8, 84, 14, 88, 42, 53]]) == 7\nassert my_solution.countBeautifulPairs(*[[64, 23, 99, 83, 5, 21, 76, 34, 99, 63]]) == 25\nassert my_solution.countBeautifulPairs(*[[18, 64, 12, 21]]) == 5\nassert my_solution.countBeautifulPairs(*[[78, 36, 58, 88]]) == 6\nassert my_solution.countBeautifulPairs(*[[99, 26, 92, 91, 53, 24, 25, 92, 73]]) == 22\nassert my_solution.countBeautifulPairs(*[[51, 65, 87, 6, 17, 32, 14, 42, 46]]) == 19\nassert my_solution.countBeautifulPairs(*[[43, 9, 75, 76, 25, 96, 46, 85, 19, 29]]) == 32\nassert my_solution.countBeautifulPairs(*[[5, 24]]) == 1\nassert my_solution.countBeautifulPairs(*[[26, 76, 24, 96, 82, 97, 97, 72, 35]]) == 25\nassert my_solution.countBeautifulPairs(*[[77, 82, 94, 55]]) == 5\nassert my_solution.countBeautifulPairs(*[[82, 3, 89, 52, 96, 72, 27, 59]]) == 17\nassert my_solution.countBeautifulPairs(*[[97, 6, 46, 88, 41, 52, 46, 4, 17]]) == 17\nassert my_solution.countBeautifulPairs(*[[95, 6]]) == 0\nassert my_solution.countBeautifulPairs(*[[69, 63, 24, 1, 71, 55, 46, 4, 61]]) == 26\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2831", "questionFrontendId": "2748", "questionTitle": "Number of Beautiful Pairs", "stats": { "totalAccepted": "5.6K", "totalSubmission": "9.7K", "totalAcceptedRaw": 5612, "totalSubmissionRaw": 9720, "acRate": "57.7%" } }
LeetCode/2837
# Minimum Operations to Make the Integer Zero You are given two integers `num1` and `num2`. In one operation, you can choose integer `i` in the range `[0, 60]` and subtract `2i + num2` from `num1`. Return *the integer denoting the **minimum** number of operations needed to make* `num1` *equal to* `0`. If it is impossible to make `num1` equal to `0`, return `-1`.   **Example 1:** ``` **Input:** num1 = 3, num2 = -2 **Output:** 3 **Explanation:** We can make 3 equal to 0 with the following operations: - We choose i = 2 and substract 22 + (-2) from 3, 3 - (4 + (-2)) = 1. - We choose i = 2 and substract 22 + (-2) from 1, 1 - (4 + (-2)) = -1. - We choose i = 0 and substract 20 + (-2) from -1, (-1) - (1 + (-2)) = 0. It can be proven, that 3 is the minimum number of operations that we need to perform. ``` **Example 2:** ``` **Input:** num1 = 5, num2 = 7 **Output:** -1 **Explanation:** It can be proven, that it is impossible to make 5 equal to 0 with the given operation. ```   **Constraints:** * `1 <= num1 <= 109` * `-109 <= num2 <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def makeTheIntegerZero(self, num1: int, num2: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.makeTheIntegerZero(*[3, -2]) == 3\nassert my_solution.makeTheIntegerZero(*[5, 7]) == -1\nassert my_solution.makeTheIntegerZero(*[5, -21]) == 3\nassert my_solution.makeTheIntegerZero(*[2, 71]) == -1\nassert my_solution.makeTheIntegerZero(*[59, 70]) == -1\nassert my_solution.makeTheIntegerZero(*[12, -55]) == 4\nassert my_solution.makeTheIntegerZero(*[71, -13]) == 5\nassert my_solution.makeTheIntegerZero(*[52, -12]) == 1\nassert my_solution.makeTheIntegerZero(*[3, -75]) == 6\nassert my_solution.makeTheIntegerZero(*[20, -12]) == 1\nassert my_solution.makeTheIntegerZero(*[29, -37]) == 3\nassert my_solution.makeTheIntegerZero(*[6, -2]) == 1\nassert my_solution.makeTheIntegerZero(*[14, 33]) == -1\nassert my_solution.makeTheIntegerZero(*[16, -11]) == 3\nassert my_solution.makeTheIntegerZero(*[16, -21]) == 4\nassert my_solution.makeTheIntegerZero(*[8, -6]) == 2\nassert my_solution.makeTheIntegerZero(*[34, 9]) == 2\nassert my_solution.makeTheIntegerZero(*[17, -65]) == 4\nassert my_solution.makeTheIntegerZero(*[74, 1]) == 2\nassert my_solution.makeTheIntegerZero(*[16, 10]) == -1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2837", "questionFrontendId": "2749", "questionTitle": "Minimum Operations to Make the Integer Zero", "stats": { "totalAccepted": "3.7K", "totalSubmission": "10.9K", "totalAcceptedRaw": 3672, "totalSubmissionRaw": 10932, "acRate": "33.6%" } }
LeetCode/2867
# Ways to Split Array Into Good Subarrays You are given a binary array `nums`. A subarray of an array is **good** if it contains **exactly** **one** element with the value `1`. Return *an integer denoting the number of ways to split the array* `nums` *into **good** subarrays*. As the number may be too large, return it **modulo** `109 + 7`. A subarray is a contiguous **non-empty** sequence of elements within an array.   **Example 1:** ``` **Input:** nums = [0,1,0,0,1] **Output:** 3 **Explanation:** There are 3 ways to split nums into good subarrays: - [0,1] [0,0,1] - [0,1,0] [0,1] - [0,1,0,0] [1] ``` **Example 2:** ``` **Input:** nums = [0,1,0] **Output:** 1 **Explanation:** There is 1 way to split nums into good subarrays: - [0,1,0] ```   **Constraints:** * `1 <= nums.length <= 105` * `0 <= nums[i] <= 1` Please make sure your answer follows the type signature below: ```python3 class Solution: def numberOfGoodSubarraySplits(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 0, 0, 1]]) == 3\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 0]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 0, 0]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 0, 1, 1]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 0, 0, 0, 0, 1, 0, 1]]) == 12\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1]]) == 8\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 1, 1, 0, 1, 1, 0, 0, 0]]) == 2\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1, 0, 0, 1, 0, 1, 0, 1, 1]]) == 12\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 1, 1, 0, 1, 0, 0, 1]]) == 12\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1]]) == 6\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1]]) == 18\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 0, 1]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 1, 0, 1, 1, 1, 1, 1]]) == 2\nassert my_solution.numberOfGoodSubarraySplits(*[[0, 1]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0]]) == 18\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 1, 0, 0]]) == 2\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 1, 0]]) == 1\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 0, 1, 0, 0, 1, 1]]) == 9\nassert my_solution.numberOfGoodSubarraySplits(*[[1, 0, 1, 1, 0, 0]]) == 2\nassert my_solution.numberOfGoodSubarraySplits(*[[1]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2867", "questionFrontendId": "2750", "questionTitle": "Ways to Split Array Into Good Subarrays", "stats": { "totalAccepted": "4.1K", "totalSubmission": "10.5K", "totalAcceptedRaw": 4148, "totalSubmissionRaw": 10543, "acRate": "39.3%" } }
LeetCode/2847
# Find Maximum Number of String Pairs You are given a **0-indexed** array `words` consisting of **distinct** strings. The string `words[i]` can be paired with the string `words[j]` if: * The string `words[i]` is equal to the reversed string of `words[j]`. * `0 <= i < j < words.length`. Return *the **maximum** number of pairs that can be formed from the array* `words`*.* Note that each string can belong in **at most one** pair.   **Example 1:** ``` **Input:** words = ["cd","ac","dc","ca","zz"] **Output:** 2 **Explanation:** In this example, we can form 2 pair of strings in the following way: - We pair the 0th string with the 2nd string, as the reversed string of word[0] is "dc" and is equal to words[2]. - We pair the 1st string with the 3rd string, as the reversed string of word[1] is "ca" and is equal to words[3]. It can be proven that 2 is the maximum number of pairs that can be formed. ``` **Example 2:** ``` **Input:** words = ["ab","ba","cc"] **Output:** 1 **Explanation:** In this example, we can form 1 pair of strings in the following way: - We pair the 0th string with the 1st string, as the reversed string of words[1] is "ab" and is equal to words[0]. It can be proven that 1 is the maximum number of pairs that can be formed. ``` **Example 3:** ``` **Input:** words = ["aa","ab"] **Output:** 0 **Explanation:** In this example, we are unable to form any pair of strings. ```   **Constraints:** * `1 <= words.length <= 50` * `words[i].length == 2` * `words` consists of distinct strings. * `words[i]` contains only lowercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumNumberOfStringPairs(self, words: List[str]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumNumberOfStringPairs(*[['cd', 'ac', 'dc', 'ca', 'zz']]) == 2\nassert my_solution.maximumNumberOfStringPairs(*[['ab', 'ba', 'cc']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['aa', 'ab']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['ku', 'dd', 'gu', 'uk']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['qf', 'ke', 'rh', 'bl']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['zs', 'pp', 'sz', 'ie']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['wk', 'xf', 'ot', 'je', 'hd', 'kw', 'fx', 'to', 'ej']]) == 4\nassert my_solution.maximumNumberOfStringPairs(*[['wm', 'lg', 'wy', 'ge', 'zq', 'kn', 'tx', 'dr', 'ws', 'iy']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['ny', 'gz', 'yr', 'kt', 'qd', 'yn', 'zg', 'ry', 'tk', 'dq']]) == 5\nassert my_solution.maximumNumberOfStringPairs(*[['xi', 'nw', 'qp', 'to', 'oo', 'xp', 'ix', 'wn', 'pq']]) == 3\nassert my_solution.maximumNumberOfStringPairs(*[['ws', 'zt', 'sw']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['qb', 'pl', 'ir', 'jj', 'pu', 'dn', 'bq', 'lp']]) == 2\nassert my_solution.maximumNumberOfStringPairs(*[['cw', 'fg', 'wc', 'gf']]) == 2\nassert my_solution.maximumNumberOfStringPairs(*[['ip', 'pi']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['ik', 'mn', 'hq', 'fx', 'ki']]) == 1\nassert my_solution.maximumNumberOfStringPairs(*[['jn', 'ik', 'ry']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['aa', 'wj', 'zp', 'df', 'xb', 'sa', 'jw', 'pz']]) == 2\nassert my_solution.maximumNumberOfStringPairs(*[['hw', 'pa', 'xx', 'la', 'am', 'vg']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['hj']]) == 0\nassert my_solution.maximumNumberOfStringPairs(*[['tg', 'gf', 'oz', 'nd', 'ks', 'fo', 'ac', 'gl', 'gt', 'fg']]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2847", "questionFrontendId": "2744", "questionTitle": "Find Maximum Number of String Pairs", "stats": { "totalAccepted": "28.4K", "totalSubmission": "32.6K", "totalAcceptedRaw": 28385, "totalSubmissionRaw": 32647, "acRate": "86.9%" } }
LeetCode/2850
# Construct the Longest New String You are given three integers `x`, `y`, and `z`. You have `x` strings equal to `"AA"`, `y` strings equal to `"BB"`, and `z` strings equal to `"AB"`. You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. This new string must not contain `"AAA"` or `"BBB"` as a substring. Return *the maximum possible length of the new string*. A **substring** is a contiguous **non-empty** sequence of characters within a string.   **Example 1:** ``` **Input:** x = 2, y = 5, z = 1 **Output:** 12 **Explanation:** We can concactenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB". That string has length 12, and we can show that it is impossible to construct a string of longer length. ``` **Example 2:** ``` **Input:** x = 3, y = 2, z = 2 **Output:** 14 **Explanation:** We can concactenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA". That string has length 14, and we can show that it is impossible to construct a string of longer length. ```   **Constraints:** * `1 <= x, y, z <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def longestString(self, x: int, y: int, z: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.longestString(*[2, 5, 1]) == 12\nassert my_solution.longestString(*[3, 2, 2]) == 14\nassert my_solution.longestString(*[1, 34, 1]) == 8\nassert my_solution.longestString(*[1, 39, 14]) == 34\nassert my_solution.longestString(*[1, 24, 7]) == 20\nassert my_solution.longestString(*[1, 43, 40]) == 86\nassert my_solution.longestString(*[1, 34, 39]) == 84\nassert my_solution.longestString(*[1, 10, 29]) == 64\nassert my_solution.longestString(*[1, 11, 9]) == 24\nassert my_solution.longestString(*[1, 34, 3]) == 12\nassert my_solution.longestString(*[1, 26, 35]) == 76\nassert my_solution.longestString(*[1, 43, 24]) == 54\nassert my_solution.longestString(*[1, 28, 11]) == 28\nassert my_solution.longestString(*[1, 40, 21]) == 48\nassert my_solution.longestString(*[1, 4, 20]) == 46\nassert my_solution.longestString(*[1, 41, 30]) == 66\nassert my_solution.longestString(*[1, 37, 7]) == 20\nassert my_solution.longestString(*[1, 23, 25]) == 56\nassert my_solution.longestString(*[1, 8, 5]) == 16\nassert my_solution.longestString(*[1, 14, 8]) == 22\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2850", "questionFrontendId": "2745", "questionTitle": "Construct the Longest New String", "stats": { "totalAccepted": "3.7K", "totalSubmission": "6.3K", "totalAcceptedRaw": 3667, "totalSubmissionRaw": 6265, "acRate": "58.5%" } }
LeetCode/2854
# Decremental String Concatenation You are given a **0-indexed** array `words` containing `n` strings. Let's define a **join** operation `join(x, y)` between two strings `x` and `y` as concatenating them into `xy`. However, if the last character of `x` is equal to the first character of `y`, one of them is **deleted**. For example `join("ab", "ba") = "aba"` and `join("ab", "cde") = "abcde"`. You are to perform `n - 1` **join** operations. Let `str0 = words[0]`. Starting from `i = 1` up to `i = n - 1`, for the `ith` operation, you can do one of the following: * Make `stri = join(stri - 1, words[i])` * Make `stri = join(words[i], stri - 1)` Your task is to **minimize** the length of `strn - 1`. Return *an integer denoting the minimum possible length of* `strn - 1`.   **Example 1:** ``` **Input:** words = ["aa","ab","bc"] **Output:** 4 **Explanation:** In this example, we can perform join operations in the following order to minimize the length of str2: str0 = "aa" str1 = join(str0, "ab") = "aab" str2 = join(str1, "bc") = "aabc" It can be shown that the minimum possible length of str2 is 4. ``` **Example 2:** ``` **Input:** words = ["ab","b"] **Output:** 2 **Explanation:** In this example, str0 = "ab", there are two ways to get str1: join(str0, "b") = "ab" or join("b", str0) = "bab". The first string, "ab", has the minimum length. Hence, the answer is 2. ``` **Example 3:** ``` **Input:** words = ["aaa","c","aba"] **Output:** 6 **Explanation:** In this example, we can perform join operations in the following order to minimize the length of str2: str0 = "aaa" str1 = join(str0, "c") = "aaac" str2 = join("aba", str1) = "abaaac" It can be shown that the minimum possible length of str2 is 6. ```     **Constraints:** * `1 <= words.length <= 1000` * `1 <= words[i].length <= 50` * Each character in `words[i]` is an English lowercase letter Please make sure your answer follows the type signature below: ```python3 class Solution: def minimizeConcatenatedLength(self, words: List[str]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimizeConcatenatedLength(*[['aa', 'ab', 'bc']]) == 4\nassert my_solution.minimizeConcatenatedLength(*[['ab', 'b']]) == 2\nassert my_solution.minimizeConcatenatedLength(*[['aaa', 'c', 'aba']]) == 6\nassert my_solution.minimizeConcatenatedLength(*[['a', 'a']]) == 1\nassert my_solution.minimizeConcatenatedLength(*[['a', 'ab']]) == 2\nassert my_solution.minimizeConcatenatedLength(*[['a', 'b']]) == 2\nassert my_solution.minimizeConcatenatedLength(*[['a', 'ca']]) == 2\nassert my_solution.minimizeConcatenatedLength(*[['a', 'cb']]) == 3\nassert my_solution.minimizeConcatenatedLength(*[['aa', 'a']]) == 2\nassert my_solution.minimizeConcatenatedLength(*[['aa', 'b']]) == 3\nassert my_solution.minimizeConcatenatedLength(*[['aa', 'ca']]) == 3\nassert my_solution.minimizeConcatenatedLength(*[['aa', 'ccc']]) == 5\nassert my_solution.minimizeConcatenatedLength(*[['aab', 'b']]) == 3\nassert my_solution.minimizeConcatenatedLength(*[['aab', 'ba']]) == 4\nassert my_solution.minimizeConcatenatedLength(*[['aab', 'bb']]) == 4\nassert my_solution.minimizeConcatenatedLength(*[['aab', 'bba']]) == 5\nassert my_solution.minimizeConcatenatedLength(*[['aab', 'c']]) == 4\nassert my_solution.minimizeConcatenatedLength(*[['aac', 'b']]) == 4\nassert my_solution.minimizeConcatenatedLength(*[['ab', 'acb']]) == 5\nassert my_solution.minimizeConcatenatedLength(*[['ab', 'bc']]) == 3\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2854", "questionFrontendId": "2746", "questionTitle": "Decremental String Concatenation", "stats": { "totalAccepted": "2.5K", "totalSubmission": "6.4K", "totalAcceptedRaw": 2537, "totalSubmissionRaw": 6371, "acRate": "39.8%" } }
LeetCode/2833
# Count Zero Request Servers You are given an integer `n` denoting the total number of servers and a **2D** **0-indexed** integer array `logs`, where `logs[i] = [server_id, time]` denotes that the server with id `server_id` received a request at time `time`. You are also given an integer `x` and a **0-indexed** integer array `queries`. Return *a **0-indexed** integer array* `arr` *of length* `queries.length` *where* `arr[i]` *represents the number of servers that **did not receive** any requests during the time interval* `[queries[i] - x, queries[i]]`. Note that the time intervals are inclusive.   **Example 1:** ``` **Input:** n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11] **Output:** [1,2] **Explanation:** For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests. For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period. ``` **Example 2:** ``` **Input:** n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4] **Output:** [0,1] **Explanation:** For queries[0]: All servers get at least one request in the duration of [1, 3]. For queries[1]: Only server with id 3 gets no request in the duration [2,4]. ```   **Constraints:** * `1 <= n <= 105` * `1 <= logs.length <= 105` * `1 <= queries.length <= 105` * `logs[i].length == 2` * `1 <= logs[i][0] <= n` * `1 <= logs[i][1] <= 106` * `1 <= x <= 105` * `x < queries[i] <= 106` Please make sure your answer follows the type signature below: ```python3 class Solution: def countServers(self, n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countServers(*[3, [[1, 3], [2, 6], [1, 5]], 5, [10, 11]]) == [1, 2]\nassert my_solution.countServers(*[3, [[2, 4], [2, 1], [1, 2], [3, 1]], 2, [3, 4]]) == [0, 1]\nassert my_solution.countServers(*[4, [[2, 30], [2, 5], [3, 9], [4, 21]], 9, [11, 28, 16, 18]]) == [2, 3, 3, 3]\nassert my_solution.countServers(*[3, [[2, 26], [3, 13], [3, 1]], 9, [28, 17]]) == [2, 2]\nassert my_solution.countServers(*[6, [[1, 21]], 10, [24, 35, 28, 26, 20, 25, 16, 31, 12]]) == [5, 6, 5, 5, 6, 5, 6, 5, 6]\nassert my_solution.countServers(*[3, [[1, 35], [1, 32], [1, 11], [1, 39], [2, 29]], 8, [38, 30, 23, 33, 15, 31, 34, 22, 11, 14]]) == [2, 2, 3, 1, 2, 2, 1, 3, 2, 2]\nassert my_solution.countServers(*[4, [[4, 3], [2, 16], [1, 21], [3, 22], [1, 13], [3, 10], [2, 1], [1, 12], [4, 13], [2, 18]], 8, [14, 28, 29]]) == [1, 2, 2]\nassert my_solution.countServers(*[4, [[1, 26], [2, 30], [4, 3], [3, 21], [4, 23], [1, 9], [1, 3], [4, 35], [1, 32], [2, 1]], 7, [35, 25, 21, 19, 8, 23, 27]]) == [1, 2, 3, 4, 1, 2, 1]\nassert my_solution.countServers(*[3, [[1, 1], [3, 28], [3, 32], [2, 22], [2, 7], [3, 30], [3, 9], [1, 18], [2, 4], [1, 19]], 6, [15, 33, 22, 29, 34, 14, 23]]) == [2, 2, 1, 2, 2, 2, 1]\nassert my_solution.countServers(*[5, [[3, 4], [5, 1], [4, 6]], 10, [32, 29]]) == [5, 5]\nassert my_solution.countServers(*[2, [[2, 24], [2, 33], [2, 12], [2, 20], [2, 23], [2, 37], [2, 34], [2, 1]], 9, [30, 29, 28, 18, 23, 26, 32]]) == [1, 1, 1, 1, 1, 1, 1]\nassert my_solution.countServers(*[2, [[1, 19], [1, 6], [2, 17], [1, 10], [2, 37]], 8, [31, 29, 18, 13, 12, 30, 34, 28]]) == [2, 2, 0, 1, 1, 2, 2, 2]\nassert my_solution.countServers(*[4, [[3, 4], [3, 1], [4, 26], [4, 24], [1, 6], [1, 25], [2, 21], [3, 28], [1, 4]], 1, [8, 15, 22, 9, 28, 19, 25]]) == [4, 4, 3, 4, 3, 4, 2]\nassert my_solution.countServers(*[9, [[9, 29], [5, 22], [4, 27], [6, 10], [9, 14]], 2, [5, 22, 12, 26, 25, 19, 16, 29, 7]]) == [9, 8, 8, 9, 9, 9, 8, 7, 9]\nassert my_solution.countServers(*[10, [[3, 3], [1, 2], [7, 1], [2, 24], [6, 23], [8, 28], [1, 28]], 4, [14, 28, 29, 34, 12, 30, 31, 19, 15]]) == [10, 7, 8, 10, 10, 8, 8, 10, 10]\nassert my_solution.countServers(*[2, [[2, 29], [2, 27], [1, 8], [2, 28], [2, 24]], 3, [27, 21, 12, 26, 18, 33, 32, 14, 4, 29]]) == [1, 2, 2, 1, 2, 2, 1, 2, 2, 1]\nassert my_solution.countServers(*[4, [[1, 10], [3, 15], [3, 19], [1, 13], [2, 24], [1, 3], [2, 26]], 2, [22, 16, 18, 5, 27]]) == [4, 3, 4, 3, 3]\nassert my_solution.countServers(*[6, [[3, 5], [1, 2], [6, 23], [2, 23], [1, 18], [3, 7], [4, 2], [4, 9], [6, 20], [1, 17]], 1, [16]]) == [6]\nassert my_solution.countServers(*[6, [[5, 34], [4, 33], [4, 16], [3, 31]], 7, [19, 15, 25, 23, 32, 31, 13]]) == [5, 6, 6, 5, 5, 5, 6]\nassert my_solution.countServers(*[6, [[5, 7], [2, 26], [4, 6]], 1, [4, 26, 15, 7, 22]]) == [6, 5, 6, 4, 6]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2833", "questionFrontendId": "2747", "questionTitle": "Count Zero Request Servers", "stats": { "totalAccepted": "2K", "totalSubmission": "5.3K", "totalAcceptedRaw": 2009, "totalSubmissionRaw": 5259, "acRate": "38.2%" } }
LeetCode/2857
# Total Distance Traveled A truck has two fuel tanks. You are given two integers, `mainTank` representing the fuel present in the main tank in liters and `additionalTank` representing the fuel present in the additional tank in liters. The truck has a mileage of `10` km per liter. Whenever `5` liters of fuel get used up in the main tank, if the additional tank has at least `1` liters of fuel, `1` liters of fuel will be transferred from the additional tank to the main tank. Return *the maximum distance which can be traveled.* **Note:** Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.   **Example 1:** ``` **Input:** mainTank = 5, additionalTank = 10 **Output:** 60 **Explanation:** After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. Total distance traveled is 60km. ``` **Example 2:** ``` **Input:** mainTank = 1, additionalTank = 2 **Output:** 10 **Explanation:** After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km. ```   **Constraints:** * `1 <= mainTank, additionalTank <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def distanceTraveled(self, mainTank: int, additionalTank: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.distanceTraveled(*[5, 10]) == 60\nassert my_solution.distanceTraveled(*[1, 2]) == 10\nassert my_solution.distanceTraveled(*[1, 1]) == 10\nassert my_solution.distanceTraveled(*[1, 3]) == 10\nassert my_solution.distanceTraveled(*[1, 4]) == 10\nassert my_solution.distanceTraveled(*[1, 5]) == 10\nassert my_solution.distanceTraveled(*[1, 6]) == 10\nassert my_solution.distanceTraveled(*[1, 7]) == 10\nassert my_solution.distanceTraveled(*[1, 8]) == 10\nassert my_solution.distanceTraveled(*[1, 9]) == 10\nassert my_solution.distanceTraveled(*[1, 10]) == 10\nassert my_solution.distanceTraveled(*[1, 11]) == 10\nassert my_solution.distanceTraveled(*[1, 12]) == 10\nassert my_solution.distanceTraveled(*[1, 13]) == 10\nassert my_solution.distanceTraveled(*[1, 14]) == 10\nassert my_solution.distanceTraveled(*[1, 15]) == 10\nassert my_solution.distanceTraveled(*[1, 16]) == 10\nassert my_solution.distanceTraveled(*[1, 17]) == 10\nassert my_solution.distanceTraveled(*[1, 18]) == 10\nassert my_solution.distanceTraveled(*[1, 19]) == 10\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2857", "questionFrontendId": "2739", "questionTitle": "Total Distance Traveled", "stats": { "totalAccepted": "7.2K", "totalSubmission": "13.8K", "totalAcceptedRaw": 7238, "totalSubmissionRaw": 13804, "acRate": "52.4%" } }
LeetCode/2845
# Find the Value of the Partition You are given a **positive** integer array `nums`. Partition `nums` into two arrays, `nums1` and `nums2`, such that: * Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`. * Both arrays are **non-empty**. * The value of the partition is **minimized**. The value of the partition is `|max(nums1) - min(nums2)|`. Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`. Return *the integer denoting the value of such partition*.   **Example 1:** ``` **Input:** nums = [1,3,2,4] **Output:** 1 **Explanation:** We can partition the array nums into nums1 = [1,2] and nums2 = [3,4]. - The maximum element of the array nums1 is equal to 2. - The minimum element of the array nums2 is equal to 3. The value of the partition is |2 - 3| = 1. It can be proven that 1 is the minimum value out of all partitions. ``` **Example 2:** ``` **Input:** nums = [100,1,10] **Output:** 9 **Explanation:** We can partition the array nums into nums1 = [10] and nums2 = [100,1]. - The maximum element of the array nums1 is equal to 10. - The minimum element of the array nums2 is equal to 1. The value of the partition is |10 - 1| = 9. It can be proven that 9 is the minimum value out of all partitions. ```   **Constraints:** * `2 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def findValueOfPartition(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findValueOfPartition(*[[1, 3, 2, 4]]) == 1\nassert my_solution.findValueOfPartition(*[[100, 1, 10]]) == 9\nassert my_solution.findValueOfPartition(*[[59, 51, 1, 98, 73]]) == 8\nassert my_solution.findValueOfPartition(*[[84, 11, 100, 100, 75]]) == 0\nassert my_solution.findValueOfPartition(*[[59, 76, 2, 26, 49]]) == 10\nassert my_solution.findValueOfPartition(*[[78, 36, 2, 70, 64, 24, 34, 63, 21, 49]]) == 1\nassert my_solution.findValueOfPartition(*[[43, 35, 19, 1, 21, 11, 59, 38, 47, 1]]) == 0\nassert my_solution.findValueOfPartition(*[[35, 74, 58, 56]]) == 2\nassert my_solution.findValueOfPartition(*[[54, 75, 6, 20, 49, 94, 97, 20, 97]]) == 0\nassert my_solution.findValueOfPartition(*[[92, 13, 30, 32, 89]]) == 2\nassert my_solution.findValueOfPartition(*[[49, 10, 36]]) == 13\nassert my_solution.findValueOfPartition(*[[37, 50, 25, 65, 41, 31]]) == 4\nassert my_solution.findValueOfPartition(*[[14, 20, 59, 42]]) == 6\nassert my_solution.findValueOfPartition(*[[43, 57, 73, 45, 30, 77, 17, 38, 20]]) == 2\nassert my_solution.findValueOfPartition(*[[11, 17, 65, 55, 85, 74, 32]]) == 6\nassert my_solution.findValueOfPartition(*[[84, 93]]) == 9\nassert my_solution.findValueOfPartition(*[[31, 61, 78, 15, 52]]) == 9\nassert my_solution.findValueOfPartition(*[[100, 65, 64, 8, 61, 17]]) == 1\nassert my_solution.findValueOfPartition(*[[3, 72, 8, 90]]) == 5\nassert my_solution.findValueOfPartition(*[[55, 55, 23]]) == 0\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2845", "questionFrontendId": "2740", "questionTitle": "Find the Value of the Partition", "stats": { "totalAccepted": "6.2K", "totalSubmission": "8.4K", "totalAcceptedRaw": 6227, "totalSubmissionRaw": 8377, "acRate": "74.3%" } }
LeetCode/2848
# Special Permutations You are given a **0-indexed** integer array `nums` containing `n` **distinct** positive integers. A permutation of `nums` is called special if: * For all indexes `0 <= i < n - 1`, either `nums[i] % nums[i+1] == 0` or `nums[i+1] % nums[i] == 0`. Return *the total number of special permutations.*As the answer could be large, return it **modulo**`109+ 7`.   **Example 1:** ``` **Input:** nums = [2,3,6] **Output:** 2 **Explanation:** [3,6,2] and [2,6,3] are the two special permutations of nums. ``` **Example 2:** ``` **Input:** nums = [1,4,3] **Output:** 2 **Explanation:** [3,1,4] and [4,1,3] are the two special permutations of nums. ```   **Constraints:** * `2 <= nums.length <= 14` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def specialPerm(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.specialPerm(*[[2, 3, 6]]) == 2\nassert my_solution.specialPerm(*[[1, 4, 3]]) == 2\nassert my_solution.specialPerm(*[[31, 93]]) == 2\nassert my_solution.specialPerm(*[[20, 100, 50, 5, 10, 70, 7]]) == 48\nassert my_solution.specialPerm(*[[24, 3, 27, 54, 9, 90, 30, 60, 20, 100]]) == 4\nassert my_solution.specialPerm(*[[3, 21, 84, 14, 56, 7]]) == 34\nassert my_solution.specialPerm(*[[6, 30, 3, 9, 36, 72, 18, 54]]) == 1440\nassert my_solution.specialPerm(*[[14, 28, 84, 21, 63, 9, 27, 81]]) == 8\nassert my_solution.specialPerm(*[[64, 8, 1, 3, 30]]) == 8\nassert my_solution.specialPerm(*[[83, 105]]) == 0\nassert my_solution.specialPerm(*[[99, 11, 44, 88, 22, 66, 33]]) == 72\nassert my_solution.specialPerm(*[[87, 29]]) == 2\nassert my_solution.specialPerm(*[[29, 87]]) == 2\nassert my_solution.specialPerm(*[[30, 6, 54, 27, 81, 9, 90, 15]]) == 56\nassert my_solution.specialPerm(*[[2, 14, 70]]) == 6\nassert my_solution.specialPerm(*[[74, 37]]) == 2\nassert my_solution.specialPerm(*[[68, 17, 51]]) == 2\nassert my_solution.specialPerm(*[[66, 11, 99, 33]]) == 12\nassert my_solution.specialPerm(*[[24, 72, 12]]) == 6\nassert my_solution.specialPerm(*[[94, 47]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2848", "questionFrontendId": "2741", "questionTitle": "Special Permutations", "stats": { "totalAccepted": "4.6K", "totalSubmission": "12.5K", "totalAcceptedRaw": 4591, "totalSubmissionRaw": 12533, "acRate": "36.6%" } }
LeetCode/2808
# Painting the Walls You are given two **0-indexed** integer arrays, `cost` and `time`, of size `n` representing the costs and the time taken to paint `n` different walls respectively. There are two painters available: * A**paid painter** that paints the `ith` wall in `time[i]` units of time and takes `cost[i]` units of money. * A**free painter** that paints **any** wall in `1` unit of time at a cost of `0`. But the free painter can only be used if the paid painter is already **occupied**. Return *the minimum amount of money required to paint the* `n`*walls.*   **Example 1:** ``` **Input:** cost = [1,2,3,2], time = [1,2,3,2] **Output:** 3 **Explanation:** The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3. ``` **Example 2:** ``` **Input:** cost = [2,3,4,2], time = [1,1,1,1] **Output:** 4 **Explanation:** The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4. ```   **Constraints:** * `1 <= cost.length <= 500` * `cost.length == time.length` * `1 <= cost[i] <= 106` * `1 <= time[i] <= 500` Please make sure your answer follows the type signature below: ```python3 class Solution: def paintWalls(self, cost: List[int], time: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.paintWalls(*[[1, 2, 3, 2], [1, 2, 3, 2]]) == 3\nassert my_solution.paintWalls(*[[2, 3, 4, 2], [1, 1, 1, 1]]) == 4\nassert my_solution.paintWalls(*[[8, 7, 5, 15], [1, 1, 2, 1]]) == 12\nassert my_solution.paintWalls(*[[42, 8, 28, 35, 21, 13, 21, 35], [2, 1, 1, 1, 2, 1, 1, 2]]) == 63\nassert my_solution.paintWalls(*[[49, 35, 32, 20, 30, 12, 42], [1, 1, 2, 2, 1, 1, 2]]) == 62\nassert my_solution.paintWalls(*[[2, 2], [1, 1]]) == 2\nassert my_solution.paintWalls(*[[26, 53, 10, 24, 25, 20, 63, 51], [1, 1, 1, 1, 2, 2, 2, 1]]) == 55\nassert my_solution.paintWalls(*[[76, 25, 96, 46, 85, 19, 29, 88, 2, 5], [1, 2, 1, 3, 1, 3, 3, 3, 2, 1]]) == 46\nassert my_solution.paintWalls(*[[82, 30, 94, 55, 76, 94, 51, 82, 3, 89], [2, 3, 3, 1, 2, 2, 1, 2, 3, 2]]) == 84\nassert my_solution.paintWalls(*[[23, 2, 9, 1, 48, 3, 31], [1, 2, 1, 1, 2, 2, 1]]) == 6\nassert my_solution.paintWalls(*[[21, 52, 42, 21, 2, 55, 63, 50], [2, 2, 1, 2, 1, 1, 2, 2]]) == 44\nassert my_solution.paintWalls(*[[7, 15, 38, 35, 61, 90, 34, 29, 68, 35], [1, 1, 3, 3, 2, 1, 3, 1, 2, 3]]) == 76\nassert my_solution.paintWalls(*[[1, 4], [1, 1]]) == 1\nassert my_solution.paintWalls(*[[23, 33, 19, 12, 27, 20], [2, 2, 2, 2, 2, 1]]) == 31\nassert my_solution.paintWalls(*[[74, 34, 54, 65, 65, 9, 62, 85, 95, 36], [2, 1, 2, 1, 1, 3, 2, 3, 2, 1]]) == 125\nassert my_solution.paintWalls(*[[9, 9, 5], [1, 1, 1]]) == 14\nassert my_solution.paintWalls(*[[4, 7, 9], [1, 1, 1]]) == 11\nassert my_solution.paintWalls(*[[1, 27, 29, 35, 36, 7], [1, 1, 1, 1, 1, 2]]) == 35\nassert my_solution.paintWalls(*[[1], [1]]) == 1\nassert my_solution.paintWalls(*[[41, 15, 27, 36, 28, 47, 36], [2, 1, 2, 2, 2, 1, 2]]) == 70\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2808", "questionFrontendId": "2742", "questionTitle": "Painting the Walls", "stats": { "totalAccepted": "3.3K", "totalSubmission": "9K", "totalAcceptedRaw": 3337, "totalSubmissionRaw": 9014, "acRate": "37.0%" } }
LeetCode/2836
# Neither Minimum nor Maximum Given an integer array `nums` containing **distinct** **positive** integers, find and return **any** number from the array that is neither the **minimum** nor the **maximum** value in the array, or **`-1`** if there is no such number. Return *the selected integer.*   **Example 1:** ``` **Input:** nums = [3,2,1,4] **Output:** 2 **Explanation:** In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers. ``` **Example 2:** ``` **Input:** nums = [1,2] **Output:** -1 **Explanation:** Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer. ``` **Example 3:** ``` **Input:** nums = [2,1,3] **Output:** 2 **Explanation:** Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 100` * All values in `nums` are distinct Please make sure your answer follows the type signature below: ```python3 class Solution: def findNonMinOrMax(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findNonMinOrMax(*[[3, 2, 1, 4]]) == 2\nassert my_solution.findNonMinOrMax(*[[1, 2]]) == -1\nassert my_solution.findNonMinOrMax(*[[2, 1, 3]]) == 2\nassert my_solution.findNonMinOrMax(*[[1]]) == -1\nassert my_solution.findNonMinOrMax(*[[2]]) == -1\nassert my_solution.findNonMinOrMax(*[[5]]) == -1\nassert my_solution.findNonMinOrMax(*[[6]]) == -1\nassert my_solution.findNonMinOrMax(*[[7]]) == -1\nassert my_solution.findNonMinOrMax(*[[8]]) == -1\nassert my_solution.findNonMinOrMax(*[[9]]) == -1\nassert my_solution.findNonMinOrMax(*[[10]]) == -1\nassert my_solution.findNonMinOrMax(*[[11]]) == -1\nassert my_solution.findNonMinOrMax(*[[12]]) == -1\nassert my_solution.findNonMinOrMax(*[[13]]) == -1\nassert my_solution.findNonMinOrMax(*[[14]]) == -1\nassert my_solution.findNonMinOrMax(*[[15]]) == -1\nassert my_solution.findNonMinOrMax(*[[17]]) == -1\nassert my_solution.findNonMinOrMax(*[[18]]) == -1\nassert my_solution.findNonMinOrMax(*[[20]]) == -1\nassert my_solution.findNonMinOrMax(*[[22]]) == -1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2836", "questionFrontendId": "2733", "questionTitle": "Neither Minimum nor Maximum", "stats": { "totalAccepted": "9.6K", "totalSubmission": "12.3K", "totalAcceptedRaw": 9556, "totalSubmissionRaw": 12269, "acRate": "77.9%" } }
LeetCode/2828
# Lexicographically Smallest String After Substring Operation You are given a string `s` consisting of only lowercase English letters. In one operation, you can do the following: * Select any non-empty substring of `s`, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'. Return *the **lexicographically smallest** string you can obtain after performing the above operation **exactly once**.* A **substring** is a contiguous sequence of characters in a string. A string `x` is **lexicographically smaller** than a string `y` of the same length if `x[i]` comes before `y[i]` in alphabetic order for the first position `i` such that `x[i] != y[i]`.   **Example 1:** ``` **Input:** s = "cbabc" **Output:** "baabc" **Explanation:** We apply the operation on the substring starting at index 0, and ending at index 1 inclusive. It can be proven that the resulting string is the lexicographically smallest. ``` **Example 2:** ``` **Input:** s = "acbbc" **Output:** "abaab" **Explanation:** We apply the operation on the substring starting at index 1, and ending at index 4 inclusive. It can be proven that the resulting string is the lexicographically smallest. ``` **Example 3:** ``` **Input:** s = "leetcode" **Output:** "kddsbncd" **Explanation:** We apply the operation on the entire string. It can be proven that the resulting string is the lexicographically smallest. ```   **Constraints:** * `1 <= s.length <= 3 * 105` * `s` consists of lowercase English letters Please make sure your answer follows the type signature below: ```python3 class Solution: def smallestString(self, s: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.smallestString(*['cbabc']) == baabc\nassert my_solution.smallestString(*['acbbc']) == abaab\nassert my_solution.smallestString(*['leetcode']) == kddsbncd\nassert my_solution.smallestString(*['a']) == z\nassert my_solution.smallestString(*['b']) == a\nassert my_solution.smallestString(*['c']) == b\nassert my_solution.smallestString(*['d']) == c\nassert my_solution.smallestString(*['e']) == d\nassert my_solution.smallestString(*['f']) == e\nassert my_solution.smallestString(*['g']) == f\nassert my_solution.smallestString(*['h']) == g\nassert my_solution.smallestString(*['i']) == h\nassert my_solution.smallestString(*['j']) == i\nassert my_solution.smallestString(*['k']) == j\nassert my_solution.smallestString(*['l']) == k\nassert my_solution.smallestString(*['m']) == l\nassert my_solution.smallestString(*['n']) == m\nassert my_solution.smallestString(*['o']) == n\nassert my_solution.smallestString(*['p']) == o\nassert my_solution.smallestString(*['q']) == p\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2828", "questionFrontendId": "2734", "questionTitle": "Lexicographically Smallest String After Substring Operation", "stats": { "totalAccepted": "6K", "totalSubmission": "17.5K", "totalAcceptedRaw": 5992, "totalSubmissionRaw": 17481, "acRate": "34.3%" } }
LeetCode/2810
# Collecting Chocolates You are given a **0-indexed** integer array `nums` of size `n` representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index `i` is `nums[i]`. Each chocolate is of a different type, and initially, the chocolate at the index `i` is of `ith` type. In one operation, you can do the following with an incurred **cost** of `x`: * Simultaneously change the chocolate of `ith` type to `((i + 1) mod n)th` type for all chocolates. Return *the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.*   **Example 1:** ``` **Input:** nums = [20,1,15], x = 5 **Output:** 13 **Explanation:** Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1. Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2ndtype of chocolate at a cost of 1. Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1. Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal. ``` **Example 2:** ``` **Input:** nums = [1,2,3], x = 4 **Output:** 6 **Explanation:** We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6. ```   **Constraints:** * `1 <= nums.length <= 1000` * `1 <= nums[i] <= 109` * `1 <= x <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def minCost(self, nums: List[int], x: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minCost(*[[20, 1, 15], 5]) == 13\nassert my_solution.minCost(*[[1, 2, 3], 4]) == 6\nassert my_solution.minCost(*[[31, 25, 18, 59], 27]) == 119\nassert my_solution.minCost(*[[5, 3], 8]) == 8\nassert my_solution.minCost(*[[15, 150, 56, 69, 214, 203], 42]) == 298\nassert my_solution.minCost(*[[3, 5], 7]) == 8\nassert my_solution.minCost(*[[733, 200, 839, 515, 852, 615, 8, 584, 250, 337], 537]) == 3188\nassert my_solution.minCost(*[[1], 1]) == 1\nassert my_solution.minCost(*[[276, 253, 157, 237, 92, 331, 19], 82]) == 607\nassert my_solution.minCost(*[[271, 902, 792, 501, 184, 559, 140, 506, 94, 161], 167]) == 1947\nassert my_solution.minCost(*[[288, 457, 953, 700, 464, 785, 203, 729, 725, 422], 76]) == 2714\nassert my_solution.minCost(*[[7, 5, 27], 23]) == 39\nassert my_solution.minCost(*[[503, 401, 517, 692, 42, 135, 823, 883, 255, 111], 334]) == 2101\nassert my_solution.minCost(*[[129, 85, 17, 150, 152, 49], 191]) == 558\nassert my_solution.minCost(*[[169, 37, 58, 175, 3, 9], 47]) == 186\nassert my_solution.minCost(*[[204, 191, 276, 165, 235, 440, 403, 22], 416]) == 1810\nassert my_solution.minCost(*[[214, 471, 451, 41, 365, 703, 327, 414, 363], 30]) == 609\nassert my_solution.minCost(*[[1, 24, 2], 16]) == 20\nassert my_solution.minCost(*[[76, 499, 188, 8, 563, 438, 363, 32, 482], 623]) == 1844\nassert my_solution.minCost(*[[22, 13, 21], 20]) == 56\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2810", "questionFrontendId": "2735", "questionTitle": "Collecting Chocolates", "stats": { "totalAccepted": "16.9K", "totalSubmission": "30.7K", "totalAcceptedRaw": 16860, "totalSubmissionRaw": 30748, "acRate": "54.8%" } }
LeetCode/2839
# Maximum Sum Queries You are given two **0-indexed** integer arrays `nums1` and `nums2`, each of length `n`, and a **1-indexed 2D array** `queries` where `queries[i] = [xi, yi]`. For the `ith` query, find the **maximum value** of `nums1[j] + nums2[j]` among all indices `j` `(0 <= j < n)`, where `nums1[j] >= xi` and `nums2[j] >= yi`, or **-1** if there is no `j` satisfying the constraints. Return *an array* `answer` *where* `answer[i]` *is the answer to the* `ith` *query.*   **Example 1:** ``` **Input:** nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]] **Output:** [6,10,7] **Explanation:** For the 1st query xi = 4 and yi = 1, we can select index j = 0 since nums1[j] >= 4 and nums2[j] >= 1. The sum nums1[j] + nums2[j] is 6, and we can show that 6 is the maximum we can obtain. For the 2nd query xi = 1 and yi = 3, we can select index j = 2 since nums1[j] >= 1 and nums2[j] >= 3. The sum nums1[j] + nums2[j] is 10, and we can show that 10 is the maximum we can obtain. For the 3rd query xi = 2 and yi = 5, we can select index j = 3 since nums1[j] >= 2 and nums2[j] >= 5. The sum nums1[j] + nums2[j] is 7, and we can show that 7 is the maximum we can obtain. Therefore, we return [6,10,7]. ``` **Example 2:** ``` **Input:** nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]] **Output:** [9,9,9] **Explanation:** For this example, we can use index j = 2 for all the queries since it satisfies the constraints for each query. ``` **Example 3:** ``` **Input:** nums1 = [2,1], nums2 = [2,3], queries = [[3,3]] **Output:** [-1] **Explanation:** There is one query in this example with xi = 3 and yi = 3. For every index, j, either nums1[j] < xi or nums2[j] < yi. Hence, there is no solution. ```   **Constraints:** * `nums1.length == nums2.length` * `n == nums1.length` * `1 <= n <= 105` * `1 <= nums1[i], nums2[i] <= 109` * `1 <= queries.length <= 105` * `queries[i].length == 2` * `xi == queries[i][1]` * `yi == queries[i][2]` * `1 <= xi, yi <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumSumQueries(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSumQueries(*[[4, 3, 1, 2], [2, 4, 9, 5], [[4, 1], [1, 3], [2, 5]]]) == [6, 10, 7]\nassert my_solution.maximumSumQueries(*[[3, 2, 5], [2, 3, 4], [[4, 4], [3, 2], [1, 1]]]) == [9, 9, 9]\nassert my_solution.maximumSumQueries(*[[2, 1], [2, 3], [[3, 3]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[6], [50], [[79, 91]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[23], [38], [[68, 22]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[25], [29], [[66, 65]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[31], [17], [[1, 79]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[39], [30], [[94, 84]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[40], [81], [[71, 45]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[76], [14], [[54, 41]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[95], [75], [[17, 57]]]) == [170]\nassert my_solution.maximumSumQueries(*[[6, 36], [5, 4], [[39, 5]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[9, 17], [37, 10], [[68, 97]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[17], [42], [[9, 60], [94, 22]]]) == [-1, -1]\nassert my_solution.maximumSumQueries(*[[18], [40], [[40, 26], [89, 31]]]) == [-1, -1]\nassert my_solution.maximumSumQueries(*[[24, 50], [62, 62], [[39, 98]]]) == [-1]\nassert my_solution.maximumSumQueries(*[[37], [5], [[23, 63], [12, 89]]]) == [-1, -1]\nassert my_solution.maximumSumQueries(*[[39], [75], [[30, 57], [46, 33]]]) == [114, -1]\nassert my_solution.maximumSumQueries(*[[41], [2], [[15, 60], [30, 17]]]) == [-1, -1]\nassert my_solution.maximumSumQueries(*[[44, 55], [77, 95], [[41, 60]]]) == [150]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2839", "questionFrontendId": "2736", "questionTitle": "Maximum Sum Queries", "stats": { "totalAccepted": "12.5K", "totalSubmission": "23.8K", "totalAcceptedRaw": 12499, "totalSubmissionRaw": 23813, "acRate": "52.5%" } }
LeetCode/2786
# Find the Longest Semi-Repetitive Substring You are given a **0-indexed** string `s` that consists of digits from `0` to `9`. A string `t` is called a **semi-repetitive** if there is at most one consecutive pair of the same digits inside `t`. For example, `0010`, `002020`, `0123`, `2002`, and `54944` are semi-repetitive while `00101022`, and `1101234883` are not. Return *the length of the longest semi-repetitive substring inside* `s`. A **substring** is a contiguous **non-empty** sequence of characters within a string.   **Example 1:** ``` **Input:** s = "52233" **Output:** 4 **Explanation:** The longest semi-repetitive substring is "5223", which starts at i = 0 and ends at j = 3. ``` **Example 2:** ``` **Input:** s = "5494" **Output:** 4 **Explanation:** s is a semi-reptitive string, so the answer is 4. ``` **Example 3:** ``` **Input:** s = "1111111" **Output:** 2 **Explanation:** The longest semi-repetitive substring is "11", which starts at i = 0 and ends at j = 1. ```   **Constraints:** * `1 <= s.length <= 50` * `'0' <= s[i] <= '9'` Please make sure your answer follows the type signature below: ```python3 class Solution: def longestSemiRepetitiveSubstring(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.longestSemiRepetitiveSubstring(*['52233']) == 4\nassert my_solution.longestSemiRepetitiveSubstring(*['5494']) == 4\nassert my_solution.longestSemiRepetitiveSubstring(*['1111111']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['0']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['1']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['2']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['3']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['4']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['5']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['6']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['7']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['8']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['9']) == 1\nassert my_solution.longestSemiRepetitiveSubstring(*['00']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['01']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['02']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['03']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['04']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['05']) == 2\nassert my_solution.longestSemiRepetitiveSubstring(*['06']) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2786", "questionFrontendId": "2730", "questionTitle": "Find the Longest Semi-Repetitive Substring", "stats": { "totalAccepted": "4.9K", "totalSubmission": "10.4K", "totalAcceptedRaw": 4896, "totalSubmissionRaw": 10440, "acRate": "46.9%" } }
LeetCode/2787
# Movement of Robots Some robots are standing on an infinite number line with their initial coordinates given by a **0-indexed** integer array `nums` and will start moving once given the command to move. The robots will move a unit distance each second. You are given a string `s` denoting the direction in which robots will move on command. `'L'` means the robot will move towards the left side or negative side of the number line, whereas `'R'` means the robot will move towards the right side or positive side of the number line. If two robots collide, they will start moving in opposite directions. Return *the sum of distances between all the pairs of robots* `d` *seconds after the command.* Since the sum can be very large, return it modulo `109 + 7`. **Note:** * For two robots at the index `i` and `j`, pair `(i,j)` and pair `(j,i)` are considered the same pair. * When robots collide, they **instantly change** their directions without wasting any time. * Collision happens when two robots share the same place in a moment. + For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right. + For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.   **Example 1:** ``` **Input:** nums = [-2,0,2], s = "RLL", d = 3 **Output:** 8 **Explanation:** After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right. After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right. After 3 seconds, the positions are [-3,-1,1]. The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2. The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4. The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2. The sum of the pairs of all distances = 2 + 4 + 2 = 8. ``` **Example 2:** ``` **Input:** nums = [1,0], s = "RL", d = 2 **Output:** 5 **Explanation:** After 1 second, the positions are [2,-1]. After 2 seconds, the positions are [3,-2]. The distance between the two robots is abs(-2 - 3) = 5. ```   **Constraints:** * `2 <= nums.length <= 105` * `-2 * 109 <= nums[i] <= 2 * 109` * `0 <= d <= 109` * `nums.length == s.length` * `s` consists of 'L' and 'R' only * `nums[i]` will be unique. Please make sure your answer follows the type signature below: ```python3 class Solution: def sumDistance(self, nums: List[int], s: str, d: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumDistance(*[[-2, 0, 2], 'RLL', 3]) == 8\nassert my_solution.sumDistance(*[[1, 0], 'RL', 2]) == 5\nassert my_solution.sumDistance(*[[-10, -13, 10, 14, 11], 'LRLLR', 2]) == 146\nassert my_solution.sumDistance(*[[1, -67, 68, -26, -13, -40, -56, 62, 21], 'LLLRLLRRR', 4]) == 2106\nassert my_solution.sumDistance(*[[-36, -72, -41, 69, -14, 44, 58, -47, 45], 'LLLRRRLRL', 2]) == 2284\nassert my_solution.sumDistance(*[[-16, 20, 11, 6, 0], 'LLLLR', 5]) == 154\nassert my_solution.sumDistance(*[[-16, -84, 49, 51, -52, 90, -9, 68, -64, -43], 'LLLRLLRLLR', 9]) == 3344\nassert my_solution.sumDistance(*[[-59, 39, -11, 53, 48, -54, 27, 17], 'RRLLLLRL', 7]) == 1440\nassert my_solution.sumDistance(*[[-16, 11, 6, -15], 'RLRR', 2]) == 90\nassert my_solution.sumDistance(*[[2, 3], 'RR', 7]) == 1\nassert my_solution.sumDistance(*[[5, 7, -5], 'LRR', 9]) == 40\nassert my_solution.sumDistance(*[[-3, 0], 'RR', 4]) == 3\nassert my_solution.sumDistance(*[[8, -8, -21, -17, 15], 'RLLRL', 10]) == 242\nassert my_solution.sumDistance(*[[4, 5, 3], 'RRR', 2]) == 4\nassert my_solution.sumDistance(*[[12, 31, 24, 49, 37, -61, 3, 43], 'LRRRLRLL', 8]) == 1086\nassert my_solution.sumDistance(*[[3, -47, -25, 15, 8, -27, -33, -16], 'RLRLRLRR', 8]) == 832\nassert my_solution.sumDistance(*[[-73, -53, -75, -59, 99, -66, 66, -28, -98, -76], 'LRLRRRLRRR', 3]) == 3091\nassert my_solution.sumDistance(*[[21, -27, -11, -42, 11, 42, 46, -26], 'RLLLLLLL', 6]) == 1160\nassert my_solution.sumDistance(*[[-3, 1], 'RR', 7]) == 4\nassert my_solution.sumDistance(*[[-59, 45, 15, 8, -4, -3, 9, 51], 'RLRLRRRR', 10]) == 1024\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2787", "questionFrontendId": "2731", "questionTitle": "Movement of Robots", "stats": { "totalAccepted": "20.3K", "totalSubmission": "53.3K", "totalAcceptedRaw": 20268, "totalSubmissionRaw": 53335, "acRate": "38.0%" } }
LeetCode/2826
# Find a Good Subset of the Matrix You are given a **0-indexed** `m x n` binary matrix `grid`. Let us call a **non-empty** subset of rows **good** if the sum of each column of the subset is at most half of the length of the subset. More formally, if the length of the chosen subset of rows is `k`, then the sum of each column should be at most `floor(k / 2)`. Return *an integer array that contains row indices of a good subset sorted in **ascending** order.* If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array. A **subset** of rows of the matrix `grid` is any matrix that can be obtained by deleting some (possibly none or all) rows from `grid`.   **Example 1:** ``` **Input:** grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]] **Output:** [0,1] **Explanation:** We can choose the 0th and 1st rows to create a good subset of rows. The length of the chosen subset is 2. - The sum of the 0th column is 0 + 0 = 0, which is at most half of the length of the subset. - The sum of the 1st column is 1 + 0 = 1, which is at most half of the length of the subset. - The sum of the 2nd column is 1 + 0 = 1, which is at most half of the length of the subset. - The sum of the 3rd column is 0 + 1 = 1, which is at most half of the length of the subset. ``` **Example 2:** ``` **Input:** grid = [[0]] **Output:** [0] **Explanation:** We can choose the 0th row to create a good subset of rows. The length of the chosen subset is 1. - The sum of the 0th column is 0, which is at most half of the length of the subset. ``` **Example 3:** ``` **Input:** grid = [[1,1,1],[1,1,1]] **Output:** [] **Explanation:** It is impossible to choose any subset of rows to create a good subset. ```   **Constraints:** * `m == grid.length` * `n == grid[i].length` * `1 <= m <= 104` * `1 <= n <= 5` * `grid[i][j]` is either `0` or `1`. Please make sure your answer follows the type signature below: ```python3 class Solution: def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 1, 1, 0], [0, 0, 0, 1], [1, 1, 1, 1]]]) == [0, 1]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1, 1], [1, 1, 1]]]) == []\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 0], [1, 1], [1, 0], [1, 0]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 0, 0, 1, 0], [1, 0, 1, 0, 1], [0, 0, 0, 0, 1], [1, 0, 1, 1, 1]]]) == [0, 2]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1, 0, 0, 1], [0, 1, 1, 1, 0], [1, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [1, 0, 1, 1, 0], [1, 0, 0, 0, 1], [1, 1, 0, 1, 0], [1, 0, 1, 0, 1], [1, 1, 1, 1, 1]]]) == [0, 3]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1], [0, 1], [1, 0], [0, 0], [1, 0], [0, 1], [1, 1], [0, 1], [1, 1]]]) == [3]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 1], [0, 1], [0, 1], [1, 1], [1, 1], [0, 0], [1, 1], [1, 1]]]) == [5]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 0, 0], [0, 0, 1]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0]]]) == []\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1], [0], [1], [0], [0], [0]]]) == [1]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 0], [1, 0], [0, 0], [1, 0], [0, 1], [1, 1], [0, 0], [0, 1], [1, 1], [0, 1]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 0], [1, 1], [0, 0], [1, 1], [1, 1]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1], [0, 1]]]) == []\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 0, 0], [1, 0, 1], [1, 1, 0], [0, 0, 1], [0, 0, 0], [0, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 1]]]) == [4]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 1, 0], [0, 1, 1, 1, 0], [1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [0, 1, 1, 1, 1]]]) == [1, 4]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 0, 0, 1, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 1, 0, 1]]]) == [0, 1]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0], [1], [1], [1], [1], [1], [0], [1], [1], [0]]]) == [0]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[1, 1], [1, 0], [1, 1], [0, 1], [1, 1], [1, 0], [1, 0], [1, 0], [0, 1], [1, 1]]]) == [1, 3]\nassert my_solution.goodSubsetofBinaryMatrix(*[[[0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [1, 0, 1, 1, 1], [0, 0, 1, 1, 1], [0, 0, 0, 1, 1], [1, 1, 1, 0, 0]]]) == [4, 5]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2826", "questionFrontendId": "2732", "questionTitle": "Find a Good Subset of the Matrix", "stats": { "totalAccepted": "1.7K", "totalSubmission": "3.1K", "totalAcceptedRaw": 1740, "totalSubmissionRaw": 3051, "acRate": "57.0%" } }
LeetCode/2825
# Minimize String Length Given a **0-indexed** string `s`, repeatedly perform the following operation **any** number of times: * Choose an index `i` in the string, and let `c` be the character in position `i`. **Delete** the **closest occurrence** of `c` to the **left** of `i` (if any) and the **closest occurrence** of `c` to the **right** of `i` (if any). Your task is to **minimize** the length of `s` by performing the above operation any number of times. Return *an integer denoting the length of the **minimized** string.*   **Example 1:** ``` **Input:** s = "aaabc" **Output:** 3 **Explanation:** In this example, s is "aaabc". We can start by selecting the character 'a' at index 1. We then remove the closest 'a' to the left of index 1, which is at index 0, and the closest 'a' to the right of index 1, which is at index 2. After this operation, the string becomes "abc". Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3. ``` **Example 2:** ``` **Input:** s = "cbbd" **Output:** 3 **Explanation:** For this we can start with character 'b' at index 1. There is no occurrence of 'b' to the left of index 1, but there is one to the right at index 2, so we delete the 'b' at index 2. The string becomes "cbd" and further operations will leave it unchanged. Hence, the minimized length is 3.  ``` **Example 3:** ``` **Input:** s = "dddaaa" **Output:** 2 **Explanation:** For this, we can start with the character 'd' at index 1. The closest occurrence of a 'd' to its left is at index 0, and the closest occurrence of a 'd' to its right is at index 2. We delete both index 0 and 2, so the string becomes "daaa". In the new string, we can select the character 'a' at index 2. The closest occurrence of an 'a' to its left is at index 1, and the closest occurrence of an 'a' to its right is at index 3. We delete both of them, and the string becomes "da". We cannot minimize this further, so the minimized length is 2. ```     **Constraints:** * `1 <= s.length <= 100` * `s` contains only lowercase English letters Please make sure your answer follows the type signature below: ```python3 class Solution: def minimizedStringLength(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimizedStringLength(*['aaabc']) == 3\nassert my_solution.minimizedStringLength(*['cbbd']) == 3\nassert my_solution.minimizedStringLength(*['dddaaa']) == 2\nassert my_solution.minimizedStringLength(*['a']) == 1\nassert my_solution.minimizedStringLength(*['b']) == 1\nassert my_solution.minimizedStringLength(*['c']) == 1\nassert my_solution.minimizedStringLength(*['d']) == 1\nassert my_solution.minimizedStringLength(*['e']) == 1\nassert my_solution.minimizedStringLength(*['f']) == 1\nassert my_solution.minimizedStringLength(*['g']) == 1\nassert my_solution.minimizedStringLength(*['h']) == 1\nassert my_solution.minimizedStringLength(*['i']) == 1\nassert my_solution.minimizedStringLength(*['j']) == 1\nassert my_solution.minimizedStringLength(*['k']) == 1\nassert my_solution.minimizedStringLength(*['l']) == 1\nassert my_solution.minimizedStringLength(*['m']) == 1\nassert my_solution.minimizedStringLength(*['n']) == 1\nassert my_solution.minimizedStringLength(*['o']) == 1\nassert my_solution.minimizedStringLength(*['p']) == 1\nassert my_solution.minimizedStringLength(*['q']) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2825", "questionFrontendId": "2716", "questionTitle": "Minimize String Length", "stats": { "totalAccepted": "7.3K", "totalSubmission": "10K", "totalAcceptedRaw": 7280, "totalSubmissionRaw": 9995, "acRate": "72.8%" } }
LeetCode/2785
# Semi-Ordered Permutation You are given a **0-indexed** permutation of `n` integers `nums`. A permutation is called **semi-ordered** if the first number equals `1` and the last number equals `n`. You can perform the below operation as many times as you want until you make `nums` a **semi-ordered** permutation: * Pick two adjacent elements in `nums`, then swap them. Return *the minimum number of operations to make* `nums` *a **semi-ordered permutation***. A **permutation** is a sequence of integers from `1` to `n` of length `n` containing each number exactly once.   **Example 1:** ``` **Input:** nums = [2,1,4,3] **Output:** 2 **Explanation:** We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation. ``` **Example 2:** ``` **Input:** nums = [2,4,1,3] **Output:** 3 **Explanation:** We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3]. 2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation. ``` **Example 3:** ``` **Input:** nums = [1,3,4,2,5] **Output:** 0 **Explanation:** The permutation is already a semi-ordered permutation. ```   **Constraints:** * `2 <= nums.length == n <= 50` * `1 <= nums[i] <= 50` * `nums is a permutation.` Please make sure your answer follows the type signature below: ```python3 class Solution: def semiOrderedPermutation(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.semiOrderedPermutation(*[[2, 1, 4, 3]]) == 2\nassert my_solution.semiOrderedPermutation(*[[2, 4, 1, 3]]) == 3\nassert my_solution.semiOrderedPermutation(*[[1, 3, 4, 2, 5]]) == 0\nassert my_solution.semiOrderedPermutation(*[[1, 2]]) == 0\nassert my_solution.semiOrderedPermutation(*[[2, 1]]) == 1\nassert my_solution.semiOrderedPermutation(*[[1, 2, 3]]) == 0\nassert my_solution.semiOrderedPermutation(*[[2, 1, 3]]) == 1\nassert my_solution.semiOrderedPermutation(*[[2, 3, 1]]) == 2\nassert my_solution.semiOrderedPermutation(*[[1, 3, 2]]) == 1\nassert my_solution.semiOrderedPermutation(*[[3, 1, 2]]) == 2\nassert my_solution.semiOrderedPermutation(*[[3, 2, 1]]) == 3\nassert my_solution.semiOrderedPermutation(*[[1, 2, 3, 4]]) == 0\nassert my_solution.semiOrderedPermutation(*[[2, 1, 3, 4]]) == 1\nassert my_solution.semiOrderedPermutation(*[[2, 3, 1, 4]]) == 2\nassert my_solution.semiOrderedPermutation(*[[2, 3, 4, 1]]) == 3\nassert my_solution.semiOrderedPermutation(*[[1, 3, 2, 4]]) == 0\nassert my_solution.semiOrderedPermutation(*[[3, 1, 2, 4]]) == 1\nassert my_solution.semiOrderedPermutation(*[[3, 2, 1, 4]]) == 2\nassert my_solution.semiOrderedPermutation(*[[3, 2, 4, 1]]) == 3\nassert my_solution.semiOrderedPermutation(*[[1, 3, 4, 2]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2785", "questionFrontendId": "2717", "questionTitle": "Semi-Ordered Permutation", "stats": { "totalAccepted": "7K", "totalSubmission": "9.5K", "totalAcceptedRaw": 6957, "totalSubmissionRaw": 9475, "acRate": "73.4%" } }
LeetCode/2757
# Count of Integers You are given two numeric strings `num1` and `num2` and two integers `max_sum` and `min_sum`. We denote an integer `x` to be *good* if: * `num1 <= x <= num2` * `min_sum <= digit_sum(x) <= max_sum`. Return *the number of good integers*. Since the answer may be large, return it modulo `109 + 7`. Note that `digit_sum(x)` denotes the sum of the digits of `x`.   **Example 1:** ``` **Input:** num1 = "1", num2 = "12", min_sum = 1, max_sum = 8 **Output:** 11 **Explanation:** There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11. ``` **Example 2:** ``` **Input:** num1 = "1", num2 = "5", min_sum = 1, max_sum = 5 **Output:** 5 **Explanation:** The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5. ```   **Constraints:** * `1 <= num1 <= num2 <= 1022` * `1 <= min_sum <= max_sum <= 400` Please make sure your answer follows the type signature below: ```python3 class Solution: def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.count(*['1', '12', 1, 8]) == 11\nassert my_solution.count(*['1', '5', 1, 5]) == 5\nassert my_solution.count(*['4179205230', '7748704426', 8, 46]) == 883045899\nassert my_solution.count(*['1479192516', '5733987233', 36, 108]) == 519488312\nassert my_solution.count(*['4859473643', '30159981595', 58, 59]) == 972251498\nassert my_solution.count(*['1012640017461217236611', '9234552128261772272769', 67, 148]) == 683479047\nassert my_solution.count(*['2991881296', '8678809677', 46, 326]) == 887229207\nassert my_solution.count(*['6312', '9416', 29, 30]) == 114\nassert my_solution.count(*['1000000007', '2000000014', 1, 400]) == 1\nassert my_solution.count(*['1', '12', 5, 8]) == 4\nassert my_solution.count(*['664', '4906', 17, 24]) == 1778\nassert my_solution.count(*['6635', '9845', 27, 29]) == 339\nassert my_solution.count(*['7809', '9275', 19, 22]) == 429\nassert my_solution.count(*['8269', '8554', 10, 14]) == 19\nassert my_solution.count(*['1554', '5658', 30, 30]) == 4\nassert my_solution.count(*['5082', '5891', 24, 24]) == 35\nassert my_solution.count(*['5410', '9277', 18, 19]) == 569\nassert my_solution.count(*['5797', '9353', 11, 30]) == 3419\nassert my_solution.count(*['3351', '7877', 16, 23]) == 2551\nassert my_solution.count(*['8542', '9075', 2, 27]) == 424\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2757", "questionFrontendId": "2719", "questionTitle": "Count of Integers", "stats": { "totalAccepted": "14.4K", "totalSubmission": "25.6K", "totalAcceptedRaw": 14375, "totalSubmissionRaw": 25620, "acRate": "56.1%" } }
LeetCode/2819
# Remove Trailing Zeros From a String Given a **positive** integer `num` represented as a string, return *the integer* `num` *without trailing zeros as a string*.   **Example 1:** ``` **Input:** num = "51230100" **Output:** "512301" **Explanation:** Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301". ``` **Example 2:** ``` **Input:** num = "123" **Output:** "123" **Explanation:** Integer "123" has no trailing zeros, we return integer "123". ```   **Constraints:** * `1 <= num.length <= 1000` * `num` consists of only digits. * `num` doesn't have any leading zeros. Please make sure your answer follows the type signature below: ```python3 class Solution: def removeTrailingZeros(self, num: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.removeTrailingZeros(*['51230100']) == 512301\nassert my_solution.removeTrailingZeros(*['123']) == 123\nassert my_solution.removeTrailingZeros(*['1720865079269529096765717822459']) == 1720865079269529096765717822459\nassert my_solution.removeTrailingZeros(*['7144743841294080781916786558429']) == 7144743841294080781916786558429\nassert my_solution.removeTrailingZeros(*['4899557969895099468817472']) == 4899557969895099468817472\nassert my_solution.removeTrailingZeros(*['411869412884994212936809415067881212124013636868841988745365283194866298']) == 411869412884994212936809415067881212124013636868841988745365283194866298\nassert my_solution.removeTrailingZeros(*['6320000060155576064433475194277496015965584784508346180457590630139671509042003']) == 6320000060155576064433475194277496015965584784508346180457590630139671509042003\nassert my_solution.removeTrailingZeros(*['18536064908756368878635753877725420832721627175417705334313163152743918284']) == 18536064908756368878635753877725420832721627175417705334313163152743918284\nassert my_solution.removeTrailingZeros(*['31514216007864075756059111751787923413952537015859352242147727420']) == 3151421600786407575605911175178792341395253701585935224214772742\nassert my_solution.removeTrailingZeros(*['778176412164709639995320949248193524679567596956633611253735320337609197903694087247']) == 778176412164709639995320949248193524679567596956633611253735320337609197903694087247\nassert my_solution.removeTrailingZeros(*['2654202174504976690262133614463853127557953924212869337916870720806625423513072805010881594']) == 2654202174504976690262133614463853127557953924212869337916870720806625423513072805010881594\nassert my_solution.removeTrailingZeros(*['201756093344491217']) == 201756093344491217\nassert my_solution.removeTrailingZeros(*['98600182211138782435099309645219818178710738351680959628189']) == 98600182211138782435099309645219818178710738351680959628189\nassert my_solution.removeTrailingZeros(*['699339439231788234588164122']) == 699339439231788234588164122\nassert my_solution.removeTrailingZeros(*['335432394']) == 335432394\nassert my_solution.removeTrailingZeros(*['434426765199302232747258078617604694844575046742075444847184456682707778402158253']) == 434426765199302232747258078617604694844575046742075444847184456682707778402158253\nassert my_solution.removeTrailingZeros(*['768228974017629407344065188577158']) == 768228974017629407344065188577158\nassert my_solution.removeTrailingZeros(*['99413590779889837']) == 99413590779889837\nassert my_solution.removeTrailingZeros(*['8964416159649830616315621934936192519393899006248536553413']) == 8964416159649830616315621934936192519393899006248536553413\nassert my_solution.removeTrailingZeros(*['161000610717026350996481067819904866052481']) == 161000610717026350996481067819904866052481\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2819", "questionFrontendId": "2710", "questionTitle": "Remove Trailing Zeros From a String", "stats": { "totalAccepted": "9.2K", "totalSubmission": "11.2K", "totalAcceptedRaw": 9183, "totalSubmissionRaw": 11198, "acRate": "82.0%" } }
LeetCode/2817
# Minimum Cost to Make All Characters Equal You are given a **0-indexed** binary string `s` of length `n` on which you can apply two types of operations: * Choose an index `i` and invert all characters from index `0` to index `i` (both inclusive), with a cost of `i + 1` * Choose an index `i` and invert all characters from index `i` to index `n - 1` (both inclusive), with a cost of `n - i` Return *the **minimum cost** to make all characters of the string **equal***. **Invert** a character means if its value is '0' it becomes '1' and vice-versa.   **Example 1:** ``` **Input:** s = "0011" **Output:** 2 **Explanation:** Apply the second operation with i = 2 to obtain s = "0000" for a cost of 2. It can be shown that 2 is the minimum cost to make all characters equal. ``` **Example 2:** ``` **Input:** s = "010101" **Output:** 9 **Explanation:** Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3. Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2. Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1. Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2. Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1. The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal. ```   **Constraints:** * `1 <= s.length == n <= 105` * `s[i]` is either `'0'` or `'1'` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*['0011']) == 2\nassert my_solution.minimumCost(*['010101']) == 9\nassert my_solution.minimumCost(*['0']) == 0\nassert my_solution.minimumCost(*['00']) == 0\nassert my_solution.minimumCost(*['000']) == 0\nassert my_solution.minimumCost(*['0000']) == 0\nassert my_solution.minimumCost(*['00000']) == 0\nassert my_solution.minimumCost(*['000000']) == 0\nassert my_solution.minimumCost(*['0000000']) == 0\nassert my_solution.minimumCost(*['00000000']) == 0\nassert my_solution.minimumCost(*['000000000']) == 0\nassert my_solution.minimumCost(*['000000001']) == 1\nassert my_solution.minimumCost(*['00000001']) == 1\nassert my_solution.minimumCost(*['000000010']) == 3\nassert my_solution.minimumCost(*['000000011']) == 2\nassert my_solution.minimumCost(*['0000001']) == 1\nassert my_solution.minimumCost(*['00000010']) == 3\nassert my_solution.minimumCost(*['000000100']) == 5\nassert my_solution.minimumCost(*['000000101']) == 6\nassert my_solution.minimumCost(*['00000011']) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2817", "questionFrontendId": "2712", "questionTitle": "Minimum Cost to Make All Characters Equal", "stats": { "totalAccepted": "4.8K", "totalSubmission": "8.4K", "totalAcceptedRaw": 4782, "totalSubmissionRaw": 8374, "acRate": "57.1%" } }
LeetCode/2756
# Buy Two Chocolates You are given an integer array `prices` representing the prices of various chocolates in a store. You are also given a single integer `money`, which represents your initial amount of money. You must buy **exactly** two chocolates in such a way that you still have some **non-negative** leftover money. You would like to minimize the sum of the prices of the two chocolates you buy. Return *the amount of money you will have leftover after buying the two chocolates*. If there is no way for you to buy two chocolates without ending up in debt, return `money`. Note that the leftover must be non-negative.   **Example 1:** ``` **Input:** prices = [1,2,2], money = 3 **Output:** 0 **Explanation:** Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0. ``` **Example 2:** ``` **Input:** prices = [3,2,3], money = 3 **Output:** 3 **Explanation:** You cannot buy 2 chocolates without going in debt, so we return 3. ```   **Constraints:** * `2 <= prices.length <= 50` * `1 <= prices[i] <= 100` * `1 <= money <= 100` Please make sure your answer follows the type signature below: ```python3 class Solution: def buyChoco(self, prices: List[int], money: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.buyChoco(*[[1, 2, 2], 3]) == 0\nassert my_solution.buyChoco(*[[3, 2, 3], 3]) == 3\nassert my_solution.buyChoco(*[[98, 54, 6, 34, 66, 63, 52, 39], 62]) == 22\nassert my_solution.buyChoco(*[[75, 28, 65, 18, 37, 18, 97], 13]) == 13\nassert my_solution.buyChoco(*[[69, 91, 78, 19, 40, 13], 94]) == 62\nassert my_solution.buyChoco(*[[88, 43, 61], 72]) == 72\nassert my_solution.buyChoco(*[[46, 56, 41], 79]) == 79\nassert my_solution.buyChoco(*[[71, 62, 57, 67, 34], 8]) == 8\nassert my_solution.buyChoco(*[[2, 12, 93, 52, 91, 86, 81, 1, 79, 64], 43]) == 40\nassert my_solution.buyChoco(*[[94, 42, 91, 9, 25], 73]) == 39\nassert my_solution.buyChoco(*[[31, 19, 70, 58, 12], 11]) == 11\nassert my_solution.buyChoco(*[[66, 63, 14, 39, 71, 38, 91], 16]) == 16\nassert my_solution.buyChoco(*[[43, 70, 27, 78, 71, 76, 37, 57, 12, 77], 50]) == 11\nassert my_solution.buyChoco(*[[74, 31, 38, 24, 25, 24, 5], 79]) == 50\nassert my_solution.buyChoco(*[[61, 9, 12, 87, 97, 17], 20]) == 20\nassert my_solution.buyChoco(*[[11, 90], 70]) == 70\nassert my_solution.buyChoco(*[[91, 68, 36, 67, 31, 28, 87, 76], 54]) == 54\nassert my_solution.buyChoco(*[[58, 64, 85, 83, 90, 46], 11]) == 11\nassert my_solution.buyChoco(*[[79, 15, 63, 76, 81, 43, 25], 32]) == 32\nassert my_solution.buyChoco(*[[94, 35], 15]) == 15\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2756", "questionFrontendId": "2706", "questionTitle": "Buy Two Chocolates", "stats": { "totalAccepted": "26.8K", "totalSubmission": "34.6K", "totalAcceptedRaw": 26830, "totalSubmissionRaw": 34620, "acRate": "77.5%" } }
LeetCode/2755
# Extra Characters in a String You are given a **0-indexed** string `s` and a dictionary of words `dictionary`. You have to break `s` into one or more **non-overlapping** substrings such that each substring is present in `dictionary`. There may be some **extra characters** in `s` which are not present in any of the substrings. Return *the **minimum** number of extra characters left over if you break up* `s` *optimally.*   **Example 1:** ``` **Input:** s = "leetscode", dictionary = ["leet","code","leetcode"] **Output:** 1 **Explanation:** We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1. ``` **Example 2:** ``` **Input:** s = "sayhelloworld", dictionary = ["hello","world"] **Output:** 3 **Explanation:** We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3. ```   **Constraints:** * `1 <= s.length <= 50` * `1 <= dictionary.length <= 50` * `1 <= dictionary[i].length <= 50` * `dictionary[i]` and `s` consists of only lowercase English letters * `dictionary` contains distinct words Please make sure your answer follows the type signature below: ```python3 class Solution: def minExtraChar(self, s: str, dictionary: List[str]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minExtraChar(*['leetscode', ['leet', 'code', 'leetcode']]) == 1\nassert my_solution.minExtraChar(*['sayhelloworld', ['hello', 'world']]) == 3\nassert my_solution.minExtraChar(*['dwmodizxvvbosxxw', ['ox', 'lb', 'diz', 'gu', 'v', 'ksv', 'o', 'nuq', 'r', 'txhe', 'e', 'wmo', 'cehy', 'tskz', 'ds', 'kzbu']]) == 7\nassert my_solution.minExtraChar(*['ecolloycollotkvzqpdaumuqgs', ['flbri', 'uaaz', 'numy', 'laper', 'ioqyt', 'tkvz', 'ndjb', 'gmg', 'gdpbo', 'x', 'collo', 'vuh', 'qhozp', 'iwk', 'paqgn', 'm', 'mhx', 'jgren', 'qqshd', 'qr', 'qpdau', 'oeeuq', 'c', 'qkot', 'uxqvx', 'lhgid', 'vchsk', 'drqx', 'keaua', 'yaru', 'mla', 'shz', 'lby', 'vdxlv', 'xyai', 'lxtgl', 'inz', 'brhi', 'iukt', 'f', 'lbjou', 'vb', 'sz', 'ilkra', 'izwk', 'muqgs', 'gom', 'je']]) == 2\nassert my_solution.minExtraChar(*['eglglxa', ['rs', 'j', 'h', 'g', 'fy', 'l', 'fc', 's', 'zf', 'i', 'k', 'x', 'gl', 'qr', 'qj', 'b', 'm', 'cm', 'pe', 'y', 'ei', 'wg', 'e', 'c', 'll', 'u', 'lb', 'kc', 'r', 'gs', 'p', 'ga', 'pq', 'o', 'wq', 'mp', 'ms', 'vp', 'kg', 'cu']]) == 1\nassert my_solution.minExtraChar(*['kngmktvcxrwubjzk', ['ktvc', 'pajp', 'x', 'emye', 'hde', 'haol', 'ubjz', 'tc', 'rb', 'kng', 'li', 'fph', 'd', 'rgrg']]) == 4\nassert my_solution.minExtraChar(*['voctvochpgutoywpnafylzelqsnzsbandjcqdciyoefi', ['tf', 'v', 'wadrya', 'a', 'cqdci', 'uqfg', 'voc', 'zelqsn', 'band', 'b', 'yoefi', 'utoywp', 'herqqn', 'umra', 'frfuyj', 'vczatj', 'sdww']]) == 11\nassert my_solution.minExtraChar(*['nwlztjn', ['a', 'f', 'v', 'me', 'm', 'bv', 'g', 'ss', 'tu', 'jm', 'z', 'kg', 'l', 'go', 'cn', 'uj', 'kx', 'w', 'qz', 'e', 'ut', 'tf', 'zn', 'ha', 'ke', 'af', 'aj', 'ls', 'r', 'no', 'pm', 'qn', 'yw', 'cs', 'oz', 'b']]) == 4\nassert my_solution.minExtraChar(*['smsvy', ['j', 'p', 'y', 'r', 't', 'nj', 'k', 'xj', 'vg', 'da', 'm', 'u', 'yq', 'as', 'wh', 'b', 'vo', 'h', 'wb', 'z', 'np', 'uy', 'i', 'f', 'w', 'wg', 's', 'ls', 'xf', 'ou', 'mj', 'pf']]) == 1\nassert my_solution.minExtraChar(*['azchrwjli', ['nrd', 'ulh', 'r', 'u', 'm', 'ue', 'jzd', 'zch', 'pbl', 'op', 'mw', 'wjl', 'wv', 'e']]) == 2\nassert my_solution.minExtraChar(*['octncmdbgnxapjoqlofuzypthlytkmchayflwky', ['m', 'its', 'imaby', 'pa', 'ijmnvj', 'k', 'mhka', 'n', 'y', 'nc', 'wq', 'p', 'mjqqa', 'ht', 'dfoa', 'yqa', 'kk', 'pixq', 'ixsdln', 'rh', 'dwl', 'dbgnxa', 'kmpfz', 'nhxjm', 'wg', 'wky', 'oct', 'og', 'uhin', 'zxb', 'qz', 'tpf', 'hlrc', 'j', 'l', 'tew', 'xbn', 'a', 'uzypt', 'uvln', 'mchay', 'onnbi', 'hlytk', 'pjoqlo', 'dxsjr', 'u', 'uj']]) == 2\nassert my_solution.minExtraChar(*['rqdrlfpthqkevauuks', ['pt', 'r', 'luet', 'hn', 'djj', 'nqk', 'yztb', 'yvrm', 'hjw', 'fon', 'hqke', 'btr', 'fg', 'guo', 'un', 'eyl', 'x', 'b', 'wj', 'yzy', 'dpj', 'z', 'oj', 'uu', 'fmp', 'ypmv', 'qd', 'rz', 'efj', 'sfc']]) == 6\nassert my_solution.minExtraChar(*['etzdfxoxyajiothfsxxcxoxhfsxxcbvfmtcibmjkhniq', ['nio', 'y', 'jkhni', 'bu', 'hfsxxc', 'iacjvr', 'xox', 'vrslt', 'bvfm', 'gi', 'tjazf', 'ipfexh', 'tcib', 'crx', 'umxvs', 'q', 'e', 'rlpgq']]) == 10\nassert my_solution.minExtraChar(*['t', ['q', 'f', 'b', 's', 'k', 't', 'p', 'i', 'o', 'v', 'l', 'u', 'c', 'm', 'r', 'd', 'g', 'e', 'j', 'h', 'y']]) == 0\nassert my_solution.minExtraChar(*['cmizgedmufhbnqge', ['yid', 'u', 'jii', 'rdg', 'zcy', 'uo', 'buy', 's', 'vhn', 'ae', 'fygs', 'vlw', 'hv', 'nqge', 'fhb', 'oh', 'm', 'z', 't', 'rqwf', 'iqp', 'xan', 'ir', 'cmi', 'bim', 'bbd', 'jf', 'gs', 'vj']]) == 3\nassert my_solution.minExtraChar(*['ybo', ['k', 'w', 'n', 's', 'v', 'i', 'q', 'j', 'h', 'o', 't', 'y', 'g', 'e', 'z', 'a', 'c', 'x', 'd', 'l', 'f']]) == 1\nassert my_solution.minExtraChar(*['msgaxldzkgi', ['ax', 'ms', 'hvb', 'w', 'z', 'ajc', 'ti', 'cn', 'x', 'j', 'gi', 'r', 'nj', 'f', 'xr', 'iuz', 'ya', 'gty', 'hgs', 'ug', 'mfv', 'bw', 'vr', 'oe', 'xv', 'jf', 'vy', 'q', 'qf', 'oo', 'iq', 't', 'v', 'htx', 'g', 'gjp', 'dis', 'gt', 'gv', 'wu', 'vtx', 'cmo', 'wdl', 'b']]) == 3\nassert my_solution.minExtraChar(*['jztkzefkuhgldeuxumtecjfhwkumklktbvoefjzgbvoebvoeo', ['tkzef', 'teq', 'idx', 'czset', 'jfh', 'fjz', 'nij', 'hpr', 'rhijyw', 'bvoe', 'k', 'umklkt', 'szbescz', 'pyiri', 'bc', 'fzyzcgc', 'lf', 's', 'kxalcws', 'w', 'euxumt', 'hlroawe', 'yvoyx', 'ucvzmc', 'blr', 'piasks', 'fblc']]) == 11\nassert my_solution.minExtraChar(*['pkfydgpkffmfam', ['pk', 'f', 'kdy', 'tw', 'm']]) == 4\nassert my_solution.minExtraChar(*['pucblaexghipwhcfkhwawsuszagdlajctwevqiccnqpyt', ['pyvu', 'vt', 'it', 'jc', 'ex', 'zmn', 'qahtts', 'c', 'q', 'jlui', 'zx', 'gt', 'djb', 'ywdyt', 'dc', 'fhw', 'ninhz', 'vqic', 'ern', 'dwzyiq', 'cvh', 'ualv', 'jgmal', 'soj', 'khwaw', 'zejzs', 'cjqrt', 'la', 'pucb', 'uc', 'ezqcsl', 'uxx', 'pvz', 'uownow', 'f', 'ipwhcf', 'us', 'cx', 'k']]) == 14\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2755", "questionFrontendId": "2707", "questionTitle": "Extra Characters in a String", "stats": { "totalAccepted": "20.2K", "totalSubmission": "31.8K", "totalAcceptedRaw": 20196, "totalSubmissionRaw": 31839, "acRate": "63.4%" } }
LeetCode/2754
# Maximum Strength of a Group You are given a **0-indexed** integer array `nums` representing the score of students in an exam. The teacher would like to form one **non-empty** group of students with maximal **strength**, where the strength of a group of students of indices `i0`, `i1`, `i2`, ... , `ik` is defined as `nums[i0] * nums[i1] * nums[i2] * ... * nums[ik​]`. Return *the maximum strength of a group the teacher can create*.   **Example 1:** ``` **Input:** nums = [3,-1,-5,2,5,-9] **Output:** 1350 **Explanation:** One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal. ``` **Example 2:** ``` **Input:** nums = [-4,-5,-4] **Output:** 20 **Explanation:** Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength. ```   **Constraints:** * `1 <= nums.length <= 13` * `-9 <= nums[i] <= 9` Please make sure your answer follows the type signature below: ```python3 class Solution: def maxStrength(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxStrength(*[[3, -1, -5, 2, 5, -9]]) == 1350\nassert my_solution.maxStrength(*[[-4, -5, -4]]) == 20\nassert my_solution.maxStrength(*[[-2, -3, 8, 9]]) == 432\nassert my_solution.maxStrength(*[[-5, 5, -3, -7, -1, -5, 5, 1, -8]]) == 105000\nassert my_solution.maxStrength(*[[-3, -1, -4, -6, -4, 8, -1, 3, 9, -3]]) == 186624\nassert my_solution.maxStrength(*[[-9, 9, -2, 1, 7, -8, -6, 1, 4]]) == 217728\nassert my_solution.maxStrength(*[[8, 6, 0, 5, -4, -8, -4, 9, -1, 6, -4, 8, -5]]) == 265420800\nassert my_solution.maxStrength(*[[-7, -4, -4, -1, 5, 5, -3, 4]]) == 33600\nassert my_solution.maxStrength(*[[-4, -3]]) == 12\nassert my_solution.maxStrength(*[[9, 6, 3]]) == 162\nassert my_solution.maxStrength(*[[-8, -5, -2, -6, 1, 2, 7, 1, -7]]) == 23520\nassert my_solution.maxStrength(*[[9, -3, 2, -5, -2, -9, -8, -4, 5, -3]]) == 1166400\nassert my_solution.maxStrength(*[[-4, 8, -1, -4, -2, 4, 9, 3, -9, 3]]) == 746496\nassert my_solution.maxStrength(*[[8, -3, 5, 5, -8, 2, 1, 3, 2, -9, -5, -9]]) == 7776000\nassert my_solution.maxStrength(*[[-8, 6, 8, -7, 6, -4, -9, 8, 4, 2, -9, 6]]) == 501645312\nassert my_solution.maxStrength(*[[-4, 3, 1, -4, 9, -9, 4, 6, 3, 1]]) == 69984\nassert my_solution.maxStrength(*[[-6, 5, 7, -5, -5, -1, 2, -8]]) == 84000\nassert my_solution.maxStrength(*[[0, -1]]) == 0\nassert my_solution.maxStrength(*[[-1, -2, 7, -1, -7, -5, 0, -8]]) == 3920\nassert my_solution.maxStrength(*[[-9, 5, 8, -7, 9, -7, 4, 5, 8]]) == 3628800\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2754", "questionFrontendId": "2708", "questionTitle": "Maximum Strength of a Group", "stats": { "totalAccepted": "4.9K", "totalSubmission": "16.1K", "totalAcceptedRaw": 4897, "totalSubmissionRaw": 16063, "acRate": "30.5%" } }
LeetCode/2827
# Greatest Common Divisor Traversal You are given a **0-indexed** integer array `nums`, and you are allowed to **traverse** between its indices. You can traverse between index `i` and index `j`, `i != j`, if and only if `gcd(nums[i], nums[j]) > 1`, where `gcd` is the **greatest common divisor**. Your task is to determine if for **every pair** of indices `i` and `j` in nums, where `i < j`, there exists a **sequence of traversals** that can take us from `i` to `j`. Return `true` *if it is possible to traverse between all such pairs of indices,* *or* `false` *otherwise.*   **Example 1:** ``` **Input:** nums = [2,3,6] **Output:** true **Explanation:** In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2). To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1. To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1. ``` **Example 2:** ``` **Input:** nums = [3,9,5] **Output:** false **Explanation:** No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false. ``` **Example 3:** ``` **Input:** nums = [4,3,12,8] **Output:** true **Explanation:** There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def canTraverseAllPairs(self, nums: List[int]) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canTraverseAllPairs(*[[2, 3, 6]]) == True\nassert my_solution.canTraverseAllPairs(*[[3, 9, 5]]) == False\nassert my_solution.canTraverseAllPairs(*[[4, 3, 12, 8]]) == True\nassert my_solution.canTraverseAllPairs(*[[10]]) == True\nassert my_solution.canTraverseAllPairs(*[[11]]) == True\nassert my_solution.canTraverseAllPairs(*[[14]]) == True\nassert my_solution.canTraverseAllPairs(*[[20]]) == True\nassert my_solution.canTraverseAllPairs(*[[28]]) == True\nassert my_solution.canTraverseAllPairs(*[[30]]) == True\nassert my_solution.canTraverseAllPairs(*[[35]]) == True\nassert my_solution.canTraverseAllPairs(*[[42]]) == True\nassert my_solution.canTraverseAllPairs(*[[44]]) == True\nassert my_solution.canTraverseAllPairs(*[[98]]) == True\nassert my_solution.canTraverseAllPairs(*[[20, 6]]) == True\nassert my_solution.canTraverseAllPairs(*[[28, 39]]) == False\nassert my_solution.canTraverseAllPairs(*[[30, 14]]) == True\nassert my_solution.canTraverseAllPairs(*[[30, 30]]) == True\nassert my_solution.canTraverseAllPairs(*[[33, 60]]) == True\nassert my_solution.canTraverseAllPairs(*[[35, 26]]) == False\nassert my_solution.canTraverseAllPairs(*[[42, 42]]) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2827", "questionFrontendId": "2709", "questionTitle": "Greatest Common Divisor Traversal", "stats": { "totalAccepted": "3K", "totalSubmission": "12.1K", "totalAcceptedRaw": 3023, "totalSubmissionRaw": 12060, "acRate": "25.1%" } }
LeetCode/2800
# Minimum String Length After Removing Substrings You are given a string `s` consisting only of **uppercase** English letters. You can apply some operations to this string where, in one operation, you can remove **any** occurrence of one of the substrings `"AB"` or `"CD"` from `s`. Return *the **minimum** possible length of the resulting string that you can obtain*. **Note** that the string concatenates after removing the substring and could produce new `"AB"` or `"CD"` substrings.   **Example 1:** ``` **Input:** s = "ABFCACDB" **Output:** 2 **Explanation:** We can do the following operations: - Remove the substring "ABFCACDB", so s = "FCACDB". - Remove the substring "FCACDB", so s = "FCAB". - Remove the substring "FCAB", so s = "FC". So the resulting length of the string is 2. It can be shown that it is the minimum length that we can obtain. ``` **Example 2:** ``` **Input:** s = "ACBBD" **Output:** 5 **Explanation:** We cannot do any operations on the string so the length remains the same. ```   **Constraints:** * `1 <= s.length <= 100` * `s` consists only of uppercase English letters. Please make sure your answer follows the type signature below: ```python3 class Solution: def minLength(self, s: str) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minLength(*['ABFCACDB']) == 2\nassert my_solution.minLength(*['ACBBD']) == 5\nassert my_solution.minLength(*['A']) == 1\nassert my_solution.minLength(*['D']) == 1\nassert my_solution.minLength(*['Z']) == 1\nassert my_solution.minLength(*['CCCCDDDD']) == 0\nassert my_solution.minLength(*['AATQCABDCBE']) == 7\nassert my_solution.minLength(*['ZKPT']) == 4\nassert my_solution.minLength(*['CCDDACDB']) == 0\nassert my_solution.minLength(*['EUO']) == 3\nassert my_solution.minLength(*['ABG']) == 1\nassert my_solution.minLength(*['FACDHM']) == 4\nassert my_solution.minLength(*['WCCDD']) == 1\nassert my_solution.minLength(*['CCDDLLDW']) == 4\nassert my_solution.minLength(*['GYCD']) == 2\nassert my_solution.minLength(*['CABDCABDAB']) == 0\nassert my_solution.minLength(*['ZCABDRIL']) == 4\nassert my_solution.minLength(*['P']) == 1\nassert my_solution.minLength(*['L']) == 1\nassert my_solution.minLength(*['BQHSABE']) == 5\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2800", "questionFrontendId": "2696", "questionTitle": "Minimum String Length After Removing Substrings", "stats": { "totalAccepted": "31.6K", "totalSubmission": "40.8K", "totalAcceptedRaw": 31637, "totalSubmissionRaw": 40759, "acRate": "77.6%" } }
LeetCode/2816
# Lexicographically Smallest Palindrome You are given a string `s` consisting of **lowercase English letters**, and you are allowed to perform operations on it. In one operation, you can **replace** a character in `s` with another lowercase English letter. Your task is to make `s` a **palindrome** with the **minimum** **number** **of operations** possible. If there are **multiple palindromes** that can be made using the **minimum** number of operations, make the **lexicographically smallest** one. A string `a` is lexicographically smaller than a string `b` (of the same length) if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. Return *the resulting palindrome string.*   **Example 1:** ``` **Input:** s = "egcfe" **Output:** "efcfe" **Explanation:** The minimum number of operations to make "egcfe" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "efcfe", by changing 'g'. ``` **Example 2:** ``` **Input:** s = "abcd" **Output:** "abba" **Explanation:** The minimum number of operations to make "abcd" a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is "abba". ``` **Example 3:** ``` **Input:** s = "seven" **Output:** "neven" **Explanation:** The minimum number of operations to make "seven" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "neven". ```   **Constraints:** * `1 <= s.length <= 1000` * `s` consists of only lowercase English letters**.** Please make sure your answer follows the type signature below: ```python3 class Solution: def makeSmallestPalindrome(self, s: str) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.makeSmallestPalindrome(*['egcfe']) == efcfe\nassert my_solution.makeSmallestPalindrome(*['abcd']) == abba\nassert my_solution.makeSmallestPalindrome(*['seven']) == neven\nassert my_solution.makeSmallestPalindrome(*['a']) == a\nassert my_solution.makeSmallestPalindrome(*['b']) == b\nassert my_solution.makeSmallestPalindrome(*['c']) == c\nassert my_solution.makeSmallestPalindrome(*['e']) == e\nassert my_solution.makeSmallestPalindrome(*['f']) == f\nassert my_solution.makeSmallestPalindrome(*['g']) == g\nassert my_solution.makeSmallestPalindrome(*['h']) == h\nassert my_solution.makeSmallestPalindrome(*['i']) == i\nassert my_solution.makeSmallestPalindrome(*['j']) == j\nassert my_solution.makeSmallestPalindrome(*['k']) == k\nassert my_solution.makeSmallestPalindrome(*['m']) == m\nassert my_solution.makeSmallestPalindrome(*['n']) == n\nassert my_solution.makeSmallestPalindrome(*['o']) == o\nassert my_solution.makeSmallestPalindrome(*['p']) == p\nassert my_solution.makeSmallestPalindrome(*['q']) == q\nassert my_solution.makeSmallestPalindrome(*['r']) == r\nassert my_solution.makeSmallestPalindrome(*['s']) == s\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2816", "questionFrontendId": "2697", "questionTitle": "Lexicographically Smallest Palindrome", "stats": { "totalAccepted": "28.8K", "totalSubmission": "32.9K", "totalAcceptedRaw": 28789, "totalSubmissionRaw": 32857, "acRate": "87.6%" } }
LeetCode/2802
# Find the Punishment Number of an Integer Given a positive integer `n`, return *the **punishment number*** of `n`. The **punishment number** of `n` is defined as the sum of the squares of all integers `i` such that: * `1 <= i <= n` * The decimal representation of `i * i` can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals `i`.   **Example 1:** ``` **Input:** n = 10 **Output:** 182 **Explanation:** There are exactly 3 integers i that satisfy the conditions in the statement: - 1 since 1 * 1 = 1 - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0. Hence, the punishment number of 10 is 1 + 81 + 100 = 182 ``` **Example 2:** ``` **Input:** n = 37 **Output:** 1478 **Explanation:** There are exactly 4 integers i that satisfy the conditions in the statement: - 1 since 1 * 1 = 1. - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0. - 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6. Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478 ```   **Constraints:** * `1 <= n <= 1000` Please make sure your answer follows the type signature below: ```python3 class Solution: def punishmentNumber(self, n: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.punishmentNumber(*[10]) == 182\nassert my_solution.punishmentNumber(*[37]) == 1478\nassert my_solution.punishmentNumber(*[1]) == 1\nassert my_solution.punishmentNumber(*[2]) == 1\nassert my_solution.punishmentNumber(*[3]) == 1\nassert my_solution.punishmentNumber(*[4]) == 1\nassert my_solution.punishmentNumber(*[5]) == 1\nassert my_solution.punishmentNumber(*[6]) == 1\nassert my_solution.punishmentNumber(*[7]) == 1\nassert my_solution.punishmentNumber(*[8]) == 1\nassert my_solution.punishmentNumber(*[9]) == 82\nassert my_solution.punishmentNumber(*[11]) == 182\nassert my_solution.punishmentNumber(*[12]) == 182\nassert my_solution.punishmentNumber(*[13]) == 182\nassert my_solution.punishmentNumber(*[14]) == 182\nassert my_solution.punishmentNumber(*[15]) == 182\nassert my_solution.punishmentNumber(*[16]) == 182\nassert my_solution.punishmentNumber(*[17]) == 182\nassert my_solution.punishmentNumber(*[18]) == 182\nassert my_solution.punishmentNumber(*[19]) == 182\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2802", "questionFrontendId": "2698", "questionTitle": "Find the Punishment Number of an Integer", "stats": { "totalAccepted": "27.1K", "totalSubmission": "34.5K", "totalAcceptedRaw": 27123, "totalSubmissionRaw": 34491, "acRate": "78.6%" } }
LeetCode/2791
# Find the Losers of the Circular Game There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend. The rules of the game are as follows: `1st` friend receives the ball. * After that, `1st` friend passes it to the friend who is `k` steps away from them in the **clockwise** direction. * After that, the friend who receives the ball should pass it to the friend who is `2 * k` steps away from them in the **clockwise** direction. * After that, the friend who receives the ball should pass it to the friend who is `3 * k` steps away from them in the **clockwise** direction, and so on and so forth. In other words, on the `ith` turn, the friend holding the ball should pass it to the friend who is `i * k` steps away from them in the **clockwise** direction. The game is finished when some friend receives the ball for the second time. The **losers** of the game are friends who did not receive the ball in the entire game. Given the number of friends, `n`, and an integer `k`, return *the array answer, which contains the losers of the game in the **ascending** order*.   **Example 1:** ``` **Input:** n = 5, k = 2 **Output:** [4,5] **Explanation:** The game goes as follows: 1) Start at 1st friend and pass the ball to the friend who is 2 steps away from them - 3rd friend. 2) 3rd friend passes the ball to the friend who is 4 steps away from them - 2nd friend. 3) 2nd friend passes the ball to the friend who is 6 steps away from them - 3rd friend. 4) The game ends as 3rd friend receives the ball for the second time. ``` **Example 2:** ``` **Input:** n = 4, k = 4 **Output:** [2,3,4] **Explanation:** The game goes as follows: 1) Start at the 1st friend and pass the ball to the friend who is 4 steps away from them - 1st friend. 2) The game ends as 1st friend receives the ball for the second time. ```   **Constraints:** * `1 <= k <= n <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def circularGameLosers(self, n: int, k: int) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.circularGameLosers(*[5, 2]) == [4, 5]\nassert my_solution.circularGameLosers(*[4, 4]) == [2, 3, 4]\nassert my_solution.circularGameLosers(*[1, 1]) == []\nassert my_solution.circularGameLosers(*[2, 1]) == []\nassert my_solution.circularGameLosers(*[2, 2]) == [2]\nassert my_solution.circularGameLosers(*[3, 1]) == [3]\nassert my_solution.circularGameLosers(*[3, 2]) == [2]\nassert my_solution.circularGameLosers(*[3, 3]) == [2, 3]\nassert my_solution.circularGameLosers(*[4, 1]) == []\nassert my_solution.circularGameLosers(*[4, 2]) == [2, 4]\nassert my_solution.circularGameLosers(*[4, 3]) == []\nassert my_solution.circularGameLosers(*[5, 1]) == [3, 5]\nassert my_solution.circularGameLosers(*[5, 3]) == [2, 3]\nassert my_solution.circularGameLosers(*[5, 4]) == [2, 4]\nassert my_solution.circularGameLosers(*[5, 5]) == [2, 3, 4, 5]\nassert my_solution.circularGameLosers(*[6, 1]) == [3, 5, 6]\nassert my_solution.circularGameLosers(*[6, 2]) == [2, 4, 5, 6]\nassert my_solution.circularGameLosers(*[6, 3]) == [2, 3, 5, 6]\nassert my_solution.circularGameLosers(*[6, 4]) == [2, 3, 4, 6]\nassert my_solution.circularGameLosers(*[6, 5]) == [2, 3, 5]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2791", "questionFrontendId": "2682", "questionTitle": "Find the Losers of the Circular Game", "stats": { "totalAccepted": "31.1K", "totalSubmission": "51.2K", "totalAcceptedRaw": 31126, "totalSubmissionRaw": 51225, "acRate": "60.8%" } }
LeetCode/2792
# Neighboring Bitwise XOR A **0-indexed** array `derived` with length `n` is derived by computing the **bitwise XOR** (⊕) of adjacent values in a **binary array** `original` of length `n`. Specifically, for each index `i` in the range `[0, n - 1]`: * If `i = n - 1`, then `derived[i] = original[i] ⊕ original[0]`. * Otherwise, `derived[i] = original[i] ⊕ original[i + 1]`. Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`. Return ***true** if such an array exists or **false** otherwise.* * A binary array is an array containing only **0's** and **1's**   **Example 1:** ``` **Input:** derived = [1,1,0] **Output:** true **Explanation:** A valid original array that gives derived is [0,1,0]. derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1 derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1 derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0 ``` **Example 2:** ``` **Input:** derived = [1,1] **Output:** true **Explanation:** A valid original array that gives derived is [0,1]. derived[0] = original[0] ⊕ original[1] = 1 derived[1] = original[1] ⊕ original[0] = 1 ``` **Example 3:** ``` **Input:** derived = [1,0] **Output:** false **Explanation:** There is no valid original array that gives derived. ```   **Constraints:** * `n == derived.length` * `1 <= n <= 105` * The values in `derived` are either **0's** or **1's** Please make sure your answer follows the type signature below: ```python3 class Solution: def doesValidArrayExist(self, derived: List[int]) -> bool: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.doesValidArrayExist(*[[1, 1, 0]]) == True\nassert my_solution.doesValidArrayExist(*[[1, 1]]) == True\nassert my_solution.doesValidArrayExist(*[[1, 0]]) == False\nassert my_solution.doesValidArrayExist(*[[0]]) == True\nassert my_solution.doesValidArrayExist(*[[1]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 0]]) == True\nassert my_solution.doesValidArrayExist(*[[0, 1]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 0, 0]]) == True\nassert my_solution.doesValidArrayExist(*[[0, 0, 1]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 1, 0]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 1, 1]]) == True\nassert my_solution.doesValidArrayExist(*[[1, 0, 0]]) == False\nassert my_solution.doesValidArrayExist(*[[1, 0, 1]]) == True\nassert my_solution.doesValidArrayExist(*[[1, 1, 1]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 0, 0, 0]]) == True\nassert my_solution.doesValidArrayExist(*[[0, 0, 0, 1]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 0, 1, 0]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 0, 1, 1]]) == True\nassert my_solution.doesValidArrayExist(*[[0, 1, 0, 0]]) == False\nassert my_solution.doesValidArrayExist(*[[0, 1, 0, 1]]) == True\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2792", "questionFrontendId": "2683", "questionTitle": "Neighboring Bitwise XOR", "stats": { "totalAccepted": "6.4K", "totalSubmission": "9.1K", "totalAcceptedRaw": 6399, "totalSubmissionRaw": 9102, "acRate": "70.3%" } }
LeetCode/2727
# Number of Senior Citizens You are given a **0-indexed** array of strings `details`. Each element of `details` provides information about a given passenger compressed into a string of length `15`. The system is such that: * The first ten characters consist of the phone number of passengers. * The next character denotes the gender of the person. * The following two characters are used to indicate the age of the person. * The last two characters determine the seat allotted to that person. Return *the number of passengers who are **strictly** **more than 60 years old**.*   **Example 1:** ``` **Input:** details = ["7868190130M7522","5303914400F9211","9273338290F4010"] **Output:** 2 **Explanation:** The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old. ``` **Example 2:** ``` **Input:** details = ["1313579440F2036","2921522980M5644"] **Output:** 0 **Explanation:** None of the passengers are older than 60. ```   **Constraints:** * `1 <= details.length <= 100` * `details[i].length == 15` * `details[i] consists of digits from '0' to '9'.` * `details[i][10] is either 'M' or 'F' or 'O'.` * The phone numbers and seat numbers of the passengers are distinct. Please make sure your answer follows the type signature below: ```python3 class Solution: def countSeniors(self, details: List[str]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSeniors(*[['7868190130M7522', '5303914400F9211', '9273338290F4010']]) == 2\nassert my_solution.countSeniors(*[['1313579440F2036', '2921522980M5644']]) == 0\nassert my_solution.countSeniors(*[['5612624052M0130', '5378802576M6424', '5447619845F0171', '2941701174O9078']]) == 2\nassert my_solution.countSeniors(*[['3991316911M8713', '6725249886O4741', '5473888009F5845', '4984061663O0864']]) == 1\nassert my_solution.countSeniors(*[['9751302862F0693', '3888560693F7262', '5485983835F0649', '2580974299F6042', '9976672161M6561', '0234451011F8013', '4294552179O6482']]) == 4\nassert my_solution.countSeniors(*[['7988027446O6842', '9520600906F3730', '8472982398F1852', '4846816953F7286', '4202235069F3818']]) == 2\nassert my_solution.countSeniors(*[['4700210787M5228', '0255398455F6295', '7384431514M7800', '1443977449M3109', '2282994255O5226', '8606200969O9277', '4437405006F0455']]) == 3\nassert my_solution.countSeniors(*[['1392071998O7877', '2871124410F4021', '8421864498O6227', '9931521255O3135', '2473473988O6615', '3280803948F1681', '0237807850F8276', '9514919390M4340']]) == 4\nassert my_solution.countSeniors(*[['0397702323O3173', '4378252056M9757', '9417440094F6155', '9386845039M5153', '6113409278O6674']]) == 3\nassert my_solution.countSeniors(*[['6754701005F5111', '2367591042M1658', '5007460685O3874']]) == 0\nassert my_solution.countSeniors(*[['0734795065O8950', '1621077912F8367']]) == 2\nassert my_solution.countSeniors(*[['2888752261F4414', '0521214419M4752', '4432525667F3437', '0832032564O5212', '2737580494O9719', '8135188724O5822', '1154907412F2727', '0356348635F6299', '6730846554O6789']]) == 3\nassert my_solution.countSeniors(*[['0596524473F9082', '6954537659O5938', '0372781636M8811', '2810637070F6751', '7996396465M0492']]) == 3\nassert my_solution.countSeniors(*[['8019564951O8970', '3548178582M6608', '6469783063O7399', '6021500511M9077', '9155267429O3772', '1544539168F8702', '5685546483F6836', '8130544867F9350']]) == 7\nassert my_solution.countSeniors(*[['4033768340M6635', '5062762244F4277', '4007914018O6198', '4587751274F2513']]) == 2\nassert my_solution.countSeniors(*[['3637439539O5371']]) == 0\nassert my_solution.countSeniors(*[['4515431186M0219', '5905210445F6568']]) == 1\nassert my_solution.countSeniors(*[['7039235288F1130', '8205469350O4522', '4713027183F0597', '1279957477O5991', '4020985356F3599', '4569195356F0641', '5728937156M7527', '9941084817F6951']]) == 2\nassert my_solution.countSeniors(*[['7714889516O0084', '3092492530F3817', '2777997043O4321', '3385255405F1280', '0552767275O7278']]) == 1\nassert my_solution.countSeniors(*[['0605578375M5168', '5369815077F2482', '6406066197M6947', '3059720887O8421', '1583105327M0903', '4276037521O2556', '3507586986M0743']]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2727", "questionFrontendId": "2678", "questionTitle": "Number of Senior Citizens", "stats": { "totalAccepted": "35.6K", "totalSubmission": "44.9K", "totalAcceptedRaw": 35613, "totalSubmissionRaw": 44872, "acRate": "79.4%" } }
LeetCode/2728
# Sum in a Matrix You are given a **0-indexed** 2D integer array `nums`. Initially, your score is `0`. Perform the following operations until the matrix becomes empty: 1. From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen. 2. Identify the highest number amongst all those removed in step 1. Add that number to your **score**. Return *the final **score**.*   **Example 1:** ``` **Input:** nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]] **Output:** 15 **Explanation:** In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15. ``` **Example 2:** ``` **Input:** nums = [[1]] **Output:** 1 **Explanation:** We remove 1 and add it to the answer. We return 1. ```   **Constraints:** * `1 <= nums.length <= 300` * `1 <= nums[i].length <= 500` * `0 <= nums[i][j] <= 103` Please make sure your answer follows the type signature below: ```python3 class Solution: def matrixSum(self, nums: List[List[int]]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.matrixSum(*[[[7, 2, 1], [6, 4, 2], [6, 5, 3], [3, 2, 1]]]) == 15\nassert my_solution.matrixSum(*[[[1]]]) == 1\nassert my_solution.matrixSum(*[[[1, 8, 16, 15, 12, 9, 15, 11, 18, 6, 16, 4, 9, 4], [3, 19, 8, 17, 19, 4, 9, 3, 2, 10, 15, 17, 3, 11], [13, 10, 19, 20, 6, 17, 15, 14, 16, 8, 1, 17, 0, 2], [12, 20, 0, 19, 15, 10, 7, 10, 2, 6, 18, 7, 7, 4], [17, 14, 2, 2, 10, 16, 15, 3, 9, 17, 9, 3, 17, 10], [17, 6, 19, 17, 18, 9, 14, 2, 19, 12, 10, 18, 7, 9], [5, 6, 5, 1, 19, 8, 15, 2, 2, 4, 4, 1, 2, 17], [12, 16, 8, 16, 7, 6, 18, 13, 18, 8, 14, 15, 20, 11], [2, 10, 19, 3, 15, 18, 20, 10, 6, 7, 0, 8, 3, 7], [11, 5, 10, 13, 1, 3, 4, 7, 1, 18, 20, 17, 19, 2], [0, 3, 20, 6, 19, 18, 3, 12, 2, 11, 3, 1, 19, 0], [6, 5, 3, 15, 6, 1, 0, 17, 13, 19, 3, 8, 2, 7], [2, 20, 9, 11, 13, 5, 1, 16, 14, 1, 19, 3, 12, 6]]]) == 190\nassert my_solution.matrixSum(*[[[15, 18, 5, 6, 1, 5, 5, 10, 16, 8, 3, 19], [14, 5, 0, 15, 13, 18, 16, 9, 20, 11, 12, 8], [4, 17, 0, 14, 2, 10, 1, 17, 8, 4, 7, 15], [11, 19, 9, 11, 18, 20, 19, 4, 9, 12, 13, 20], [2, 0, 19, 6, 10, 5, 7, 7, 20, 14, 12, 18], [13, 1, 12, 18, 13, 1, 5, 14, 2, 8, 5, 14], [16, 15, 17, 19, 0, 1, 15, 10, 9, 14, 1, 13], [6, 17, 20, 2, 4, 0, 12, 13, 10, 0, 6, 0], [0, 16, 19, 3, 6, 3, 19, 20, 6, 9, 8, 5]]]) == 167\nassert my_solution.matrixSum(*[[[12, 20, 2, 0, 8, 14, 3, 8, 4, 20, 16, 20, 20, 11, 3, 4], [8, 0, 1, 1, 6, 8, 17, 10, 11, 18, 1, 19, 20, 15, 20, 14], [20, 13, 11, 17, 5, 6, 12, 18, 9, 0, 4, 4, 8, 10, 10, 11], [2, 10, 19, 1, 1, 8, 5, 4, 18, 9, 11, 12, 17, 4, 9, 3]]]) == 184\nassert my_solution.matrixSum(*[[[1, 9, 5, 16, 2, 9, 12, 10], [9, 13, 3, 3, 17, 15, 15, 10], [10, 3, 15, 3, 15, 13, 1, 9], [10, 4, 5, 20, 18, 12, 20, 2], [2, 2, 6, 7, 1, 12, 0, 3], [12, 17, 16, 9, 14, 15, 18, 6], [13, 2, 11, 7, 8, 18, 5, 13], [6, 11, 3, 2, 0, 16, 14, 6], [3, 15, 12, 8, 6, 20, 1, 6], [19, 4, 3, 6, 14, 12, 11, 17], [4, 3, 19, 15, 4, 18, 12, 20], [13, 16, 15, 10, 15, 15, 20, 6], [17, 19, 7, 0, 10, 10, 10, 1], [16, 4, 8, 19, 4, 12, 18, 9], [15, 2, 2, 16, 1, 2, 7, 4], [1, 9, 0, 14, 10, 5, 4, 20]]]) == 117\nassert my_solution.matrixSum(*[[[16, 12, 16, 16, 1, 18, 2, 16, 19, 2, 13, 6], [9, 17, 19, 13, 15, 12, 19, 18, 7, 0, 0, 5], [9, 16, 18, 8, 10, 2, 15, 8, 9, 13, 12, 12], [1, 5, 20, 4, 7, 9, 10, 1, 1, 15, 13, 4], [15, 19, 2, 4, 11, 13, 1, 19, 14, 12, 14, 1], [3, 15, 4, 0, 1, 19, 19, 4, 20, 10, 3, 17], [20, 11, 6, 12, 15, 3, 1, 19, 14, 19, 20, 10], [20, 3, 19, 9, 4, 12, 9, 3, 16, 6, 1, 12], [14, 11, 6, 14, 11, 20, 2, 1, 1, 15, 8, 0], [16, 18, 18, 6, 7, 2, 20, 16, 16, 13, 16, 9], [3, 4, 13, 18, 13, 2, 3, 13, 2, 3, 13, 4], [0, 14, 13, 13, 0, 15, 10, 8, 2, 11, 2, 3], [11, 0, 11, 11, 5, 0, 7, 11, 2, 19, 4, 6], [0, 6, 3, 0, 9, 11, 0, 19, 7, 4, 5, 14], [3, 15, 11, 8, 4, 0, 6, 11, 10, 15, 9, 9]]]) == 167\nassert my_solution.matrixSum(*[[[5, 18, 2, 3, 17, 18, 9, 5, 12, 4, 4], [7, 10, 16, 7, 7, 5, 9, 11, 13, 1, 4], [19, 0, 12, 2, 2, 4, 13, 9, 17, 13, 4], [18, 13, 9, 20, 11, 2, 7, 14, 20, 11, 20], [16, 1, 12, 13, 0, 13, 10, 14, 6, 11, 9], [15, 2, 5, 3, 8, 3, 17, 19, 4, 14, 12], [5, 13, 13, 5, 7, 14, 10, 16, 4, 11, 14], [20, 20, 2, 15, 6, 9, 0, 14, 19, 14, 0], [6, 9, 3, 20, 9, 17, 19, 4, 13, 15, 2], [15, 7, 17, 12, 8, 20, 0, 3, 8, 1, 0], [8, 12, 16, 18, 12, 14, 3, 8, 11, 9, 6], [19, 2, 1, 2, 8, 9, 17, 10, 3, 16, 7], [5, 2, 13, 9, 9, 16, 4, 18, 16, 20, 6], [17, 3, 13, 20, 17, 12, 8, 9, 14, 11, 18], [20, 4, 5, 3, 3, 12, 12, 18, 14, 4, 17], [9, 11, 20, 15, 13, 6, 15, 15, 16, 10, 15], [20, 1, 14, 9, 4, 15, 1, 19, 6, 0, 11], [15, 12, 0, 16, 2, 2, 12, 0, 11, 1, 3]]]) == 157\nassert my_solution.matrixSum(*[[[8], [20], [9], [7], [4], [18], [9], [6], [3], [13], [14], [10], [12], [5], [10], [13], [20], [13], [4], [14]]]) == 20\nassert my_solution.matrixSum(*[[[10, 4, 6, 5, 14, 11, 12, 13, 15, 12, 7, 6, 14, 6, 18, 1, 12], [1, 7, 20, 2, 5, 11, 1, 20, 5, 7, 19, 9, 19, 2, 16, 9, 11], [13, 14, 1, 20, 16, 20, 17, 13, 18, 14, 15, 8, 15, 6, 10, 8, 1], [1, 1, 5, 11, 0, 9, 20, 0, 4, 2, 13, 7, 19, 12, 17, 7, 14], [6, 10, 19, 3, 19, 2, 10, 10, 17, 14, 10, 8, 0, 16, 1, 6, 11]]]) == 215\nassert my_solution.matrixSum(*[[[16, 11, 6, 6, 8, 9, 9], [16, 12, 8, 15, 11, 7, 1], [9, 17, 2, 0, 14, 15, 14]]]) == 82\nassert my_solution.matrixSum(*[[[15, 14, 14, 3, 2, 2, 7, 3, 4, 13, 6, 14, 19, 2], [13, 17, 12, 1, 5, 7, 15, 7, 4, 8, 11, 10, 13, 3]]]) == 135\nassert my_solution.matrixSum(*[[[19, 17, 6, 9, 14, 16, 19, 14, 17, 20], [8, 8, 7, 0, 3, 19, 3, 5, 13, 7], [6, 9, 0, 17, 16, 13, 1, 3, 12, 20], [8, 3, 18, 11, 7, 17, 9, 7, 7, 2], [16, 9, 10, 7, 11, 20, 15, 9, 18, 5], [4, 0, 17, 16, 10, 11, 18, 20, 0, 4], [12, 4, 5, 16, 2, 4, 6, 15, 18, 6], [7, 4, 7, 12, 11, 19, 18, 4, 20, 15], [3, 19, 0, 16, 19, 11, 15, 14, 9, 0], [7, 17, 20, 5, 15, 15, 17, 10, 2, 8], [4, 19, 12, 6, 10, 9, 12, 1, 6, 1], [10, 7, 10, 14, 7, 8, 11, 5, 9, 0], [11, 18, 17, 1, 20, 4, 11, 0, 15, 20], [1, 0, 7, 1, 0, 7, 20, 10, 2, 1], [11, 13, 4, 6, 14, 13, 4, 11, 9, 5], [20, 10, 13, 12, 0, 13, 8, 17, 17, 14], [1, 18, 3, 13, 12, 5, 0, 16, 4, 19], [16, 4, 2, 10, 7, 5, 7, 0, 5, 17]]]) == 152\nassert my_solution.matrixSum(*[[[13, 19, 3], [19, 20, 14], [4, 19, 19], [1, 8, 10], [12, 0, 20], [1, 15, 2]]]) == 53\nassert my_solution.matrixSum(*[[[4, 14, 7, 16, 11, 5, 12, 10, 8, 15], [12, 0, 9, 16, 9, 17, 15, 1, 17, 18], [17, 8, 1, 14, 12, 3, 12, 11, 15, 1], [0, 8, 1, 8, 18, 9, 6, 16, 16, 10], [12, 8, 6, 3, 18, 10, 7, 18, 17, 11], [5, 4, 10, 0, 18, 1, 18, 4, 11, 11], [9, 20, 9, 10, 15, 12, 19, 13, 5, 0], [4, 18, 1, 14, 4, 10, 0, 15, 8, 19], [6, 2, 17, 13, 8, 5, 16, 5, 2, 20], [5, 18, 3, 16, 20, 17, 19, 12, 13, 8], [9, 9, 0, 13, 8, 8, 17, 16, 17, 10], [10, 6, 13, 4, 0, 16, 4, 18, 12, 11]]]) == 136\nassert my_solution.matrixSum(*[[[17, 13], [20, 19], [7, 0], [11, 16], [5, 6], [20, 11], [20, 15], [0, 7], [18, 7], [8, 5], [13, 2], [18, 14], [7, 14], [16, 3], [6, 5]]]) == 39\nassert my_solution.matrixSum(*[[[13, 20, 12], [8, 8, 13], [11, 19, 10], [2, 9, 0], [15, 0, 8], [6, 12, 12], [13, 20, 20], [12, 1, 18], [14, 11, 18], [4, 18, 8], [10, 0, 12], [15, 16, 4], [1, 2, 18], [11, 11, 0], [2, 6, 3]]]) == 53\nassert my_solution.matrixSum(*[[[1, 10, 0, 10, 12, 4, 20, 8, 13, 4, 19, 4, 12, 9, 16, 1], [5, 4, 4, 15, 20, 1, 16, 1, 17, 20, 12, 5, 11, 18, 2, 2], [17, 5, 8, 6, 8, 10, 8, 8, 16, 14, 4, 14, 17, 4, 1, 20], [18, 5, 20, 16, 1, 10, 2, 6, 20, 14, 19, 7, 14, 16, 5, 10], [20, 4, 15, 17, 1, 17, 2, 16, 10, 0, 2, 3, 13, 19, 11, 18], [14, 10, 12, 16, 11, 20, 3, 4, 10, 0, 5, 4, 0, 10, 19, 6], [1, 13, 20, 1, 9, 12, 1, 19, 5, 11, 2, 13, 1, 14, 11, 19], [19, 8, 9, 18, 14, 13, 5, 0, 14, 8, 6, 12, 2, 11, 3, 3], [0, 11, 0, 5, 12, 19, 20, 0, 10, 14, 17, 15, 15, 2, 1, 17], [12, 8, 0, 20, 16, 3, 2, 10, 11, 3, 15, 1, 4, 16, 20, 9], [1, 0, 12, 10, 5, 17, 4, 5, 5, 5, 20, 7, 19, 10, 0, 15], [20, 12, 1, 7, 7, 20, 9, 10, 5, 7, 11, 7, 5, 13, 14, 11], [18, 4, 12, 18, 0, 5, 18, 0, 12, 5, 4, 0, 0, 10, 16, 0], [1, 1, 3, 18, 19, 4, 4, 12, 0, 13, 13, 18, 10, 7, 4, 11], [16, 6, 17, 12, 2, 4, 13, 18, 11, 3, 13, 13, 7, 15, 12, 7], [12, 7, 20, 15, 12, 18, 2, 8, 8, 16, 11, 17, 0, 19, 19, 15], [7, 8, 1, 19, 10, 12, 20, 3, 17, 1, 4, 12, 0, 13, 12, 13], [3, 14, 19, 14, 5, 5, 10, 15, 13, 5, 18, 9, 16, 3, 11, 11]]]) == 206\nassert my_solution.matrixSum(*[[[15, 20, 16, 1, 6, 8, 5, 18, 10, 9, 12, 20], [1, 9, 17, 13, 1, 13, 8, 12, 6, 11, 4, 4], [3, 19, 11, 5, 0, 13, 18, 12, 14, 2, 20, 2], [13, 17, 17, 4, 5, 4, 6, 5, 7, 0, 16, 4], [15, 11, 19, 9, 10, 3, 13, 8, 5, 10, 20, 16]]]) == 147\nassert my_solution.matrixSum(*[[[4, 12, 17, 9, 7, 12, 11, 12, 15, 16, 9, 13, 13, 3, 4, 4, 0, 18], [19, 20, 16, 3, 20, 6, 19, 20, 19, 16, 3, 8, 19, 5, 12, 2, 1, 0], [3, 11, 15, 10, 3, 14, 11, 18, 8, 15, 7, 5, 17, 17, 2, 16, 5, 0], [20, 5, 16, 13, 19, 6, 14, 12, 8, 0, 18, 4, 12, 5, 14, 18, 1, 12], [2, 20, 18, 12, 10, 7, 16, 14, 1, 15, 19, 3, 8, 16, 15, 17, 20, 12], [15, 8, 5, 7, 17, 11, 5, 9, 19, 4, 14, 2, 2, 15, 12, 18, 13, 17], [2, 8, 15, 7, 3, 9, 4, 11, 3, 4, 1, 4, 18, 17, 6, 0, 1, 12], [17, 19, 15, 3, 15, 17, 11, 10, 3, 0, 7, 7, 15, 9, 8, 7, 0, 15], [11, 16, 10, 2, 2, 9, 18, 13, 7, 11, 12, 4, 7, 9, 6, 15, 11, 9], [12, 19, 4, 3, 12, 11, 16, 15, 7, 20, 11, 20, 11, 13, 8, 11, 12, 9], [3, 15, 9, 3, 14, 4, 11, 7, 5, 10, 15, 7, 3, 12, 12, 14, 16, 14]]]) == 248\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2728", "questionFrontendId": "2679", "questionTitle": "Sum in a Matrix", "stats": { "totalAccepted": "26K", "totalSubmission": "33.3K", "totalAcceptedRaw": 25981, "totalSubmissionRaw": 33306, "acRate": "78.0%" } }
LeetCode/2730
# Maximum OR You are given a **0-indexed** integer array `nums` of length `n` and an integer `k`. In an operation, you can choose an element and multiply it by `2`. Return *the maximum possible value of* `nums[0] | nums[1] | ... | nums[n - 1]` *that can be obtained after applying the operation on nums at most* `k` *times*. Note that `a | b` denotes the **bitwise or** between two integers `a` and `b`.   **Example 1:** ``` **Input:** nums = [12,9], k = 1 **Output:** 30 **Explanation:** If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30. ``` **Example 2:** ``` **Input:** nums = [8,1,2], k = 2 **Output:** 35 **Explanation:** If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` * `1 <= k <= 15` Please make sure your answer follows the type signature below: ```python3 class Solution: def maximumOr(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumOr(*[[12, 9], 1]) == 30\nassert my_solution.maximumOr(*[[8, 1, 2], 2]) == 35\nassert my_solution.maximumOr(*[[98, 54, 6, 34, 66, 63, 52, 39, 62, 46, 75, 28, 65, 18, 37, 18, 97, 13, 80, 33, 69, 91, 78, 19, 40], 2]) == 511\nassert my_solution.maximumOr(*[[88, 43, 61, 72, 13], 6]) == 5759\nassert my_solution.maximumOr(*[[41, 79, 82, 27, 71, 62, 57, 67, 34, 8, 71, 2, 12, 93, 52, 91, 86, 81, 1, 79, 64, 43, 32, 94, 42, 91, 9, 25], 10]) == 96383\nassert my_solution.maximumOr(*[[31, 19, 70, 58, 12, 11, 41, 66, 63, 14, 39, 71, 38, 91, 16], 9]) == 46719\nassert my_solution.maximumOr(*[[70, 27, 78, 71, 76, 37, 57, 12, 77, 50, 41, 74, 31, 38, 24, 25, 24, 5, 79, 85, 34, 61], 2]) == 383\nassert my_solution.maximumOr(*[[87, 97, 17, 20, 5, 11], 9]) == 49759\nassert my_solution.maximumOr(*[[91, 68, 36, 67, 31, 28, 87, 76, 54, 75, 36, 58, 64, 85, 83, 90, 46, 11, 42, 79, 15, 63, 76, 81, 43, 25], 4]) == 1535\nassert my_solution.maximumOr(*[[94, 35], 2]) == 379\nassert my_solution.maximumOr(*[[48, 22, 43, 55, 8, 13, 19, 90, 29, 6, 74, 82, 69, 78, 88], 2]) == 383\nassert my_solution.maximumOr(*[[16, 82], 4]) == 1328\nassert my_solution.maximumOr(*[[74, 16, 51, 12, 48, 15, 5, 78, 3, 25, 24, 92, 16, 62, 27, 94, 8, 87, 3, 70, 55, 80, 13, 34, 9, 29, 10, 83, 39, 45, 56, 24, 8, 65, 60, 6, 77, 13, 90], 7]) == 12159\nassert my_solution.maximumOr(*[[34, 46, 94, 61, 73, 22, 90, 87, 27, 99, 8, 87, 21], 3]) == 895\nassert my_solution.maximumOr(*[[68, 33, 16, 77, 57, 86, 23, 2, 61, 88, 53, 73, 66, 40, 84, 46, 50, 85, 33, 20, 72, 89], 1]) == 255\nassert my_solution.maximumOr(*[[95, 11, 43, 95, 6, 70, 36, 18, 31, 98, 62, 46, 79, 37, 87, 46, 76, 82, 80, 17, 92, 40, 50, 96, 54, 84, 11, 1, 77, 25], 6]) == 6399\nassert my_solution.maximumOr(*[[31, 29, 82, 58, 49, 91, 87, 73, 54, 5, 52], 10]) == 93311\nassert my_solution.maximumOr(*[[99, 85, 91, 6, 22, 58, 9, 34, 90, 21, 58, 68, 63, 72, 78, 97, 1, 5, 64, 42, 40, 60, 7, 54, 25, 71, 82], 2]) == 511\nassert my_solution.maximumOr(*[[2, 52, 87, 54, 41, 1, 28, 2, 92], 1]) == 255\nassert my_solution.maximumOr(*[[79, 13, 25, 16, 78, 84, 26, 39, 36, 89, 24, 13, 61, 51, 81, 11, 3, 36, 58, 15, 33, 18, 84, 67, 84, 83, 45, 15, 20, 36, 3, 6, 6, 27], 5]) == 2943\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2730", "questionFrontendId": "2680", "questionTitle": "Maximum OR", "stats": { "totalAccepted": "3.5K", "totalSubmission": "8K", "totalAcceptedRaw": 3508, "totalSubmissionRaw": 8048, "acRate": "43.6%" } }
LeetCode/2784
# Power of Heroes You are given a **0-indexed** integer array `nums` representing the strength of some heroes. The **power** of a group of heroes is defined as follows: * Let `i0`, `i1`, ... ,`ik` be the indices of the heroes in a group. Then, the power of this group is `max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik])`. Return *the sum of the **power** of all **non-empty** groups of heroes possible.* Since the sum could be very large, return it **modulo** `109 + 7`.   **Example 1:** ``` **Input:** nums = [2,1,4] **Output:** 141 **Explanation:** 1st group: [2] has power = 22 * 2 = 8. 2nd group: [1] has power = 12 * 1 = 1. 3rd group: [4] has power = 42 * 4 = 64. 4th group: [2,1] has power = 22 * 1 = 4. 5th group: [2,4] has power = 42 * 2 = 32. 6th group: [1,4] has power = 42 * 1 = 16. ​​​​​​​7th group: [2,1,4] has power = 42​​​​​​​ * 1 = 16. The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141. ``` **Example 2:** ``` **Input:** nums = [1,1,1] **Output:** 7 **Explanation:** A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7. ```   **Constraints:** * `1 <= nums.length <= 105` * `1 <= nums[i] <= 109` Please make sure your answer follows the type signature below: ```python3 class Solution: def sumOfPower(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumOfPower(*[[2, 1, 4]]) == 141\nassert my_solution.sumOfPower(*[[1, 1, 1]]) == 7\nassert my_solution.sumOfPower(*[[4, 4]]) == 192\nassert my_solution.sumOfPower(*[[8, 4]]) == 832\nassert my_solution.sumOfPower(*[[5, 3]]) == 227\nassert my_solution.sumOfPower(*[[6, 1, 4]]) == 513\nassert my_solution.sumOfPower(*[[3, 2, 3]]) == 143\nassert my_solution.sumOfPower(*[[7, 4, 9]]) == 2547\nassert my_solution.sumOfPower(*[[4, 6]]) == 424\nassert my_solution.sumOfPower(*[[2, 6]]) == 296\nassert my_solution.sumOfPower(*[[9, 8, 5]]) == 3144\nassert my_solution.sumOfPower(*[[3, 1, 3]]) == 109\nassert my_solution.sumOfPower(*[[8, 3, 9]]) == 2594\nassert my_solution.sumOfPower(*[[8, 2]]) == 648\nassert my_solution.sumOfPower(*[[3, 5]]) == 227\nassert my_solution.sumOfPower(*[[8, 4, 7]]) == 2075\nassert my_solution.sumOfPower(*[[3, 4]]) == 139\nassert my_solution.sumOfPower(*[[8, 7]]) == 1303\nassert my_solution.sumOfPower(*[[6, 6]]) == 648\nassert my_solution.sumOfPower(*[[2, 4, 6]]) == 608\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2784", "questionFrontendId": "2681", "questionTitle": "Power of Heroes", "stats": { "totalAccepted": "18.7K", "totalSubmission": "44.8K", "totalAcceptedRaw": 18652, "totalSubmissionRaw": 44844, "acRate": "41.6%" } }
LeetCode/2777
# Find the Distinct Difference Array You are given a **0-indexed** array `nums` of length `n`. The **distinct difference** array of `nums` is an array `diff` of length `n` such that `diff[i]` is equal to the number of distinct elements in the suffix `nums[i + 1, ..., n - 1]` **subtracted from** the number of distinct elements in the prefix `nums[0, ..., i]`. Return *the **distinct difference** array of* `nums`. Note that `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j` inclusive. Particularly, if `i > j` then `nums[i, ..., j]` denotes an empty subarray.   **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** [-3,-1,1,3,5] **Explanation:** For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3. For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1. For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1. For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3. For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5. ``` **Example 2:** ``` **Input:** nums = [3,2,3,4,2] **Output:** [-2,-1,0,2,3] **Explanation:** For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2. For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1. For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0. For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2. For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3. ```   **Constraints:** * `1 <= n == nums.length <= 50` * `1 <= nums[i] <= 50` Please make sure your answer follows the type signature below: ```python3 class Solution: def distinctDifferenceArray(self, nums: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.distinctDifferenceArray(*[[1, 2, 3, 4, 5]]) == [-3, -1, 1, 3, 5]\nassert my_solution.distinctDifferenceArray(*[[3, 2, 3, 4, 2]]) == [-2, -1, 0, 2, 3]\nassert my_solution.distinctDifferenceArray(*[[16]]) == [1]\nassert my_solution.distinctDifferenceArray(*[[16, 16]]) == [0, 1]\nassert my_solution.distinctDifferenceArray(*[[13, 13, 13]]) == [0, 0, 1]\nassert my_solution.distinctDifferenceArray(*[[36, 36, 36, 36]]) == [0, 0, 0, 1]\nassert my_solution.distinctDifferenceArray(*[[40, 40, 40, 40, 40]]) == [0, 0, 0, 0, 1]\nassert my_solution.distinctDifferenceArray(*[[37, 37, 37, 37, 33]]) == [-1, -1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[42, 42, 42, 42, 47]]) == [-1, -1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[9, 9, 9, 9, 31]]) == [-1, -1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[14, 14, 14, 14, 5]]) == [-1, -1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[41, 41, 41, 17]]) == [-1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[9, 9, 9, 30, 9]]) == [-1, -1, -1, 1, 2]\nassert my_solution.distinctDifferenceArray(*[[21, 21, 21, 4, 4]]) == [-1, -1, 0, 1, 2]\nassert my_solution.distinctDifferenceArray(*[[38, 38, 38, 14, 19]]) == [-2, -2, -1, 1, 3]\nassert my_solution.distinctDifferenceArray(*[[11, 11, 11, 7, 13]]) == [-2, -2, -1, 1, 3]\nassert my_solution.distinctDifferenceArray(*[[36, 36, 36, 18, 27]]) == [-2, -2, -1, 1, 3]\nassert my_solution.distinctDifferenceArray(*[[37, 37, 37, 47]]) == [-1, -1, 0, 2]\nassert my_solution.distinctDifferenceArray(*[[13, 13, 13, 34, 13]]) == [-1, -1, -1, 1, 2]\nassert my_solution.distinctDifferenceArray(*[[39, 39, 39, 38, 1]]) == [-2, -2, -1, 1, 3]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2777", "questionFrontendId": "2670", "questionTitle": "Find the Distinct Difference Array", "stats": { "totalAccepted": "7.6K", "totalSubmission": "9.9K", "totalAcceptedRaw": 7588, "totalSubmissionRaw": 9899, "acRate": "76.7%" } }
LeetCode/2779
# Number of Adjacent Elements With the Same Color There is a **0-indexed** array `nums` of length `n`. Initially, all elements are **uncolored** (has a value of `0`). You are given a 2D integer array `queries` where `queries[i] = [indexi, colori]`. For each query, you color the index `indexi` with the color `colori` in the array `nums`. Return *an array* `answer` *of the same length as* `queries` *where* `answer[i]` *is the number of adjacent elements with the same color **after** the* `ith` *query*. More formally, `answer[i]` is the number of indices `j`, such that `0 <= j < n - 1` and `nums[j] == nums[j + 1]` and `nums[j] != 0` after the `ith` query.   **Example 1:** ``` **Input:** n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]] **Output:** [0,1,1,0,2] **Explanation:** Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array. - After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0. - After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1. - After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1. - After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0. - After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2. ``` **Example 2:** ``` **Input:** n = 1, queries = [[0,100000]] **Output:** [0] **Explanation:** Initially array nums = [0], where 0 denotes uncolored elements of the array. - After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0. ```   **Constraints:** * `1 <= n <= 105` * `1 <= queries.length <= 105` * `queries[i].length == 2` * `0 <= indexi <= n - 1` * `1 <=  colori <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.colorTheArray(*[4, [[0, 2], [1, 2], [3, 1], [1, 1], [2, 1]]]) == [0, 1, 1, 0, 2]\nassert my_solution.colorTheArray(*[1, [[0, 100000]]]) == [0]\nassert my_solution.colorTheArray(*[8, [[6, 2], [2, 1], [0, 6], [0, 1], [0, 4], [0, 1], [5, 7], [5, 3], [7, 6], [6, 7], [0, 4], [4, 6], [4, 2], [3, 7], [4, 4], [5, 1]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[18, [[9, 5], [14, 15], [8, 14], [0, 4], [10, 19], [13, 11], [11, 18], [8, 15], [17, 9], [10, 1], [17, 8], [9, 13], [2, 17], [0, 10], [10, 15], [10, 19], [1, 13], [7, 1], [2, 7], [13, 16], [2, 12], [1, 19], [0, 9], [4, 1], [1, 7], [3, 18], [10, 7], [13, 2], [13, 9], [0, 17], [14, 11], [12, 7], [12, 18], [16, 15], [16, 13], [7, 12], [15, 12], [7, 18], [15, 16], [15, 6]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]\nassert my_solution.colorTheArray(*[17, [[11, 3], [5, 1], [16, 2], [4, 4], [5, 1], [13, 2], [15, 1], [15, 3], [8, 1], [14, 4], [1, 3], [6, 2], [8, 2], [2, 2], [3, 4], [7, 1], [10, 2], [14, 3], [6, 5], [3, 5], [5, 5], [9, 2], [2, 3], [3, 3], [4, 1], [12, 1], [0, 4], [16, 4], [8, 1], [14, 3], [15, 3], [12, 1], [11, 5], [3, 1], [2, 4], [10, 1], [14, 5], [15, 5], [5, 2], [8, 1], [6, 5], [10, 2]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 1, 2, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 3, 4, 3, 3, 3, 4]\nassert my_solution.colorTheArray(*[15, [[10, 2], [12, 1], [7, 1], [11, 1], [5, 3], [14, 3], [12, 2], [14, 3], [3, 2], [13, 3], [11, 1], [2, 2], [2, 1], [4, 2]]]) == [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 2, 1, 2]\nassert my_solution.colorTheArray(*[9, [[7, 3], [7, 5], [2, 13], [0, 15], [7, 8], [8, 2], [7, 7], [4, 2], [2, 13]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[11, [[1, 14], [4, 15], [0, 13], [3, 19]]]) == [0, 0, 0, 0]\nassert my_solution.colorTheArray(*[7, [[4, 5], [2, 2], [1, 1], [4, 3], [4, 2], [5, 3], [6, 5], [0, 5], [1, 6], [2, 2], [5, 3], [3, 4], [6, 5], [2, 4], [3, 6], [5, 6], [2, 5], [6, 4]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[4, [[2, 14], [3, 8], [1, 13], [0, 3], [1, 11], [1, 15], [1, 11], [1, 5], [0, 8], [1, 15], [3, 1]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[9, [[1, 19], [7, 19], [0, 7], [6, 9], [0, 18], [7, 6], [4, 16], [2, 13], [5, 9], [2, 1], [2, 3], [7, 10], [5, 1], [4, 19], [7, 14], [6, 19], [0, 5], [6, 5], [1, 8], [3, 13], [1, 9], [4, 13], [3, 17], [5, 8], [1, 5], [7, 11]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1]\nassert my_solution.colorTheArray(*[7, [[2, 15], [6, 19], [6, 12], [1, 20], [1, 10], [1, 3], [1, 17], [3, 19], [1, 8], [6, 16], [4, 4], [3, 17], [3, 2], [3, 5], [0, 18], [0, 14], [3, 6], [2, 9], [1, 8], [2, 4], [6, 7], [5, 1], [3, 5], [4, 1], [2, 2], [0, 1], [4, 17], [0, 11], [4, 10], [1, 2], [0, 15], [2, 14], [6, 1]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1]\nassert my_solution.colorTheArray(*[1, [[0, 4], [0, 5], [0, 2], [0, 2], [0, 7], [0, 1], [0, 3], [0, 2], [0, 2], [0, 8], [0, 5], [0, 6], [0, 4], [0, 7], [0, 6], [0, 2], [0, 2], [0, 8], [0, 1], [0, 4], [0, 6], [0, 7], [0, 6], [0, 3], [0, 7], [0, 4], [0, 4], [0, 4], [0, 8], [0, 4], [0, 6], [0, 7], [0, 2], [0, 3], [0, 4], [0, 5], [0, 3]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[11, [[3, 2], [4, 1], [4, 2], [2, 2], [7, 2], [5, 1], [9, 1], [0, 1], [2, 1], [2, 2], [4, 2], [2, 2], [8, 1], [7, 2], [1, 2], [6, 1], [4, 2], [9, 2], [8, 2], [4, 2], [7, 2], [0, 2], [6, 2], [4, 1], [0, 2], [5, 2], [4, 2], [8, 2], [10, 2], [1, 2], [4, 2], [6, 2], [8, 1], [7, 1]]]) == [0, 0, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 3, 3, 4, 5, 5, 4, 6, 6, 6, 7, 7, 7, 7, 7, 9, 9, 10, 10, 10, 10, 8, 8]\nassert my_solution.colorTheArray(*[20, [[14, 8], [14, 11], [17, 5], [1, 3], [3, 6], [16, 3], [11, 4]]]) == [0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[14, [[7, 14], [10, 18], [11, 6], [2, 18], [9, 15], [4, 1], [1, 16], [6, 6], [9, 9], [0, 16], [3, 9], [11, 9], [0, 14], [5, 3], [8, 18], [10, 11], [7, 15], [11, 4], [11, 12], [8, 9], [11, 3], [3, 11], [9, 1], [7, 16], [9, 17], [8, 18], [3, 15], [13, 17], [10, 14], [4, 10], [1, 13], [11, 3], [5, 14], [4, 18], [3, 2], [6, 3], [6, 7], [11, 4], [5, 14], [2, 3], [13, 8], [4, 8], [6, 3], [9, 6], [5, 4], [12, 7], [9, 7], [8, 1], [12, 2]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[16, [[12, 3], [9, 9], [11, 13], [6, 7], [10, 15], [11, 4], [9, 13], [2, 4], [3, 15], [12, 2], [0, 1], [0, 7], [3, 12], [1, 8], [2, 13], [14, 11], [0, 13], [4, 13], [13, 15], [6, 6]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[6, [[0, 2], [5, 2], [4, 1], [0, 2], [3, 1], [4, 1], [2, 2], [3, 1], [2, 1], [2, 1], [4, 1], [3, 1], [3, 1], [2, 1], [3, 1], [1, 1], [1, 2], [2, 1], [5, 2], [5, 2], [3, 2], [2, 1], [1, 2], [4, 1], [2, 2], [5, 2], [0, 2], [4, 1], [1, 1], [5, 1], [1, 1], [3, 1], [4, 1], [5, 1], [1, 1], [4, 2], [1, 2], [1, 2], [2, 1], [5, 2], [1, 1], [1, 2], [4, 2], [3, 1], [4, 2], [5, 1], [3, 1], [2, 1], [5, 2], [2, 2]]]) == [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 3, 3, 3, 3, 1, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3]\nassert my_solution.colorTheArray(*[6, [[4, 8], [3, 8], [4, 8], [4, 6], [0, 8], [5, 6], [0, 2], [1, 6], [1, 1], [4, 5], [3, 7], [1, 7], [5, 6], [2, 4], [2, 2], [3, 1], [3, 7], [2, 6], [3, 4], [5, 1], [4, 6], [3, 8], [3, 1], [5, 1], [5, 4], [4, 9], [0, 2], [5, 6], [1, 6], [5, 8], [5, 9], [1, 5], [2, 7], [0, 7], [4, 7], [3, 3], [2, 1], [3, 4]]]) == [0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 0, 0, 0, 0]\nassert my_solution.colorTheArray(*[16, [[1, 1], [2, 6], [13, 4], [2, 4], [12, 7], [3, 16], [14, 12], [0, 6], [5, 10], [9, 14], [3, 12], [6, 17]]]) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2779", "questionFrontendId": "2672", "questionTitle": "Number of Adjacent Elements With the Same Color", "stats": { "totalAccepted": "5.3K", "totalSubmission": "9K", "totalAcceptedRaw": 5287, "totalSubmissionRaw": 9036, "acRate": "58.5%" } }
LeetCode/2684
# Determine the Winner of a Bowling Game You are given two **0-indexed** integer arrays `player1` and `player2`, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling game consists of `n` turns, and the number of pins in each turn is exactly `10`. Assume a player hit `xi` pins in the `ith` turn. The value of the `ith` turn for the player is: * `2xi` if the player hit `10` pins in any of the previous two turns. * Otherwise, It is `xi`. The score of the player is the sum of the values of their `n` turns. Return * `1` *if the score of player 1 is more than the score of player 2,* * `2` *if the score of player 2 is more than the score of player 1, and* * `0` *in case of a draw.*   **Example 1:** ``` **Input:** player1 = [4,10,7,9], player2 = [6,5,2,3] **Output:** 1 **Explanation:** The score of player1 is 4 + 10 + 2*7 + 2*9 = 46. The score of player2 is 6 + 5 + 2 + 3 = 16. Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1. ``` **Example 2:** ``` **Input:** player1 = [3,5,7,6], player2 = [8,10,10,2] **Output:** 2 **Explanation:** The score of player1 is 3 + 5 + 7 + 6 = 21. The score of player2 is 8 + 10 + 2*10 + 2*2 = 42. Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2. ``` **Example 3:** ``` **Input:** player1 = [2,3], player2 = [4,1] **Output:** 0 **Explanation:** The score of player1 is 2 + 3 = 5 The score of player2 is 4 + 1 = 5 The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0. ```   **Constraints:** * `n == player1.length == player2.length` * `1 <= n <= 1000` * `0 <= player1[i], player2[i] <= 10` Please make sure your answer follows the type signature below: ```python3 class Solution: def isWinner(self, player1: List[int], player2: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.isWinner(*[[4, 10, 7, 9], [6, 5, 2, 3]]) == 1\nassert my_solution.isWinner(*[[3, 5, 7, 6], [8, 10, 10, 2]]) == 2\nassert my_solution.isWinner(*[[2, 3], [4, 1]]) == 0\nassert my_solution.isWinner(*[[0, 4, 7, 2, 0], [2, 3, 3, 0, 1]]) == 1\nassert my_solution.isWinner(*[[5, 6, 1, 10], [5, 1, 10, 5]]) == 2\nassert my_solution.isWinner(*[[0, 9, 2, 0], [4, 6, 9, 1]]) == 2\nassert my_solution.isWinner(*[[7, 3, 6, 7], [4, 2, 6, 7]]) == 1\nassert my_solution.isWinner(*[[6, 10, 4], [5, 9, 2]]) == 1\nassert my_solution.isWinner(*[[9, 8, 5, 3, 7], [8, 7, 4, 9, 0]]) == 1\nassert my_solution.isWinner(*[[2, 8, 10, 6], [8, 0, 4, 2]]) == 1\nassert my_solution.isWinner(*[[3, 2, 6, 6, 2], [1, 4, 5, 2, 5]]) == 1\nassert my_solution.isWinner(*[[0, 1, 8], [3, 9, 1]]) == 2\nassert my_solution.isWinner(*[[5, 2, 4, 2, 0], [6, 1, 5, 2, 6]]) == 2\nassert my_solution.isWinner(*[[3, 6, 9], [0, 7, 4]]) == 1\nassert my_solution.isWinner(*[[1, 2, 7, 6], [6, 7, 0, 0]]) == 1\nassert my_solution.isWinner(*[[7, 5, 10, 5], [2, 5, 7, 10]]) == 1\nassert my_solution.isWinner(*[[9, 6, 10, 0], [4, 4, 0, 5]]) == 1\nassert my_solution.isWinner(*[[7, 1, 3, 10], [10, 9, 0, 9]]) == 2\nassert my_solution.isWinner(*[[7], [8]]) == 2\nassert my_solution.isWinner(*[[1, 3, 0], [2, 0, 0]]) == 1\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2684", "questionFrontendId": "2660", "questionTitle": "Determine the Winner of a Bowling Game", "stats": { "totalAccepted": "27K", "totalSubmission": "58.4K", "totalAcceptedRaw": 26998, "totalSubmissionRaw": 58387, "acRate": "46.2%" } }
LeetCode/2686
# Minimum Cost of a Path With Special Roads You are given an array `start` where `start = [startX, startY]` represents your initial position `(startX, startY)` in a 2D space. You are also given the array `target` where `target = [targetX, targetY]` represents your target position `(targetX, targetY)`. The cost of going from a position `(x1, y1)` to any other position in the space `(x2, y2)` is `|x2 - x1| + |y2 - y1|`. There are also some special roads. You are given a 2D array `specialRoads` where `specialRoads[i] = [x1i, y1i, x2i, y2i, costi]` indicates that the `ith` special road can take you from `(x1i, y1i)` to `(x2i, y2i)` with a cost equal to `costi`. You can use each special road any number of times. Return *the minimum cost required to go from* `(startX, startY)` to `(targetX, targetY)`.   **Example 1:** ``` **Input:** start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]] **Output:** 5 **Explanation:** The optimal path from (1,1) to (4,5) is the following: - (1,1) -> (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1. - (1,2) -> (3,3). This move uses the first special edge, the cost is 2. - (3,3) -> (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1. - (3,4) -> (4,5). This move uses the second special edge, the cost is 1. So the total cost is 1 + 2 + 1 + 1 = 5. It can be shown that we cannot achieve a smaller total cost than 5. ``` **Example 2:** ``` **Input:** start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]] **Output:** 7 **Explanation:** It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7. ```   **Constraints:** * `start.length == target.length == 2` * `1 <= startX <= targetX <= 105` * `1 <= startY <= targetY <= 105` * `1 <= specialRoads.length <= 200` * `specialRoads[i].length == 5` * `startX <= x1i, x2i <= targetX` * `startY <= y1i, y2i <= targetY` * `1 <= costi <= 105` Please make sure your answer follows the type signature below: ```python3 class Solution: def minimumCost(self, start: List[int], target: List[int], specialRoads: List[List[int]]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 1], [4, 5], [[1, 2, 3, 3, 2], [3, 4, 4, 5, 1]]]) == 5\nassert my_solution.minimumCost(*[[3, 2], [5, 7], [[3, 2, 3, 4, 4], [3, 3, 5, 5, 5], [3, 4, 5, 6, 6]]]) == 7\nassert my_solution.minimumCost(*[[1, 1], [5, 10], [[3, 4, 5, 2, 5], [4, 5, 3, 8, 3], [3, 2, 5, 3, 1]]]) == 11\nassert my_solution.minimumCost(*[[1, 1], [4, 6], [[3, 4, 2, 4, 1], [2, 5, 4, 2, 5], [3, 2, 1, 6, 3]]]) == 8\nassert my_solution.minimumCost(*[[1, 1], [10, 6], [[4, 3, 6, 4, 5], [8, 1, 4, 6, 2], [8, 5, 5, 6, 2], [1, 5, 1, 4, 2]]]) == 14\nassert my_solution.minimumCost(*[[1, 1], [10, 4], [[4, 2, 1, 1, 3], [1, 2, 7, 4, 4], [10, 3, 6, 1, 2], [6, 1, 1, 2, 3]]]) == 8\nassert my_solution.minimumCost(*[[1, 1], [9, 3], [[6, 3, 9, 1, 1], [7, 1, 6, 3, 1], [7, 3, 4, 2, 2], [3, 3, 1, 1, 2]]]) == 10\nassert my_solution.minimumCost(*[[1, 1], [9, 5], [[7, 4, 1, 5, 3], [7, 1, 4, 4, 1], [9, 4, 8, 4, 1], [4, 4, 3, 2, 1], [5, 1, 2, 3, 2]]]) == 12\nassert my_solution.minimumCost(*[[1, 1], [8, 8], [[4, 2, 2, 3, 1], [2, 2, 3, 4, 1], [5, 5, 2, 8, 4], [2, 6, 1, 6, 5]]]) == 12\nassert my_solution.minimumCost(*[[1, 1], [4, 6], [[4, 3, 4, 3, 3], [2, 4, 3, 4, 4], [4, 6, 1, 3, 5], [1, 5, 1, 2, 4], [1, 2, 3, 4, 1]]]) == 5\nassert my_solution.minimumCost(*[[1, 1], [3, 4], [[1, 2, 3, 2, 3], [3, 1, 3, 2, 4], [3, 2, 1, 3, 2]]]) == 5\nassert my_solution.minimumCost(*[[1, 1], [9, 5], [[8, 4, 6, 5, 1], [7, 4, 9, 2, 3], [8, 5, 3, 2, 1], [5, 2, 8, 2, 4], [1, 5, 9, 2, 3]]]) == 10\nassert my_solution.minimumCost(*[[1, 1], [10, 9], [[5, 2, 3, 6, 3], [5, 6, 9, 5, 3], [5, 9, 1, 2, 5], [8, 6, 9, 8, 1]]]) == 15\nassert my_solution.minimumCost(*[[1, 1], [7, 8], [[1, 6, 7, 2, 2]]]) == 13\nassert my_solution.minimumCost(*[[1, 1], [8, 5], [[1, 3, 8, 5, 2], [6, 5, 3, 3, 1], [4, 3, 3, 1, 4], [2, 3, 8, 3, 5]]]) == 4\nassert my_solution.minimumCost(*[[1, 1], [8, 8], [[3, 3, 4, 2, 4], [4, 2, 7, 1, 1], [3, 3, 7, 7, 3]]]) == 9\nassert my_solution.minimumCost(*[[1, 1], [8, 9], [[8, 9, 8, 4, 3], [2, 4, 8, 1, 4], [7, 6, 5, 9, 4], [4, 6, 5, 4, 1]]]) == 15\nassert my_solution.minimumCost(*[[1, 1], [8, 10], [[1, 10, 5, 9, 2], [2, 3, 2, 4, 4], [7, 9, 4, 5, 1], [3, 1, 4, 6, 4]]]) == 14\nassert my_solution.minimumCost(*[[1, 1], [9, 9], [[8, 3, 2, 4, 2], [3, 8, 5, 4, 5], [1, 8, 7, 7, 4], [1, 6, 9, 5, 2]]]) == 11\nassert my_solution.minimumCost(*[[1, 1], [5, 9], [[4, 9, 1, 2, 3]]]) == 12\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2686", "questionFrontendId": "2662", "questionTitle": "Minimum Cost of a Path With Special Roads", "stats": { "totalAccepted": "3.9K", "totalSubmission": "10.2K", "totalAcceptedRaw": 3874, "totalSubmissionRaw": 10238, "acRate": "37.8%" } }
LeetCode/2687
# Lexicographically Smallest Beautiful String A string is **beautiful** if: * It consists of the first `k` letters of the English lowercase alphabet. * It does not contain any substring of length `2` or more which is a palindrome. You are given a beautiful string `s` of length `n` and a positive integer `k`. Return *the lexicographically smallest string of length* `n`*, which is larger than* `s` *and is **beautiful***. If there is no such string, return an empty string. A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`. * For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.   **Example 1:** ``` **Input:** s = "abcz", k = 26 **Output:** "abda" **Explanation:** The string "abda" is beautiful and lexicographically larger than the string "abcz". It can be proven that there is no string that is lexicographically larger than the string "abcz", beautiful, and lexicographically smaller than the string "abda". ``` **Example 2:** ``` **Input:** s = "dc", k = 4 **Output:** "" **Explanation:** It can be proven that there is no string that is lexicographically larger than the string "dc" and is beautiful. ```   **Constraints:** * `1 <= n == s.length <= 105` * `4 <= k <= 26` * `s` is a beautiful string. Please make sure your answer follows the type signature below: ```python3 class Solution: def smallestBeautifulString(self, s: str, k: int) -> str: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.smallestBeautifulString(*['abcz', 26]) == abda\nassert my_solution.smallestBeautifulString(*['dc', 4]) == \nassert my_solution.smallestBeautifulString(*['abc', 8]) == abd\nassert my_solution.smallestBeautifulString(*['dfa', 6]) == dfb\nassert my_solution.smallestBeautifulString(*['dca', 4]) == dcb\nassert my_solution.smallestBeautifulString(*['da', 8]) == db\nassert my_solution.smallestBeautifulString(*['feda', 7]) == fedb\nassert my_solution.smallestBeautifulString(*['b', 6]) == c\nassert my_solution.smallestBeautifulString(*['ced', 6]) == cef\nassert my_solution.smallestBeautifulString(*['gfc', 8]) == gfd\nassert my_solution.smallestBeautifulString(*['facf', 7]) == facg\nassert my_solution.smallestBeautifulString(*['cegaf', 7]) == cegba\nassert my_solution.smallestBeautifulString(*['edg', 7]) == efa\nassert my_solution.smallestBeautifulString(*['acbac', 5]) == acbad\nassert my_solution.smallestBeautifulString(*['fedc', 6]) == fedf\nassert my_solution.smallestBeautifulString(*['cd', 4]) == da\nassert my_solution.smallestBeautifulString(*['fadce', 7]) == fadcf\nassert my_solution.smallestBeautifulString(*['cfb', 6]) == cfd\nassert my_solution.smallestBeautifulString(*['dfce', 6]) == dfea\nassert my_solution.smallestBeautifulString(*['ebga', 8]) == ebgc\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2687", "questionFrontendId": "2663", "questionTitle": "Lexicographically Smallest Beautiful String", "stats": { "totalAccepted": "1.9K", "totalSubmission": "4.2K", "totalAcceptedRaw": 1924, "totalSubmissionRaw": 4196, "acRate": "45.9%" } }
LeetCode/2767
# Maximum Sum With Exactly K Elements You are given a **0-indexed** integer array `nums` and an integer `k`. Your task is to perform the following operation **exactly** `k` times in order to maximize your score: 1. Select an element `m` from `nums`. 2. Remove the selected element `m` from the array. 3. Add a new element with a value of `m + 1` to the array. 4. Increase your score by `m`. Return *the maximum score you can achieve after performing the operation exactly* `k` *times.*   **Example 1:** ``` **Input:** nums = [1,2,3,4,5], k = 3 **Output:** 18 **Explanation:** We need to choose exactly 3 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6] For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7] For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8] So, we will return 18. It can be proven, that 18 is the maximum answer that we can achieve. ``` **Example 2:** ``` **Input:** nums = [5,5,5], k = 2 **Output:** 11 **Explanation:** We need to choose exactly 2 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6] For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7] So, we will return 11. It can be proven, that 11 is the maximum answer that we can achieve. ```   **Constraints:** * `1 <= nums.length <= 100` * `1 <= nums[i] <= 100` * `1 <= k <= 100`   .spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} Please make sure your answer follows the type signature below: ```python3 class Solution: def maximizeSum(self, nums: List[int], k: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximizeSum(*[[1, 2, 3, 4, 5], 3]) == 18\nassert my_solution.maximizeSum(*[[5, 5, 5], 2]) == 11\nassert my_solution.maximizeSum(*[[4, 4, 9, 10, 10, 9, 3, 8, 4, 2, 5, 3, 8, 6, 1, 10, 4, 5, 3, 2, 3, 9, 5, 7, 10, 4, 9, 10, 1, 10, 4], 6]) == 75\nassert my_solution.maximizeSum(*[[1, 10, 2, 6, 7, 9, 8, 5, 8, 3, 1, 3, 10, 5, 8, 3, 9, 3, 8, 2, 3, 3, 10, 5, 8, 8, 4, 7, 2, 3, 4, 3, 10, 8, 7, 9, 1, 3, 4, 2, 6, 6, 9, 6, 2, 10, 10, 4, 6, 3, 4, 1, 1, 3, 8, 4, 10, 3, 9, 5, 3, 10, 4, 7, 10, 7, 1, 7], 9]) == 126\nassert my_solution.maximizeSum(*[[8, 8, 1, 6, 6, 7, 6, 1, 3, 1, 1, 8, 9, 2, 8, 3, 1, 9, 7, 6, 1, 8, 10, 3, 7, 10, 6], 3]) == 33\nassert my_solution.maximizeSum(*[[1, 10, 7, 8, 7, 6, 8, 2, 8, 9, 3, 3, 5, 6, 10, 1, 2, 5, 5, 8, 5, 4, 9, 5, 2, 3, 10, 5, 1, 9, 1, 8, 9, 2, 10, 2, 7, 8, 9, 7, 6, 6, 9, 5, 3, 10, 7, 5, 9, 10, 10, 6, 6, 8, 10, 7, 10, 9, 10, 6, 1, 10, 10, 5, 7, 9, 9, 2, 8, 5, 8, 3, 5], 2]) == 21\nassert my_solution.maximizeSum(*[[9, 7, 10, 5, 2, 3, 9, 9, 5, 10, 10, 5], 3]) == 33\nassert my_solution.maximizeSum(*[[3, 10, 4, 7, 9, 1, 10, 5, 2, 6, 1, 7, 8, 9, 9, 2], 3]) == 33\nassert my_solution.maximizeSum(*[[3, 2, 3, 5, 1, 2, 4, 7, 4, 7, 9, 7, 9], 9]) == 117\nassert my_solution.maximizeSum(*[[2, 10, 9, 9, 8, 5, 6, 4, 7, 6, 3, 9, 4, 2, 10, 5, 9, 7, 7, 3, 10, 9, 7, 4, 3, 1, 1, 1, 1, 1, 7, 1, 2, 6, 6, 6, 8, 7], 1]) == 10\nassert my_solution.maximizeSum(*[[5, 5, 4, 4, 5, 8, 6, 2, 10, 5, 3, 8, 8, 5, 10, 7, 1, 2, 6, 10, 7, 6, 6, 9, 5, 8, 9, 5, 6, 1, 9, 4, 5, 7, 2, 9, 1, 5, 6, 8, 6, 10, 1, 7, 4, 1, 2, 4, 10, 7, 8, 2, 6, 1, 10], 1]) == 10\nassert my_solution.maximizeSum(*[[3, 1, 1, 4, 2, 9, 6, 4, 7, 1, 7, 5, 10, 1, 9, 8, 6, 7, 4, 7, 9, 9, 8, 9, 7, 4, 6, 8, 6, 4, 9, 8, 8], 8]) == 108\nassert my_solution.maximizeSum(*[[6, 5, 3, 1, 9, 4, 3, 8, 3, 2, 7, 3, 8, 2, 8, 6, 5, 2, 8, 8, 1, 6, 4], 4]) == 42\nassert my_solution.maximizeSum(*[[4, 2, 4, 2, 7, 4, 2, 6, 3, 8, 5, 4, 10, 2, 9, 3, 9, 5, 4, 2, 6, 2, 5, 3, 2, 7, 1, 1, 8, 9, 7, 5, 1, 8, 6, 8], 6]) == 75\nassert my_solution.maximizeSum(*[[7, 1, 6, 10, 2, 2, 2, 8, 6, 2, 8, 9, 8, 10, 3, 4, 5, 2, 4, 10, 6, 3, 6, 4, 8, 1, 2, 6, 9, 6, 10, 4, 6, 3, 3, 5, 3, 2, 5, 8, 8, 3, 8, 5, 3, 1, 8, 8, 9, 2, 8, 7, 5, 2, 3, 2, 7, 5, 8, 1, 10, 7, 4, 10, 10, 10, 6, 4, 3, 1, 10, 5, 10, 3, 5, 9, 2, 10, 4, 6, 3, 5, 7, 8, 10, 6], 7]) == 91\nassert my_solution.maximizeSum(*[[6, 10, 7, 10, 6, 7, 7, 4, 4, 7, 2, 2, 3, 6, 4, 8, 4, 6, 4, 3, 1, 4, 4, 8, 7, 1, 10, 2, 10, 8, 10, 1, 4, 7, 10, 5, 1, 9, 8, 3, 5, 8, 3, 7, 6, 5, 3, 1, 3, 2, 8, 5, 6, 1, 5, 10, 8, 7, 7, 10, 1, 3, 7, 3], 2]) == 21\nassert my_solution.maximizeSum(*[[4, 7, 2, 5, 5, 7, 4, 9, 6, 4, 2, 3, 8, 6, 6, 8, 10, 6, 4, 10, 3, 5, 3, 2, 3, 9, 7, 10, 4, 4], 8]) == 108\nassert my_solution.maximizeSum(*[[2, 7, 9, 8, 1, 8, 3, 1, 9, 1, 7, 7, 3, 6, 5, 3, 4, 6, 2, 4, 1, 8, 3, 9, 1, 6, 1, 2, 1, 9, 9, 2, 6, 10, 5, 3, 1, 2, 8, 6, 7, 1, 10, 4, 4, 5, 5, 5, 10, 2, 3, 2, 8, 10, 9, 7, 1, 1, 2, 9, 3, 3, 2, 2, 2, 4, 9, 8, 9, 3, 5, 4, 6, 1, 10, 10, 4, 1], 10]) == 145\nassert my_solution.maximizeSum(*[[5, 6, 3, 2, 10, 9, 2, 9, 2, 8, 9, 8, 2, 1, 8, 4, 9, 4, 6, 2, 7, 9, 1, 10, 6, 10, 7, 3, 9, 2, 9, 10, 7, 10, 10, 4, 4, 10, 5, 4, 10, 3, 4, 2, 8, 9, 9, 3, 4, 5, 6, 9, 9, 2, 7], 5]) == 60\nassert my_solution.maximizeSum(*[[3, 3, 4, 4, 6, 5, 4, 3, 4, 10, 5, 5, 4], 5]) == 60\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2767", "questionFrontendId": "2656", "questionTitle": "Maximum Sum With Exactly K Elements ", "stats": { "totalAccepted": "35.7K", "totalSubmission": "41.1K", "totalAcceptedRaw": 35732, "totalSubmissionRaw": 41144, "acRate": "86.8%" } }
LeetCode/2766
# Find the Prefix Common Array of Two Arrays You are given two **0-indexed** integerpermutations `A` and `B` of length `n`. A **prefix common array** of `A` and `B` is an array `C` such that `C[i]` is equal to the count of numbers that are present at or before the index `i` in both `A` and `B`. Return *the **prefix common array** of* `A` *and* `B`. A sequence of `n` integers is called a **permutation** if it contains all integers from `1` to `n` exactly once.   **Example 1:** ``` **Input:** A = [1,3,2,4], B = [3,1,2,4] **Output:** [0,2,3,4] **Explanation:** At i = 0: no number is common, so C[0] = 0. At i = 1: 1 and 3 are common in A and B, so C[1] = 2. At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3. At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4. ``` **Example 2:** ``` **Input:** A = [2,3,1], B = [3,1,2] **Output:** [0,1,3] **Explanation:** At i = 0: no number is common, so C[0] = 0. At i = 1: only 3 is common in A and B, so C[1] = 1. At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3. ```   **Constraints:** * `1 <= A.length == B.length == n <= 50` * `1 <= A[i], B[i] <= n` * `It is guaranteed that A and B are both a permutation of n integers.` Please make sure your answer follows the type signature below: ```python3 class Solution: def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findThePrefixCommonArray(*[[1, 3, 2, 4], [3, 1, 2, 4]]) == [0, 2, 3, 4]\nassert my_solution.findThePrefixCommonArray(*[[2, 3, 1], [3, 1, 2]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[1], [1]]) == [1]\nassert my_solution.findThePrefixCommonArray(*[[1, 2], [1, 2]]) == [1, 2]\nassert my_solution.findThePrefixCommonArray(*[[1, 2], [2, 1]]) == [0, 2]\nassert my_solution.findThePrefixCommonArray(*[[2, 1], [1, 2]]) == [0, 2]\nassert my_solution.findThePrefixCommonArray(*[[2, 1], [2, 1]]) == [1, 2]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [1, 2, 3]]) == [1, 2, 3]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [2, 1, 3]]) == [0, 2, 3]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [2, 3, 1]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [1, 3, 2]]) == [1, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [3, 1, 2]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[1, 2, 3], [3, 2, 1]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [1, 2, 3]]) == [0, 2, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [2, 1, 3]]) == [1, 2, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [2, 3, 1]]) == [1, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [1, 3, 2]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [3, 1, 2]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 1, 3], [3, 2, 1]]) == [0, 1, 3]\nassert my_solution.findThePrefixCommonArray(*[[2, 3, 1], [1, 2, 3]]) == [0, 1, 3]\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2766", "questionFrontendId": "2657", "questionTitle": "Find the Prefix Common Array of Two Arrays", "stats": { "totalAccepted": "4.7K", "totalSubmission": "5.8K", "totalAcceptedRaw": 4684, "totalSubmissionRaw": 5756, "acRate": "81.4%" } }
LeetCode/2765
# Make Array Empty You are given an integer array `nums` containing **distinct** numbers, and you can perform the following operations **until the array is empty**: * If the first element has the **smallest** value, remove it * Otherwise, put the first element at the **end** of the array. Return *an integer denoting the number of operations it takes to make* `nums` *empty.*   **Example 1:** ``` **Input:** nums = [3,4,-1] **Output:** 5 ``` | Operation | Array | | --- | --- | | 1 | [4, -1, 3] | | 2 | [-1, 3, 4] | | 3 | [3, 4] | | 4 | [4] | | 5 | [] | **Example 2:** ``` **Input:** nums = [1,2,4,3] **Output:** 5 ``` | Operation | Array | | --- | --- | | 1 | [2, 4, 3] | | 2 | [4, 3] | | 3 | [3, 4] | | 4 | [4] | | 5 | [] | **Example 3:** ``` **Input:** nums = [1,2,3] **Output:** 3 ``` | Operation | Array | | --- | --- | | 1 | [2, 3] | | 2 | [3] | | 3 | [] |   **Constraints:** * `1 <= nums.length <= 105` * `-109<= nums[i] <= 109` * All values in `nums` are **distinct**. Please make sure your answer follows the type signature below: ```python3 class Solution: def countOperationsToEmptyArray(self, nums: List[int]) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countOperationsToEmptyArray(*[[3, 4, -1]]) == 5\nassert my_solution.countOperationsToEmptyArray(*[[1, 2, 4, 3]]) == 5\nassert my_solution.countOperationsToEmptyArray(*[[1, 2, 3]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[-18]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[-17]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[-13]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[-8]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[-6]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[3]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[14]]) == 1\nassert my_solution.countOperationsToEmptyArray(*[[-20, -6]]) == 2\nassert my_solution.countOperationsToEmptyArray(*[[-19, -11]]) == 2\nassert my_solution.countOperationsToEmptyArray(*[[-17, -18]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[-10, -1]]) == 2\nassert my_solution.countOperationsToEmptyArray(*[[-10, 10]]) == 2\nassert my_solution.countOperationsToEmptyArray(*[[-9, -10]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[-3, -4]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[1, -4]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[1, 0]]) == 3\nassert my_solution.countOperationsToEmptyArray(*[[5, 18]]) == 2\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2765", "questionFrontendId": "2659", "questionTitle": "Make Array Empty", "stats": { "totalAccepted": "2.6K", "totalSubmission": "6.5K", "totalAcceptedRaw": 2564, "totalSubmissionRaw": 6505, "acRate": "39.4%" } }
LeetCode/2748
# Calculate Delayed Arrival Time You are given a positive integer `arrivalTime` denoting the arrival time of a train in hours, and another positive integer `delayedTime` denoting the amount of delay in hours. Return *the time when the train will arrive at the station.* Note that the time in this problem is in 24-hours format.   **Example 1:** ``` **Input:** arrivalTime = 15, delayedTime = 5 **Output:** 20 **Explanation:** Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours). ``` **Example 2:** ``` **Input:** arrivalTime = 13, delayedTime = 11 **Output:** 0 **Explanation:** Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0). ```   **Constraints:** * `1 <= arrivaltime < 24` * `1 <= delayedTime <= 24` Please make sure your answer follows the type signature below: ```python3 class Solution: def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int: ```
{ "code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findDelayedArrivalTime(*[15, 5]) == 20\nassert my_solution.findDelayedArrivalTime(*[13, 11]) == 0\nassert my_solution.findDelayedArrivalTime(*[1, 1]) == 2\nassert my_solution.findDelayedArrivalTime(*[1, 2]) == 3\nassert my_solution.findDelayedArrivalTime(*[1, 3]) == 4\nassert my_solution.findDelayedArrivalTime(*[1, 4]) == 5\nassert my_solution.findDelayedArrivalTime(*[1, 5]) == 6\nassert my_solution.findDelayedArrivalTime(*[1, 6]) == 7\nassert my_solution.findDelayedArrivalTime(*[1, 7]) == 8\nassert my_solution.findDelayedArrivalTime(*[1, 8]) == 9\nassert my_solution.findDelayedArrivalTime(*[1, 9]) == 10\nassert my_solution.findDelayedArrivalTime(*[1, 10]) == 11\nassert my_solution.findDelayedArrivalTime(*[1, 11]) == 12\nassert my_solution.findDelayedArrivalTime(*[1, 12]) == 13\nassert my_solution.findDelayedArrivalTime(*[1, 13]) == 14\nassert my_solution.findDelayedArrivalTime(*[1, 14]) == 15\nassert my_solution.findDelayedArrivalTime(*[1, 15]) == 16\nassert my_solution.findDelayedArrivalTime(*[1, 16]) == 17\nassert my_solution.findDelayedArrivalTime(*[1, 17]) == 18\nassert my_solution.findDelayedArrivalTime(*[1, 18]) == 19\n" }
{ "programming_language": "python", "execution_language": "python", "questionId": "2748", "questionFrontendId": "2651", "questionTitle": "Calculate Delayed Arrival Time", "stats": { "totalAccepted": "40.6K", "totalSubmission": "46.3K", "totalAcceptedRaw": 40577, "totalSubmissionRaw": 46342, "acRate": "87.6%" } }