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 distributeCoins(root: Optional[TreeNode]) -> int:
    """
    You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.
    In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.
    Return the minimum number of moves required to make every node have exactly one coin.
    
    Example 1:
    
    
    >>> __init__(root = [3,0,0])
    >>> 2
    Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
    
    Example 2:
    
    
    >>> __init__(root = [0,3,0])
    >>> 3
    Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
    """
def uniquePathsIII(grid: List[List[int]]) -> int:
    """
    You are given an m x n integer array grid where grid[i][j] could be:
    
    1 representing the starting square. There is exactly one starting square.
    2 representing the ending square. There is exactly one ending square.
    0 representing empty squares we can walk over.
    -1 representing obstacles that we cannot walk over.
    
    Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
    
    Example 1:
    
    
    >>> uniquePathsIII(grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]])
    >>> 2
    Explanation: We have the following two paths:
    1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
    2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
    
    Example 2:
    
    
    >>> uniquePathsIII(grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]])
    >>> 4
    Explanation: We have the following four paths:
    1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
    2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
    3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
    4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
    
    Example 3:
    
    
    >>> uniquePathsIII(grid = [[0,1],[2,0]])
    >>> 0
    Explanation: There is no path that walks over every empty square exactly once.
    Note that the starting and ending square can be anywhere in the grid.
    """
def countTriplets(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of AND triples.
    An AND triple is a triple of indices (i, j, k) such that:
    
    0 <= i < nums.length
    0 <= j < nums.length
    0 <= k < nums.length
    nums[i] & nums[j] & nums[k] == 0, where & represents the bitwise-AND operator.
    
    
    Example 1:
    
    >>> countTriplets(nums = [2,1,3])
    >>> 12
    Explanation: We could choose the following i, j, k triples:
    (i=0, j=0, k=1) : 2 & 2 & 1
    (i=0, j=1, k=0) : 2 & 1 & 2
    (i=0, j=1, k=1) : 2 & 1 & 1
    (i=0, j=1, k=2) : 2 & 1 & 3
    (i=0, j=2, k=1) : 2 & 3 & 1
    (i=1, j=0, k=0) : 1 & 2 & 2
    (i=1, j=0, k=1) : 1 & 2 & 1
    (i=1, j=0, k=2) : 1 & 2 & 3
    (i=1, j=1, k=0) : 1 & 1 & 2
    (i=1, j=2, k=0) : 1 & 3 & 2
    (i=2, j=0, k=1) : 3 & 2 & 1
    (i=2, j=1, k=0) : 3 & 1 & 2
    
    Example 2:
    
    >>> countTriplets(nums = [0,0,0])
    >>> 27
    """
def mincostTickets(days: List[int], costs: List[int]) -> int:
    """
    You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array days. Each day is an integer from 1 to 365.
    Train tickets are sold in three different ways:
    
    a 1-day pass is sold for costs[0] dollars,
    a 7-day pass is sold for costs[1] dollars, and
    a 30-day pass is sold for costs[2] dollars.
    
    The passes allow that many days of consecutive travel.
    
    For example, if we get a 7-day pass on day 2, then we can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8.
    
    Return the minimum number of dollars you need to travel every day in the given list of days.
    
    Example 1:
    
    >>> mincostTickets(days = [1,4,6,7,8,20], costs = [2,7,15])
    >>> 11
    Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
    On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
    On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
    On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
    In total, you spent $11 and covered all the days of your travel.
    
    Example 2:
    
    >>> mincostTickets(days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15])
    >>> 17
    Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
    On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
    On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
    In total, you spent $17 and covered all the days of your travel.
    """
def strWithout3a3b(a: int, b: int) -> str:
    """
    Given two integers a and b, return any string s such that:
    
    s has length a + b and contains exactly a 'a' letters, and exactly b 'b' letters,
    The substring 'aaa' does not occur in s, and
    The substring 'bbb' does not occur in s.
    
    
    Example 1:
    
    >>> strWithout3a3b(a = 1, b = 2)
    >>> "abb"
    Explanation: "abb", "bab" and "bba" are all correct answers.
    
    Example 2:
    
    >>> strWithout3a3b(a = 4, b = 1)
    >>> "aabaa"
    """
def sumEvenAfterQueries(nums: List[int], queries: List[List[int]]) -> List[int]:
    """
    You are given an integer array nums and an array queries where queries[i] = [vali, indexi].
    For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.
    Return an integer array answer where answer[i] is the answer to the ith query.
    
    Example 1:
    
    >>> sumEvenAfterQueries(nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]])
    >>> [8,6,2,4]
    Explanation: At the beginning, the array is [1,2,3,4].
    After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
    After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
    After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
    After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
    
    Example 2:
    
    >>> sumEvenAfterQueries(nums = [1], queries = [[4,0]])
    >>> [0]
    """
def intervalIntersection(firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
    """
    You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
    Return the intersection of these two interval lists.
    A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
    The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].
    
    Example 1:
    
    
    >>> intervalIntersection(firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]])
    >>> [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
    
    Example 2:
    
    >>> intervalIntersection(firstList = [[1,3],[5,9]], secondList = [])
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def verticalTraversal(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, calculate the vertical order traversal of the binary tree.
    For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).
    The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.
    Return the vertical order traversal of the binary tree.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [[9],[3,15],[20],[7]]
    Explanation:
    Column -1: Only node 9 is in this column.
    Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
    Column 1: Only node 20 is in this column.
    Column 2: Only node 7 is in this column.
    Example 2:
    
    
    >>> __init__(root = [1,2,3,4,5,6,7])
    >>> [[4],[2],[1,5,6],[3],[7]]
    Explanation:
    Column -2: Only node 4 is in this column.
    Column -1: Only node 2 is in this column.
    Column 0: Nodes 1, 5, and 6 are in this column.
              1 is at the top, so it comes first.
              5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
    Column 1: Only node 3 is in this column.
    Column 2: Only node 7 is in this column.
    
    Example 3:
    
    
    >>> __init__(root = [1,2,3,4,6,5,7])
    >>> [[4],[2],[1,5,6],[3],[7]]
    Explanation:
    This case is the exact same as example 2, but with nodes 5 and 6 swapped.
    Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def smallestFromLeaf(root: Optional[TreeNode]) -> str:
    """
    You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.
    Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
    As a reminder, any shorter prefix of a string is lexicographically smaller.
    
    For example, "ab" is lexicographically smaller than "aba".
    
    A leaf of a node is a node that has no children.
    
    Example 1:
    
    
    >>> __init__(root = [0,1,2,3,4,3,4])
    >>> "dba"
    
    Example 2:
    
    
    >>> __init__(root = [25,1,3,1,3,0,2])
    >>> "adz"
    
    Example 3:
    
    
    >>> __init__(root = [2,2,1,null,1,0,null,0])
    >>> "abc"
    """
def addToArrayForm(num: List[int], k: int) -> List[int]:
    """
    The array-form of an integer num is an array representing its digits in left to right order.
    
    For example, for num = 1321, the array form is [1,3,2,1].
    
    Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.
    
    Example 1:
    
    >>> addToArrayForm(num = [1,2,0,0], k = 34)
    >>> [1,2,3,4]
    Explanation: 1200 + 34 = 1234
    
    Example 2:
    
    >>> addToArrayForm(num = [2,7,4], k = 181)
    >>> [4,5,5]
    Explanation: 274 + 181 = 455
    
    Example 3:
    
    >>> addToArrayForm(num = [2,1,5], k = 806)
    >>> [1,0,2,1]
    Explanation: 215 + 806 = 1021
    """
def equationsPossible(equations: List[str]) -> bool:
    """
    You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: "xi==yi" or "xi!=yi".Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names.
    Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.
    
    Example 1:
    
    >>> equationsPossible(equations = ["a==b","b!=a"])
    >>> false
    Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.
    There is no way to assign the variables to satisfy both equations.
    
    Example 2:
    
    >>> equationsPossible(equations = ["b==a","a==b"])
    >>> true
    Explanation: We could assign a = 1 and b = 1 to satisfy both equations.
    """
def brokenCalc(startValue: int, target: int) -> int:
    """
    There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
    
    multiply the number on display by 2, or
    subtract 1 from the number on display.
    
    Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
    
    Example 1:
    
    >>> brokenCalc(startValue = 2, target = 3)
    >>> 2
    Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
    
    Example 2:
    
    >>> brokenCalc(startValue = 5, target = 8)
    >>> 2
    Explanation: Use decrement and then double {5 -> 4 -> 8}.
    
    Example 3:
    
    >>> brokenCalc(startValue = 3, target = 10)
    >>> 3
    Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
    """
def subarraysWithKDistinct(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the number of good subarrays of nums.
    A good array is an array where the number of different integers in that array is exactly k.
    
    For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
    
    A subarray is a contiguous part of an array.
    
    Example 1:
    
    >>> subarraysWithKDistinct(nums = [1,2,1,2,3], k = 2)
    >>> 7
    Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
    
    Example 2:
    
    >>> subarraysWithKDistinct(nums = [1,2,1,3,4], k = 3)
    >>> 3
    Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isCousins(root: Optional[TreeNode], x: int, y: int) -> bool:
    """
    Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise.
    Two nodes of a binary tree are cousins if they have the same depth with different parents.
    Note that in a binary tree, the root node is at the depth 0, and children of each depth k node are at the depth k + 1.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4], x = 4, y = 3)
    >>> false
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3,null,4,null,5], x = 5, y = 4)
    >>> true
    
    Example 3:
    
    
    >>> __init__(root = [1,2,3,null,4], x = 2, y = 3)
    >>> false
    """
def orangesRotting(grid: List[List[int]]) -> int:
    """
    You are given an m x n grid where each cell can have one of three values:
    
    0 representing an empty cell,
    1 representing a fresh orange, or
    2 representing a rotten orange.
    
    Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
    Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
    
    Example 1:
    
    
    >>> orangesRotting(grid = [[2,1,1],[1,1,0],[0,1,1]])
    >>> 4
    
    Example 2:
    
    >>> orangesRotting(grid = [[2,1,1],[0,1,1],[1,0,1]])
    >>> -1
    Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
    
    Example 3:
    
    >>> orangesRotting(grid = [[0,2]])
    >>> 0
    Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
    """
def minKBitFlips(nums: List[int], k: int) -> int:
    """
    You are given a binary array nums and an integer k.
    A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.
    Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1.
    A subarray is a contiguous part of an array.
    
    Example 1:
    
    >>> minKBitFlips(nums = [0,1,0], k = 1)
    >>> 2
    Explanation: Flip nums[0], then flip nums[2].
    
    Example 2:
    
    >>> minKBitFlips(nums = [1,1,0], k = 2)
    >>> -1
    Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
    
    Example 3:
    
    >>> minKBitFlips(nums = [0,0,0,1,0,1,1,0], k = 3)
    >>> 3
    Explanation:
    Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
    Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
    Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
    """
def numSquarefulPerms(nums: List[int]) -> int:
    """
    An array is squareful if the sum of every pair of adjacent elements is a perfect square.
    Given an integer array nums, return the number of permutations of nums that are squareful.
    Two permutations perm1 and perm2 are different if there is some index i such that perm1[i] != perm2[i].
    
    Example 1:
    
    >>> numSquarefulPerms(nums = [1,17,8])
    >>> 2
    Explanation: [1,8,17] and [17,8,1] are the valid permutations.
    
    Example 2:
    
    >>> numSquarefulPerms(nums = [2,2,2])
    >>> 1
    """
def findJudge(n: int, trust: List[List[int]]) -> int:
    """
    In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.
    If the town judge exists, then:
    
    The town judge trusts nobody.
    Everybody (except for the town judge) trusts the town judge.
    There is exactly one person that satisfies properties 1 and 2.
    
    You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.
    Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
    
    Example 1:
    
    >>> findJudge(n = 2, trust = [[1,2]])
    >>> 2
    
    Example 2:
    
    >>> findJudge(n = 3, trust = [[1,3],[2,3]])
    >>> 3
    
    Example 3:
    
    >>> findJudge(n = 3, trust = [[1,3],[2,3],[3,1]])
    >>> -1
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
    """
    A maximum tree is a tree where every node has a value greater than any other value in its subtree.
    You are given the root of a maximum binary tree and an integer val.
    Just as in the previous problem, the given tree was constructed from a list a (root = Construct(a)) recursively with the following Construct(a) routine:
    
    If a is empty, return null.
    Otherwise, let a[i] be the largest element of a. Create a root node with the value a[i].
    The left child of root will be Construct([a[0], a[1], ..., a[i - 1]]).
    The right child of root will be Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]]).
    Return root.
    
    Note that we were not given a directly, only a root node root = Construct(a).
    Suppose b is a copy of a with the value val appended to it. It is guaranteed that b has unique values.
    Return Construct(b).
    
    Example 1:
    
    
    >>> __init__(root = [4,1,3,null,null,2], val = 5)
    >>> [5,4,null,1,3,null,null,2]
    Explanation: a = [1,4,2,3], b = [1,4,2,3,5]
    
    Example 2:
    
    
    >>> __init__(root = [5,2,4,null,1], val = 3)
    >>> [5,2,4,null,1,null,3]
    Explanation: a = [2,1,5,4], b = [2,1,5,4,3]
    
    Example 3:
    
    
    >>> __init__(root = [5,2,3,null,1], val = 4)
    >>> [5,2,4,null,1,3]
    Explanation: a = [2,1,5,3], b = [2,1,5,3,4]
    """
def numRookCaptures(board: List[List[str]]) -> int:
    """
    You are given an 8 x 8 matrix representing a chessboard. There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'. Empty squares are represented by '.'.
    A rook can move any number of squares horizontally or vertically (up, down, left, right) until it reaches another piece or the edge of the board. A rook is attacking a pawn if it can move to the pawn's square in one move.
    Note: A rook cannot move through other pieces, such as bishops or pawns. This means a rook cannot attack a pawn if there is another piece blocking the path.
    Return the number of pawns the white rook is attacking.
    
    Example 1:
    
    
    >>> numRookCaptures(board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]])
    >>> 3
    Explanation:
    In this example, the rook is attacking all the pawns.
    
    Example 2:
    
    
    >>> numRookCaptures(board = [[".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]])
    >>> 0
    Explanation:
    The bishops are blocking the rook from attacking any of the pawns.
    
    Example 3:
    
    
    >>> numRookCaptures(board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]])
    >>> 3
    Explanation:
    The rook is attacking the pawns at positions b5, d6, and f5.
    """
def mergeStones(stones: List[int], k: int) -> int:
    """
    There are n piles of stones arranged in a row. The ith pile has stones[i] stones.
    A move consists of merging exactly k consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these k piles.
    Return the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1.
    
    Example 1:
    
    >>> mergeStones(stones = [3,2,4,1], k = 2)
    >>> 20
    Explanation: We start with [3, 2, 4, 1].
    We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
    We merge [4, 1] for a cost of 5, and we are left with [5, 5].
    We merge [5, 5] for a cost of 10, and we are left with [10].
    The total cost was 20, and this is the minimum possible.
    
    Example 2:
    
    >>> mergeStones(stones = [3,2,4,1], k = 3)
    >>> -1
    Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore.  So the task is impossible.
    
    Example 3:
    
    >>> mergeStones(stones = [3,5,1,2,6], k = 3)
    >>> 25
    Explanation: We start with [3, 5, 1, 2, 6].
    We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
    We merge [3, 8, 6] for a cost of 17, and we are left with [17].
    The total cost was 25, and this is the minimum possible.
    """