ShwStone/humaneval-leetcode huggingface dataset:
def gridIllumination(n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
    """
    There is a 2D grid of size n x n where each cell of this grid has a lamp that is initially turned off.
    You are given a 2D array of lamp positions lamps, where lamps[i] = [rowi, coli] indicates that the lamp at grid[rowi][coli] is turned on. Even if the same lamp is listed more than once, it is turned on.
    When a lamp is turned on, it illuminates its cell and all other cells in the same row, column, or diagonal.
    You are also given another 2D array queries, where queries[j] = [rowj, colj]. For the jth query, determine whether grid[rowj][colj] is illuminated or not. After answering the jth query, turn off the lamp at grid[rowj][colj] and its 8 adjacent lamps if they exist. A lamp is adjacent if its cell shares either a side or corner with grid[rowj][colj].
    Return an array of integers ans, where ans[j] should be 1 if the cell in the jth query was illuminated, or 0 if the lamp was not.
    
    Example 1:
    
    
    >>> gridIllumination(n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]])
    >>> [1,0]
    Explanation: We have the initial grid with all lamps turned off. In the above picture we see the grid after turning on the lamp at grid[0][0] then turning on the lamp at grid[4][4].
    The 0th query asks if the lamp at grid[1][1] is illuminated or not (the blue square). It is illuminated, so set ans[0] = 1. Then, we turn off all lamps in the red square.
    
    The 1st query asks if the lamp at grid[1][0] is illuminated or not (the blue square). It is not illuminated, so set ans[1] = 0. Then, we turn off all lamps in the red rectangle.
    
    
    Example 2:
    
    >>> gridIllumination(n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]])
    >>> [1,1]
    
    Example 3:
    
    >>> gridIllumination(n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]])
    >>> [1,1,0]
    """
def commonChars(words: List[str]) -> List[str]:
    """
    Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
    
    Example 1:
    >>> commonChars(words = ["bella","label","roller"])
    >>> ["e","l","l"]
    Example 2:
    >>> commonChars(words = ["cool","lock","cook"])
    >>> ["c","o"]
    """
def isValid(s: str) -> bool:
    """
    Given a string s, determine if it is valid.
    A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times:
    
    Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note that tleft and tright may be empty.
    
    Return true if s is a valid string, otherwise, return false.
    
    Example 1:
    
    >>> isValid(s = "aabcbc")
    >>> true
    Explanation:
    "" -> "abc" -> "aabcbc"
    Thus, "aabcbc" is valid.
    Example 2:
    
    >>> isValid(s = "abcabcababcc")
    >>> true
    Explanation:
    "" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
    Thus, "abcabcababcc" is valid.
    
    Example 3:
    
    >>> isValid(s = "abccba")
    >>> false
    Explanation: It is impossible to get "abccba" using the operation.
    """
def longestOnes(nums: List[int], k: int) -> int:
    """
    Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
    
    Example 1:
    
    >>> longestOnes(nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2)
    >>> 6
    Explanation: [1,1,1,0,0,1,1,1,1,1,1]
    Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
    Example 2:
    
    >>> longestOnes(nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3)
    >>> 10
    Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
    Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
    """
def largestSumAfterKNegations(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, modify the array in the following way:
    
    choose an index i and replace nums[i] with -nums[i].
    
    You should apply this process exactly k times. You may choose the same index i multiple times.
    Return the largest possible sum of the array after modifying it in this way.
    
    Example 1:
    
    >>> largestSumAfterKNegations(nums = [4,2,3], k = 1)
    >>> 5
    Explanation: Choose index 1 and nums becomes [4,-2,3].
    
    Example 2:
    
    >>> largestSumAfterKNegations(nums = [3,-1,0,2], k = 3)
    >>> 6
    Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
    
    Example 3:
    
    >>> largestSumAfterKNegations(nums = [2,-3,-1,5,-4], k = 2)
    >>> 13
    Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
    """
def clumsy(n: int) -> int:
    """
    The factorial of a positive integer n is the product of all positive integers less than or equal to n.
    
    For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
    
    We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-' in this order.
    
    For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.
    
    However, these operations are still applied using the usual order of operations of arithmetic. We do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
    Additionally, the division that we use is floor division such that 10 * 9 / 8 = 90 / 8 = 11.
    Given an integer n, return the clumsy factorial of n.
    
    Example 1:
    
    >>> clumsy(n = 4)
    >>> 7
    Explanation: 7 = 4 * 3 / 2 + 1
    
    Example 2:
    
    >>> clumsy(n = 10)
    >>> 12
    Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
    """
def minDominoRotations(tops: List[int], bottoms: List[int]) -> int:
    """
    In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
    We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.
    Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.
    If it cannot be done, return -1.
    
    Example 1:
    
    
    >>> minDominoRotations(tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2])
    >>> 2
    Explanation:
    The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
    If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
    
    Example 2:
    
    >>> minDominoRotations(tops = [3,5,1,2,3], bottoms = [3,6,3,3,4])
    >>> -1
    Explanation:
    In this case, it is not possible to rotate the dominoes to make one row of values equal.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def bstFromPreorder(preorder: List[int]) -> Optional[TreeNode]:
    """
    Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its root.
    It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases.
    A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less than Node.val, and any descendant of Node.right has a value strictly greater than Node.val.
    A preorder traversal of a binary tree displays the value of the node first, then traverses Node.left, then traverses Node.right.
    
    Example 1:
    
    
    >>> __init__(preorder = [8,5,1,7,10,12])
    >>> [8,5,10,1,7,null,12]
    
    Example 2:
    
    >>> __init__(preorder = [1,3])
    >>> [1,null,3]
    """
def bitwiseComplement(n: int) -> int:
    """
    The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
    
    For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
    
    Given an integer n, return its complement.
    
    Example 1:
    
    >>> bitwiseComplement(n = 5)
    >>> 2
    Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
    
    Example 2:
    
    >>> bitwiseComplement(n = 7)
    >>> 0
    Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
    
    Example 3:
    
    >>> bitwiseComplement(n = 10)
    >>> 5
    Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
    """
def numPairsDivisibleBy60(time: List[int]) -> int:
    """
    You are given a list of songs where the ith song has a duration of time[i] seconds.
    Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.
    
    Example 1:
    
    >>> numPairsDivisibleBy60(time = [30,20,150,100,40])
    >>> 3
    Explanation: Three pairs have a total duration divisible by 60:
    (time[0] = 30, time[2] = 150): total duration 180
    (time[1] = 20, time[3] = 100): total duration 120
    (time[1] = 20, time[4] = 40): total duration 60
    
    Example 2:
    
    >>> numPairsDivisibleBy60(time = [60,60,60])
    >>> 3
    Explanation: All three pairs have a total duration of 120, which is divisible by 60.
    """
def shipWithinDays(weights: List[int], days: int) -> int:
    """
    A conveyor belt has packages that must be shipped from one port to another within days days.
    The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
    Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.
    
    Example 1:
    
    >>> shipWithinDays(weights = [1,2,3,4,5,6,7,8,9,10], days = 5)
    >>> 15
    Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
    1st day: 1, 2, 3, 4, 5
    2nd day: 6, 7
    3rd day: 8
    4th day: 9
    5th day: 10
    
    Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
    
    Example 2:
    
    >>> shipWithinDays(weights = [3,2,2,4,1,4], days = 3)
    >>> 6
    Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
    1st day: 3, 2
    2nd day: 2, 4
    3rd day: 1, 4
    
    Example 3:
    
    >>> shipWithinDays(weights = [1,2,3,1,1], days = 4)
    >>> 3
    Explanation:
    1st day: 1
    2nd day: 2
    3rd day: 3
    4th day: 1, 1
    """
def numDupDigitsAtMostN(n: int) -> int:
    """
    Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.
    
    Example 1:
    
    >>> numDupDigitsAtMostN(n = 20)
    >>> 1
    Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.
    
    Example 2:
    
    >>> numDupDigitsAtMostN(n = 100)
    >>> 10
    Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
    
    Example 3:
    
    >>> numDupDigitsAtMostN(n = 1000)
    >>> 262
    """
def canThreePartsEqualSum(arr: List[int]) -> bool:
    """
    Given an array of integers arr, return true if we can partition the array into three non-empty parts with equal sums.
    Formally, we can partition the array if we can find indexes i + 1 < j with (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])
    
    Example 1:
    
    >>> canThreePartsEqualSum(arr = [0,2,1,-6,6,-7,9,1,2,0,1])
    >>> true
    Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
    
    Example 2:
    
    >>> canThreePartsEqualSum(arr = [0,2,1,-6,6,7,9,-1,2,0,1])
    >>> false
    
    Example 3:
    
    >>> canThreePartsEqualSum(arr = [3,3,6,5,-2,2,5,1,-9,4])
    >>> true
    Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
    """
def maxScoreSightseeingPair(values: List[int]) -> int:
    """
    You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them.
    The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the sightseeing spots, minus the distance between them.
    Return the maximum score of a pair of sightseeing spots.
    
    Example 1:
    
    >>> maxScoreSightseeingPair(values = [8,1,5,2,6])
    >>> 11
    Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11
    
    Example 2:
    
    >>> maxScoreSightseeingPair(values = [1,2])
    >>> 2
    """
def smallestRepunitDivByK(k: int) -> int:
    """
    Given a positive integer k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1.
    Return the length of n. If there is no such n, return -1.
    Note: n may not fit in a 64-bit signed integer.
    
    Example 1:
    
    >>> smallestRepunitDivByK(k = 1)
    >>> 1
    Explanation: The smallest answer is n = 1, which has length 1.
    
    Example 2:
    
    >>> smallestRepunitDivByK(k = 2)
    >>> -1
    Explanation: There is no such positive integer n divisible by 2.
    
    Example 3:
    
    >>> smallestRepunitDivByK(k = 3)
    >>> 3
    Explanation: The smallest answer is n = 111, which has length 3.
    """
def queryString(s: str, n: int) -> bool:
    """
    Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    >>> queryString(s = "0110", n = 3)
    >>> true
    Example 2:
    >>> queryString(s = "0110", n = 4)
    >>> false
    """
def baseNeg2(n: int) -> str:
    """
    Given an integer n, return a binary string representing its representation in base -2.
    Note that the returned string should not have leading zeros unless the string is "0".
    
    Example 1:
    
    >>> baseNeg2(n = 2)
    >>> "110"
    Explantion: (-2)2 + (-2)1 = 2
    
    Example 2:
    
    >>> baseNeg2(n = 3)
    >>> "111"
    Explantion: (-2)2 + (-2)1 + (-2)0 = 3
    
    Example 3:
    
    >>> baseNeg2(n = 4)
    >>> "100"
    Explantion: (-2)2 = 4
    """
def prefixesDivBy5(nums: List[int]) -> List[bool]:
    """
    You are given a binary array nums (0-indexed).
    We define xi as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit).
    
    For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
    
    Return an array of booleans answer where answer[i] is true if xi is divisible by 5.
    
    Example 1:
    
    >>> prefixesDivBy5(nums = [0,1,1])
    >>> [true,false,false]
    Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.
    Only the first number is divisible by 5, so answer[0] is true.
    
    Example 2:
    
    >>> prefixesDivBy5(nums = [1,1,1])
    >>> [false,false,false]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nextLargerNodes(head: Optional[ListNode]) -> List[int]:
    """
    You are given the head of a linked list with n nodes.
    For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.
    Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.
    
    Example 1:
    
    
    >>> __init__(head = [2,1,5])
    >>> [5,5,0]
    
    Example 2:
    
    
    >>> __init__(head = [2,7,4,3,5])
    >>> [7,0,5,5,0]
    """
def numEnclaves(grid: List[List[int]]) -> int:
    """
    You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.
    A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.
    Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.
    
    Example 1:
    
    
    >>> numEnclaves(grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]])
    >>> 3
    Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
    
    Example 2:
    
    
    >>> numEnclaves(grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]])
    >>> 0
    Explanation: All 1s are either on the boundary or can reach the boundary.
    """
def removeOuterParentheses(s: str) -> str:
    """
    A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.
    
    For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
    
    A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B, with A and B nonempty valid parentheses strings.
    Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 + ... + Pk, where Pi are primitive valid parentheses strings.
    Return s after removing the outermost parentheses of every primitive string in the primitive decomposition of s.
    
    Example 1:
    
    >>> removeOuterParentheses(s = "(()())(())")
    >>> "()()()"
    Explanation:
    The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
    After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
    
    Example 2:
    
    >>> removeOuterParentheses(s = "(()())(())(()(()))")
    >>> "()()()()(())"
    Explanation:
    The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
    After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
    
    Example 3:
    
    >>> removeOuterParentheses(s = "()()")
    >>> ""
    Explanation:
    The input string is "()()", with primitive decomposition "()" + "()".
    After removing outer parentheses of each part, this is "" + "" = "".
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumRootToLeaf(root: Optional[TreeNode]) -> int:
    """
    You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.
    
    For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
    
    For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
    The test cases are generated so that the answer fits in a 32-bits integer.
    
    Example 1:
    
    
    >>> __init__(root = [1,0,1,0,1,0,1])
    >>> 22
    Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
    
    Example 2:
    
    >>> __init__(root = [0])
    >>> 0
    """
def camelMatch(queries: List[str], pattern: str) -> List[bool]:
    """
    Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise.
    A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all.
    
    Example 1:
    
    >>> camelMatch(queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB")
    >>> [true,false,true,true,false]
    Explanation: "FooBar" can be generated like this "F" + "oo" + "B" + "ar".
    "FootBall" can be generated like this "F" + "oot" + "B" + "all".
    "FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
    
    Example 2:
    
    >>> camelMatch(queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa")
    >>> [true,false,true,false,false]
    Explanation: "FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
    "FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
    
    Example 3:
    
    >>> camelMatch(queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT")
    >>> [false,true,false,false,false]
    Explanation: "FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
    """
def videoStitching(clips: List[List[int]], time: int) -> int:
    """
    You are given a series of video clips from a sporting event that lasted time seconds. These video clips can be overlapping with each other and have varying lengths.
    Each video clip is described by an array clips where clips[i] = [starti, endi] indicates that the ith clip started at starti and ended at endi.
    We can cut these clips into segments freely.
    
    For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].
    
    Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event [0, time]. If the task is impossible, return -1.
    
    Example 1:
    
    >>> videoStitching(clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10)
    >>> 3
    Explanation: We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
    Then, we can reconstruct the sporting event as follows:
    We cut [1,9] into segments [1,2] + [2,8] + [8,9].
    Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].
    
    Example 2:
    
    >>> videoStitching(clips = [[0,1],[1,2]], time = 5)
    >>> -1
    Explanation: We cannot cover [0,5] with only [0,1] and [1,2].
    
    Example 3:
    
    >>> videoStitching(clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], time = 9)
    >>> 3
    Explanation: We can take clips [0,4], [4,7], and [6,9].
    """
def divisorGame(n: int) -> bool:
    """
    Alice and Bob take turns playing a game, with Alice starting first.
    Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of:
    
    Choosing any x with 0 < x < n and n % x == 0.
    Replacing the number n on the chalkboard with n - x.
    
    Also, if a player cannot make a move, they lose the game.
    Return true if and only if Alice wins the game, assuming both players play optimally.
    
    Example 1:
    
    >>> divisorGame(n = 2)
    >>> true
    Explanation: Alice chooses 1, and Bob has no more moves.
    
    Example 2:
    
    >>> divisorGame(n = 3)
    >>> false
    Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
    """