ShwStone/humaneval-leetcode huggingface dataset:
def maxBoxesInWarehouse(boxes: List[int], warehouse: List[int]) -> int:
    """
    You are given two arrays of positive integers, boxes and warehouse, representing the heights of some boxes of unit width and the heights of n rooms in a warehouse respectively. The warehouse's rooms are labeled from 0 to n - 1 from left to right where warehouse[i] (0-indexed) is the height of the ith room.
    Boxes are put into the warehouse by the following rules:
    
    Boxes cannot be stacked.
    You can rearrange the insertion order of the boxes.
    Boxes can be pushed into the warehouse from either side (left or right)
    If the height of some room in the warehouse is less than the height of a box, then that box and all other boxes behind it will be stopped before that room.
    
    Return the maximum number of boxes you can put into the warehouse.
    
    Example 1:
    
    
    >>> maxBoxesInWarehouse(boxes = [1,2,2,3,4], warehouse = [3,4,1,2])
    >>> 4
    Explanation:
    
    We can store the boxes in the following order:
    1- Put the yellow box in room 2 from either the left or right side.
    2- Put the orange box in room 3 from the right side.
    3- Put the green box in room 1 from the left side.
    4- Put the red box in room 0 from the left side.
    Notice that there are other valid ways to put 4 boxes such as swapping the red and green boxes or the red and orange boxes.
    
    Example 2:
    
    
    >>> maxBoxesInWarehouse(boxes = [3,5,5,2], warehouse = [2,1,3,4,5])
    >>> 3
    Explanation:
    
    It is not possible to put the two boxes of height 5 in the warehouse since there's only 1 room of height >= 5.
    Other valid solutions are to put the green box in room 2 or to put the orange box first in room 2 before putting the green and red boxes.
    """
def numSpecial(mat: List[List[int]]) -> int:
    """
    Given an m x n binary matrix mat, return the number of special positions in mat.
    A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).
    
    Example 1:
    
    
    >>> numSpecial(mat = [[1,0,0],[0,0,1],[1,0,0]])
    >>> 1
    Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
    
    Example 2:
    
    
    >>> numSpecial(mat = [[1,0,0],[0,1,0],[0,0,1]])
    >>> 3
    Explanation: (0, 0), (1, 1) and (2, 2) are special positions.
    """
def unhappyFriends(n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
    """
    You are given a list of preferences for n friends, where n is always even.
    For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1.
    All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi.
    However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but:
    
    x prefers u over y, and
    u prefers x over v.
    
    Return the number of unhappy friends.
    
    Example 1:
    
    >>> unhappyFriends(n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]])
    >>> 2
    Explanation:
    Friend 1 is unhappy because:
    - 1 is paired with 0 but prefers 3 over 0, and
    - 3 prefers 1 over 2.
    Friend 3 is unhappy because:
    - 3 is paired with 2 but prefers 1 over 2, and
    - 1 prefers 3 over 0.
    Friends 0 and 2 are happy.
    
    Example 2:
    
    >>> unhappyFriends(n = 2, preferences = [[1], [0]], pairs = [[1, 0]])
    >>> 0
    Explanation: Both friends 0 and 1 are happy.
    
    Example 3:
    
    >>> unhappyFriends(n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]])
    >>> 4
    """
def minCostConnectPoints(points: List[List[int]]) -> int:
    """
    You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].
    The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.
    Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.
    
    Example 1:
    
    
    >>> minCostConnectPoints(points = [[0,0],[2,2],[3,10],[5,2],[7,0]])
    >>> 20
    Explanation:
    
    We can connect the points as shown above to get the minimum cost of 20.
    Notice that there is a unique path between every pair of points.
    
    Example 2:
    
    >>> minCostConnectPoints(points = [[3,12],[-2,5],[-4,1]])
    >>> 18
    """
def isTransformable(s: str, t: str) -> bool:
    """
    Given two strings s and t, transform string s into string t using the following operation any number of times:
    
    Choose a non-empty substring in s and sort it in place so the characters are in ascending order.
    
    
    For example, applying the operation on the underlined substring in "14234" results in "12344".
    
    
    
    Return true if it is possible to transform s into t. Otherwise, return false.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> isTransformable(s = "84532", t = "34852")
    >>> true
    Explanation: You can transform s into t using the following sort operations:
    "84532" (from index 2 to 3) -> "84352"
    "84352" (from index 0 to 2) -> "34852"
    
    Example 2:
    
    >>> isTransformable(s = "34521", t = "23415")
    >>> true
    Explanation: You can transform s into t using the following sort operations:
    "34521" -> "23451"
    "23451" -> "23415"
    
    Example 3:
    
    >>> isTransformable(s = "12345", t = "12435")
    >>> false
    """
def sumOddLengthSubarrays(arr: List[int]) -> int:
    """
    Given an array of positive integers arr, return the sum of all possible odd-length subarrays of arr.
    A subarray is a contiguous subsequence of the array.
    
    Example 1:
    
    >>> sumOddLengthSubarrays(arr = [1,4,2,5,3])
    >>> 58
    Explanation: The odd-length subarrays of arr and their sums are:
    [1] = 1
    [4] = 4
    [2] = 2
    [5] = 5
    [3] = 3
    [1,4,2] = 7
    [4,2,5] = 11
    [2,5,3] = 10
    [1,4,2,5,3] = 15
    If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58
    Example 2:
    
    >>> sumOddLengthSubarrays(arr = [1,2])
    >>> 3
    Explanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.
    Example 3:
    
    >>> sumOddLengthSubarrays(arr = [10,11,12])
    >>> 66
    """
def maxSumRangeQuery(nums: List[int], requests: List[List[int]]) -> int:
    """
    We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi]. The ith request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi]. Both starti and endi are 0-indexed.
    Return the maximum total sum of all requests among all permutations of nums.
    Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> maxSumRangeQuery(nums = [1,2,3,4,5], requests = [[1,3],[0,1]])
    >>> 19
    Explanation: One permutation of nums is [2,1,3,4,5] with the following result:
    requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
    requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
    Total sum: 8 + 3 = 11.
    A permutation with a higher total sum is [3,5,4,2,1] with the following result:
    requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
    requests[1] -> nums[0] + nums[1] = 3 + 5  = 8
    Total sum: 11 + 8 = 19, which is the best that you can do.
    
    Example 2:
    
    >>> maxSumRangeQuery(nums = [1,2,3,4,5,6], requests = [[0,1]])
    >>> 11
    Explanation: A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].
    Example 3:
    
    >>> maxSumRangeQuery(nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]])
    >>> 47
    Explanation: A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].
    """
def minSubarray(nums: List[int], p: int) -> int:
    """
    Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.
    Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
    A subarray is defined as a contiguous block of elements in the array.
    
    Example 1:
    
    >>> minSubarray(nums = [3,1,4,2], p = 6)
    >>> 1
    Explanation: The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
    
    Example 2:
    
    >>> minSubarray(nums = [6,3,5,2], p = 9)
    >>> 2
    Explanation: We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.
    
    Example 3:
    
    >>> minSubarray(nums = [1,2,3], p = 3)
    >>> 0
    Explanation: Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.
    """
def isPrintable(targetGrid: List[List[int]]) -> bool:
    """
    There is a strange printer with the following two special requirements:
    
    On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.
    Once the printer has used a color for the above operation, the same color cannot be used again.
    
    You are given a m x n matrix targetGrid, where targetGrid[row][col] is the color in the position (row, col) of the grid.
    Return true if it is possible to print the matrix targetGrid, otherwise, return false.
    
    Example 1:
    
    
    >>> isPrintable(targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]])
    >>> true
    
    Example 2:
    
    
    >>> isPrintable(targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]])
    >>> true
    
    Example 3:
    
    >>> isPrintable(targetGrid = [[1,2,1],[2,1,2],[1,2,1]])
    >>> false
    Explanation: It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
    """
def reorderSpaces(text: str) -> str:
    """
    You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word.
    Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text.
    Return the string after rearranging the spaces.
    
    Example 1:
    
    >>> reorderSpaces(text = "  this   is  a sentence ")
    >>> "this   is   a   sentence"
    Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
    
    Example 2:
    
    >>> reorderSpaces(text = " practice   makes   perfect")
    >>> "practice   makes   perfect "
    Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
    """
def maxUniqueSplit(s: str) -> int:
    """
    Given a string s, return the maximum number of unique substrings that the given string can be split into.
    You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> maxUniqueSplit(s = "ababccc")
    >>> 5
    Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
    
    Example 2:
    
    >>> maxUniqueSplit(s = "aba")
    >>> 2
    Explanation: One way to split maximally is ['a', 'ba'].
    
    Example 3:
    
    >>> maxUniqueSplit(s = "aa")
    >>> 1
    Explanation: It is impossible to split the string any further.
    """
def maxProductPath(grid: List[List[int]]) -> int:
    """
    You are given a m x n matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix.
    Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (m - 1, n - 1), find the path with the maximum non-negative product. The product of a path is the product of all integers in the grid cells visited along the path.
    Return the maximum non-negative product modulo 109 + 7. If the maximum product is negative, return -1.
    Notice that the modulo is performed after getting the maximum product.
    
    Example 1:
    
    
    >>> maxProductPath(grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]])
    >>> -1
    Explanation: It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.
    
    Example 2:
    
    
    >>> maxProductPath(grid = [[1,-2,1],[1,-2,1],[3,-4,1]])
    >>> 8
    Explanation: Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).
    
    Example 3:
    
    
    >>> maxProductPath(grid = [[1,3],[0,-4]])
    >>> 0
    Explanation: Maximum non-negative product is shown (1 * 0 * -4 = 0).
    """
def connectTwoGroups(cost: List[List[int]]) -> int:
    """
    You are given two groups of points where the first group has size1 points, the second group has size2 points, and size1 >= size2.
    The cost of the connection between any two points are given in an size1 x size2 matrix where cost[i][j] is the cost of connecting point i of the first group and point j of the second group. The groups are connected if each point in both groups is connected to one or more points in the opposite group. In other words, each point in the first group must be connected to at least one point in the second group, and each point in the second group must be connected to at least one point in the first group.
    Return the minimum cost it takes to connect the two groups.
    
    Example 1:
    
    
    >>> connectTwoGroups(cost = [[15, 96], [36, 2]])
    >>> 17
    Explanation: The optimal way of connecting the groups is:
    1--A
    2--B
    This results in a total cost of 17.
    
    Example 2:
    
    
    >>> connectTwoGroups(cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]])
    >>> 4
    Explanation: The optimal way of connecting the groups is:
    1--A
    2--B
    2--C
    3--A
    This results in a total cost of 4.
    Note that there are multiple points connected to point 2 in the first group and point A in the second group. This does not matter as there is no limit to the number of points that can be connected. We only care about the minimum total cost.
    
    Example 3:
    
    >>> connectTwoGroups(cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]])
    >>> 10
    """
def minOperations(logs: List[str]) -> int:
    """
    The Leetcode file system keeps a log each time some user performs a change folder operation.
    The operations are described below:
    
    "../" : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
    "./" : Remain in the same folder.
    "x/" : Move to the child folder named x (This folder is guaranteed to always exist).
    
    You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.
    The file system starts in the main folder, then the operations in logs are performed.
    Return the minimum number of operations needed to go back to the main folder after the change folder operations.
    
    Example 1:
    
    
    >>> minOperations(logs = ["d1/","d2/","../","d21/","./"])
    >>> 2
    Explanation: Use this change folder operation "../" 2 times and go back to the main folder.
    
    Example 2:
    
    
    >>> minOperations(logs = ["d1/","d2/","./","d3/","../","d31/"])
    >>> 3
    
    Example 3:
    
    >>> minOperations(logs = ["d1/","../","../","../"])
    >>> 0
    """
def maximumRequests(n: int, requests: List[List[int]]) -> int:
    """
    We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.
    You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
    All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.
    Return the maximum number of achievable requests.
    
    Example 1:
    
    
    >>> maximumRequests(n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]])
    >>> 5
    Explantion: Let's see the requests:
    From building 0 we have employees x and y and both want to move to building 1.
    From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
    From building 2 we have employee z and they want to move to building 0.
    From building 3 we have employee c and they want to move to building 4.
    From building 4 we don't have any requests.
    We can achieve the requests of users x and b by swapping their places.
    We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
    
    Example 2:
    
    
    >>> maximumRequests(n = 3, requests = [[0,0],[1,2],[2,1]])
    >>> 3
    Explantion: Let's see the requests:
    From building 0 we have employee x and they want to stay in the same building 0.
    From building 1 we have employee y and they want to move to building 2.
    From building 2 we have employee z and they want to move to building 1.
    We can achieve all the requests.
    Example 3:
    
    >>> maximumRequests(n = 4, requests = [[0,3],[3,1],[1,2],[2,0]])
    >>> 4
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findNearestRightNode(root: TreeNode, u: TreeNode) -> Optional[TreeNode]:
    """
    Given the root of a binary tree and a node u in the tree, return the nearest node on the same level that is to the right of u, or return null if u is the rightmost node in its level.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,null,4,5,6], u = 4)
    >>> 5
    Explanation: The nearest node on the same level to the right of node 4 is node 5.
    
    Example 2:
    
    
    >>> __init__(root = [3,null,4,2], u = 2)
    >>> null
    Explanation: There are no nodes to the right of 2.
    """
def alertNames(keyName: List[str], keyTime: List[str]) -> List[str]:
    """
    LeetCode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an alert if any worker uses the key-card three or more times in a one-hour period.
    You are given a list of strings keyName and keyTime where [keyName[i], keyTime[i]] corresponds to a person's name and the time when their key-card was used in a single day.
    Access times are given in the 24-hour time format "HH:MM", such as "23:51" and "09:49".
    Return a list of unique worker names who received an alert for frequent keycard use. Sort the names in ascending order alphabetically.
    Notice that "10:00" - "11:00" is considered to be within a one-hour period, while "22:51" - "23:52" is not considered to be within a one-hour period.
    
    Example 1:
    
    >>> alertNames(keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"])
    >>> ["daniel"]
    Explanation: "daniel" used the keycard 3 times in a one-hour period ("10:00","10:40", "11:00").
    
    Example 2:
    
    >>> alertNames(keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"])
    >>> ["bob"]
    Explanation: "bob" used the keycard 3 times in a one-hour period ("21:00","21:20", "21:30").
    """
def restoreMatrix(rowSum: List[int], colSum: List[int]) -> List[List[int]]:
    """
    You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.
    Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements.
    Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that at least one matrix that fulfills the requirements exists.
    
    Example 1:
    
    >>> restoreMatrix(rowSum = [3,8], colSum = [4,7])
    >>> [[3,0],
             [1,7]]
    Explanation:
    0th row: 3 + 0 = 3 == rowSum[0]
    1st row: 1 + 7 = 8 == rowSum[1]
    0th column: 3 + 1 = 4 == colSum[0]
    1st column: 0 + 7 = 7 == colSum[1]
    The row and column sums match, and all matrix elements are non-negative.
    Another possible matrix is: [[1,2],
                                 [3,5]]
    
    Example 2:
    
    >>> restoreMatrix(rowSum = [5,7,10], colSum = [8,6,8])
    >>> [[0,5,0],
             [6,1,0],
             [2,0,8]]
    """