ShwStone/humaneval-leetcode huggingface dataset:
def subArrayRanges(nums: List[int]) -> int:
    """
    You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray.
    Return the sum of all subarray ranges of nums.
    A subarray is a contiguous non-empty sequence of elements within an array.
    
    Example 1:
    
    >>> subArrayRanges(nums = [1,2,3])
    >>> 4
    Explanation: The 6 subarrays of nums are the following:
    [1], range = largest - smallest = 1 - 1 = 0
    [2], range = 2 - 2 = 0
    [3], range = 3 - 3 = 0
    [1,2], range = 2 - 1 = 1
    [2,3], range = 3 - 2 = 1
    [1,2,3], range = 3 - 1 = 2
    So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.
    Example 2:
    
    >>> subArrayRanges(nums = [1,3,3])
    >>> 4
    Explanation: The 6 subarrays of nums are the following:
    [1], range = largest - smallest = 1 - 1 = 0
    [3], range = 3 - 3 = 0
    [3], range = 3 - 3 = 0
    [1,3], range = 3 - 1 = 2
    [3,3], range = 3 - 3 = 0
    [1,3,3], range = 3 - 1 = 2
    So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
    
    Example 3:
    
    >>> subArrayRanges(nums = [4,-2,-3,4,1])
    >>> 59
    Explanation: The sum of all subarray ranges of nums is 59.
    """
def minimumRefill(plants: List[int], capacityA: int, capacityB: int) -> int:
    """
    Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i.
    Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way:
    
    Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously.
    It takes the same amount of time to water each plant regardless of how much water it needs.
    Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant.
    In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant.
    
    Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's and Bob's watering cans respectively, return the number of times they have to refill to water all the plants.
    
    Example 1:
    
    >>> minimumRefill(plants = [2,2,3,3], capacityA = 5, capacityB = 5)
    >>> 1
    Explanation:
    - Initially, Alice and Bob have 5 units of water each in their watering cans.
    - Alice waters plant 0, Bob waters plant 3.
    - Alice and Bob now have 3 units and 2 units of water respectively.
    - Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it.
    So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.
    
    Example 2:
    
    >>> minimumRefill(plants = [2,2,3,3], capacityA = 3, capacityB = 4)
    >>> 2
    Explanation:
    - Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively.
    - Alice waters plant 0, Bob waters plant 3.
    - Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.
    - Since neither of them have enough water for their current plants, they refill their cans and then water the plants.
    So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.
    
    Example 3:
    
    >>> minimumRefill(plants = [5], capacityA = 10, capacityB = 8)
    >>> 0
    Explanation:
    - There is only one plant.
    - Alice's watering can has 10 units of water, whereas Bob's can has 8 units. Since Alice has more water in her can, she waters this plant.
    So, the total number of times they have to refill is 0.
    """
def maxTotalFruits(fruits: List[List[int]], startPos: int, k: int) -> int:
    """
    Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, and each positioni is unique.
    You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
    Return the maximum total number of fruits you can harvest.
    
    Example 1:
    
    
    >>> maxTotalFruits(fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4)
    >>> 9
    Explanation:
    The optimal way is to:
    - Move right to position 6 and harvest 3 fruits
    - Move right to position 8 and harvest 6 fruits
    You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
    
    Example 2:
    
    
    >>> maxTotalFruits(fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4)
    >>> 14
    Explanation:
    You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
    The optimal way is to:
    - Harvest the 7 fruits at the starting position 5
    - Move left to position 4 and harvest 1 fruit
    - Move right to position 6 and harvest 2 fruits
    - Move right to position 7 and harvest 4 fruits
    You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
    
    Example 3:
    
    
    >>> maxTotalFruits(fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2)
    >>> 0
    Explanation:
    You can move at most k = 2 steps and cannot reach any position with fruits.
    """
def shareCandies(candies: List[int], k: int) -> int:
    """
    You are given a 0-indexed integer array candies, where candies[i] represents the flavor of the ith candy. Your mom wants you to share these candies with your little sister by giving her k consecutive candies, but you want to keep as many flavors of candies as possible.
    Return the maximum number of unique flavors of candy you can keep after sharing  with your sister.
    
    Example 1:
    
    >>> shareCandies(candies = [1,2,2,3,4,3], k = 3)
    >>> 3
    Explanation:
    Give the candies in the range [1, 3] (inclusive) with flavors [2,2,3].
    You can eat candies with flavors [1,4,3].
    There are 3 unique flavors, so return 3.
    
    Example 2:
    
    >>> shareCandies(candies = [2,2,2,2,3,3], k = 2)
    >>> 2
    Explanation:
    Give the candies in the range [3, 4] (inclusive) with flavors [2,3].
    You can eat candies with flavors [2,2,2,3].
    There are 2 unique flavors, so return 2.
    Note that you can also share the candies with flavors [2,2] and eat the candies with flavors [2,2,3,3].
    
    Example 3:
    
    >>> shareCandies(candies = [2,4,5], k = 0)
    >>> 3
    Explanation:
    You do not have to give any candies.
    You can eat the candies with flavors [2,4,5].
    There are 3 unique flavors, so return 3.
    """
def firstPalindrome(words: List[str]) -> str:
    """
    Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".
    A string is palindromic if it reads the same forward and backward.
    
    Example 1:
    
    >>> firstPalindrome(words = ["abc","car","ada","racecar","cool"])
    >>> "ada"
    Explanation: The first string that is palindromic is "ada".
    Note that "racecar" is also palindromic, but it is not the first.
    
    Example 2:
    
    >>> firstPalindrome(words = ["notapalindrome","racecar"])
    >>> "racecar"
    Explanation: The first and only string that is palindromic is "racecar".
    
    Example 3:
    
    >>> firstPalindrome(words = ["def","ghi"])
    >>> ""
    Explanation: There are no palindromic strings, so the empty string is returned.
    """
def addSpaces(s: str, spaces: List[int]) -> str:
    """
    You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.
    
    For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y' and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".
    
    Return the modified string after the spaces have been added.
    
    Example 1:
    
    >>> addSpaces(s = "LeetcodeHelpsMeLearn", spaces = [8,13,15])
    >>> "Leetcode Helps Me Learn"
    Explanation:
    The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn".
    We then place spaces before those characters.
    
    Example 2:
    
    >>> addSpaces(s = "icodeinpython", spaces = [1,5,7,9])
    >>> "i code in py thon"
    Explanation:
    The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython".
    We then place spaces before those characters.
    
    Example 3:
    
    >>> addSpaces(s = "spacing", spaces = [0,1,2,3,4,5,6])
    >>> " s p a c i n g"
    Explanation:
    We are also able to place spaces before the first character of the string.
    """
def getDescentPeriods(prices: List[int]) -> int:
    """
    You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day.
    A smooth descent period of a stock consists of one or more contiguous days such that the price on each day is lower than the price on the preceding day by exactly 1. The first day of the period is exempted from this rule.
    Return the number of smooth descent periods.
    
    Example 1:
    
    >>> getDescentPeriods(prices = [3,2,1,4])
    >>> 7
    Explanation: There are 7 smooth descent periods:
    [3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
    Note that a period with one day is a smooth descent period by the definition.
    
    Example 2:
    
    >>> getDescentPeriods(prices = [8,6,7,7])
    >>> 4
    Explanation: There are 4 smooth descent periods: [8], [6], [7], and [7]
    Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1.
    
    Example 3:
    
    >>> getDescentPeriods(prices = [1])
    >>> 1
    Explanation: There is 1 smooth descent period: [1]
    """
def kIncreasing(arr: List[int], k: int) -> int:
    """
    You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k.
    The array arr is called K-increasing if arr[i-k] <= arr[i] holds for every index i, where k <= i <= n-1.
    
    For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because:
    
    
    arr[0] <= arr[2] (4 <= 5)
    arr[1] <= arr[3] (1 <= 2)
    arr[2] <= arr[4] (5 <= 6)
    arr[3] <= arr[5] (2 <= 2)
    
    
    However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because arr[0] > arr[3]).
    
    In one operation, you can choose an index i and change arr[i] into any positive integer.
    Return the minimum number of operations required to make the array K-increasing for the given k.
    
    Example 1:
    
    >>> kIncreasing(arr = [5,4,3,2,1], k = 1)
    >>> 4
    Explanation:
    For k = 1, the resultant array has to be non-decreasing.
    Some of the K-increasing arrays that can be formed are [5,6,7,8,9], [1,1,1,1,1], [2,2,3,4,4]. All of them require 4 operations.
    It is suboptimal to change the array to, for example, [6,7,8,9,10] because it would take 5 operations.
    It can be shown that we cannot make the array K-increasing in less than 4 operations.
    
    Example 2:
    
    >>> kIncreasing(arr = [4,1,5,2,6,2], k = 2)
    >>> 0
    Explanation:
    This is the same example as the one in the problem description.
    Here, for every index i where 2 <= i <= 5, arr[i-2] <= arr[i].
    Since the given array is already K-increasing, we do not need to perform any operations.
    Example 3:
    
    >>> kIncreasing(arr = [4,1,5,2,6,2], k = 3)
    >>> 2
    Explanation:
    Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.
    One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.
    The array will now be [4,1,5,4,6,5].
    Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.
    """
def elementInNums(nums: List[int], queries: List[List[int]]) -> List[int]:
    """
    You are given a 0-indexed integer array nums. Initially on minute 0, the array is unchanged. Every minute, the leftmost element in nums is removed until no elements remain. Then, every minute, one element is appended to the end of nums, in the order they were removed in, until the original array is restored. This process repeats indefinitely.
    
    For example, the array [0,1,2] would change as follows: [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → ...
    
    You are also given a 2D integer array queries of size n where queries[j] = [timej, indexj]. The answer to the jth query is:
    
    nums[indexj] if indexj < nums.length at minute timej
    -1 if indexj >= nums.length at minute timej
    
    Return an integer array ans of size n where ans[j] is the answer to the jth query.
    
    Example 1:
    
    >>> elementInNums(nums = [0,1,2], queries = [[0,2],[2,0],[3,2],[5,0]])
    >>> [2,2,-1,0]
    Explanation:
    Minute 0: [0,1,2] - All elements are in the nums.
    Minute 1: [1,2]   - The leftmost element, 0, is removed.
    Minute 2: [2]     - The leftmost element, 1, is removed.
    Minute 3: []      - The leftmost element, 2, is removed.
    Minute 4: [0]     - 0 is added to the end of nums.
    Minute 5: [0,1]   - 1 is added to the end of nums.
    
    At minute 0, nums[2] is 2.
    At minute 2, nums[0] is 2.
    At minute 3, nums[2] does not exist.
    At minute 5, nums[0] is 0.
    
    Example 2:
    
    >>> elementInNums(nums = [2], queries = [[0,0],[1,0],[2,0],[3,0]])
    >>> [2,-1,2,-1]
    Minute 0: [2] - All elements are in the nums.
    Minute 1: []  - The leftmost element, 2, is removed.
    Minute 2: [2] - 2 is added to the end of nums.
    Minute 3: []  - The leftmost element, 2, is removed.
    
    At minute 0, nums[0] is 2.
    At minute 1, nums[0] does not exist.
    At minute 2, nums[0] is 2.
    At minute 3, nums[0] does not exist.
    """
def mostWordsFound(sentences: List[str]) -> int:
    """
    A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
    You are given an array of strings sentences, where each sentences[i] represents a single sentence.
    Return the maximum number of words that appear in a single sentence.
    
    Example 1:
    
    >>> mostWordsFound(sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"])
    >>> 6
    Explanation:
    - The first sentence, "alice and bob love leetcode", has 5 words in total.
    - The second sentence, "i think so too", has 4 words in total.
    - The third sentence, "this is great thanks very much", has 6 words in total.
    Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.
    
    Example 2:
    
    >>> mostWordsFound(sentences = ["please wait", "continue to fight", "continue to win"])
    >>> 3
    Explanation: It is possible that multiple sentences contain the same number of words.
    In this example, the second and third sentences (underlined) have the same number of words.
    """
def findAllRecipes(recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
    """
    You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.
    You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.
    Return a list of all the recipes that you can create. You may return the answer in any order.
    Note that two recipes may contain each other in their ingredients.
    
    Example 1:
    
    >>> findAllRecipes(recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"])
    >>> ["bread"]
    Explanation:
    We can create "bread" since we have the ingredients "yeast" and "flour".
    
    Example 2:
    
    >>> findAllRecipes(recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"])
    >>> ["bread","sandwich"]
    Explanation:
    We can create "bread" since we have the ingredients "yeast" and "flour".
    We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
    
    Example 3:
    
    >>> findAllRecipes(recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"])
    >>> ["bread","sandwich","burger"]
    Explanation:
    We can create "bread" since we have the ingredients "yeast" and "flour".
    We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
    We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
    """
def canBeValid(s: str, locked: str) -> bool:
    """
    A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:
    
    It is ().
    It can be written as AB (A concatenated with B), where A and B are valid parentheses strings.
    It can be written as (A), where A is a valid parentheses string.
    
    You are given a parentheses string s and a string locked, both of length n. locked is a binary string consisting only of '0's and '1's. For each index i of locked,
    
    If locked[i] is '1', you cannot change s[i].
    But if locked[i] is '0', you can change s[i] to either '(' or ')'.
    
    Return true if you can make s a valid parentheses string. Otherwise, return false.
    
    Example 1:
    
    
    >>> canBeValid(s = "))()))", locked = "010100")
    >>> true
    Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
    We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.
    Example 2:
    
    >>> canBeValid(s = "()()", locked = "0000")
    >>> true
    Explanation: We do not need to make any changes because s is already valid.
    
    Example 3:
    
    >>> canBeValid(s = ")", locked = "0")
    >>> false
    Explanation: locked permits us to change s[0].
    Changing s[0] to either '(' or ')' will not make s valid.
    
    Example 4:
    
    >>> canBeValid(s = "(((())(((())", locked = "111111010111")
    >>> true
    Explanation: locked permits us to change s[6] and s[8].
    We change s[6] and s[8] to ')' to make s valid.
    """
def abbreviateProduct(left: int, right: int) -> str:
    """
    You are given two positive integers left and right with left <= right. Calculate the product of all integers in the inclusive range [left, right].
    Since the product may be very large, you will abbreviate it following these steps:
    
    Count all trailing zeros in the product and remove them. Let us denote this count as C.
    
    
    For example, there are 3 trailing zeros in 1000, and there are 0 trailing zeros in 546.
    
    
    Denote the remaining number of digits in the product as d. If d > 10, then express the product as ... where  denotes the first 5 digits of the product, and  denotes the last 5 digits of the product after removing all trailing zeros. If d <= 10, we keep it unchanged.
    
    For example, we express 1234567654321 as 12345...54321, but 1234567 is represented as 1234567.
    
    
    Finally, represent the product as a string "...eC".
    
    For example, 12345678987600000 will be represented as "12345...89876e5".
    
    
    
    Return a string denoting the abbreviated product of all integers in the inclusive range [left, right].
    
    Example 1:
    
    >>> abbreviateProduct(left = 1, right = 4)
    >>> "24e0"
    Explanation: The product is 1 × 2 × 3 × 4 = 24.
    There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
    Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
    Thus, the final representation is "24e0".
    
    Example 2:
    
    >>> abbreviateProduct(left = 2, right = 11)
    >>> "399168e2"
    Explanation: The product is 39916800.
    There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
    The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
    Hence, the abbreviated product is "399168e2".
    
    Example 3:
    
    >>> abbreviateProduct(left = 371, right = 375)
    >>> "7219856259e3"
    Explanation: The product is 7219856259000.
    """
def isSameAfterReversals(num: int) -> bool:
    """
    Reversing an integer means to reverse all its digits.
    
    For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.
    
    Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.
    
    Example 1:
    
    >>> isSameAfterReversals(num = 526)
    >>> true
    Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
    
    Example 2:
    
    >>> isSameAfterReversals(num = 1800)
    >>> false
    Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
    
    Example 3:
    
    >>> isSameAfterReversals(num = 0)
    >>> true
    Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.
    """