ShwStone/humaneval-leetcode huggingface dataset:
def collectTheCoins(coins: List[int], edges: List[List[int]]) -> int:
    """
    There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given an 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. You are also given an array coins of size n where coins[i] can be either 0 or 1, where 1 indicates the presence of a coin in the vertex i.
    Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times:
    
    Collect all the coins that are at a distance of at most 2 from the current vertex, or
    Move to any adjacent vertex in the tree.
    
    Find the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex.
    Note that if you pass an edge several times, you need to count it into the answer several times.
    
    Example 1:
    
    
    >>> collectTheCoins(coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]])
    >>> 2
    Explanation: Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2.
    
    Example 2:
    
    
    >>> collectTheCoins(coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]])
    >>> 2
    Explanation: Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2,  collect the coin at vertex 7, then move back to vertex 0.
    """
def minimumTime(hens: List[int], grains: List[int]) -> int:
    """
    There are n hens and m grains on a line. You are given the initial positions of the hens and the grains in two integer arrays hens and grains of size n and m respectively.
    Any hen can eat a grain if they are on the same position. The time taken for this is negligible. One hen can also eat multiple grains.
    In 1 second, a hen can move right or left by 1 unit. The hens can move simultaneously and independently of each other.
    Return the minimum time to eat all grains if the hens act optimally.
    
    Example 1:
    
    >>> minimumTime(hens = [3,6,7], grains = [2,4,7,9])
    >>> 2
    Explanation:
    One of the ways hens eat all grains in 2 seconds is described below:
    - The first hen eats the grain at position 2 in 1 second.
    - The second hen eats the grain at position 4 in 2 seconds.
    - The third hen eats the grains at positions 7 and 9 in 2 seconds.
    So, the maximum time needed is 2.
    It can be proven that the hens cannot eat all grains before 2 seconds.
    Example 2:
    
    >>> minimumTime(hens = [4,6,109,111,213,215], grains = [5,110,214])
    >>> 1
    Explanation:
    One of the ways hens eat all grains in 1 second is described below:
    - The first hen eats the grain at position 5 in 1 second.
    - The fourth hen eats the grain at position 110 in 1 second.
    - The sixth hen eats the grain at position 214 in 1 second.
    - The other hens do not move.
    So, the maximum time needed is 1.
    """
def minNumber(nums1: List[int], nums2: List[int]) -> int:
    """
    Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array.
    
    Example 1:
    
    >>> minNumber(nums1 = [4,1,3], nums2 = [5,7])
    >>> 15
    Explanation: The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
    
    Example 2:
    
    >>> minNumber(nums1 = [3,5,2,6], nums2 = [3,1,7])
    >>> 3
    Explanation: The number 3 contains the digit 3 which exists in both arrays.
    """
def maximumCostSubstring(s: str, chars: str, vals: List[int]) -> int:
    """
    You are given a string s, a string chars of distinct characters and an integer array vals of the same length as chars.
    The cost of the substring is the sum of the values of each character in the substring. The cost of an empty string is considered 0.
    The value of the character is defined in the following way:
    
    If the character is not in the string chars, then its value is its corresponding position (1-indexed) in the alphabet.
    
    
    For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.
    
    
    Otherwise, assuming i is the index where the character occurs in the string chars, then its value is vals[i].
    
    Return the maximum cost among all substrings of the string s.
    
    Example 1:
    
    >>> maximumCostSubstring(s = "adaa", chars = "d", vals = [-1000])
    >>> 2
    Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively.
    The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2.
    It can be proven that 2 is the maximum cost.
    
    Example 2:
    
    >>> maximumCostSubstring(s = "abc", chars = "abc", vals = [-1,-1,-1])
    >>> 0
    Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively.
    The substring with the maximum cost is the empty substring "" and its cost is 0.
    It can be proven that 0 is the maximum cost.
    """
def makeSubKSumEqual(arr: List[int], k: int) -> int:
    """
    You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.
    You can do the following operation any number of times:
    
    Pick any element from arr and increase or decrease it by 1.
    
    Return the minimum number of operations such that the sum of each subarray of length k is equal.
    A subarray is a contiguous part of the array.
    
    Example 1:
    
    >>> makeSubKSumEqual(arr = [1,4,1,3], k = 2)
    >>> 1
    Explanation: we can do one operation on index 1 to make its value equal to 3.
    The array after the operation is [1,3,1,3]
    - Subarray starts at index 0 is [1, 3], and its sum is 4
    - Subarray starts at index 1 is [3, 1], and its sum is 4
    - Subarray starts at index 2 is [1, 3], and its sum is 4
    - Subarray starts at index 3 is [3, 1], and its sum is 4
    
    Example 2:
    
    >>> makeSubKSumEqual(arr = [2,5,5,7], k = 3)
    >>> 5
    Explanation: we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
    The array after the operations is [5,5,5,5]
    - Subarray starts at index 0 is [5, 5, 5], and its sum is 15
    - Subarray starts at index 1 is [5, 5, 5], and its sum is 15
    - Subarray starts at index 2 is [5, 5, 5], and its sum is 15
    - Subarray starts at index 3 is [5, 5, 5], and its sum is 15
    """
def findShortestCycle(n: int, edges: List[List[int]]) -> int:
    """
    There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1. The edges in the graph are represented by a given 2D integer array edges, where edges[i] = [ui, vi] denotes an edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
    Return the length of the shortest cycle in the graph. If no cycle exists, return -1.
    A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.
    
    Example 1:
    
    
    >>> findShortestCycle(n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]])
    >>> 3
    Explanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0
    
    Example 2:
    
    
    >>> findShortestCycle(n = 4, edges = [[0,1],[0,2]])
    >>> -1
    Explanation: There are no cycles in this graph.
    """
def findTheLongestBalancedSubstring(s: str) -> int:
    """
    You are given a binary string s consisting only of zeroes and ones.
    A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.
    Return the length of the longest balanced substring of s.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> findTheLongestBalancedSubstring(s = "01000111")
    >>> 6
    Explanation: The longest balanced substring is "000111", which has length 6.
    
    Example 2:
    
    >>> findTheLongestBalancedSubstring(s = "00111")
    >>> 4
    Explanation: The longest balanced substring is "0011", which has length 4.
    
    Example 3:
    
    >>> findTheLongestBalancedSubstring(s = "111")
    >>> 0
    Explanation: There is no balanced substring except the empty substring, so the answer is 0.
    """
def findMatrix(nums: List[int]) -> List[List[int]]:
    """
    You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:
    
    The 2D array should contain only the elements of the array nums.
    Each row in the 2D array contains distinct integers.
    The number of rows in the 2D array should be minimal.
    
    Return the resulting array. If there are multiple answers, return any of them.
    Note that the 2D array can have a different number of elements on each row.
    
    Example 1:
    
    >>> findMatrix(nums = [1,3,4,1,2,3,1])
    >>> [[1,3,4,2],[1,3],[1]]
    Explanation: We can create a 2D array that contains the following rows:
    - 1,3,4,2
    - 1,3
    - 1
    All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
    It can be shown that we cannot have less than 3 rows in a valid array.
    Example 2:
    
    >>> findMatrix(nums = [1,2,3,4])
    >>> [[4,3,2,1]]
    Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
    """
def miceAndCheese(reward1: List[int], reward2: List[int], k: int) -> int:
    """
    There are two mice and n different types of cheese, each type of cheese should be eaten by exactly one mouse.
    A point of the cheese with index i (0-indexed) is:
    
    reward1[i] if the first mouse eats it.
    reward2[i] if the second mouse eats it.
    
    You are given a positive integer array reward1, a positive integer array reward2, and a non-negative integer k.
    Return the maximum points the mice can achieve if the first mouse eats exactly k types of cheese.
    
    Example 1:
    
    >>> miceAndCheese(reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2)
    >>> 15
    Explanation: In this example, the first mouse eats the 2nd (0-indexed) and the 3rd types of cheese, and the second mouse eats the 0th and the 1st types of cheese.
    The total points are 4 + 4 + 3 + 4 = 15.
    It can be proven that 15 is the maximum total points that the mice can achieve.
    
    Example 2:
    
    >>> miceAndCheese(reward1 = [1,1], reward2 = [1,1], k = 2)
    >>> 2
    Explanation: In this example, the first mouse eats the 0th (0-indexed) and 1st types of cheese, and the second mouse does not eat any cheese.
    The total points are 1 + 1 = 2.
    It can be proven that 2 is the maximum total points that the mice can achieve.
    """
def beautifulPair(nums1: List[int], nums2: List[int]) -> List[int]:
    """
    You are given two 0-indexed integer arrays nums1 and nums2 of the same length. A pair of indices (i,j) is called beautiful if|nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is the smallest amongst all possible indices pairs where i < j.
    Return the beautiful pair. In the case that there are multiple beautiful pairs, return the lexicographically smallest pair.
    Note that
    
    |x| denotes the absolute value of x.
    A pair of indices (i1, j1) is lexicographically smaller than (i2, j2) if i1 < i2 or i1 == i2 and j1 < j2.
    
    
    Example 1:
    
    >>> beautifulPair(nums1 = [1,2,3,2,4], nums2 = [2,3,1,2,3])
    >>> [0,3]
    Explanation: Consider index 0 and index 3. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
    
    Example 2:
    
    >>> beautifulPair(nums1 = [1,2,4,3,2,5], nums2 = [1,4,2,3,5,1])
    >>> [1,4]
    Explanation: Consider index 1 and index 4. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
    """
def diagonalPrime(nums: List[List[int]]) -> int:
    """
    You are given a 0-indexed two-dimensional integer array nums.
    Return the largest prime number that lies on at least one of the diagonals of nums. In case, no prime is present on any of the diagonals, return 0.
    Note that:
    
    An integer is prime if it is greater than 1 and has no positive integer divisors other than 1 and itself.
    An integer val is on one of the diagonals of nums if there exists an integer i for which nums[i][i] = val or an i for which nums[i][nums.length - i - 1] = val.
    
    
    In the above diagram, one diagonal is [1,5,9] and another diagonal is [3,5,7].
    
    Example 1:
    
    >>> diagonalPrime(nums = [[1,2,3],[5,6,7],[9,10,11]])
    >>> 11
    Explanation: The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
    
    Example 2:
    
    >>> diagonalPrime(nums = [[1,2,3],[5,17,7],[9,11,10]])
    >>> 17
    Explanation: The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
    """
def distance(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed integer array nums. There exists an array arr of length nums.length, where arr[i] is the sum of |i - j| over all j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i] to be 0.
    Return the array arr.
    
    Example 1:
    
    >>> distance(nums = [1,3,1,1,2])
    >>> [5,0,3,4,0]
    Explanation:
    When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
    When i = 1, arr[1] = 0 because there is no other index with value 3.
    When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
    When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
    When i = 4, arr[4] = 0 because there is no other index with value 2.
    
    
    Example 2:
    
    >>> distance(nums = [0,5,3])
    >>> [0,0,0]
    Explanation: Since each element in nums is distinct, arr[i] = 0 for all i.
    """
def minimizeMax(nums: List[int], p: int) -> int:
    """
    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs.
    Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents the absolute value of x.
    Return the minimum maximum difference among all p pairs. We define the maximum of an empty set to be zero.
    
    Example 1:
    
    >>> minimizeMax(nums = [10,1,2,7,1,3], p = 2)
    >>> 1
    Explanation: The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
    The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
    
    Example 2:
    
    >>> minimizeMax(nums = [4,2,1,2], p = 1)
    >>> 0
    Explanation: Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
    """
def minimumVisitedCells(grid: List[List[int]]) -> int:
    """
    You are given a 0-indexed m x n integer matrix grid. Your initial position is at the top-left cell (0, 0).
    Starting from the cell (i, j), you can move to one of the following cells:
    
    Cells (i, k) with j < k <= grid[i][j] + j (rightward movement), or
    Cells (k, j) with i < k <= grid[i][j] + i (downward movement).
    
    Return the minimum number of cells you need to visit to reach the bottom-right cell (m - 1, n - 1). If there is no valid path, return -1.
    
    Example 1:
    
    
    >>> minimumVisitedCells(grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]])
    >>> 4
    Explanation: The image above shows one of the paths that visits exactly 4 cells.
    
    Example 2:
    
    
    >>> minimumVisitedCells(grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]])
    >>> 3
    Explanation: The image above shows one of the paths that visits exactly 3 cells.
    
    Example 3:
    
    
    >>> minimumVisitedCells(grid = [[2,1,0],[1,0,0]])
    >>> -1
    Explanation: It can be proven that no path exists.
    """
def countTheNumOfKFreeSubsets(nums: List[int], k: int) -> int:
    """
    You are given an integer array nums, which contains distinct elements and an integer k.
    A subset is called a k-Free subset if it contains no two elements with an absolute difference equal to k. Notice that the empty set is a k-Free subset.
    Return the number of k-Free subsets of nums.
    A subset of an array is a selection of elements (possibly none) of the array.
    
    Example 1:
    
    >>> countTheNumOfKFreeSubsets(nums = [5,4,6], k = 1)
    >>> 5
    Explanation: There are 5 valid subsets: {}, {5}, {4}, {6} and {4, 6}.
    
    Example 2:
    
    >>> countTheNumOfKFreeSubsets(nums = [2,3,5,8], k = 5)
    >>> 12
    Explanation: There are 12 valid subsets: {}, {2}, {3}, {5}, {8}, {2, 3}, {2, 3, 5}, {2, 5}, {2, 5, 8}, {2, 8}, {3, 5} and {5, 8}.
    
    Example 3:
    
    >>> countTheNumOfKFreeSubsets(nums = [10,5,9,11], k = 20)
    >>> 16
    Explanation: All subsets are valid. Since the total count of subsets is 24 = 16, so the answer is 16.
    """
def findColumnWidth(grid: List[List[int]]) -> List[int]:
    """
    You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.
    
    For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3.
    
    Return an integer array ans of size n where ans[i] is the width of the ith column.
    The length of an integer x with len digits is equal to len if x is non-negative, and len + 1 otherwise.
    
    Example 1:
    
    >>> findColumnWidth(grid = [[1],[22],[333]])
    >>> [3]
    Explanation: In the 0th column, 333 is of length 3.
    
    Example 2:
    
    >>> findColumnWidth(grid = [[-15,1,3],[15,7,12],[5,6,-2]])
    >>> [3,1,2]
    Explanation:
    In the 0th column, only -15 is of length 3.
    In the 1st column, all integers are of length 1.
    In the 2nd column, both 12 and -2 are of length 2.
    """
def findPrefixScore(nums: List[int]) -> List[int]:
    """
    We define the conversion array conver of an array arr as follows:
    
    conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.
    
    We also define the score of an array arr as the sum of the values of the conversion array of arr.
    Given a 0-indexed integer array nums of length n, return an array ans of length n where ans[i] is the score of the prefix nums[0..i].
    
    Example 1:
    
    >>> findPrefixScore(nums = [2,3,7,5,10])
    >>> [4,10,24,36,56]
    Explanation:
    For the prefix [2], the conversion array is [4] hence the score is 4
    For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
    For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
    For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
    For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
    
    Example 2:
    
    >>> findPrefixScore(nums = [1,1,2,4,8,16])
    >>> [2,4,8,16,32,64]
    Explanation:
    For the prefix [1], the conversion array is [2] hence the score is 2
    For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
    For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
    For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
    For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
    For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def replaceValueInTree(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values.
    Two nodes of a binary tree are cousins if they have the same depth with different parents.
    Return the root of the modified tree.
    Note that the depth of a node is the number of edges in the path from the root node to it.
    
    Example 1:
    
    
    >>> __init__(root = [5,4,9,1,10,null,7])
    >>> [0,0,0,7,7,null,11]
    Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
    - Node with value 5 does not have any cousins so its sum is 0.
    - Node with value 4 does not have any cousins so its sum is 0.
    - Node with value 9 does not have any cousins so its sum is 0.
    - Node with value 1 has a cousin with value 7 so its sum is 7.
    - Node with value 10 has a cousin with value 7 so its sum is 7.
    - Node with value 7 has cousins with values 1 and 10 so its sum is 11.
    
    Example 2:
    
    
    >>> __init__(root = [3,1,2])
    >>> [0,0,0]
    Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
    - Node with value 3 does not have any cousins so its sum is 0.
    - Node with value 1 does not have any cousins so its sum is 0.
    - Node with value 2 does not have any cousins so its sum is 0.
    """
def rowAndMaximumOnes(mat: List[List[int]]) -> List[int]:
    """
    Given a m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.
    In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.
    Return an array containing the index of the row, and the number of ones in it.
    
    Example 1:
    
    >>> rowAndMaximumOnes(mat = [[0,1],[1,0]])
    >>> [0,1]
    Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1). So, the answer is [0,1].
    
    Example 2:
    
    >>> rowAndMaximumOnes(mat = [[0,0,0],[0,1,1]])
    >>> [1,2]
    Explanation: The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].
    
    Example 3:
    
    >>> rowAndMaximumOnes(mat = [[0,0],[1,1],[0,0]])
    >>> [1,2]
    Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
    """