ShwStone/humaneval-leetcode huggingface dataset:
def distinctAverages(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums of even length.
    As long as nums is not empty, you must repetitively:
    
    Find the minimum number in nums and remove it.
    Find the maximum number in nums and remove it.
    Calculate the average of the two removed numbers.
    
    The average of two numbers a and b is (a + b) / 2.
    
    For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5.
    
    Return the number of distinct averages calculated using the above process.
    Note that when there is a tie for a minimum or maximum number, any can be removed.
    
    Example 1:
    
    >>> distinctAverages(nums = [4,1,4,0,3,5])
    >>> 2
    Explanation:
    1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
    2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
    3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
    Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
    
    Example 2:
    
    >>> distinctAverages(nums = [1,100])
    >>> 1
    Explanation:
    There is only one average to be calculated after removing 1 and 100, so we return 1.
    """
def countGoodStrings(low: int, high: int, zero: int, one: int) -> int:
    """
    Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following:
    
    Append the character '0' zero times.
    Append the character '1' one times.
    
    This can be performed any number of times.
    A good string is a string constructed by the above process having a length between low and high (inclusive).
    Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> countGoodStrings(low = 3, high = 3, zero = 1, one = 1)
    >>> 8
    Explanation:
    One possible valid good string is "011".
    It can be constructed as follows: "" -> "0" -> "01" -> "011".
    All binary strings from "000" to "111" are good strings in this example.
    
    Example 2:
    
    >>> countGoodStrings(low = 2, high = 3, zero = 1, one = 2)
    >>> 5
    Explanation: The good strings are "00", "11", "000", "110", and "011".
    """
def mostProfitablePath(edges: List[List[int]], bob: int, amount: List[int]) -> int:
    """
    There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
    At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:
    
    the price needed to open the gate at node i, if amount[i] is negative, or,
    the cash reward obtained on opening the gate at node i, otherwise.
    
    The game goes on as follows:
    
    Initially, Alice is at node 0 and Bob is at node bob.
    At every second, Alice and Bob each move to an adjacent node. Alice moves towards some leaf node, while Bob moves towards node 0.
    For every node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
    
    If the gate is already open, no price will be required, nor will there be any cash reward.
    If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob pay c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.
    
    
    If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node 0, he stops moving. Note that these events are independent of each other.
    
    Return the maximum net income Alice can have if she travels towards the optimal leaf node.
    
    Example 1:
    
    
    >>> mostProfitablePath(edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6])
    >>> 6
    Explanation:
    The above diagram represents the given tree. The game goes as follows:
    - Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
      Alice's net income is now -2.
    - Both Alice and Bob move to node 1.
      Since they reach here simultaneously, they open the gate together and share the reward.
      Alice's net income becomes -2 + (4 / 2) = 0.
    - Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
      Bob moves on to node 0, and stops moving.
    - Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
    Now, neither Alice nor Bob can make any further moves, and the game ends.
    It is not possible for Alice to get a higher net income.
    
    Example 2:
    
    
    >>> mostProfitablePath(edges = [[0,1]], bob = 1, amount = [-7280,2350])
    >>> -7280
    Explanation:
    Alice follows the path 0->1 whereas Bob follows the path 1->0.
    Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
    """
def splitMessage(message: str, limit: int) -> List[str]:
    """
    You are given a string, message, and a positive integer, limit.
    You must split message into one or more parts based on limit. Each resulting part should have the suffix "", where "b" is to be replaced with the total number of parts and "a" is to be replaced with the index of the part, starting from 1 and going up to b. Additionally, the length of each resulting part (including its suffix) should be equal to limit, except for the last part whose length can be at most limit.
    The resulting parts should be formed such that when their suffixes are removed and they are all concatenated in order, they should be equal to message. Also, the result should contain as few parts as possible.
    Return the parts message would be split into as an array of strings. If it is impossible to split message as required, return an empty array.
    
    Example 1:
    
    >>> splitMessage(message = "this is really a very awesome message", limit = 9)
    >>> ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
    Explanation:
    The first 9 parts take 3 characters each from the beginning of message.
    The next 5 parts take 2 characters each to finish splitting message.
    In this example, each part, including the last, has length 9.
    It can be shown it is not possible to split message into less than 14 parts.
    
    Example 2:
    
    >>> splitMessage(message = "short message", limit = 15)
    >>> ["short mess<1/2>","age<2/2>"]
    Explanation:
    Under the given constraints, the string can be split into two parts:
    - The first part comprises of the first 10 characters, and has a length 15.
    - The next part comprises of the last 3 characters, and has a length 8.
    """
def convertTemperature(celsius: float) -> List[float]:
    """
    You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.
    You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].
    Return the array ans. Answers within 10-5 of the actual answer will be accepted.
    Note that:
    
    Kelvin = Celsius + 273.15
    Fahrenheit = Celsius * 1.80 + 32.00
    
    
    Example 1:
    
    >>> convertTemperature(celsius = 36.50)
    >>> [309.65000,97.70000]
    Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
    
    Example 2:
    
    >>> convertTemperature(celsius = 122.11)
    >>> [395.26000,251.79800]
    Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
    """
def subarrayLCM(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray's elements is k.
    A subarray is a contiguous non-empty sequence of elements within an array.
    The least common multiple of an array is the smallest positive integer that is divisible by all the array elements.
    
    Example 1:
    
    >>> subarrayLCM(nums = [3,6,2,7,1], k = 6)
    >>> 4
    Explanation: The subarrays of nums where 6 is the least common multiple of all the subarray's elements are:
    - [3,6,2,7,1]
    - [3,6,2,7,1]
    - [3,6,2,7,1]
    - [3,6,2,7,1]
    
    Example 2:
    
    >>> subarrayLCM(nums = [3], k = 2)
    >>> 0
    Explanation: There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minimumOperations(root: Optional[TreeNode]) -> int:
    """
    You are given the root of a binary tree with unique values.
    In one operation, you can choose any two nodes at the same level and swap their values.
    Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order.
    The level of a node is the number of edges along the path between it and the root node.
    
    Example 1:
    
    
    >>> __init__(root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10])
    >>> 3
    Explanation:
    - Swap 4 and 3. The 2nd level becomes [3,4].
    - Swap 7 and 5. The 3rd level becomes [5,6,8,7].
    - Swap 8 and 7. The 3rd level becomes [5,6,7,8].
    We used 3 operations so return 3.
    It can be proven that 3 is the minimum number of operations needed.
    
    Example 2:
    
    
    >>> __init__(root = [1,3,2,7,6,5,4])
    >>> 3
    Explanation:
    - Swap 3 and 2. The 2nd level becomes [2,3].
    - Swap 7 and 4. The 3rd level becomes [4,6,5,7].
    - Swap 6 and 5. The 3rd level becomes [4,5,6,7].
    We used 3 operations so return 3.
    It can be proven that 3 is the minimum number of operations needed.
    
    Example 3:
    
    
    >>> __init__(root = [1,2,3,4,5,6])
    >>> 0
    Explanation: Each level is already sorted in increasing order so return 0.
    """
def maxPalindromes(s: str, k: int) -> int:
    """
    You are given a string s and a positive integer k.
    Select a set of non-overlapping substrings from the string s that satisfy the following conditions:
    
    The length of each substring is at least k.
    Each substring is a palindrome.
    
    Return the maximum number of substrings in an optimal selection.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> maxPalindromes(s = "abaccdbbd", k = 3)
    >>> 2
    Explanation: We can select the substrings underlined in s = "abaccdbbd". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
    It can be shown that we cannot find a selection with more than two valid substrings.
    
    Example 2:
    
    >>> maxPalindromes(s = "adbcda", k = 2)
    >>> 0
    Explanation: There is no palindrome substring of length at least 2 in the string.
    """
def minCost(n: int, roads: List[List[int]], appleCost: List[int], k: int) -> List[int]:
    """
    You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads, where roads[i] = [ai, bi, costi] indicates that there is a bidirectional road between cities ai and bi with a cost of traveling equal to costi.
    You can buy apples in any city you want, but some cities have different costs to buy apples. You are given the 1-based array appleCost where appleCost[i] is the cost of buying one apple from city i.
    You start at some city, traverse through various roads, and eventually buy exactly one apple from any city. After you buy that apple, you have to return back to the city you started at, but now the cost of all the roads will be multiplied by a given factor k.
    Given the integer k, return a 1-based array answer of size n where answer[i] is the minimum total cost to buy an apple if you start at city i.
    
    Example 1:
    
    
    >>> minCost(n = 4, roads = [[1,2,4],[2,3,2],[2,4,5],[3,4,1],[1,3,4]], appleCost = [56,42,102,301], k = 2)
    >>> [54,42,48,51]
    Explanation: The minimum cost for each starting city is the following:
    - Starting at city 1: You take the path 1 -> 2, buy an apple at city 2, and finally take the path 2 -> 1. The total cost is 4 + 42 + 4 * 2 = 54.
    - Starting at city 2: You directly buy an apple at city 2. The total cost is 42.
    - Starting at city 3: You take the path 3 -> 2, buy an apple at city 2, and finally take the path 2 -> 3. The total cost is 2 + 42 + 2 * 2 = 48.
    - Starting at city 4: You take the path 4 -> 3 -> 2 then you buy at city 2, and finally take the path 2 -> 3 -> 4. The total cost is 1 + 2 + 42 + 1 * 2 + 2 * 2 = 51.
    
    Example 2:
    
    
    >>> minCost(n = 3, roads = [[1,2,5],[2,3,1],[3,1,2]], appleCost = [2,3,1], k = 3)
    >>> [2,3,1]
    Explanation: It is always optimal to buy the apple in the starting city.
    """
def unequalTriplets(nums: List[int]) -> int:
    """
    You are given a 0-indexed array of positive integers nums. Find the number of triplets (i, j, k) that meet the following conditions:
    
    0 <= i < j < k < nums.length
    nums[i], nums[j], and nums[k] are pairwise distinct.
    
    In other words, nums[i] != nums[j], nums[i] != nums[k], and nums[j] != nums[k].
    
    
    
    Return the number of triplets that meet the conditions.
    
    Example 1:
    
    >>> unequalTriplets(nums = [4,4,2,4,3])
    >>> 3
    Explanation: The following triplets meet the conditions:
    - (0, 2, 4) because 4 != 2 != 3
    - (1, 2, 4) because 4 != 2 != 3
    - (2, 3, 4) because 2 != 4 != 3
    Since there are 3 triplets, we return 3.
    Note that (2, 0, 4) is not a valid triplet because 2 > 0.
    
    Example 2:
    
    >>> unequalTriplets(nums = [1,1,1,1,1])
    >>> 0
    Explanation: No triplets meet the conditions so we return 0.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def closestNodes(root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
    """
    You are given the root of a binary search tree and an array queries of size n consisting of positive integers.
    Find a 2D array answer of size n where answer[i] = [mini, maxi]:
    
    mini is the largest value in the tree that is smaller than or equal to queries[i]. If a such value does not exist, add -1 instead.
    maxi is the smallest value in the tree that is greater than or equal to queries[i]. If a such value does not exist, add -1 instead.
    
    Return the array answer.
    
    Example 1:
    
    
    >>> __init__(root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16])
    >>> [[2,2],[4,6],[15,-1]]
    Explanation: We answer the queries in the following way:
    - The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
    - The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
    - The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].
    
    Example 2:
    
    
    >>> __init__(root = [4,null,9], queries = [3])
    >>> [[-1,4]]
    Explanation: The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].
    """
def minimumFuelCost(roads: List[List[int]], seats: int) -> int:
    """
    There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.
    There is a meeting for the representatives of each city. The meeting is in the capital city.
    There is a car in each city. You are given an integer seats that indicates the number of seats in each car.
    A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.
    Return the minimum number of liters of fuel to reach the capital city.
    
    Example 1:
    
    
    >>> minimumFuelCost(roads = [[0,1],[0,2],[0,3]], seats = 5)
    >>> 3
    Explanation:
    - Representative1 goes directly to the capital with 1 liter of fuel.
    - Representative2 goes directly to the capital with 1 liter of fuel.
    - Representative3 goes directly to the capital with 1 liter of fuel.
    It costs 3 liters of fuel at minimum.
    It can be proven that 3 is the minimum number of liters of fuel needed.
    
    Example 2:
    
    
    >>> minimumFuelCost(roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2)
    >>> 7
    Explanation:
    - Representative2 goes directly to city 3 with 1 liter of fuel.
    - Representative2 and representative3 go together to city 1 with 1 liter of fuel.
    - Representative2 and representative3 go together to the capital with 1 liter of fuel.
    - Representative1 goes directly to the capital with 1 liter of fuel.
    - Representative5 goes directly to the capital with 1 liter of fuel.
    - Representative6 goes directly to city 4 with 1 liter of fuel.
    - Representative4 and representative6 go together to the capital with 1 liter of fuel.
    It costs 7 liters of fuel at minimum.
    It can be proven that 7 is the minimum number of liters of fuel needed.
    
    Example 3:
    
    
    >>> minimumFuelCost(roads = [], seats = 1)
    >>> 0
    Explanation: No representatives need to travel to the capital city.
    """
def beautifulPartitions(s: str, k: int, minLength: int) -> int:
    """
    You are given a string s that consists of the digits '1' to '9' and two integers k and minLength.
    A partition of s is called beautiful if:
    
    s is partitioned into k non-intersecting substrings.
    Each substring has a length of at least minLength.
    Each substring starts with a prime digit and ends with a non-prime digit. Prime digits are '2', '3', '5', and '7', and the rest of the digits are non-prime.
    
    Return the number of beautiful partitions of s. Since the answer may be very large, return it modulo 109 + 7.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> beautifulPartitions(s = "23542185131", k = 3, minLength = 2)
    >>> 3
    Explanation: There exists three ways to create a beautiful partition:
    "2354 | 218 | 5131"
    "2354 | 21851 | 31"
    "2354218 | 51 | 31"
    
    Example 2:
    
    >>> beautifulPartitions(s = "23542185131", k = 3, minLength = 3)
    >>> 1
    Explanation: There exists one way to create a beautiful partition: "2354 | 218 | 5131".
    
    Example 3:
    
    >>> beautifulPartitions(s = "3312958", k = 3, minLength = 1)
    >>> 1
    Explanation: There exists one way to create a beautiful partition: "331 | 29 | 58".
    """
def maxXor(n: int, edges: List[List[int]], values: List[int]) -> int:
    """
    There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. The root of the tree is the node labeled 0.
    Each node has an associated value. You are given an array values of length n, where values[i] is the value of the ith node.
    Select any two non-overlapping subtrees. Your score is the bitwise XOR of the sum of the values within those subtrees.
    Return the maximum possible score you can achieve. If it is impossible to find two nonoverlapping subtrees, return 0.
    Note that:
    
    The subtree of a node is the tree consisting of that node and all of its descendants.
    Two subtrees are non-overlapping if they do not share any common node.
    
    
    Example 1:
    
    
    >>> maxXor(n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [2,8,3,6,2,5])
    >>> 24
    Explanation: Node 1's subtree has sum of values 16, while node 2's subtree has sum of values 8, so choosing these nodes will yield a score of 16 XOR 8 = 24. It can be proved that is the maximum possible score we can obtain.
    
    Example 2:
    
    
    >>> maxXor(n = 3, edges = [[0,1],[1,2]], values = [4,6,1])
    >>> 0
    Explanation: There is no possible way to select two non-overlapping subtrees, so we just return 0.
    """
def numberOfCuts(n: int) -> int:
    """
    A valid cut in a circle can be:
    
    A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or
    A cut that is represented by a straight line that touches one point on the edge of the circle and its center.
    
    Some valid and invalid cuts are shown in the figures below.
    
    Given the integer n, return the minimum number of cuts needed to divide a circle into n equal slices.
    
    Example 1:
    
    
    >>> numberOfCuts(n = 4)
    >>> 2
    Explanation:
    The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
    
    Example 2:
    
    
    >>> numberOfCuts(n = 3)
    >>> 3
    Explanation:
    At least 3 cuts are needed to divide the circle into 3 equal slices.
    It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
    Also note that the first cut will not divide the circle into distinct parts.
    """
def onesMinusZeros(grid: List[List[int]]) -> List[List[int]]:
    """
    You are given a 0-indexed m x n binary matrix grid.
    A 0-indexed m x n difference matrix diff is created with the following procedure:
    
    Let the number of ones in the ith row be onesRowi.
    Let the number of ones in the jth column be onesColj.
    Let the number of zeros in the ith row be zerosRowi.
    Let the number of zeros in the jth column be zerosColj.
    diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
    
    Return the difference matrix diff.
    
    Example 1:
    
    
    >>> onesMinusZeros(grid = [[0,1,1],[1,0,1],[0,0,1]])
    >>> [[0,0,4],[0,0,4],[-2,-2,2]]
    Explanation:
    - diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 2 + 1 - 1 - 2 = 0
    - diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 2 + 1 - 1 - 2 = 0
    - diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 2 + 3 - 1 - 0 = 4
    - diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 2 + 1 - 1 - 2 = 0
    - diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 2 + 1 - 1 - 2 = 0
    - diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 2 + 3 - 1 - 0 = 4
    - diff[2][0] = onesRow2 + onesCol0 - zerosRow2 - zerosCol0 = 1 + 1 - 2 - 2 = -2
    - diff[2][1] = onesRow2 + onesCol1 - zerosRow2 - zerosCol1 = 1 + 1 - 2 - 2 = -2
    - diff[2][2] = onesRow2 + onesCol2 - zerosRow2 - zerosCol2 = 1 + 3 - 2 - 0 = 2
    
    Example 2:
    
    
    >>> onesMinusZeros(grid = [[1,1,1],[1,1,1]])
    >>> [[5,5,5],[5,5,5]]
    Explanation:
    - diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 3 + 2 - 0 - 0 = 5
    - diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 3 + 2 - 0 - 0 = 5
    - diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 3 + 2 - 0 - 0 = 5
    - diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 3 + 2 - 0 - 0 = 5
    - diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 3 + 2 - 0 - 0 = 5
    - diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 3 + 2 - 0 - 0 = 5
    """