ShwStone/humaneval-leetcode huggingface dataset:
def checkWays(pairs: List[List[int]]) -> int:
    """
    You are given an array pairs, where pairs[i] = [xi, yi], and:
    
    There are no duplicates.
    xi < yi
    
    Let ways be the number of rooted trees that satisfy the following conditions:
    
    The tree consists of nodes whose values appeared in pairs.
    A pair [xi, yi] exists in pairs if and only if xi is an ancestor of yi or yi is an ancestor of xi.
    Note: the tree does not have to be a binary tree.
    
    Two ways are considered to be different if there is at least one node that has different parents in both ways.
    Return:
    
    0 if ways == 0
    1 if ways == 1
    2 if ways > 1
    
    A rooted tree is a tree that has a single root node, and all edges are oriented to be outgoing from the root.
    An ancestor of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.
    
    Example 1:
    
    
    >>> checkWays(pairs = [[1,2],[2,3]])
    >>> 1
    Explanation: There is exactly one valid rooted tree, which is shown in the above figure.
    
    Example 2:
    
    
    >>> checkWays(pairs = [[1,2],[2,3],[1,3]])
    >>> 2
    Explanation: There are multiple valid rooted trees. Three of them are shown in the above figures.
    
    Example 3:
    
    >>> checkWays(pairs = [[1,2],[2,3],[2,4],[1,5]])
    >>> 0
    Explanation: There are no valid rooted trees.
    """
def decode(encoded: List[int], first: int) -> List[int]:
    """
    There is a hidden integer array arr that consists of n non-negative integers.
    It was encoded into another integer array encoded of length n - 1, such that encoded[i] = arr[i] XOR arr[i + 1]. For example, if arr = [1,0,2,1], then encoded = [1,2,3].
    You are given the encoded array. You are also given an integer first, that is the first element of arr, i.e. arr[0].
    Return the original array arr. It can be proved that the answer exists and is unique.
    
    Example 1:
    
    >>> decode(encoded = [1,2,3], first = 1)
    >>> [1,0,2,1]
    Explanation: If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]
    
    Example 2:
    
    >>> decode(encoded = [6,2,7,3], first = 4)
    >>> [4,2,0,7,4]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapNodes(head: Optional[ListNode], k: int) -> Optional[ListNode]:
    """
    You are given the head of a linked list, and an integer k.
    Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5], k = 2)
    >>> [1,4,3,2,5]
    
    Example 2:
    
    >>> __init__(head = [7,9,6,6,7,8,3,0,9,5], k = 5)
    >>> [7,9,6,6,8,7,3,0,9,5]
    """
def minimumHammingDistance(source: List[int], target: List[int], allowedSwaps: List[List[int]]) -> int:
    """
    You are given two integer arrays, source and target, both of length n. You are also given an array allowedSwaps where each allowedSwaps[i] = [ai, bi] indicates that you are allowed to swap the elements at index ai and index bi (0-indexed) of array source. Note that you can swap elements at a specific pair of indices multiple times and in any order.
    The Hamming distance of two arrays of the same length, source and target, is the number of positions where the elements are different. Formally, it is the number of indices i for 0 <= i <= n-1 where source[i] != target[i] (0-indexed).
    Return the minimum Hamming distance of source and target after performing any amount of swap operations on array source.
    
    Example 1:
    
    >>> minimumHammingDistance(source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]])
    >>> 1
    Explanation: source can be transformed the following way:
    - Swap indices 0 and 1: source = [2,1,3,4]
    - Swap indices 2 and 3: source = [2,1,4,3]
    The Hamming distance of source and target is 1 as they differ in 1 position: index 3.
    
    Example 2:
    
    >>> minimumHammingDistance(source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = [])
    >>> 2
    Explanation: There are no allowed swaps.
    The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.
    
    Example 3:
    
    >>> minimumHammingDistance(source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]])
    >>> 0
    """
def minimumTimeRequired(jobs: List[int], k: int) -> int:
    """
    You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job.
    There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the maximum working time of any worker is minimized.
    Return the minimum possible maximum working time of any assignment.
    
    Example 1:
    
    >>> minimumTimeRequired(jobs = [3,2,3], k = 3)
    >>> 3
    Explanation: By assigning each person one job, the maximum time is 3.
    
    Example 2:
    
    >>> minimumTimeRequired(jobs = [1,2,4,7,8], k = 2)
    >>> 11
    Explanation: Assign the jobs the following way:
    Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
    Worker 2: 4, 7 (working time = 4 + 7 = 11)
    The maximum working time is 11.
    """
def countGoodRectangles(rectangles: List[List[int]]) -> int:
    """
    You are given an array rectangles where rectangles[i] = [li, wi] represents the ith rectangle of length li and width wi.\r
    \r
    You can cut the ith rectangle to form a square with a side length of k if both k <= li and k <= wi. For example, if you have a rectangle [4,6], you can cut it to get a square with a side length of at most 4.\r
    \r
    Let maxLen be the side length of the largest square you can obtain from any of the given rectangles.\r
    \r
    Return the number of rectangles that can make a square with a side length of maxLen.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> countGoodRectangles(rectangles = [[5,8],[3,9],[5,12],[16,5]]\r)
    >>> 3\r
    Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5].\r
    The largest possible square is of length 5, and you can get it out of 3 rectangles.\r
    \r
    \r
    Example 2:\r
    \r
    \r
    >>> countGoodRectangles(rectangles = [[2,3],[3,7],[4,3],[3,7]]\r)
    >>> 3\r
    \r
    \r
     \r
    """
def tupleSameProduct(nums: List[int]) -> int:
    """
    Given an array nums of distinct positive integers, return the number of tuples (a, b, c, d) such that a * b = c * d where a, b, c, and d are elements of nums, and a != b != c != d.
    
    Example 1:
    
    >>> tupleSameProduct(nums = [2,3,4,6])
    >>> 8
    Explanation: There are 8 valid tuples:
    (2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
    (3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
    
    Example 2:
    
    >>> tupleSameProduct(nums = [1,2,4,5,10])
    >>> 16
    Explanation: There are 16 valid tuples:
    (1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
    (2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
    (2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
    (4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
    """
def largestSubmatrix(matrix: List[List[int]]) -> int:
    """
    You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.
    Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.
    
    Example 1:
    
    
    >>> largestSubmatrix(matrix = [[0,0,1],[1,1,1],[1,0,1]])
    >>> 4
    Explanation: You can rearrange the columns as shown above.
    The largest submatrix of 1s, in bold, has an area of 4.
    
    Example 2:
    
    
    >>> largestSubmatrix(matrix = [[1,0,1,0,1]])
    >>> 3
    Explanation: You can rearrange the columns as shown above.
    The largest submatrix of 1s, in bold, has an area of 3.
    
    Example 3:
    
    >>> largestSubmatrix(matrix = [[1,1,0],[1,0,1]])
    >>> 2
    Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
    """
def canMouseWin(grid: List[str], catJump: int, mouseJump: int) -> bool:
    """
    A game is played by a cat and a mouse named Cat and Mouse.
    The environment is represented by a grid of size rows x cols, where each element is a wall, floor, player (Cat, Mouse), or food.
    
    Players are represented by the characters 'C'(Cat),'M'(Mouse).
    Floors are represented by the character '.' and can be walked on.
    Walls are represented by the character '#' and cannot be walked on.
    Food is represented by the character 'F' and can be walked on.
    There is only one of each character 'C', 'M', and 'F' in grid.
    
    Mouse and Cat play according to the following rules:
    
    Mouse moves first, then they take turns to move.
    During each turn, Cat and Mouse can jump in one of the four directions (left, right, up, down). They cannot jump over the wall nor outside of the grid.
    catJump, mouseJump are the maximum lengths Cat and Mouse can jump at a time, respectively. Cat and Mouse can jump less than the maximum length.
    Staying in the same position is allowed.
    Mouse can jump over Cat.
    
    The game can end in 4 ways:
    
    If Cat occupies the same position as Mouse, Cat wins.
    If Cat reaches the food first, Cat wins.
    If Mouse reaches the food first, Mouse wins.
    If Mouse cannot get to the food within 1000 turns, Cat wins.
    
    Given a rows x cols matrix grid and two integers catJump and mouseJump, return true if Mouse can win the game if both Cat and Mouse play optimally, otherwise return false.
    
    Example 1:
    
    
    >>> canMouseWin(grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2)
    >>> true
    Explanation: Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
    
    Example 2:
    
    
    >>> canMouseWin(grid = ["M.C...F"], catJump = 1, mouseJump = 4)
    >>> true
    
    Example 3:
    
    >>> canMouseWin(grid = ["M.C...F"], catJump = 1, mouseJump = 3)
    >>> false
    """
def getFood(grid: List[List[str]]) -> int:
    """
    You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.
    You are given an m x n character matrix, grid, of these different types of cells:
    
    '*' is your location. There is exactly one '*' cell.
    '#' is a food cell. There may be multiple food cells.
    'O' is free space, and you can travel through these cells.
    'X' is an obstacle, and you cannot travel through these cells.
    
    You can travel to any adjacent cell north, east, south, or west of your current location if there is not an obstacle.
    Return the length of the shortest path for you to reach any food cell. If there is no path for you to reach food, return -1.
    
    Example 1:
    
    
    >>> getFood(grid = [["X","X","X","X","X","X"],["X","*","O","O","O","X"],["X","O","O","#","O","X"],["X","X","X","X","X","X"]])
    >>> 3
    Explanation: It takes 3 steps to reach the food.
    
    Example 2:
    
    
    >>> getFood(grid = [["X","X","X","X","X"],["X","*","X","O","X"],["X","O","X","#","X"],["X","X","X","X","X"]])
    >>> -1
    Explanation: It is not possible to reach the food.
    
    Example 3:
    
    
    >>> getFood(grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["X","X","X","X","X","X","X","X"]])
    >>> 6
    Explanation: There can be multiple food cells. It only takes 6 steps to reach the bottom food.
    Example 4:
    
    >>> getFood(grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["O","O","O","O","O","O","O","O"]])
    >>> 5
    """
def largestAltitude(gain: List[int]) -> int:
    """
    There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.
    You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.
    
    Example 1:
    
    >>> largestAltitude(gain = [-5,1,5,0,-7])
    >>> 1
    Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
    
    Example 2:
    
    >>> largestAltitude(gain = [-4,-3,-2,-1,4,3,2])
    >>> 0
    Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
    """
def minimumTeachings(n: int, languages: List[List[int]], friendships: List[List[int]]) -> int:
    """
    On a social network consisting of m users and some friendships between users, two users can communicate with each other if they know a common language.
    You are given an integer n, an array languages, and an array friendships where:
    
    There are n languages numbered 1 through n,
    languages[i] is the set of languages the i​​​​​​th​​​​ user knows, and
    friendships[i] = [u​​​​​​i​​​, v​​​​​​i] denotes a friendship between the users u​​​​​​​​​​​i​​​​​ and vi.
    
    You can choose one language and teach it to some users so that all friends can communicate with each other. Return the minimum number of users you need to teach.
    Note that friendships are not transitive, meaning if x is a friend of y and y is a friend of z, this doesn't guarantee that x is a friend of z.
    
    Example 1:
    
    >>> minimumTeachings(n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]])
    >>> 1
    Explanation: You can either teach user 1 the second language or user 2 the first language.
    
    Example 2:
    
    >>> minimumTeachings(n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]])
    >>> 2
    Explanation: Teach the third language to users 1 and 3, yielding two users to teach.
    """
def decode(encoded: List[int]) -> List[int]:
    """
    There is an integer array perm that is a permutation of the first n positive integers, where n is always odd.
    It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].
    Given the encoded array, return the original array perm. It is guaranteed that the answer exists and is unique.
    
    Example 1:
    
    >>> decode(encoded = [3,1])
    >>> [1,2,3]
    Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
    
    Example 2:
    
    >>> decode(encoded = [6,5,4,6])
    >>> [2,4,1,5,3]
    """
def maximumTime(time: str) -> str:
    """
    You are given a string time in the form of  hh:mm, where some of the digits in the string are hidden (represented by ?).
    The valid times are those inclusively between 00:00 and 23:59.
    Return the latest valid time you can get from time by replacing the hidden digits.
    
    Example 1:
    
    >>> maximumTime(time = "2?:?0")
    >>> "23:50"
    Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
    
    Example 2:
    
    >>> maximumTime(time = "0?:3?")
    >>> "09:39"
    
    Example 3:
    
    >>> maximumTime(time = "1?:22")
    >>> "19:22"
    """
def minCharacters(a: str, b: str) -> int:
    """
    You are given two strings a and b that consist of lowercase letters. In one operation, you can change any character in a or b to any lowercase letter.
    Your goal is to satisfy one of the following three conditions:
    
    Every letter in a is strictly less than every letter in b in the alphabet.
    Every letter in b is strictly less than every letter in a in the alphabet.
    Both a and b consist of only one distinct letter.
    
    Return the minimum number of operations needed to achieve your goal.
    
    Example 1:
    
    >>> minCharacters(a = "aba", b = "caa")
    >>> 2
    Explanation: Consider the best way to make each condition true:
    1) Change b to "ccc" in 2 operations, then every letter in a is less than every letter in b.
    2) Change a to "bbb" and b to "aaa" in 3 operations, then every letter in b is less than every letter in a.
    3) Change a to "aaa" and b to "aaa" in 2 operations, then a and b consist of one distinct letter.
    The best way was done in 2 operations (either condition 1 or condition 3).
    
    Example 2:
    
    >>> minCharacters(a = "dabadd", b = "cda")
    >>> 3
    Explanation: The best way is to make condition 1 true by changing b to "eee".
    """
def kthLargestValue(matrix: List[List[int]], k: int) -> int:
    """
    You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k.
    The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexed).
    Find the kth largest value (1-indexed) of all the coordinates of matrix.
    
    Example 1:
    
    >>> kthLargestValue(matrix = [[5,2],[1,6]], k = 1)
    >>> 7
    Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
    
    Example 2:
    
    >>> kthLargestValue(matrix = [[5,2],[1,6]], k = 2)
    >>> 5
    Explanation: The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.
    
    Example 3:
    
    >>> kthLargestValue(matrix = [[5,2],[1,6]], k = 3)
    >>> 4
    Explanation: The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.
    """
def minimumBoxes(n: int) -> int:
    """
    You have a cubic storeroom where the width, length, and height of the room are all equal to n units. You are asked to place n boxes in this room where each box is a cube of unit side length. There are however some rules to placing the boxes:
    
    You can place the boxes anywhere on the floor.
    If box x is placed on top of the box y, then each side of the four vertical sides of the box y must either be adjacent to another box or to a wall.
    
    Given an integer n, return the minimum possible number of boxes touching the floor.
    
    Example 1:
    
    
    >>> minimumBoxes(n = 3)
    >>> 3
    Explanation: The figure above is for the placement of the three boxes.
    These boxes are placed in the corner of the room, where the corner is on the left side.
    
    Example 2:
    
    
    >>> minimumBoxes(n = 4)
    >>> 3
    Explanation: The figure above is for the placement of the four boxes.
    These boxes are placed in the corner of the room, where the corner is on the left side.
    
    Example 3:
    
    
    >>> minimumBoxes(n = 10)
    >>> 6
    Explanation: The figure above is for the placement of the ten boxes.
    These boxes are placed in the corner of the room, where the corner is on the back side.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findDistance(root: Optional[TreeNode], p: int, q: int) -> int:
    """
    Given the root of a binary tree and two integers p and q, return the distance between the nodes of value p and value q in the tree.
    The distance between two nodes is the number of edges on the path from one to the other.
    
    Example 1:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 0)
    >>> 3
    Explanation: There are 3 edges between 5 and 0: 5-3-1-0.
    Example 2:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 7)
    >>> 2
    Explanation: There are 2 edges between 5 and 7: 5-2-7.
    Example 3:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 5)
    >>> 0
    Explanation: The distance between a node and itself is 0.
    """
def countBalls(lowLimit: int, highLimit: int) -> int:
    """
    You are working in a ball factory where you have n balls numbered from lowLimit up to highLimit inclusive (i.e., n == highLimit - lowLimit + 1), and an infinite number of boxes numbered from 1 to infinity.
    Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball's number. For example, the ball number 321 will be put in the box number 3 + 2 + 1 = 6 and the ball number 10 will be put in the box number 1 + 0 = 1.
    Given two integers lowLimit and highLimit, return the number of balls in the box with the most balls.
    
    Example 1:
    
    >>> countBalls(lowLimit = 1, highLimit = 10)
    >>> 2
    Explanation:
    Box Number:  1 2 3 4 5 6 7 8 9 10 11 ...
    Ball Count:  2 1 1 1 1 1 1 1 1 0  0  ...
    Box 1 has the most number of balls with 2 balls.
    Example 2:
    
    >>> countBalls(lowLimit = 5, highLimit = 15)
    >>> 2
    Explanation:
    Box Number:  1 2 3 4 5 6 7 8 9 10 11 ...
    Ball Count:  1 1 1 1 2 2 1 1 1 0  0  ...
    Boxes 5 and 6 have the most number of balls with 2 balls in each.
    
    Example 3:
    
    >>> countBalls(lowLimit = 19, highLimit = 28)
    >>> 2
    Explanation:
    Box Number:  1 2 3 4 5 6 7 8 9 10 11 12 ...
    Ball Count:  0 1 1 1 1 1 1 1 1 2  0  0  ...
    Box 10 has the most number of balls with 2 balls.
    """
def restoreArray(adjacentPairs: List[List[int]]) -> List[int]:
    """
    There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.
    You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.
    It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.
    Return the original array nums. If there are multiple solutions, return any of them.
    
    Example 1:
    
    >>> restoreArray(adjacentPairs = [[2,1],[3,4],[3,2]])
    >>> [1,2,3,4]
    Explanation: This array has all its adjacent pairs in adjacentPairs.
    Notice that adjacentPairs[i] may not be in left-to-right order.
    
    Example 2:
    
    >>> restoreArray(adjacentPairs = [[4,-2],[1,4],[-3,1]])
    >>> [-2,4,1,-3]
    Explanation: There can be negative numbers.
    Another solution is [-3,1,4,-2], which would also be accepted.
    
    Example 3:
    
    >>> restoreArray(adjacentPairs = [[100000,-100000]])
    >>> [100000,-100000]
    """