ShwStone/humaneval-leetcode huggingface dataset:
def defangIPaddr(address: str) -> str:
    """
    Given a valid (IPv4) IP address, return a defanged version of that IP address.\r
    \r
    A defanged IP address replaces every period "." with "[.]".\r
    \r
     \r
    Example 1:\r
    >>> defangIPaddr(address = "1.1.1.1"\r)
    >>> "1[.]1[.]1[.]1"\r
    Example 2:\r
    >>> defangIPaddr(address = "255.100.50.0"\r)
    >>> "255[.]100[.]50[.]0"\r
    \r
     \r
    """
def corpFlightBookings(bookings: List[List[int]], n: int) -> List[int]:
    """
    There are n flights that are labeled from 1 to n.
    You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi seats reserved for each flight in the range.
    Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i.
    
    Example 1:
    
    >>> corpFlightBookings(bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5)
    >>> [10,55,45,25,25]
    Explanation:
    Flight labels:        1   2   3   4   5
    Booking 1 reserved:  10  10
    Booking 2 reserved:      20  20
    Booking 3 reserved:      25  25  25  25
    Total seats:         10  55  45  25  25
    Hence, answer = [10,55,45,25,25]
    
    Example 2:
    
    >>> corpFlightBookings(bookings = [[1,2,10],[2,2,15]], n = 2)
    >>> [10,25]
    Explanation:
    Flight labels:        1   2
    Booking 1 reserved:  10  10
    Booking 2 reserved:      15
    Total seats:         10  25
    Hence, answer = [10,25]
    """
def maxDepthAfterSplit(seq: str) -> List[int]:
    """
    A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:\r
    \r
    \r
    	It is the empty string, or\r
    	It can be written as AB (A concatenated with B), where A and B are VPS's, or\r
    	It can be written as (A), where A is a VPS.\r
    \r
    \r
    We can similarly define the nesting depth depth(S) of any VPS S as follows:\r
    \r
    \r
    	depth("") = 0\r
    	depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's\r
    	depth("(" + A + ")") = 1 + depth(A), where A is a VPS.\r
    \r
    \r
    For example,  "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.\r
    \r
     \r
    \r
    Given a VPS seq, split it into two disjoint subsequences A and B, such that A and B are VPS's (and A.length + B.length = seq.length).\r
    \r
    Now choose any such A and B such that max(depth(A), depth(B)) is the minimum possible value.\r
    \r
    Return an answer array (of length seq.length) that encodes such a choice of A and B:  answer[i] = 0 if seq[i] is part of A, else answer[i] = 1.  Note that even though multiple answers may exist, you may return any of them.\r
    
    
    Example 1:
    
    >>> maxDepthAfterSplit(seq = "(()())")
    >>> [0,1,1,1,1,0]
    
    Example 2:
    
    >>> maxDepthAfterSplit(seq = "()(())()")
    >>> [0,0,0,1,1,0,1,1]
    """
def numberOfDays(year: int, month: int) -> int:
    """
    Given a year year and a month month, return the number of days of that month.
    
    Example 1:
    >>> numberOfDays(year = 1992, month = 7)
    >>> 31
    Example 2:
    >>> numberOfDays(year = 2000, month = 2)
    >>> 29
    Example 3:
    >>> numberOfDays(year = 1900, month = 2)
    >>> 28
    """
def removeVowels(s: str) -> str:
    """
    Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string.
    
    Example 1:
    
    >>> removeVowels(s = "leetcodeisacommunityforcoders")
    >>> "ltcdscmmntyfrcdrs"
    
    Example 2:
    
    >>> removeVowels(s = "aeiou")
    >>> ""
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maximumAverageSubtree(root: Optional[TreeNode]) -> float:
    """
    Given the root of a binary tree, return the maximum average value of a subtree of that tree. Answers within 10-5 of the actual answer will be accepted.
    A subtree of a tree is any node of that tree plus all its descendants.
    The average value of a tree is the sum of its values, divided by the number of nodes.
    
    Example 1:
    
    
    >>> __init__(root = [5,6,1])
    >>> 6.00000
    Explanation:
    For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4.
    For the node with value = 6 we have an average of 6 / 1 = 6.
    For the node with value = 1 we have an average of 1 / 1 = 1.
    So the answer is 6 which is the maximum.
    
    Example 2:
    
    >>> __init__(root = [0,null,1])
    >>> 1.00000
    """
def canDivideIntoSubsequences(nums: List[int], k: int) -> bool:
    """
    Given an integer array nums sorted in non-decreasing order and an integer k, return true if this array can be divided into one or more disjoint increasing subsequences of length at least k, or false otherwise.
    
    Example 1:
    
    >>> canDivideIntoSubsequences(nums = [1,2,2,3,3,4,4], k = 3)
    >>> true
    Explanation: The array can be divided into two subsequences [1,2,3,4] and [2,3,4] with lengths at least 3 each.
    
    Example 2:
    
    >>> canDivideIntoSubsequences(nums = [5,6,6,7,8], k = 3)
    >>> false
    Explanation: There is no way to divide the array using the conditions required.
    """
def relativeSortArray(arr1: List[int], arr2: List[int]) -> List[int]:
    """
    Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
    Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.
    
    Example 1:
    
    >>> relativeSortArray(arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6])
    >>> [2,2,2,1,4,3,3,9,6,7,19]
    
    Example 2:
    
    >>> relativeSortArray(arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6])
    >>> [22,28,8,6,17,44]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def lcaDeepestLeaves(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary tree, return the lowest common ancestor of its deepest leaves.
    Recall that:
    
    The node of a binary tree is a leaf if and only if it has no children
    The depth of the root of the tree is 0. if the depth of a node is d, the depth of each of its children is d + 1.
    The lowest common ancestor of a set S of nodes, is the node A with the largest depth such that every node in S is in the subtree with root A.
    
    
    Example 1:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4])
    >>> [2,7,4]
    Explanation: We return the node with value 2, colored in yellow in the diagram.
    The nodes coloured in blue are the deepest leaf-nodes of the tree.
    Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
    Example 2:
    
    >>> __init__(root = [1])
    >>> [1]
    Explanation: The root is the deepest node in the tree, and it's the lca of itself.
    
    Example 3:
    
    >>> __init__(root = [0,1,3,null,2])
    >>> [2]
    Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.
    """
def longestWPI(hours: List[int]) -> int:
    """
    We are given hours, a list of the number of hours worked per day for a given employee.
    A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8.
    A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.
    Return the length of the longest well-performing interval.
    
    Example 1:
    
    >>> longestWPI(hours = [9,9,6,0,6,6,9])
    >>> 3
    Explanation: The longest well-performing interval is [9,9,6].
    
    Example 2:
    
    >>> longestWPI(hours = [6,6,6])
    >>> 0
    """
def smallestSufficientTeam(req_skills: List[str], people: List[List[str]]) -> List[int]:
    """
    In a project, you have a list of required skills req_skills, and a list of people. The ith person people[i] contains a list of skills that the person has.
    Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person.
    
    For example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
    
    Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.
    It is guaranteed an answer exists.
    
    Example 1:
    >>> smallestSufficientTeam(req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]])
    >>> [0,2]
    Example 2:
    >>> smallestSufficientTeam(req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]])
    >>> [1,2]
    """
def numEquivDominoPairs(dominoes: List[List[int]]) -> int:
    """
    Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino.
    Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
    
    Example 1:
    
    >>> numEquivDominoPairs(dominoes = [[1,2],[2,1],[3,4],[5,6]])
    >>> 1
    
    Example 2:
    
    >>> numEquivDominoPairs(dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]])
    >>> 3
    """
def shortestAlternatingPaths(n: int, redEdges: List[List[int]], blueEdges: List[List[int]]) -> List[int]:
    """
    You are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to n - 1. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.
    You are given two arrays redEdges and blueEdges where:
    
    redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the graph, and
    blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in the graph.
    
    Return an array answer of length n, where each answer[x] is the length of the shortest path from node 0 to node x such that the edge colors alternate along the path, or -1 if such a path does not exist.
    
    Example 1:
    
    >>> shortestAlternatingPaths(n = 3, redEdges = [[0,1],[1,2]], blueEdges = [])
    >>> [0,1,-1]
    
    Example 2:
    
    >>> shortestAlternatingPaths(n = 3, redEdges = [[0,1]], blueEdges = [[2,1]])
    >>> [0,1,-1]
    """
def mctFromLeafValues(arr: List[int]) -> int:
    """
    Given an array arr of positive integers, consider all binary trees such that:
    
    Each node has either 0 or 2 children;
    The values of arr correspond to the values of each leaf in an in-order traversal of the tree.
    The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree, respectively.
    
    Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node. It is guaranteed this sum fits into a 32-bit integer.
    A node is a leaf if and only if it has zero children.
    
    Example 1:
    
    
    >>> mctFromLeafValues(arr = [6,2,4])
    >>> 32
    Explanation: There are two possible trees shown.
    The first has a non-leaf node sum 36, and the second has non-leaf node sum 32.
    
    Example 2:
    
    
    >>> mctFromLeafValues(arr = [4,11])
    >>> 44
    """
def maxAbsValExpr(arr1: List[int], arr2: List[int]) -> int:
    """
    Given two arrays of integers with equal lengths, return the maximum value of:\r
    \r
    |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|\r
    \r
    where the maximum is taken over all 0 <= i, j < arr1.length.\r
    
    
    Example 1:
    
    >>> maxAbsValExpr(arr1 = [1,2,3,4], arr2 = [-1,4,5,6])
    >>> 13
    
    Example 2:
    
    >>> maxAbsValExpr(arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4])
    >>> 20
    """
def largestUniqueNumber(nums: List[int]) -> int:
    """
    Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.
    
    Example 1:
    
    >>> largestUniqueNumber(nums = [5,7,3,9,4,9,8,3,1])
    >>> 8
    Explanation: The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it is the answer.
    Example 2:
    
    >>> largestUniqueNumber(nums = [9,9,8,8])
    >>> -1
    Explanation: There is no number that occurs only once.
    """
def isArmstrong(n: int) -> bool:
    """
    Given an integer n, return true if and only if it is an Armstrong number.
    The k-digit number n is an Armstrong number if and only if the kth power of each digit sums to n.
    
    Example 1:
    
    >>> isArmstrong(n = 153)
    >>> true
    Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.
    
    Example 2:
    
    >>> isArmstrong(n = 123)
    >>> false
    Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.
    """
def minimumCost(n: int, connections: List[List[int]]) -> int:
    """
    There are n cities labeled from 1 to n. You are given the integer n and an array connections where connections[i] = [xi, yi, costi] indicates that the cost of connecting city xi and city yi (bidirectional connection) is costi.
    Return the minimum cost to connect all the n cities such that there is at least one path between each pair of cities. If it is impossible to connect all the n cities, return -1,
    The cost is the sum of the connections' costs used.
    
    Example 1:
    
    
    >>> minimumCost(n = 3, connections = [[1,2,5],[1,3,6],[2,3,1]])
    >>> 6
    Explanation: Choosing any 2 edges will connect all cities so we choose the minimum 2.
    
    Example 2:
    
    
    >>> minimumCost(n = 4, connections = [[1,2,3],[3,4,4]])
    >>> -1
    Explanation: There is no way to connect all cities even if all edges are used.
    """
def minimumSemesters(n: int, relations: List[List[int]]) -> int:
    """
    You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei.
    In one semester, you can take any number of courses as long as you have taken all the prerequisites in the previous semester for the courses you are taking.
    Return the minimum number of semesters needed to take all courses. If there is no way to take all the courses, return -1.
    
    Example 1:
    
    
    >>> minimumSemesters(n = 3, relations = [[1,3],[2,3]])
    >>> 2
    Explanation: The figure above represents the given graph.
    In the first semester, you can take courses 1 and 2.
    In the second semester, you can take course 3.
    
    Example 2:
    
    
    >>> minimumSemesters(n = 3, relations = [[1,2],[2,3],[3,1]])
    >>> -1
    Explanation: No course can be studied because they are prerequisites of each other.
    """
def tribonacci(n: int) -> int:
    """
    The Tribonacci sequence Tn is defined as follows:
    T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
    Given n, return the value of Tn.
    
    Example 1:
    
    >>> tribonacci(n = 4)
    >>> 4
    Explanation:
    T_3 = 0 + 1 + 1 = 2
    T_4 = 1 + 1 + 2 = 4
    
    Example 2:
    
    >>> tribonacci(n = 25)
    >>> 1389537
    """
def alphabetBoardPath(target: str) -> str:
    """
    On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].\r
    \r
    Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"], as shown in the diagram below.\r
    \r
    \r
    \r
    We may make the following moves:\r
    \r
    \r
    	'U' moves our position up one row, if the position exists on the board;\r
    	'D' moves our position down one row, if the position exists on the board;\r
    	'L' moves our position left one column, if the position exists on the board;\r
    	'R' moves our position right one column, if the position exists on the board;\r
    	'!' adds the character board[r][c] at our current position (r, c) to the answer.\r
    \r
    \r
    (Here, the only positions that exist on the board are positions with letters on them.)\r
    \r
    Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.\r
    \r
     \r
    Example 1:\r
    >>> alphabetBoardPath(target = "leet"\r)
    >>> "DDR!UURRR!!DDD!"\r
    Example 2:\r
    >>> alphabetBoardPath(target = "code"\r)
    >>> "RR!DDRR!UUL!R!"\r
    \r
     \r
    """
def largest1BorderedSquare(grid: List[List[int]]) -> int:
    """
    Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> largest1BorderedSquare(grid = [[1,1,1],[1,0,1],[1,1,1]]\r)
    >>> 9\r
    \r
    \r
    Example 2:\r
    \r
    \r
    >>> largest1BorderedSquare(grid = [[1,1,0,0]]\r)
    >>> 1\r
    \r
    \r
     \r
    """
def stoneGameII(piles: List[int]) -> int:
    """
    Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.
    Alice and Bob take turns, with Alice starting first.
    On each player's turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X). Initially, M = 1.
    The game continues until all the stones have been taken.
    Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
    
    Example 1:
    
    >>> stoneGameII(piles = [2,7,9,4,4])
    >>> 10
    Explanation:
    
    If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get 2 + 4 + 4 = 10 stones in total.
    If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 stones in total.
    
    So we return 10 since it's larger.
    
    Example 2:
    
    >>> stoneGameII(piles = [1,2,3,4,5,100])
    >>> 104
    """
def longestCommonSubsequence(text1: str, text2: str) -> int:
    """
    Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
    A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
    
    For example, "ace" is a subsequence of "abcde".
    
    A common subsequence of two strings is a subsequence that is common to both strings.
    
    Example 1:
    
    >>> longestCommonSubsequence(text1 = "abcde", text2 = "ace")
    >>> 3
    Explanation: The longest common subsequence is "ace" and its length is 3.
    
    Example 2:
    
    >>> longestCommonSubsequence(text1 = "abc", text2 = "abc")
    >>> 3
    Explanation: The longest common subsequence is "abc" and its length is 3.
    
    Example 3:
    
    >>> longestCommonSubsequence(text1 = "abc", text2 = "def")
    >>> 0
    Explanation: There is no such common subsequence, so the result is 0.
    """
def movesToMakeZigzag(nums: List[int]) -> int:
    """
    Given an array nums of integers, a move consists of choosing any element and decreasing it by 1.
    An array A is a zigzag array if either:
    
    Every even-indexed element is greater than adjacent elements, ie. A[0] > A[1] < A[2] > A[3] < A[4] > ...
    OR, every odd-indexed element is greater than adjacent elements, ie. A[0] < A[1] > A[2] < A[3] > A[4] < ...
    
    Return the minimum number of moves to transform the given array nums into a zigzag array.
    
    Example 1:
    
    >>> movesToMakeZigzag(nums = [1,2,3])
    >>> 2
    Explanation: We can decrease 2 to 0 or 3 to 1.
    
    Example 2:
    
    >>> movesToMakeZigzag(nums = [9,6,1,6,2])
    >>> 4
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def btreeGameWinningMove(root: Optional[TreeNode], n: int, x: int) -> bool:
    """
    Two players play a turn based game on a binary tree. We are given the root of this binary tree, and the number of nodes n in the tree. n is odd, and each node has a distinct value from 1 to n.
    Initially, the first player names a value x with 1 <= x <= n, and the second player names a value y with 1 <= y <= n and y != x. The first player colors the node with value x red, and the second player colors the node with value y blue.
    Then, the players take turns starting with the first player. In each turn, that player chooses a node of their color (red if player 1, blue if player 2) and colors an uncolored neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)
    If (and only if) a player cannot choose such a node in this way, they must pass their turn. If both players pass their turn, the game ends, and the winner is the player that colored more nodes.
    You are the second player. If it is possible to choose such a y to ensure you win the game, return true. If it is not possible, return false.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3)
    >>> true
    Explanation: The second player can choose the node with value 2.
    
    Example 2:
    
    >>> __init__(root = [1,2,3], n = 3, x = 1)
    >>> false
    """
def longestDecomposition(text: str) -> int:
    """
    You are given a string text. You should split it to k substrings (subtext1, subtext2, ..., subtextk) such that:
    
    subtexti is a non-empty string.
    The concatenation of all the substrings is equal to text (i.e., subtext1 + subtext2 + ... + subtextk == text).
    subtexti == subtextk - i + 1 for all valid values of i (i.e., 1 <= i <= k).
    
    Return the largest possible value of k.
    
    Example 1:
    
    >>> longestDecomposition(text = "ghiabcdefhelloadamhelloabcdefghi")
    >>> 7
    Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)".
    
    Example 2:
    
    >>> longestDecomposition(text = "merchant")
    >>> 1
    Explanation: We can split the string on "(merchant)".
    
    Example 3:
    
    >>> longestDecomposition(text = "antaprezatepzapreanta")
    >>> 11
    Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tep)(za)(pre)(a)(nt)(a)".
    """
def isMajorityElement(nums: List[int], target: int) -> bool:
    """
    Given an integer array nums sorted in non-decreasing order and an integer target, return true if target is a majority element, or false otherwise.
    A majority element in an array nums is an element that appears more than nums.length / 2 times in the array.
    
    Example 1:
    
    >>> isMajorityElement(nums = [2,4,5,5,5,5,5,6,6], target = 5)
    >>> true
    Explanation: The value 5 appears 5 times and the length of the array is 9.
    Thus, 5 is a majority element because 5 > 9/2 is true.
    
    Example 2:
    
    >>> isMajorityElement(nums = [10,100,101,101], target = 101)
    >>> false
    Explanation: The value 101 appears 2 times and the length of the array is 4.
    Thus, 101 is not a majority element because 2 > 4/2 is false.
    """