ShwStone/humaneval-leetcode huggingface dataset:
def beautifulNumbers(l: int, r: int) -> int:
    """
    You are given two positive integers, l and r. A positive integer is called beautiful if the product of its digits is divisible by the sum of its digits.
    Return the count of beautiful numbers between l and r, inclusive.
    
    Example 1:
    
    >>> beautifulNumbers(l = 10, r = 20)
    >>> 2
    Explanation:
    The beautiful numbers in the range are 10 and 20.
    
    Example 2:
    
    >>> beautifulNumbers(l = 1, r = 15)
    >>> 10
    Explanation:
    The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
    """
def phonePrefix(numbers: List[str]) -> bool:
    """
    You are given a string array numbers that represents phone numbers. Return true if no phone number is a prefix of any other phone number; otherwise, return false.
    
    Example 1:
    
    >>> phonePrefix(numbers = ["1","2","4","3"])
    >>> true
    Explanation:
    No number is a prefix of another number, so the output is true.
    
    Example 2:
    
    >>> phonePrefix(numbers = ["001","007","15","00153"])
    >>> false
    Explanation:
    The string "001" is a prefix of the string "00153". Thus, the output is false.
    """
def maxContainers(n: int, w: int, maxWeight: int) -> int:
    """
    You are given a positive integer n representing an n x n cargo deck on a ship. Each cell on the deck can hold one container with a weight of exactly w.
    However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, maxWeight.
    Return the maximum number of containers that can be loaded onto the ship.
    
    Example 1:
    
    >>> maxContainers(n = 2, w = 3, maxWeight = 15)
    >>> 4
    Explanation:
    The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed maxWeight.
    
    Example 2:
    
    >>> maxContainers(n = 3, w = 5, maxWeight = 20)
    >>> 4
    Explanation:
    The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding maxWeight is 4.
    """
def numberOfComponents(properties: List[List[int]], k: int) -> int:
    """
    You are given a 2D integer array properties having dimensions n x m and an integer k.
    Define a function intersect(a, b) that returns the number of distinct integers common to both arrays a and b.
    Construct an undirected graph where each index i corresponds to properties[i]. There is an edge between node i and node j if and only if intersect(properties[i], properties[j]) >= k, where i and j are in the range [0, n - 1] and i != j.
    Return the number of connected components in the resulting graph.
    
    Example 1:
    
    >>> numberOfComponents(properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1)
    >>> 3
    Explanation:
    The graph formed has 3 connected components:
    
    
    Example 2:
    
    >>> numberOfComponents(properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2)
    >>> 1
    Explanation:
    The graph formed has 1 connected component:
    
    
    Example 3:
    
    >>> numberOfComponents(properties = [[1,1],[1,1]], k = 2)
    >>> 2
    Explanation:
    intersect(properties[0], properties[1]) = 1, which is less than k. This means there is no edge between properties[0] and properties[1] in the graph.
    """
def minTime(skill: List[int], mana: List[int]) -> int:
    """
    You are given two integer arrays, skill and mana, of length n and m, respectively.
    In a laboratory, n wizards must brew m potions in order. Each potion has a mana capacity mana[j] and must pass through all the wizards sequentially to be brewed properly. The time taken by the ith wizard on the jth potion is timeij = skill[i] * mana[j].
    Since the brewing process is delicate, a potion must be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be synchronized so that each wizard begins working on a potion exactly when it arrives. ​
    Return the minimum amount of time required for the potions to be brewed properly.
    
    Example 1:
    
    >>> minTime(skill = [1,5,2,4], mana = [5,1,4,2])
    >>> 110
    Explanation:
    
    
    
    Potion Number
    Start time
    Wizard 0 done by
    Wizard 1 done by
    Wizard 2 done by
    Wizard 3 done by
    
    
    0
    0
    5
    30
    40
    60
    
    
    1
    52
    53
    58
    60
    64
    
    
    2
    54
    58
    78
    86
    102
    
    
    3
    86
    88
    98
    102
    110
    
    
    
    As an example for why wizard 0 cannot start working on the 1st potion before time t = 52, consider the case where the wizards started preparing the 1st potion at time t = 50. At time t = 58, wizard 2 is done with the 1st potion, but wizard 3 will still be working on the 0th potion till time t = 60.
    
    Example 2:
    
    >>> minTime(skill = [1,1,1], mana = [1,1,1])
    >>> 5
    Explanation:
    
    Preparation of the 0th potion begins at time t = 0, and is completed by time t = 3.
    Preparation of the 1st potion begins at time t = 1, and is completed by time t = 4.
    Preparation of the 2nd potion begins at time t = 2, and is completed by time t = 5.
    
    
    Example 3:
    
    >>> minTime(skill = [1,2,3,4], mana = [1,2])
    >>> 21
    """
def minOperations(queries: List[List[int]]) -> int:
    """
    You are given a 2D array queries, where queries[i] is of the form [l, r]. Each queries[i] defines an array of integers nums consisting of elements ranging from l to r, both inclusive.
    In one operation, you can:
    
    Select two integers a and b from the array.
    Replace them with floor(a / 4) and floor(b / 4).
    
    Your task is to determine the minimum number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries.
    
    Example 1:
    
    >>> minOperations(queries = [[1,2],[2,4]])
    >>> 3
    Explanation:
    For queries[0]:
    
    The initial array is nums = [1, 2].
    In the first operation, select nums[0] and nums[1]. The array becomes [0, 0].
    The minimum number of operations required is 1.
    
    For queries[1]:
    
    The initial array is nums = [2, 3, 4].
    In the first operation, select nums[0] and nums[2]. The array becomes [0, 3, 1].
    In the second operation, select nums[1] and nums[2]. The array becomes [0, 0, 0].
    The minimum number of operations required is 2.
    
    The output is 1 + 2 = 3.
    
    Example 2:
    
    >>> minOperations(queries = [[2,6]])
    >>> 4
    Explanation:
    For queries[0]:
    
    The initial array is nums = [2, 3, 4, 5, 6].
    In the first operation, select nums[0] and nums[3]. The array becomes [0, 3, 4, 1, 6].
    In the second operation, select nums[2] and nums[4]. The array becomes [0, 3, 1, 1, 1].
    In the third operation, select nums[1] and nums[2]. The array becomes [0, 0, 0, 1, 1].
    In the fourth operation, select nums[3] and nums[4]. The array becomes [0, 0, 0, 0, 0].
    The minimum number of operations required is 4.
    
    The output is 4.
    """
def maxScore(nums: List[int]) -> int:
    """
    You are given an array of integers nums. You must repeatedly perform one of the following operations while the array has more than two elements:
    
    Remove the first two elements.
    Remove the last two elements.
    Remove the first and last element.
    
    For each operation, add the sum of the removed elements to your total score.
    Return the maximum possible score you can achieve.
    
    Example 1:
    
    >>> maxScore(nums = [2,4,1])
    >>> 6
    Explanation:
    The possible operations are:
    
    Remove the first two elements (2 + 4) = 6. The remaining array is [1].
    Remove the last two elements (4 + 1) = 5. The remaining array is [2].
    Remove the first and last elements (2 + 1) = 3. The remaining array is [4].
    
    The maximum score is obtained by removing the first two elements, resulting in a final score of 6.
    
    Example 2:
    
    >>> maxScore(nums = [5,-1,4,2])
    >>> 7
    Explanation:
    The possible operations are:
    
    Remove the first and last elements (5 + 2) = 7. The remaining array is [-1, 4].
    Remove the first two elements (5 + -1) = 4. The remaining array is [4, 2].
    Remove the last two elements (4 + 2) = 6. The remaining array is [5, -1].
    
    The maximum score is obtained by removing the first and last elements, resulting in a total score of 7.
    """
def reverseDegree(s: str) -> int:
    """
    Given a string s, calculate its reverse degree.
    The reverse degree is calculated as follows:
    
    For each character, multiply its position in the reversed alphabet ('a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-indexed).
    Sum these products for all characters in the string.
    
    Return the reverse degree of s.
    
    Example 1:
    
    >>> reverseDegree(s = "abc")
    >>> 148
    Explanation:
    
    
    
    Letter
    Index in Reversed Alphabet
    Index in String
    Product
    
    
    'a'
    26
    1
    26
    
    
    'b'
    25
    2
    50
    
    
    'c'
    24
    3
    72
    
    
    
    The reversed degree is 26 + 50 + 72 = 148.
    
    Example 2:
    
    >>> reverseDegree(s = "zaza")
    >>> 160
    Explanation:
    
    
    
    Letter
    Index in Reversed Alphabet
    Index in String
    Product
    
    
    'z'
    1
    1
    1
    
    
    'a'
    26
    2
    52
    
    
    'z'
    1
    3
    3
    
    
    'a'
    26
    4
    104
    
    
    
    The reverse degree is 1 + 52 + 3 + 104 = 160.
    """
def maxActiveSectionsAfterTrade(s: str) -> int:
    """
    You are given a binary string s of length n, where:
    
    '1' represents an active section.
    '0' represents an inactive section.
    
    You can perform at most one trade to maximize the number of active sections in s. In a trade, you:
    
    Convert a contiguous block of '1's that is surrounded by '0's to all '0's.
    Afterward, convert a contiguous block of '0's that is surrounded by '1's to all '1's.
    
    Return the maximum number of active sections in s after making the optimal trade.
    Note: Treat s as if it is augmented with a '1' at both ends, forming t = '1' + s + '1'. The augmented '1's do not contribute to the final count.
    
    Example 1:
    
    >>> maxActiveSectionsAfterTrade(s = "01")
    >>> 1
    Explanation:
    Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 1.
    
    Example 2:
    
    >>> maxActiveSectionsAfterTrade(s = "0100")
    >>> 4
    Explanation:
    
    String "0100" → Augmented to "101001".
    Choose "0100", convert "101001" → "100001" → "111111".
    The final string without augmentation is "1111". The maximum number of active sections is 4.
    
    
    Example 3:
    
    >>> maxActiveSectionsAfterTrade(s = "1000100")
    >>> 7
    Explanation:
    
    String "1000100" → Augmented to "110001001".
    Choose "000100", convert "110001001" → "110000001" → "111111111".
    The final string without augmentation is "1111111". The maximum number of active sections is 7.
    
    
    Example 4:
    
    >>> maxActiveSectionsAfterTrade(s = "01010")
    >>> 4
    Explanation:
    
    String "01010" → Augmented to "1010101".
    Choose "010", convert "1010101" → "1000101" → "1111101".
    The final string without augmentation is "11110". The maximum number of active sections is 4.
    """
def minimumCost(nums: List[int], cost: List[int], k: int) -> int:
    """
    You are given two integer arrays, nums and cost, of the same size, and an integer k.
    You can divide nums into subarrays. The cost of the ith subarray consisting of elements nums[l..r] is:
    
    (nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r]).
    
    Note that i represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
    Return the minimum total cost possible from any valid division.
    
    Example 1:
    
    >>> minimumCost(nums = [3,1,4], cost = [4,6,6], k = 1)
    >>> 110
    Explanation:
    The minimum total cost possible can be achieved by dividing nums into subarrays [3, 1] and [4].
    
    
    The cost of the first subarray [3,1] is (3 + 1 + 1 * 1) * (4 + 6) = 50.
    The cost of the second subarray [4] is (3 + 1 + 4 + 1 * 2) * 6 = 60.
    
    
    Example 2:
    
    >>> minimumCost(nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7)
    >>> 985
    Explanation:
    The minimum total cost possible can be achieved by dividing nums into subarrays [4, 8, 5, 1], [14, 2, 2], and [12, 1].
    
    
    The cost of the first subarray [4, 8, 5, 1] is (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525.
    The cost of the second subarray [14, 2, 2] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250.
    The cost of the third subarray [12, 1] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210.
    """
def minCosts(cost: List[int]) -> List[int]:
    """
    You are given an integer array cost of size n. You are currently at position n (at the end of the line) in a line of n + 1 people (numbered from 0 to n).
    You wish to move forward in the line, but each person in front of you charges a specific amount to swap places. The cost to swap with person i is given by cost[i].
    You are allowed to swap places with people as follows:
    
    If they are in front of you, you must pay them cost[i] to swap with them.
    If they are behind you, they can swap with you for free.
    
    Return an array answer of size n, where answer[i] is the minimum total cost to reach each position i in the line.
    
    Example 1:
    
    >>> minCosts(cost = [5,3,4,1,3,2])
    >>> [5,3,3,1,1,1]
    Explanation:
    We can get to each position in the following way:
    
    i = 0. We can swap with person 0 for a cost of 5.
    i = 1. We can swap with person 1 for a cost of 3.
    i = 2. We can swap with person 1 for a cost of 3, then swap with person 2 for free.
    i = 3. We can swap with person 3 for a cost of 1.
    i = 4. We can swap with person 3 for a cost of 1, then swap with person 4 for free.
    i = 5. We can swap with person 3 for a cost of 1, then swap with person 5 for free.
    
    
    Example 2:
    
    >>> minCosts(cost = [1,2,4,6,7])
    >>> [1,1,1,1,1]
    Explanation:
    We can swap with person 0 for a cost of 1, then we will be able to reach any position i for free.
    """
def longestPalindrome(s: str, t: str) -> int:
    """
    You are given two strings, s and t.
    You can create a new string by selecting a substring from s (possibly empty) and a substring from t (possibly empty), then concatenating them in order.
    Return the length of the longest palindrome that can be formed this way.
    
    Example 1:
    
    >>> longestPalindrome(s = "a", t = "a")
    >>> 2
    Explanation:
    Concatenating "a" from s and "a" from t results in "aa", which is a palindrome of length 2.
    
    Example 2:
    
    >>> longestPalindrome(s = "abc", t = "def")
    >>> 1
    Explanation:
    Since all characters are different, the longest palindrome is any single character, so the answer is 1.
    
    Example 3:
    
    >>> longestPalindrome(s = "b", t = "aaaa")
    >>> 4
    Explanation:
    Selecting "aaaa" from t is the longest palindrome, so the answer is 4.
    
    Example 4:
    
    >>> longestPalindrome(s = "abcde", t = "ecdba")
    >>> 5
    Explanation:
    Concatenating "abc" from s and "ba" from t results in "abcba", which is a palindrome of length 5.
    """
def minOperations(nums: List[int], x: int, k: int) -> int:
    """
    You are given an integer array nums and two integers, x and k. You can perform the following operation any number of times (including zero):
    
    Increase or decrease any element of nums by 1.
    
    Return the minimum number of operations needed to have at least k non-overlapping subarrays of size exactly x in nums, where all elements within each subarray are equal.
    
    Example 1:
    
    >>> minOperations(nums = [5,-2,1,3,7,3,6,4,-1], x = 3, k = 2)
    >>> 8
    Explanation:
    
    Use 3 operations to add 3 to nums[1] and use 2 operations to subtract 2 from nums[3]. The resulting array is [5, 1, 1, 1, 7, 3, 6, 4, -1].
    Use 1 operation to add 1 to nums[5] and use 2 operations to subtract 2 from nums[6]. The resulting array is [5, 1, 1, 1, 7, 4, 4, 4, -1].
    Now, all elements within each subarray [1, 1, 1] (from indices 1 to 3) and [4, 4, 4] (from indices 5 to 7) are equal. Since 8 total operations were used, 8 is the output.
    
    
    Example 2:
    
    >>> minOperations(nums = [9,-2,-2,-2,1,5], x = 2, k = 2)
    >>> 3
    Explanation:
    
    Use 3 operations to subtract 3 from nums[4]. The resulting array is [9, -2, -2, -2, -2, 5].
    Now, all elements within each subarray [-2, -2] (from indices 1 to 2) and [-2, -2] (from indices 3 to 4) are equal. Since 3 operations were used, 3 is the output.
    """