ShwStone/humaneval-leetcode huggingface dataset:
def substringXorQueries(s: str, queries: List[List[int]]) -> List[List[int]]:
    """
    You are given a binary string s, and a 2D integer array queries where queries[i] = [firsti, secondi].
    For the ith query, find the shortest substring of s whose decimal value, val, yields secondi when bitwise XORed with firsti. In other words, val ^ firsti == secondi.
    The answer to the ith query is the endpoints (0-indexed) of the substring [lefti, righti] or [-1, -1] if no such substring exists. If there are multiple answers, choose the one with the minimum lefti.
    Return an array ans where ans[i] = [lefti, righti] is the answer to the ith query.
    A substring is a contiguous non-empty sequence of characters within a string.
    
    Example 1:
    
    >>> substringXorQueries(s = "101101", queries = [[0,5],[1,2]])
    >>> [[0,2],[2,3]]
    Explanation: For the first query the substring in range [0,2] is "101" which has a decimal value of 5, and 5 ^ 0 = 5, hence the answer to the first query is [0,2]. In the second query, the substring in range [2,3] is "11", and has a decimal value of 3, and 3 ^ 1 = 2. So, [2,3] is returned for the second query.
    
    
    Example 2:
    
    >>> substringXorQueries(s = "0101", queries = [[12,8]])
    >>> [[-1,-1]]
    Explanation: In this example there is no substring that answers the query, hence [-1,-1] is returned.
    
    Example 3:
    
    >>> substringXorQueries(s = "1", queries = [[4,5]])
    >>> [[0,0]]
    Explanation: For this example, the substring in range [0,0] has a decimal value of 1, and 1 ^ 4 = 5. So, the answer is [0,0].
    """
def minimumScore(s: str, t: str) -> int:
    """
    You are given two strings s and t.
    You are allowed to remove any number of characters from the string t.
    The score of the string is 0 if no characters are removed from the string t, otherwise:
    
    Let left be the minimum index among all removed characters.
    Let right be the maximum index among all removed characters.
    
    Then the score of the string is right - left + 1.
    Return the minimum possible score to make t a subsequence of s.
    A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
    
    Example 1:
    
    >>> minimumScore(s = "abacaba", t = "bzaa")
    >>> 1
    Explanation: In this example, we remove the character "z" at index 1 (0-indexed).
    The string t becomes "baa" which is a subsequence of the string "abacaba" and the score is 1 - 1 + 1 = 1.
    It can be proven that 1 is the minimum score that we can achieve.
    
    Example 2:
    
    >>> minimumScore(s = "cde", t = "xyz")
    >>> 3
    Explanation: In this example, we remove characters "x", "y" and "z" at indices 0, 1, and 2 (0-indexed).
    The string t becomes "" which is a subsequence of the string "cde" and the score is 2 - 0 + 1 = 3.
    It can be proven that 3 is the minimum score that we can achieve.
    """
def minMaxDifference(num: int) -> int:
    """
    You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
    Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
    Notes:
    
    When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2.
    Bob can remap a digit to itself, in which case num does not change.
    Bob can remap different digits for obtaining minimum and maximum values respectively.
    The resulting number after remapping can contain leading zeroes.
    
    
    Example 1:
    
    >>> minMaxDifference(num = 11891)
    >>> 99009
    Explanation:
    To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
    To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
    The difference between these two numbers is 99009.
    
    Example 2:
    
    >>> minMaxDifference(num = 90)
    >>> 99
    Explanation:
    The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
    Thus, we return 99.
    """
def minimizeSum(nums: List[int]) -> int:
    """
    You are given an integer array nums.
    
    The low score of nums is the minimum absolute difference between any two integers.
    The high score of nums is the maximum absolute difference between any two integers.
    The score of nums is the sum of the high and low scores.
    
    Return the minimum score after changing two elements of nums.
    
    Example 1:
    
    >>> minimizeSum(nums = [1,4,7,8,5])
    >>> 3
    Explanation:
    
    Change nums[0] and nums[1] to be 6 so that nums becomes [6,6,7,8,5].
    The low score is the minimum absolute difference: |6 - 6| = 0.
    The high score is the maximum absolute difference: |8 - 5| = 3.
    The sum of high and low score is 3.
    
    
    Example 2:
    
    >>> minimizeSum(nums = [1,4,3])
    >>> 0
    Explanation:
    
    Change nums[1] and nums[2] to 1 so that nums becomes [1,1,1].
    The sum of maximum absolute difference and minimum absolute difference is 0.
    """
def minImpossibleOR(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums.
    We say that an integer x is expressible from nums if there exist some integers 0 <= index1 < index2 < ... < indexk < nums.length for which nums[index1] | nums[index2] | ... | nums[indexk] = x. In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of nums.
    Return the minimum positive non-zero integer that is not expressible from nums.
    
    Example 1:
    
    >>> minImpossibleOR(nums = [2,1])
    >>> 4
    Explanation: 1 and 2 are already present in the array. We know that 3 is expressible, since nums[0] | nums[1] = 2 | 1 = 3. Since 4 is not expressible, we return 4.
    
    Example 2:
    
    >>> minImpossibleOR(nums = [5,3,2])
    >>> 1
    Explanation: We can show that 1 is the smallest number that is not expressible.
    """
def handleQuery(nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:
    """
    You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries:
    
    For a query of type 1, queries[i] = [1, l, r]. Flip the values from 0 to 1 and from 1 to 0 in nums1 from index l to index r. Both l and r are 0-indexed.
    For a query of type 2, queries[i] = [2, p, 0]. For every index 0 <= i < n, set nums2[i] = nums2[i] + nums1[i] * p.
    For a query of type 3, queries[i] = [3, 0, 0]. Find the sum of the elements in nums2.
    
    Return an array containing all the answers to the third type queries.
    
    Example 1:
    
    >>> handleQuery(nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]])
    >>> [3]
    Explanation: After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned.
    
    Example 2:
    
    >>> handleQuery(nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]])
    >>> [5]
    Explanation: After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.
    """
def mergeArrays(nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
    """
    You are given two 2D integer arrays nums1 and nums2.
    
    nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
    nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
    
    Each array contains unique ids and is sorted in ascending order by id.
    Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:
    
    Only ids that appear in at least one of the two arrays should be included in the resulting array.
    Each id should be included only once and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be 0.
    
    Return the resulting array. The returned array must be sorted in ascending order by id.
    
    Example 1:
    
    >>> mergeArrays(nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]])
    >>> [[1,6],[2,3],[3,2],[4,6]]
    Explanation: The resulting array contains the following:
    - id = 1, the value of this id is 2 + 4 = 6.
    - id = 2, the value of this id is 3.
    - id = 3, the value of this id is 2.
    - id = 4, the value of this id is 5 + 1 = 6.
    
    Example 2:
    
    >>> mergeArrays(nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]])
    >>> [[1,3],[2,4],[3,6],[4,3],[5,5]]
    Explanation: There are no common ids, so we just include each id with its value in the resulting list.
    """
def minOperations(n: int) -> int:
    """
    You are given a positive integer n, you can do the following operation any number of times:
    
    Add or subtract a power of 2 from n.
    
    Return the minimum number of operations to make n equal to 0.
    A number x is power of 2 if x == 2i where i >= 0.
    
    Example 1:
    
    >>> minOperations(n = 39)
    >>> 3
    Explanation: We can do the following operations:
    - Add 20 = 1 to n, so now n = 40.
    - Subtract 23 = 8 from n, so now n = 32.
    - Subtract 25 = 32 from n, so now n = 0.
    It can be shown that 3 is the minimum number of operations we need to make n equal to 0.
    
    Example 2:
    
    >>> minOperations(n = 54)
    >>> 3
    Explanation: We can do the following operations:
    - Add 21 = 2 to n, so now n = 56.
    - Add 23 = 8 to n, so now n = 64.
    - Subtract 26 = 64 from n, so now n = 0.
    So the minimum number of operations is 3.
    """
def squareFreeSubsets(nums: List[int]) -> int:
    """
    You are given a positive integer 0-indexed array nums.
    A subset of the array nums is square-free if the product of its elements is a square-free integer.
    A square-free integer is an integer that is divisible by no square number other than 1.
    Return the number of square-free non-empty subsets of the array nums. Since the answer may be too large, return it modulo 109 + 7.
    A non-empty subset of nums is an array that can be obtained by deleting some (possibly none but not all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.
    
    Example 1:
    
    >>> squareFreeSubsets(nums = [3,4,4,5])
    >>> 3
    Explanation: There are 3 square-free subsets in this example:
    - The subset consisting of the 0th element [3]. The product of its elements is 3, which is a square-free integer.
    - The subset consisting of the 3rd element [5]. The product of its elements is 5, which is a square-free integer.
    - The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is 15, which is a square-free integer.
    It can be proven that there are no more than 3 square-free subsets in the given array.
    Example 2:
    
    >>> squareFreeSubsets(nums = [1])
    >>> 1
    Explanation: There is 1 square-free subset in this example:
    - The subset consisting of the 0th element [1]. The product of its elements is 1, which is a square-free integer.
    It can be proven that there is no more than 1 square-free subset in the given array.
    """
def findTheString(lcp: List[List[int]]) -> str:
    """
    We define the lcp matrix of any 0-indexed string word of n lowercase English letters as an n x n grid such that:
    
    lcp[i][j] is equal to the length of the longest common prefix between the substrings word[i,n-1] and word[j,n-1].
    
    Given an n x n matrix lcp, return the alphabetically smallest string word that corresponds to lcp. If there is no such string, return an empty string.
    A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "aabd" is lexicographically smaller than "aaca" because the first position they differ is at the third letter, and 'b' comes before 'c'.
    
    Example 1:
    
    >>> findTheString(lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]])
    >>> "abab"
    Explanation: lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is "abab".
    
    Example 2:
    
    >>> findTheString(lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]])
    >>> "aaaa"
    Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa".
    
    Example 3:
    
    >>> findTheString(lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]])
    >>> ""
    Explanation: lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists.
    """
def leftRightDifference(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed integer array nums of size n.
    Define two arrays leftSum and rightSum where:
    
    leftSum[i] is the sum of elements to the left of the index i in the array nums. If there is no such element, leftSum[i] = 0.
    rightSum[i] is the sum of elements to the right of the index i in the array nums. If there is no such element, rightSum[i] = 0.
    
    Return an integer array answer of size n where answer[i] = |leftSum[i] - rightSum[i]|.
    
    Example 1:
    
    >>> leftRightDifference(nums = [10,4,8,3])
    >>> [15,1,11,22]
    Explanation: The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
    The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
    
    Example 2:
    
    >>> leftRightDifference(nums = [1])
    >>> [0]
    Explanation: The array leftSum is [0] and the array rightSum is [0].
    The array answer is [|0 - 0|] = [0].
    """
def divisibilityArray(word: str, m: int) -> List[int]:
    """
    You are given a 0-indexed string word of length n consisting of digits, and a positive integer m.
    The divisibility array div of word is an integer array of length n such that:
    
    div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or
    div[i] = 0 otherwise.
    
    Return the divisibility array of word.
    
    Example 1:
    
    >>> divisibilityArray(word = "998244353", m = 3)
    >>> [1,1,0,0,0,1,1,0,0]
    Explanation: There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
    
    Example 2:
    
    >>> divisibilityArray(word = "1010", m = 10)
    >>> [0,1,0,1]
    Explanation: There are only 2 prefixes that are divisible by 10: "10", and "1010".
    """
def maxNumOfMarkedIndices(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums.
    Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:
    
    Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j.
    
    Return the maximum possible number of marked indices in nums using the above operation any number of times.
    
    Example 1:
    
    >>> maxNumOfMarkedIndices(nums = [3,5,2,4])
    >>> 2
    Explanation: In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 * nums[2] <= nums[1]. Then mark index 2 and 1.
    It can be shown that there's no other valid operation so the answer is 2.
    
    Example 2:
    
    >>> maxNumOfMarkedIndices(nums = [9,2,5,4])
    >>> 4
    Explanation: In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 * nums[3] <= nums[0]. Then mark index 3 and 0.
    In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 * nums[1] <= nums[2]. Then mark index 1 and 2.
    Since there is no other operation, the answer is 4.
    
    Example 3:
    
    >>> maxNumOfMarkedIndices(nums = [7,6,8])
    >>> 0
    Explanation: There is no valid operation to do, so the answer is 0.
    """
def minimumTime(grid: List[List[int]]) -> int:
    """
    You are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater than or equal to grid[row][col].
    You are standing in the top-left cell of the matrix in the 0th second, and you must move to any adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.
    Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return -1.
    
    Example 1:
    
    
    >>> minimumTime(grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]])
    >>> 7
    Explanation: One of the paths that we can take is the following:
    - at t = 0, we are on the cell (0,0).
    - at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.
    - at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.
    - at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.
    - at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.
    - at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.
    - at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.
    - at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.
    The final time is 7. It can be shown that it is the minimum time possible.
    
    Example 2:
    
    
    >>> minimumTime(grid = [[0,2,4],[3,2,1],[1,0,4]])
    >>> -1
    Explanation: There is no path from the top left to the bottom-right cell.
    """
def splitNum(num: int) -> int:
    """
    Given a positive integer num, split it into two non-negative integers num1 and num2 such that:
    
    The concatenation of num1 and num2 is a permutation of num.
    
    
    In other words, the sum of the number of occurrences of each digit in num1 and num2 is equal to the number of occurrences of that digit in num.
    
    
    num1 and num2 can contain leading zeros.
    
    Return the minimum possible sum of num1 and num2.
    Notes:
    
    It is guaranteed that num does not contain any leading zeros.
    The order of occurrence of the digits in num1 and num2 may differ from the order of occurrence of num.
    
    
    Example 1:
    
    >>> splitNum(num = 4325)
    >>> 59
    Explanation: We can split 4325 so that num1 is 24 and num2 is 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
    
    Example 2:
    
    >>> splitNum(num = 687)
    >>> 75
    Explanation: We can split 687 so that num1 is 68 and num2 is 7, which would give an optimal sum of 75.
    """
def coloredCells(n: int) -> int:
    """
    There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer n, indicating that you must do the following routine for n minutes:
    
    At the first minute, color any arbitrary unit cell blue.
    Every minute thereafter, color blue every uncolored cell that touches a blue cell.
    
    Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.
    
    Return the number of colored cells at the end of n minutes.
    
    Example 1:
    
    >>> coloredCells(n = 1)
    >>> 1
    Explanation: After 1 minute, there is only 1 blue cell, so we return 1.
    
    Example 2:
    
    >>> coloredCells(n = 2)
    >>> 5
    Explanation: After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
    """
def rootCount(edges: List[List[int]], guesses: List[List[int]], k: int) -> int:
    """
    Alice has an undirected tree with n nodes labeled from 0 to n - 1. The tree is represented as a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
    Alice wants Bob to find the root of the tree. She allows Bob to make several guesses about her tree. In one guess, he does the following:
    
    Chooses two distinct integers u and v such that there exists an edge [u, v] in the tree.
    He tells Alice that u is the parent of v in the tree.
    
    Bob's guesses are represented by a 2D integer array guesses where guesses[j] = [uj, vj] indicates Bob guessed uj to be the parent of vj.
    Alice being lazy, does not reply to each of Bob's guesses, but just says that at least k of his guesses are true.
    Given the 2D integer arrays edges, guesses and the integer k, return the number of possible nodes that can be the root of Alice's tree. If there is no such tree, return 0.
    
    Example 1:
    
    
    >>> rootCount(edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3)
    >>> 3
    Explanation:
    Root = 0, correct guesses = [1,3], [0,1], [2,4]
    Root = 1, correct guesses = [1,3], [1,0], [2,4]
    Root = 2, correct guesses = [1,3], [1,0], [2,4]
    Root = 3, correct guesses = [1,0], [2,4]
    Root = 4, correct guesses = [1,3], [1,0]
    Considering 0, 1, or 2 as root node leads to 3 correct guesses.
    
    
    Example 2:
    
    
    >>> rootCount(edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1)
    >>> 5
    Explanation:
    Root = 0, correct guesses = [3,4]
    Root = 1, correct guesses = [1,0], [3,4]
    Root = 2, correct guesses = [1,0], [2,1], [3,4]
    Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
    Root = 4, correct guesses = [1,0], [2,1], [3,2]
    Considering any node as root will give at least 1 correct guess.
    """
def passThePillow(n: int, time: int) -> int:
    """
    There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.
    
    For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.
    
    Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.
    
    Example 1:
    
    >>> passThePillow(n = 4, time = 5)
    >>> 2
    Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
    After five seconds, the 2nd person is holding the pillow.
    
    Example 2:
    
    >>> passThePillow(n = 3, time = 2)
    >>> 3
    Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
    After two seconds, the 3rd person is holding the pillow.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def kthLargestLevelSum(root: Optional[TreeNode], k: int) -> int:
    """
    You are given the root of a binary tree and a positive integer k.
    The level sum in the tree is the sum of the values of the nodes that are on the same level.
    Return the kth largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.
    Note that two nodes are on the same level if they have the same distance from the root.
    
    Example 1:
    
    
    >>> __init__(root = [5,8,9,2,1,3,7,4,6], k = 2)
    >>> 13
    Explanation: The level sums are the following:
    - Level 1: 5.
    - Level 2: 8 + 9 = 17.
    - Level 3: 2 + 1 + 3 + 7 = 13.
    - Level 4: 4 + 6 = 10.
    The 2nd largest level sum is 13.
    
    Example 2:
    
    
    >>> __init__(root = [1,2,null,3], k = 1)
    >>> 3
    Explanation: The largest level sum is 3.
    """