ShwStone/humaneval-leetcode huggingface dataset:
def findClosestElements(arr: List[int], k: int, x: int) -> List[int]:
    """
    Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.
    An integer a is closer to x than an integer b if:
    
    |a - x| < |b - x|, or
    |a - x| == |b - x| and a < b
    
    
    Example 1:
    
    >>> findClosestElements(arr = [1,2,3,4,5], k = 4, x = 3)
    >>> [1,2,3,4]
    
    Example 2:
    
    >>> findClosestElements(arr = [1,1,2,3,4,5], k = 4, x = -1)
    >>> [1,1,2,3]
    """
def isPossible(nums: List[int]) -> bool:
    """
    You are given an integer array nums that is sorted in non-decreasing order.
    Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:
    
    Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
    All subsequences have a length of 3 or more.
    
    Return true if you can split nums according to the above conditions, or false otherwise.
    A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).
    
    Example 1:
    
    >>> isPossible(nums = [1,2,3,3,4,5])
    >>> true
    Explanation: nums can be split into the following subsequences:
    [1,2,3,3,4,5] --> 1, 2, 3
    [1,2,3,3,4,5] --> 3, 4, 5
    
    Example 2:
    
    >>> isPossible(nums = [1,2,3,3,4,4,5,5])
    >>> true
    Explanation: nums can be split into the following subsequences:
    [1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
    [1,2,3,3,4,4,5,5] --> 3, 4, 5
    
    Example 3:
    
    >>> isPossible(nums = [1,2,3,4,4,5])
    >>> false
    Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
    """
def newInteger(n: int) -> int:
    """
    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...
    Now, you will have a new integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...].
    Given an integer n, return the nth (1-indexed) integer in the new sequence.
    
    Example 1:
    
    >>> newInteger(n = 9)
    >>> 10
    
    Example 2:
    
    >>> newInteger(n = 10)
    >>> 11
    """
def imageSmoother(img: List[List[int]]) -> List[List[int]]:
    """
    An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
    
    Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.
    
    Example 1:
    
    
    >>> imageSmoother(img = [[1,1,1],[1,0,1],[1,1,1]])
    >>> [[0,0,0],[0,0,0],[0,0,0]]
    Explanation:
    For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
    For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
    For the point (1,1): floor(8/9) = floor(0.88888889) = 0
    
    Example 2:
    
    
    >>> imageSmoother(img = [[100,200,100],[200,50,200],[100,200,100]])
    >>> [[137,141,137],[141,138,141],[137,141,137]]
    Explanation:
    For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
    For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
    For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def widthOfBinaryTree(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the maximum width of the given tree.
    The maximum width of a tree is the maximum width among all levels.
    The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
    It is guaranteed that the answer will in the range of a 32-bit signed integer.
    
    Example 1:
    
    
    >>> __init__(root = [1,3,2,5,3,null,9])
    >>> 4
    Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
    
    Example 2:
    
    
    >>> __init__(root = [1,3,2,5,null,null,9,6,null,7])
    >>> 7
    Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
    
    Example 3:
    
    
    >>> __init__(root = [1,3,2,5])
    >>> 2
    Explanation: The maximum width exists in the second level with length 2 (3,2).
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def checkEqualTree(root: Optional[TreeNode]) -> bool:
    """
    Given the root of a binary tree, return true if you can partition the tree into two trees with equal sums of values after removing exactly one edge on the original tree.
    
    Example 1:
    
    
    >>> __init__(root = [5,10,10,null,null,2,3])
    >>> true
    
    Example 2:
    
    
    >>> __init__(root = [1,2,10,null,null,2,20])
    >>> false
    Explanation: You cannot split the tree into two trees with equal sums after removing exactly one edge on the tree.
    """
def strangePrinter(s: str) -> int:
    """
    There is a strange printer with the following two special properties:
    
    The printer can only print a sequence of the same character each time.
    At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
    
    Given a string s, return the minimum number of turns the printer needed to print it.
    
    Example 1:
    
    >>> strangePrinter(s = "aaabbb")
    >>> 2
    Explanation: Print "aaa" first and then print "bbb".
    
    Example 2:
    
    >>> strangePrinter(s = "aba")
    >>> 2
    Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
    """
def checkPossibility(nums: List[int]) -> bool:
    """
    Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
    We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
    
    Example 1:
    
    >>> checkPossibility(nums = [4,2,3])
    >>> true
    Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
    
    Example 2:
    
    >>> checkPossibility(nums = [4,2,1])
    >>> false
    Explanation: You cannot get a non-decreasing array by modifying at most one element.
    """
def pathSum(nums: List[int]) -> int:
    """
    If the depth of a tree is smaller than 5, then this tree can be represented by an array of three-digit integers. You are given an ascending array nums consisting of three-digit integers representing a binary tree with a depth smaller than 5, where for each integer:
    
    The hundreds digit represents the depth d of this node, where 1 <= d <= 4.
    The tens digit represents the position p of this node within its level, where 1 <= p <= 8, corresponding to its position in a full binary tree.
    The units digit represents the value v of this node, where 0 <= v <= 9.
    
    Return the sum of all paths from the root towards the leaves.
    It is guaranteed that the given array represents a valid connected binary tree.
    
    Example 1:
    
    
    >>> pathSum(nums = [113,215,221])
    >>> 12
    Explanation:
    The tree that the list represents is shown.
    The path sum is (3 + 5) + (3 + 1) = 12.
    
    Example 2:
    
    
    >>> pathSum(nums = [113,221])
    >>> 4
    Explanation:
    The tree that the list represents is shown.
    The path sum is (3 + 1) = 4.
    """
def constructArray(n: int, k: int) -> List[int]:
    """
    Given two integers n and k, construct a list answer that contains n different positive integers ranging from 1 to n and obeys the following requirement:
    
    Suppose this list is answer = [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.
    
    Return the list answer. If there multiple valid answers, return any of them.
    
    Example 1:
    
    >>> constructArray(n = 3, k = 1)
    >>> [1,2,3]
    Explanation: The [1,2,3] has three different positive integers ranging from 1 to 3, and the [1,1] has exactly 1 distinct integer: 1
    
    Example 2:
    
    >>> constructArray(n = 3, k = 2)
    >>> [1,3,2]
    Explanation: The [1,3,2] has three different positive integers ranging from 1 to 3, and the [2,1] has exactly 2 distinct integers: 1 and 2.
    """
def findKthNumber(m: int, n: int, k: int) -> int:
    """
    Nearly everyone has used the Multiplication Table. The multiplication table of size m x n is an integer matrix mat where mat[i][j] == i * j (1-indexed).
    Given three integers m, n, and k, return the kth smallest element in the m x n multiplication table.
    
    Example 1:
    
    
    >>> findKthNumber(m = 3, n = 3, k = 5)
    >>> 3
    Explanation: The 5th smallest number is 3.
    
    Example 2:
    
    
    >>> findKthNumber(m = 2, n = 3, k = 6)
    >>> 6
    Explanation: The 6th smallest number is 6.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def trimBST(root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
    """
    Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.
    Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
    
    Example 1:
    
    
    >>> __init__(root = [1,0,2], low = 1, high = 2)
    >>> [1,null,2]
    
    Example 2:
    
    
    >>> __init__(root = [3,0,4,null,2,null,null,1], low = 1, high = 3)
    >>> [3,2,null,1]
    """
def maximumSwap(num: int) -> int:
    """
    You are given an integer num. You can swap two digits at most once to get the maximum valued number.
    Return the maximum valued number you can get.
    
    Example 1:
    
    >>> maximumSwap(num = 2736)
    >>> 7236
    Explanation: Swap the number 2 and the number 7.
    
    Example 2:
    
    >>> maximumSwap(num = 9973)
    >>> 9973
    Explanation: No swap.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findSecondMinimumValue(root: Optional[TreeNode]) -> int:
    """
    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
    Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
    If no such second minimum value exists, output -1 instead.
    
    
    Example 1:
    
    
    >>> __init__(root = [2,2,5,null,null,5,7])
    >>> 5
    Explanation: The smallest value is 2, the second smallest value is 5.
    
    Example 2:
    
    
    >>> __init__(root = [2,2,2])
    >>> -1
    Explanation: The smallest value is 2, but there isn't any second smallest value.
    """
def flipLights(n: int, presses: int) -> int:
    """
    There is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall. Each of the four buttons has a different functionality where:
    
    Button 1: Flips the status of all the bulbs.
    Button 2: Flips the status of all the bulbs with even labels (i.e., 2, 4, ...).
    Button 3: Flips the status of all the bulbs with odd labels (i.e., 1, 3, ...).
    Button 4: Flips the status of all the bulbs with a label j = 3k + 1 where k = 0, 1, 2, ... (i.e., 1, 4, 7, 10, ...).
    
    You must make exactly presses button presses in total. For each press, you may pick any of the four buttons to press.
    Given the two integers n and presses, return the number of different possible statuses after performing all presses button presses.
    
    Example 1:
    
    >>> flipLights(n = 1, presses = 1)
    >>> 2
    Explanation: Status can be:
    - [off] by pressing button 1
    - [on] by pressing button 2
    
    Example 2:
    
    >>> flipLights(n = 2, presses = 1)
    >>> 3
    Explanation: Status can be:
    - [off, off] by pressing button 1
    - [on, off] by pressing button 2
    - [off, on] by pressing button 3
    
    Example 3:
    
    >>> flipLights(n = 3, presses = 1)
    >>> 4
    Explanation: Status can be:
    - [off, off, off] by pressing button 1
    - [off, on, off] by pressing button 2
    - [on, off, on] by pressing button 3
    - [off, on, on] by pressing button 4
    """
def findNumberOfLIS(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of longest increasing subsequences.
    Notice that the sequence has to be strictly increasing.
    
    Example 1:
    
    >>> findNumberOfLIS(nums = [1,3,5,4,7])
    >>> 2
    Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
    
    Example 2:
    
    >>> findNumberOfLIS(nums = [2,2,2,2,2])
    >>> 5
    Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
    """
def findLengthOfLCIS(nums: List[int]) -> int:
    """
    Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
    A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].
    
    Example 1:
    
    >>> findLengthOfLCIS(nums = [1,3,5,4,7])
    >>> 3
    Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
    Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
    4.
    
    Example 2:
    
    >>> findLengthOfLCIS(nums = [2,2,2,2,2])
    >>> 1
    Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
    increasing.
    """
def cutOffTree(forest: List[List[int]]) -> int:
    """
    You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an m x n matrix. In this matrix:
    
    0 means the cell cannot be walked through.
    1 represents an empty cell that can be walked through.
    A number greater than 1 represents a tree in a cell that can be walked through, and this number is the tree's height.
    
    In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off.
    You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes 1 (an empty cell).
    Starting from the point (0, 0), return the minimum steps you need to walk to cut off all the trees. If you cannot cut off all the trees, return -1.
    Note: The input is generated such that no two trees have the same height, and there is at least one tree needs to be cut off.
    
    Example 1:
    
    
    >>> cutOffTree(forest = [[1,2,3],[0,0,4],[7,6,5]])
    >>> 6
    Explanation: Following the path above allows you to cut off the trees from shortest to tallest in 6 steps.
    
    Example 2:
    
    
    >>> cutOffTree(forest = [[1,2,3],[0,0,0],[7,6,5]])
    >>> -1
    Explanation: The trees in the bottom row cannot be accessed as the middle row is blocked.
    
    Example 3:
    
    >>> cutOffTree(forest = [[2,3,4],[0,0,5],[8,7,6]])
    >>> 6
    Explanation: You can follow the same path as Example 1 to cut off all the trees.
    Note that you can cut off the first tree at (0, 0) before making any steps.
    """
def checkValidString(s: str) -> bool:
    """
    Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.
    The following rules define a valid string:
    
    Any left parenthesis '(' must have a corresponding right parenthesis ')'.
    Any right parenthesis ')' must have a corresponding left parenthesis '('.
    Left parenthesis '(' must go before the corresponding right parenthesis ')'.
    '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".
    
    
    Example 1:
    >>> checkValidString(s = "()")
    >>> true
    Example 2:
    >>> checkValidString(s = "(*)")
    >>> true
    Example 3:
    >>> checkValidString(s = "(*))")
    >>> true
    """
def judgePoint24(cards: List[int]) -> bool:
    """
    You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24.
    You are restricted with the following rules:
    
    The division operator '/' represents real division, not integer division.
    
    
    For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
    
    
    Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
    
    For example, if cards = [1, 1, 1, 1], the expression "-1 - 1 - 1 - 1" is not allowed.
    
    
    You cannot concatenate numbers together
    
    For example, if cards = [1, 2, 1, 2], the expression "12 + 12" is not valid.
    
    
    
    Return true if you can get such expression that evaluates to 24, and false otherwise.
    
    Example 1:
    
    >>> judgePoint24(cards = [4,1,8,7])
    >>> true
    Explanation: (8-4) * (7-1) = 24
    
    Example 2:
    
    >>> judgePoint24(cards = [1,2,1,2])
    >>> false
    """
def validPalindrome(s: str) -> bool:
    """
    Given a string s, return true if the s can be palindrome after deleting at most one character from it.
    
    Example 1:
    
    >>> validPalindrome(s = "aba")
    >>> true
    
    Example 2:
    
    >>> validPalindrome(s = "abca")
    >>> true
    Explanation: You could delete the character 'c'.
    
    Example 3:
    
    >>> validPalindrome(s = "abc")
    >>> false
    """
def nextClosestTime(time: str) -> str:
    """
    Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.
    You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.
    
    Example 1:
    
    >>> nextClosestTime(time = "19:34")
    >>> "19:39"
    Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later.
    It is not 19:33, because this occurs 23 hours and 59 minutes later.
    
    Example 2:
    
    >>> nextClosestTime(time = "23:59")
    >>> "22:22"
    Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22.
    It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.
    """
def calPoints(operations: List[str]) -> int:
    """
    You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
    You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:
    
    An integer x.
    
    
    Record a new score of x.
    
    
    '+'.
    
    Record a new score that is the sum of the previous two scores.
    
    
    'D'.
    
    Record a new score that is the double of the previous score.
    
    
    'C'.
    
    Invalidate the previous score, removing it from the record.
    
    
    
    Return the sum of all the scores on the record after applying all the operations.
    The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
    
    Example 1:
    
    >>> calPoints(ops = ["5","2","C","D","+"])
    >>> 30
    Explanation:
    "5" - Add 5 to the record, record is now [5].
    "2" - Add 2 to the record, record is now [5, 2].
    "C" - Invalidate and remove the previous score, record is now [5].
    "D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
    "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
    The total sum is 5 + 10 + 15 = 30.
    
    Example 2:
    
    >>> calPoints(ops = ["5","-2","4","C","D","9","+","+"])
    >>> 27
    Explanation:
    "5" - Add 5 to the record, record is now [5].
    "-2" - Add -2 to the record, record is now [5, -2].
    "4" - Add 4 to the record, record is now [5, -2, 4].
    "C" - Invalidate and remove the previous score, record is now [5, -2].
    "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
    "9" - Add 9 to the record, record is now [5, -2, -4, 9].
    "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
    "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
    The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
    
    Example 3:
    
    >>> calPoints(ops = ["1","C"])
    >>> 0
    Explanation:
    "1" - Add 1 to the record, record is now [1].
    "C" - Invalidate and remove the previous score, record is now [].
    Since the record is empty, the total sum is 0.
    """
def kEmptySlots(bulbs: List[int], k: int) -> int:
    """
    You have n bulbs in a row numbered from 1 to n. Initially, all the bulbs are turned off. We turn on exactly one bulb every day until all bulbs are on after n days.
    You are given an array bulbs of length n where bulbs[i] = x means that on the (i+1)th day, we will turn on the bulb at position x where i is 0-indexed and x is 1-indexed.
    Given an integer k, return the minimum day number such that there exists two turned on bulbs that have exactly k bulbs between them that are all turned off. If there isn't such day, return -1.
    
    Example 1:
    
    >>> kEmptySlots(bulbs = [1,3,2], k = 1)
    >>> 2
    Explanation:
    On the first day: bulbs[0] = 1, first bulb is turned on: [1,0,0]
    On the second day: bulbs[1] = 3, third bulb is turned on: [1,0,1]
    On the third day: bulbs[2] = 2, second bulb is turned on: [1,1,1]
    We return 2 because on the second day, there were two on bulbs with one off bulb between them.
    Example 2:
    
    >>> kEmptySlots(bulbs = [1,2,3], k = 1)
    >>> -1
    """