ShwStone/humaneval-leetcode huggingface dataset:
def gcdSort(nums: List[int]) -> bool:
    """
    You are given an integer array nums, and you can perform the following operation any number of times on nums:
    
    Swap the positions of two elements nums[i] and nums[j] if gcd(nums[i], nums[j]) > 1 where gcd(nums[i], nums[j]) is the greatest common divisor of nums[i] and nums[j].
    
    Return true if it is possible to sort nums in non-decreasing order using the above swap method, or false otherwise.
    
    Example 1:
    
    >>> gcdSort(nums = [7,21,3])
    >>> true
    Explanation: We can sort [7,21,3] by performing the following operations:
    - Swap 7 and 21 because gcd(7,21) = 7. nums = [21,7,3]
    - Swap 21 and 3 because gcd(21,3) = 3. nums = [3,7,21]
    
    Example 2:
    
    >>> gcdSort(nums = [5,2,6,2])
    >>> false
    Explanation: It is impossible to sort the array because 5 cannot be swapped with any other element.
    
    Example 3:
    
    >>> gcdSort(nums = [10,5,9,3,15])
    >>> true
    We can sort [10,5,9,3,15] by performing the following operations:
    - Swap 10 and 15 because gcd(10,15) = 5. nums = [15,5,9,3,10]
    - Swap 15 and 3 because gcd(15,3) = 3. nums = [3,5,9,15,10]
    - Swap 10 and 15 because gcd(10,15) = 5. nums = [3,5,9,10,15]
    """
def findInteger(k: int, digit1: int, digit2: int) -> int:
    """
    Given three integers, k, digit1, and digit2, you want to find the smallest integer that is:
    
    Larger than k,
    A multiple of k, and
    Comprised of only the digits digit1 and/or digit2.
    
    Return the smallest such integer. If no such integer exists or the integer exceeds the limit of a signed 32-bit integer (231 - 1), return -1.
    
    Example 1:
    
    >>> findInteger(k = 2, digit1 = 0, digit2 = 2)
    >>> 20
    Explanation:
    20 is the first integer larger than 2, a multiple of 2, and comprised of only the digits 0 and/or 2.
    
    Example 2:
    
    >>> findInteger(k = 3, digit1 = 4, digit2 = 2)
    >>> 24
    Explanation:
    24 is the first integer larger than 3, a multiple of 3, and comprised of only the digits 4 and/or 2.
    
    Example 3:
    
    >>> findInteger(k = 2, digit1 = 0, digit2 = 0)
    >>> -1
    Explanation:
    No integer meets the requirements so return -1.
    """
def reversePrefix(word: str, ch: str) -> str:
    """
    Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.
    
    For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".
    
    Return the resulting string.
    
    Example 1:
    
    >>> reversePrefix(word = "abcdefd", ch = "d")
    >>> "dcbaefd"
    Explanation: The first occurrence of "d" is at index 3.
    Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".
    
    Example 2:
    
    >>> reversePrefix(word = "xyxzxe", ch = "z")
    >>> "zxyxxe"
    Explanation: The first and only occurrence of "z" is at index 3.
    Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
    
    Example 3:
    
    >>> reversePrefix(word = "abcd", ch = "z")
    >>> "abcd"
    Explanation: "z" does not exist in word.
    You should not do any reverse operation, the resulting string is "abcd".
    """
def interchangeableRectangles(rectangles: List[List[int]]) -> int:
    """
    You are given n rectangles represented by a 0-indexed 2D integer array rectangles, where rectangles[i] = [widthi, heighti] denotes the width and height of the ith rectangle.
    Two rectangles i and j (i < j) are considered interchangeable if they have the same width-to-height ratio. More formally, two rectangles are interchangeable if widthi/heighti == widthj/heightj (using decimal division, not integer division).
    Return the number of pairs of interchangeable rectangles in rectangles.
    
    Example 1:
    
    >>> interchangeableRectangles(rectangles = [[4,8],[3,6],[10,20],[15,30]])
    >>> 6
    Explanation: The following are the interchangeable pairs of rectangles by index (0-indexed):
    - Rectangle 0 with rectangle 1: 4/8 == 3/6.
    - Rectangle 0 with rectangle 2: 4/8 == 10/20.
    - Rectangle 0 with rectangle 3: 4/8 == 15/30.
    - Rectangle 1 with rectangle 2: 3/6 == 10/20.
    - Rectangle 1 with rectangle 3: 3/6 == 15/30.
    - Rectangle 2 with rectangle 3: 10/20 == 15/30.
    
    Example 2:
    
    >>> interchangeableRectangles(rectangles = [[4,5],[7,8]])
    >>> 0
    Explanation: There are no interchangeable pairs of rectangles.
    """
def maxProduct(s: str) -> int:
    """
    Given a string s, find two disjoint palindromic subsequences of s such that the product of their lengths is maximized. The two subsequences are disjoint if they do not both pick a character at the same index.
    Return the maximum possible product of the lengths of the two palindromic subsequences.
    A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A string is palindromic if it reads the same forward and backward.
    
    Example 1:
    
    
    >>> maxProduct(s = "leetcodecom")
    >>> 9
    Explanation: An optimal solution is to choose "ete" for the 1st subsequence and "cdc" for the 2nd subsequence.
    The product of their lengths is: 3 * 3 = 9.
    
    Example 2:
    
    >>> maxProduct(s = "bb")
    >>> 1
    Explanation: An optimal solution is to choose "b" (the first character) for the 1st subsequence and "b" (the second character) for the 2nd subsequence.
    The product of their lengths is: 1 * 1 = 1.
    
    Example 3:
    
    >>> maxProduct(s = "accbcaxxcxx")
    >>> 25
    Explanation: An optimal solution is to choose "accca" for the 1st subsequence and "xxcxx" for the 2nd subsequence.
    The product of their lengths is: 5 * 5 = 25.
    """
def smallestMissingValueSubtree(parents: List[int], nums: List[int]) -> List[int]:
    """
    There is a family tree rooted at 0 consisting of n nodes numbered 0 to n - 1. You are given a 0-indexed integer array parents, where parents[i] is the parent for node i. Since node 0 is the root, parents[0] == -1.
    There are 105 genetic values, each represented by an integer in the inclusive range [1, 105]. You are given a 0-indexed integer array nums, where nums[i] is a distinct genetic value for node i.
    Return an array ans of length n where ans[i] is the smallest genetic value that is missing from the subtree rooted at node i.
    The subtree rooted at a node x contains node x and all of its descendant nodes.
    
    Example 1:
    
    
    >>> smallestMissingValueSubtree(parents = [-1,0,0,2], nums = [1,2,3,4])
    >>> [5,1,1,1]
    Explanation: The answer for each subtree is calculated as follows:
    - 0: The subtree contains nodes [0,1,2,3] with values [1,2,3,4]. 5 is the smallest missing value.
    - 1: The subtree contains only node 1 with value 2. 1 is the smallest missing value.
    - 2: The subtree contains nodes [2,3] with values [3,4]. 1 is the smallest missing value.
    - 3: The subtree contains only node 3 with value 4. 1 is the smallest missing value.
    
    Example 2:
    
    
    >>> smallestMissingValueSubtree(parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3])
    >>> [7,1,1,4,2,1]
    Explanation: The answer for each subtree is calculated as follows:
    - 0: The subtree contains nodes [0,1,2,3,4,5] with values [5,4,6,2,1,3]. 7 is the smallest missing value.
    - 1: The subtree contains nodes [1,2] with values [4,6]. 1 is the smallest missing value.
    - 2: The subtree contains only node 2 with value 6. 1 is the smallest missing value.
    - 3: The subtree contains nodes [3,4,5] with values [2,1,3]. 4 is the smallest missing value.
    - 4: The subtree contains only node 4 with value 1. 2 is the smallest missing value.
    - 5: The subtree contains only node 5 with value 3. 1 is the smallest missing value.
    
    Example 3:
    
    >>> smallestMissingValueSubtree(parents = [-1,2,3,0,2,4,1], nums = [2,3,4,5,6,7,8])
    >>> [1,1,1,1,1,1,1]
    Explanation: The value 1 is missing from all the subtrees.
    """
def findGameWinner(n: int) -> bool:
    """
    A Fibonacci tree is a binary tree created using the order function order(n):
    
    order(0) is the empty tree.
    order(1) is a binary tree with only one node.
    order(n) is a binary tree that consists of a root node with the left subtree as order(n - 2) and the right subtree as order(n - 1).
    
    Alice and Bob are playing a game with a Fibonacci tree with Alice staring first. On each turn, a player selects a node and removes that node and its subtree. The player that is forced to delete root loses.
    Given the integer n, return true if Alice wins the game or false if Bob wins, assuming both players play optimally.
    A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
    
    Example 1:
    
    
    >>> findGameWinner(n = 3)
    >>> true
    Explanation:
    Alice takes the node 1 in the right subtree.
    Bob takes either the 1 in the left subtree or the 2 in the right subtree.
    Alice takes whichever node Bob doesn't take.
    Bob is forced to take the root node 3, so Bob will lose.
    Return true because Alice wins.
    
    Example 2:
    
    
    >>> findGameWinner(n = 1)
    >>> false
    Explanation:
    Alice is forced to take the root node 1, so Alice will lose.
    Return false because Alice loses.
    
    Example 3:
    
    
    >>> findGameWinner(n = 2)
    >>> true
    Explanation:
    Alice takes the node 1.
    Bob is forced to take the root node 2, so Bob will lose.
    Return true because Alice wins.
    """
def countKDifference(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
    The value of |x| is defined as:
    
    x if x >= 0.
    -x if x < 0.
    
    
    Example 1:
    
    >>> countKDifference(nums = [1,2,2,1], k = 1)
    >>> 4
    Explanation: The pairs with an absolute difference of 1 are:
    - [1,2,2,1]
    - [1,2,2,1]
    - [1,2,2,1]
    - [1,2,2,1]
    
    Example 2:
    
    >>> countKDifference(nums = [1,3], k = 3)
    >>> 0
    Explanation: There are no pairs with an absolute difference of 3.
    
    Example 3:
    
    >>> countKDifference(nums = [3,2,1,5,4], k = 2)
    >>> 3
    Explanation: The pairs with an absolute difference of 2 are:
    - [3,2,1,5,4]
    - [3,2,1,5,4]
    - [3,2,1,5,4]
    """
def findOriginalArray(changed: List[int]) -> List[int]:
    """
    An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.
    Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.
    
    Example 1:
    
    >>> findOriginalArray(changed = [1,3,4,2,6,8])
    >>> [1,3,4]
    Explanation: One possible original array could be [1,3,4]:
    - Twice the value of 1 is 1 * 2 = 2.
    - Twice the value of 3 is 3 * 2 = 6.
    - Twice the value of 4 is 4 * 2 = 8.
    Other original arrays could be [4,3,1] or [3,1,4].
    
    Example 2:
    
    >>> findOriginalArray(changed = [6,3,0,1])
    >>> []
    Explanation: changed is not a doubled array.
    
    Example 3:
    
    >>> findOriginalArray(changed = [1])
    >>> []
    Explanation: changed is not a doubled array.
    """
def maxTaxiEarnings(n: int, rides: List[List[int]]) -> int:
    """
    There are n points on a road you are driving your taxi on. The n points on the road are labeled from 1 to n in the direction you are going, and you want to drive from point 1 to point n to make money by picking up passengers. You cannot change the direction of the taxi.
    The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip.
    For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time.
    Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally.
    Note: You may drop off a passenger and pick up a different passenger at the same point.
    
    Example 1:
    
    >>> maxTaxiEarnings(n = 5, rides = [[2,5,4],[1,5,1]])
    >>> 7
    Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
    
    Example 2:
    
    >>> maxTaxiEarnings(n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]])
    >>> 20
    Explanation: We will pick up the following passengers:
    - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
    - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
    - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
    We earn 9 + 5 + 6 = 20 dollars in total.
    """
def minOperations(nums: List[int]) -> int:
    """
    You are given an integer array nums. In one operation, you can replace any element in nums with any integer.
    nums is considered continuous if both of the following conditions are fulfilled:
    
    All elements in nums are unique.
    The difference between the maximum element and the minimum element in nums equals nums.length - 1.
    
    For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.
    Return the minimum number of operations to make nums continuous.
    
    Example 1:
    
    >>> minOperations(nums = [4,2,5,3])
    >>> 0
    Explanation: nums is already continuous.
    
    Example 2:
    
    >>> minOperations(nums = [1,2,3,5,6])
    >>> 1
    Explanation: One possible solution is to change the last element to 4.
    The resulting array is [1,2,3,5,4], which is continuous.
    
    Example 3:
    
    >>> minOperations(nums = [1,10,100,1000])
    >>> 3
    Explanation: One possible solution is to:
    - Change the second element to 2.
    - Change the third element to 3.
    - Change the fourth element to 4.
    The resulting array is [1,2,3,4], which is continuous.
    """
def finalValueAfterOperations(operations: List[str]) -> int:
    """
    There is a programming language with only four operations and one variable X:
    
    ++X and X++ increments the value of the variable X by 1.
    --X and X-- decrements the value of the variable X by 1.
    
    Initially, the value of X is 0.
    Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
    
    Example 1:
    
    >>> finalValueAfterOperations(operations = ["--X","X++","X++"])
    >>> 1
    Explanation: The operations are performed as follows:
    Initially, X = 0.
    --X: X is decremented by 1, X =  0 - 1 = -1.
    X++: X is incremented by 1, X = -1 + 1 =  0.
    X++: X is incremented by 1, X =  0 + 1 =  1.
    
    Example 2:
    
    >>> finalValueAfterOperations(operations = ["++X","++X","X++"])
    >>> 3
    Explanation: The operations are performed as follows:
    Initially, X = 0.
    ++X: X is incremented by 1, X = 0 + 1 = 1.
    ++X: X is incremented by 1, X = 1 + 1 = 2.
    X++: X is incremented by 1, X = 2 + 1 = 3.
    
    Example 3:
    
    >>> finalValueAfterOperations(operations = ["X++","++X","--X","X--"])
    >>> 0
    Explanation: The operations are performed as follows:
    Initially, X = 0.
    X++: X is incremented by 1, X = 0 + 1 = 1.
    ++X: X is incremented by 1, X = 1 + 1 = 2.
    --X: X is decremented by 1, X = 2 - 1 = 1.
    X--: X is decremented by 1, X = 1 - 1 = 0.
    """
def sumOfBeauties(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums. For each index i (1 <= i <= nums.length - 2) the beauty of nums[i] equals:
    
    2, if nums[j] < nums[i] < nums[k], for all 0 <= j < i and for all i < k <= nums.length - 1.
    1, if nums[i - 1] < nums[i] < nums[i + 1], and the previous condition is not satisfied.
    0, if none of the previous conditions holds.
    
    Return the sum of beauty of all nums[i] where 1 <= i <= nums.length - 2.
    
    Example 1:
    
    >>> sumOfBeauties(nums = [1,2,3])
    >>> 2
    Explanation: For each index i in the range 1 <= i <= 1:
    - The beauty of nums[1] equals 2.
    
    Example 2:
    
    >>> sumOfBeauties(nums = [2,4,6,4])
    >>> 1
    Explanation: For each index i in the range 1 <= i <= 2:
    - The beauty of nums[1] equals 1.
    - The beauty of nums[2] equals 0.
    
    Example 3:
    
    >>> sumOfBeauties(nums = [3,2,1])
    >>> 0
    Explanation: For each index i in the range 1 <= i <= 1:
    - The beauty of nums[1] equals 0.
    """
def longestSubsequenceRepeatedK(s: str, k: int) -> str:
    """
    You are given a string s of length n, and an integer k. You are tasked to find the longest subsequence repeated k times in string s.
    A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
    A subsequence seq is repeated k times in the string s if seq * k is a subsequence of s, where seq * k represents a string constructed by concatenating seq k times.
    
    For example, "bba" is repeated 2 times in the string "bababcba", because the string "bbabba", constructed by concatenating "bba" 2 times, is a subsequence of the string "bababcba".
    
    Return the longest subsequence repeated k times in string s. If multiple such subsequences are found, return the lexicographically largest one. If there is no such subsequence, return an empty string.
    
    Example 1:
    
    
    >>> longestSubsequenceRepeatedK(s = "letsleetcode", k = 2)
    >>> "let"
    Explanation: There are two longest subsequences repeated 2 times: "let" and "ete".
    "let" is the lexicographically largest one.
    
    Example 2:
    
    >>> longestSubsequenceRepeatedK(s = "bb", k = 2)
    >>> "b"
    Explanation: The longest subsequence repeated 2 times is "b".
    
    Example 3:
    
    >>> longestSubsequenceRepeatedK(s = "ab", k = 2)
    >>> ""
    Explanation: There is no subsequence repeated 2 times. Empty string is returned.
    """
def averageHeightOfBuildings(buildings: List[List[int]]) -> List[List[int]]:
    """
    A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array buildings, where buildings[i] = [starti, endi, heighti]. This means that there is a building with heighti in the half-closed segment [starti, endi).
    You want to describe the heights of the buildings on the street with the minimum number of non-overlapping segments. The street can be represented by the 2D integer array street where street[j] = [leftj, rightj, averagej] describes a half-closed segment [leftj, rightj) of the road where the average heights of the buildings in the segment is averagej.
    
    For example, if buildings = [[1,5,2],[3,10,4]], the street could be represented by street = [[1,3,2],[3,5,3],[5,10,4]] because:
    
    
    From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
    From 3 to 5, both the first and the second building are there with an average height of (2+4) / 2 = 3.
    From 5 to 10, there is only the second building with an average height of 4 / 1 = 4.
    
    
    
    Given buildings, return the 2D integer array street as described above (excluding any areas of the street where there are no buldings). You may return the array in any order.
    The average of n elements is the sum of the n elements divided (integer division) by n.
    A half-closed segment [a, b) is the section of the number line between points a and b including point a and not including point b.
    
    Example 1:
    
    
    >>> averageHeightOfBuildings(buildings = [[1,4,2],[3,9,4]])
    >>> [[1,3,2],[3,4,3],[4,9,4]]
    Explanation:
    From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
    From 3 to 4, both the first and the second building are there with an average height of (2+4) / 2 = 3.
    From 4 to 9, there is only the second building with an average height of 4 / 1 = 4.
    
    Example 2:
    
    >>> averageHeightOfBuildings(buildings = [[1,3,2],[2,5,3],[2,8,3]])
    >>> [[1,3,2],[3,8,3]]
    Explanation:
    From 1 to 2, there is only the first building with an average height of 2 / 1 = 2.
    From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2.
    From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3.
    From 5 to 8, there is only the last building with an average height of 3 / 1 = 3.
    The average height from 1 to 3 is the same so we can group them into one segment.
    The average height from 3 to 8 is the same so we can group them into one segment.
    
    Example 3:
    
    >>> averageHeightOfBuildings(buildings = [[1,2,1],[5,6,1]])
    >>> [[1,2,1],[5,6,1]]
    Explanation:
    From 1 to 2, there is only the first building with an average height of 1 / 1 = 1.
    From 2 to 5, there are no buildings, so it is not included in the output.
    From 5 to 6, there is only the second building with an average height of 1 / 1 = 1.
    We cannot group the segments together because an empty space with no buildings seperates the segments.
    """
def maximumDifference(nums: List[int]) -> int:
    """
    Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j].
    Return the maximum difference. If no such i and j exists, return -1.
    
    Example 1:
    
    >>> maximumDifference(nums = [7,1,5,4])
    >>> 4
    Explanation:
    The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
    Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
    
    Example 2:
    
    >>> maximumDifference(nums = [9,4,3,2])
    >>> -1
    Explanation:
    There is no i and j such that i < j and nums[i] < nums[j].
    
    Example 3:
    
    >>> maximumDifference(nums = [1,5,2,10])
    >>> 9
    Explanation:
    The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
    """