ShwStone/humaneval-leetcode huggingface dataset:
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxSumBST(root: Optional[TreeNode]) -> int:
    """
    Given a binary tree root, return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST).
    Assume a BST is defined as follows:
    
    The left subtree of a node contains only nodes with keys less than the node's key.
    The right subtree of a node contains only nodes with keys greater than the node's key.
    Both the left and right subtrees must also be binary search trees.
    
    
    Example 1:
    
    
    >>> __init__(root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6])
    >>> 20
    Explanation: Maximum sum in a valid Binary search tree is obtained in root node with key equal to 3.
    
    Example 2:
    
    
    >>> __init__(root = [4,3,null,1,2])
    >>> 2
    Explanation: Maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2.
    
    Example 3:
    
    >>> __init__(root = [-4,-2,-5])
    >>> 0
    Explanation: All values are negatives. Return an empty BST.
    """
def generateTheString(n: int) -> str:
    """
    Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.
    The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.
    
    Example 1:
    
    >>> generateTheString(n = 4)
    >>> "pppz"
    Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
    
    Example 2:
    
    >>> generateTheString(n = 2)
    >>> "xy"
    Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".
    
    Example 3:
    
    >>> generateTheString(n = 7)
    >>> "holasss"
    """
def numTimesAllBlue(flips: List[int]) -> int:
    """
    You have a 1-indexed binary string of length n where all the bits are 0 initially. We will flip all the bits of this binary string (i.e., change them from 0 to 1) one by one. You are given a 1-indexed integer array flips where flips[i] indicates that the bit at index i will be flipped in the ith step.
    A binary string is prefix-aligned if, after the ith step, all the bits in the inclusive range [1, i] are ones and all the other bits are zeros.
    Return the number of times the binary string is prefix-aligned during the flipping process.
    
    Example 1:
    
    >>> numTimesAllBlue(flips = [3,2,4,1,5])
    >>> 2
    Explanation: The binary string is initially "00000".
    After applying step 1: The string becomes "00100", which is not prefix-aligned.
    After applying step 2: The string becomes "01100", which is not prefix-aligned.
    After applying step 3: The string becomes "01110", which is not prefix-aligned.
    After applying step 4: The string becomes "11110", which is prefix-aligned.
    After applying step 5: The string becomes "11111", which is prefix-aligned.
    We can see that the string was prefix-aligned 2 times, so we return 2.
    
    Example 2:
    
    >>> numTimesAllBlue(flips = [4,1,2,3])
    >>> 1
    Explanation: The binary string is initially "0000".
    After applying step 1: The string becomes "0001", which is not prefix-aligned.
    After applying step 2: The string becomes "1001", which is not prefix-aligned.
    After applying step 3: The string becomes "1101", which is not prefix-aligned.
    After applying step 4: The string becomes "1111", which is prefix-aligned.
    We can see that the string was prefix-aligned 1 time, so we return 1.
    """
def numOfMinutes(n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
    """
    A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.
    Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.
    The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
    The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
    Return the number of minutes needed to inform all the employees about the urgent news.
    
    Example 1:
    
    >>> numOfMinutes(n = 1, headID = 0, manager = [-1], informTime = [0])
    >>> 0
    Explanation: The head of the company is the only employee in the company.
    
    Example 2:
    
    
    >>> numOfMinutes(n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0])
    >>> 1
    Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
    The tree structure of the employees in the company is shown.
    """
def frogPosition(n: int, edges: List[List[int]], t: int, target: int) -> float:
    """
    Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from vertex 1. In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. The frog can not jump back to a visited vertex. In case the frog can jump to several vertices, it jumps randomly to one of them with the same probability. Otherwise, when the frog can not jump to any unvisited vertex, it jumps forever on the same vertex.
    The edges of the undirected tree are given in the array edges, where edges[i] = [ai, bi] means that exists an edge connecting the vertices ai and bi.
    Return the probability that after t seconds the frog is on the vertex target. Answers within 10-5 of the actual answer will be accepted.
    
    Example 1:
    
    
    >>> frogPosition(n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4)
    >>> 0.16666666666666666
    Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and then jumping with 1/2 probability to vertex 4 after second 2. Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666.
    
    Example 2:
    
    
    >>> frogPosition(n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7)
    >>> 0.3333333333333333
    Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 = 0.3333333333333333 probability to the vertex 7 after second 1.
    """
# class TreeNode:
#     def __init__(x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def getTargetCopy(original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
    """
    Given two binary trees original and cloned and given a reference to a node target in the original tree.
    The cloned tree is a copy of the original tree.
    Return a reference to the same node in the cloned tree.
    Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.
    
    Example 1:
    
    
    >>> __init__(tree = [7,4,3,null,null,6,19], target = 3)
    >>> 3
    Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.
    
    Example 2:
    
    
    >>> __init__(tree = [7], target =  7)
    >>> 7
    
    Example 3:
    
    
    >>> __init__(tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4)
    >>> 4
    """
def luckyNumbers(matrix: List[List[int]]) -> List[int]:
    """
    Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.
    A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
    
    Example 1:
    
    >>> luckyNumbers(matrix = [[3,7,8],[9,11,13],[15,16,17]])
    >>> [15]
    Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
    
    Example 2:
    
    >>> luckyNumbers(matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]])
    >>> [12]
    Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
    
    Example 3:
    
    >>> luckyNumbers(matrix = [[7,8],[1,2]])
    >>> [7]
    Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def balanceBST(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them.
    A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1.
    
    Example 1:
    
    
    >>> __init__(root = [1,null,2,null,3,null,4,null,null])
    >>> [2,1,3,null,null,null,4]
    Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
    
    Example 2:
    
    
    >>> __init__(root = [2,1,3])
    >>> [2,1,3]
    """
def maxPerformance(n: int, speed: List[int], efficiency: List[int], k: int) -> int:
    """
    You are given two integers n and k and two integer arrays speed and efficiency both of length n. There are n engineers numbered from 1 to n. speed[i] and efficiency[i] represent the speed and efficiency of the ith engineer respectively.
    Choose at most k different engineers out of the n engineers to form a team with the maximum performance.
    The performance of a team is the sum of its engineers' speeds multiplied by the minimum efficiency among its engineers.
    Return the maximum performance of this team. Since the answer can be a huge number, return it modulo 109 + 7.
    
    Example 1:
    
    >>> maxPerformance(n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2)
    >>> 60
    Explanation:
    We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
    
    Example 2:
    
    >>> maxPerformance(n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3)
    >>> 68
    Explanation:
    This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
    
    Example 3:
    
    >>> maxPerformance(n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4)
    >>> 72
    """
def findTheDistanceValue(arr1: List[int], arr2: List[int], d: int) -> int:
    """
    Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.
    The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.
    
    Example 1:
    
    >>> findTheDistanceValue(arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2)
    >>> 2
    Explanation:
    For arr1[0]=4 we have:
    |4-10|=6 > d=2
    |4-9|=5 > d=2
    |4-1|=3 > d=2
    |4-8|=4 > d=2
    For arr1[1]=5 we have:
    |5-10|=5 > d=2
    |5-9|=4 > d=2
    |5-1|=4 > d=2
    |5-8|=3 > d=2
    For arr1[2]=8 we have:
    |8-10|=2 <= d=2
    |8-9|=1 <= d=2
    |8-1|=7 > d=2
    |8-8|=0 <= d=2
    
    Example 2:
    
    >>> findTheDistanceValue(arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3)
    >>> 2
    
    Example 3:
    
    >>> findTheDistanceValue(arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6)
    >>> 1
    """
def maxNumberOfFamilies(n: int, reservedSeats: List[List[int]]) -> int:
    """
    A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.
    Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved.
    Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.
    
    Example 1:
    
    
    >>> maxNumberOfFamilies(n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]])
    >>> 4
    Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.
    
    Example 2:
    
    >>> maxNumberOfFamilies(n = 2, reservedSeats = [[2,1],[1,8],[2,6]])
    >>> 2
    
    Example 3:
    
    >>> maxNumberOfFamilies(n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]])
    >>> 4
    """
def getKth(lo: int, hi: int, k: int) -> int:
    """
    The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps:
    
    if x is even then x = x / 2
    if x is odd then x = 3 * x + 1
    
    For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).
    Given three integers lo, hi and k. The task is to sort all integers in the interval [lo, hi] by the power value in ascending order, if two or more integers have the same power value sort them by ascending order.
    Return the kth integer in the range [lo, hi] sorted by the power value.
    Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform into 1 using these steps and that the power of x is will fit in a 32-bit signed integer.
    
    Example 1:
    
    >>> getKth(lo = 12, hi = 15, k = 2)
    >>> 13
    Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
    The power of 13 is 9
    The power of 14 is 17
    The power of 15 is 17
    The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.
    Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.
    
    Example 2:
    
    >>> getKth(lo = 7, hi = 11, k = 4)
    >>> 7
    Explanation: The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].
    The interval sorted by power is [8, 10, 11, 7, 9].
    The fourth number in the sorted array is 7.
    """
def maxSizeSlices(slices: List[int]) -> int:
    """
    There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows:
    
    You will pick any pizza slice.
    Your friend Alice will pick the next slice in the anti-clockwise direction of your pick.
    Your friend Bob will pick the next slice in the clockwise direction of your pick.
    Repeat until there are no more slices of pizzas.
    
    Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.
    
    Example 1:
    
    
    >>> maxSizeSlices(slices = [1,2,3,4,5,6])
    >>> 10
    Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6.
    
    Example 2:
    
    
    >>> maxSizeSlices(slices = [8,9,8,6,1,1])
    >>> 16
    Explanation: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8.
    """
def createTargetArray(nums: List[int], index: List[int]) -> List[int]:
    """
    Given two arrays of integers nums and index. Your task is to create target array under the following rules:
    
    Initially target array is empty.
    From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
    Repeat the previous step until there are no elements to read in nums and index.
    
    Return the target array.
    It is guaranteed that the insertion operations will be valid.
    
    Example 1:
    
    >>> createTargetArray(nums = [0,1,2,3,4], index = [0,1,2,2,1])
    >>> [0,4,1,3,2]
    Explanation:
    nums       index     target
    0            0        [0]
    1            1        [0,1]
    2            2        [0,1,2]
    3            2        [0,1,3,2]
    4            1        [0,4,1,3,2]
    
    Example 2:
    
    >>> createTargetArray(nums = [1,2,3,4,0], index = [0,1,2,3,0])
    >>> [0,1,2,3,4]
    Explanation:
    nums       index     target
    1            0        [1]
    2            1        [1,2]
    3            2        [1,2,3]
    4            3        [1,2,3,4]
    0            0        [0,1,2,3,4]
    
    Example 3:
    
    >>> createTargetArray(nums = [1], index = [0])
    >>> [1]
    """
def sumFourDivisors(nums: List[int]) -> int:
    """
    Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.
    
    Example 1:
    
    >>> sumFourDivisors(nums = [21,4,7])
    >>> 32
    Explanation:
    21 has 4 divisors: 1, 3, 7, 21
    4 has 3 divisors: 1, 2, 4
    7 has 2 divisors: 1, 7
    The answer is the sum of divisors of 21 only.
    
    Example 2:
    
    >>> sumFourDivisors(nums = [21,21])
    >>> 64
    
    Example 3:
    
    >>> sumFourDivisors(nums = [1,2,3,4,5])
    >>> 0
    """
def hasValidPath(grid: List[List[int]]) -> bool:
    """
    You are given an m x n grid. Each cell of grid represents a street. The street of grid[i][j] can be:
    
    1 which means a street connecting the left cell and the right cell.
    2 which means a street connecting the upper cell and the lower cell.
    3 which means a street connecting the left cell and the lower cell.
    4 which means a street connecting the right cell and the lower cell.
    5 which means a street connecting the left cell and the upper cell.
    6 which means a street connecting the right cell and the upper cell.
    
    
    You will initially start at the street of the upper-left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1). The path should only follow the streets.
    Notice that you are not allowed to change any street.
    Return true if there is a valid path in the grid or false otherwise.
    
    Example 1:
    
    
    >>> hasValidPath(grid = [[2,4,3],[6,5,2]])
    >>> true
    Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).
    
    Example 2:
    
    
    >>> hasValidPath(grid = [[1,2,1],[1,2,1]])
    >>> false
    Explanation: As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)
    
    Example 3:
    
    >>> hasValidPath(grid = [[1,1,2]])
    >>> false
    Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
    """
def longestPrefix(s: str) -> str:
    """
    A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself).
    Given a string s, return the longest happy prefix of s. Return an empty string "" if no such prefix exists.
    
    Example 1:
    
    >>> longestPrefix(s = "level")
    >>> "l"
    Explanation: s contains 4 prefix excluding itself ("l", "le", "lev", "leve"), and suffix ("l", "el", "vel", "evel"). The largest prefix which is also suffix is given by "l".
    
    Example 2:
    
    >>> longestPrefix(s = "ababab")
    >>> "abab"
    Explanation: "abab" is the largest prefix which is also suffix. They can overlap in the original string.
    """
def findLucky(arr: List[int]) -> int:
    """
    Given an array of integers arr, a lucky integer is an integer that has a frequency in the array equal to its value.
    Return the largest lucky integer in the array. If there is no lucky integer return -1.
    
    Example 1:
    
    >>> findLucky(arr = [2,2,3,4])
    >>> 2
    Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
    
    Example 2:
    
    >>> findLucky(arr = [1,2,2,3,3,3])
    >>> 3
    Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
    
    Example 3:
    
    >>> findLucky(arr = [2,2,2,3,3])
    >>> -1
    Explanation: There are no lucky numbers in the array.
    """
def numTeams(rating: List[int]) -> int:
    """
    There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
    You have to form a team of 3 soldiers amongst them under the following rules:
    
    Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
    A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
    
    Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
    
    Example 1:
    
    >>> numTeams(rating = [2,5,3,4,1])
    >>> 3
    Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
    
    Example 2:
    
    >>> numTeams(rating = [2,1,3])
    >>> 0
    Explanation: We can't form any team given the conditions.
    
    Example 3:
    
    >>> numTeams(rating = [1,2,3,4])
    >>> 4
    """
def findGoodStrings(n: int, s1: str, s2: str, evil: str) -> int:
    """
    Given the strings s1 and s2 of size n and the string evil, return the number of good strings.
    A good string has size n, it is alphabetically greater than or equal to s1, it is alphabetically smaller than or equal to s2, and it does not contain the string evil as a substring. Since the answer can be a huge number, return this modulo 109 + 7.
    
    Example 1:
    
    >>> findGoodStrings(n = 2, s1 = "aa", s2 = "da", evil = "b")
    >>> 51
    Explanation: There are 25 good strings starting with 'a': "aa","ac","ad",...,"az". Then there are 25 good strings starting with 'c': "ca","cc","cd",...,"cz" and finally there is one good string starting with 'd': "da".
    
    Example 2:
    
    >>> findGoodStrings(n = 8, s1 = "leetcode", s2 = "leetgoes", evil = "leet")
    >>> 0
    Explanation: All strings greater than or equal to s1 and smaller than or equal to s2 start with the prefix "leet", therefore, there is not any good string.
    
    Example 3:
    
    >>> findGoodStrings(n = 2, s1 = "gx", s2 = "gz", evil = "x")
    >>> 2
    """
def countLargestGroup(n: int) -> int:
    """
    You are given an integer n.
    Each number from 1 to n is grouped according to the sum of its digits.
    Return the number of groups that have the largest size.
    
    Example 1:
    
    >>> countLargestGroup(n = 13)
    >>> 4
    Explanation: There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:
    [1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9].
    There are 4 groups with largest size.
    
    Example 2:
    
    >>> countLargestGroup(n = 2)
    >>> 2
    Explanation: There are 2 groups [1], [2] of size 1.
    """