ShwStone/humaneval-leetcode huggingface dataset:
def decompressRLElist(nums: List[int]) -> List[int]:
    """
    We are given a list nums of integers representing a list compressed with run-length encoding.
    Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0).  For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
    Return the decompressed list.
    
    Example 1:
    
    >>> decompressRLElist(nums = [1,2,3,4])
    >>> [2,4,4,4]
    Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
    The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
    At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
    
    Example 2:
    
    >>> decompressRLElist(nums = [1,1,2,3])
    >>> [1,3,3]
    """
def matrixBlockSum(mat: List[List[int]], k: int) -> List[List[int]]:
    """
    Given a m x n matrix mat and an integer k, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for:
    
    i - k <= r <= i + k,
    j - k <= c <= j + k, and
    (r, c) is a valid position in the matrix.
    
    
    Example 1:
    
    >>> matrixBlockSum(mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1)
    >>> [[12,21,16],[27,45,33],[24,39,28]]
    
    Example 2:
    
    >>> matrixBlockSum(mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2)
    >>> [[45,45,45],[45,45,45],[45,45,45]]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumEvenGrandparent(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.
    A grandparent of a node is the parent of its parent if it exists.
    
    Example 1:
    
    
    >>> __init__(root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5])
    >>> 18
    Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
    
    Example 2:
    
    
    >>> __init__(root = [1])
    >>> 0
    """
def distinctEchoSubstrings(text: str) -> int:
    """
    Return the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).
    
    Example 1:
    
    >>> distinctEchoSubstrings(text = "abcabcabc")
    >>> 3
    Explanation: The 3 substrings are "abcabc", "bcabca" and "cabcab".
    
    Example 2:
    
    >>> distinctEchoSubstrings(text = "leetcodeleetcode")
    >>> 2
    Explanation: The 2 substrings are "ee" and "leetcodeleetcode".
    """
def getNoZeroIntegers(n: int) -> List[int]:
    """
    No-Zero integer is a positive integer that does not contain any 0 in its decimal representation.
    Given an integer n, return a list of two integers [a, b] where:
    
    a and b are No-Zero integers.
    a + b = n
    
    The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
    
    Example 1:
    
    >>> getNoZeroIntegers(n = 2)
    >>> [1,1]
    Explanation: Let a = 1 and b = 1.
    Both a and b are no-zero integers, and a + b = 2 = n.
    
    Example 2:
    
    >>> getNoZeroIntegers(n = 11)
    >>> [2,9]
    Explanation: Let a = 2 and b = 9.
    Both a and b are no-zero integers, and a + b = 11 = n.
    Note that there are other valid answers as [8, 3] that can be accepted.
    """
def minFlips(a: int, b: int, c: int) -> int:
    """
    Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).\r
    Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    \r
    \r
    >>> minFlips(a = 2, b = 6, c = 5\r)
    >>> 3\r
    Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)\r
    \r
    Example 2:\r
    \r
    \r
    >>> minFlips(a = 4, b = 2, c = 7\r)
    >>> 1\r
    \r
    \r
    Example 3:\r
    \r
    \r
    >>> minFlips(a = 1, b = 2, c = 3\r)
    >>> 0\r
    \r
    \r
     \r
    """
def makeConnected(n: int, connections: List[List[int]]) -> int:
    """
    There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
    You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
    Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
    
    Example 1:
    
    
    >>> makeConnected(n = 4, connections = [[0,1],[0,2],[1,2]])
    >>> 1
    Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
    
    Example 2:
    
    
    >>> makeConnected(n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]])
    >>> 2
    
    Example 3:
    
    >>> makeConnected(n = 6, connections = [[0,1],[0,2],[0,3],[1,2]])
    >>> -1
    Explanation: There are not enough cables.
    """
def minimumDistance(word: str) -> int:
    """
    You have a keyboard layout as shown above in the X-Y plane, where each English uppercase letter is located at some coordinate.
    
    For example, the letter 'A' is located at coordinate (0, 0), the letter 'B' is located at coordinate (0, 1), the letter 'P' is located at coordinate (2, 3) and the letter 'Z' is located at coordinate (4, 1).
    
    Given the string word, return the minimum total distance to type such string using only two fingers.
    The distance between coordinates (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|.
    Note that the initial positions of your two fingers are considered free so do not count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.
    
    Example 1:
    
    >>> minimumDistance(word = "CAKE")
    >>> 3
    Explanation: Using two fingers, one optimal way to type "CAKE" is:
    Finger 1 on letter 'C' -> cost = 0
    Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2
    Finger 2 on letter 'K' -> cost = 0
    Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1
    Total distance = 3
    
    Example 2:
    
    >>> minimumDistance(word = "HAPPY")
    >>> 6
    Explanation: Using two fingers, one optimal way to type "HAPPY" is:
    Finger 1 on letter 'H' -> cost = 0
    Finger 1 on letter 'A' -> cost = Distance from letter 'H' to letter 'A' = 2
    Finger 2 on letter 'P' -> cost = 0
    Finger 2 on letter 'P' -> cost = Distance from letter 'P' to letter 'P' = 0
    Finger 1 on letter 'Y' -> cost = Distance from letter 'A' to letter 'Y' = 4
    Total distance = 6
    """
def printVertically(s: str) -> List[str]:
    """
    Given a string s. Return all the words vertically in the same order in which they appear in s.\r
    Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).\r
    Each word would be put on only one column and that in one column there will be only one word.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> printVertically(s = "HOW ARE YOU"\r)
    >>> ["HAY","ORO","WEU"]\r
    Explanation: Each word is printed vertically. \r
     "HAY"\r
     "ORO"\r
     "WEU"\r
    \r
    \r
    Example 2:\r
    \r
    \r
    >>> printVertically(s = "TO BE OR NOT TO BE"\r)
    >>> ["TBONTB","OEROOE","   T"]\r
    Explanation: Trailing spaces is not allowed. \r
    "TBONTB"\r
    "OEROOE"\r
    "   T"\r
    \r
    \r
    Example 3:\r
    \r
    \r
    >>> printVertically(s = "CONTEST IS COMING"\r)
    >>> ["CIC","OSO","N M","T I","E N","S G","T"]\r
    \r
    \r
     \r
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def removeLeafNodes(root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
    """
    Given a binary tree root and an integer target, delete all the leaf nodes with value target.
    Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,2,null,2,4], target = 2)
    >>> [1,null,3,null,4]
    Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left).
    After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
    
    Example 2:
    
    
    >>> __init__(root = [1,3,3,3,2], target = 3)
    >>> [1,3,null,null,2]
    
    Example 3:
    
    
    >>> __init__(root = [1,2,null,2,null,2], target = 2)
    >>> [1]
    Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
    """
def minTaps(n: int, ranges: List[int]) -> int:
    """
    There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e., the length of the garden is n).
    There are n + 1 taps located at points [0, 1, ..., n] in the garden.
    Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
    Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
    
    Example 1:
    
    
    >>> minTaps(n = 5, ranges = [3,4,1,1,0,0])
    >>> 1
    Explanation: The tap at point 0 can cover the interval [-3,3]
    The tap at point 1 can cover the interval [-3,5]
    The tap at point 2 can cover the interval [1,3]
    The tap at point 3 can cover the interval [2,4]
    The tap at point 4 can cover the interval [4,4]
    The tap at point 5 can cover the interval [5,5]
    Opening Only the second tap will water the whole garden [0,5]
    
    Example 2:
    
    >>> minTaps(n = 3, ranges = [0,0,0,0])
    >>> -1
    Explanation: Even if you activate all the four taps you cannot water the whole garden.
    """
def breakPalindrome(palindrome: str) -> str:
    """
    Given a palindromic string of lowercase English letters palindrome, replace exactly one character with any lowercase English letter so that the resulting string is not a palindrome and that it is the lexicographically smallest one possible.
    Return the resulting string. If there is no way to replace a character to make it not a palindrome, 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, a has a character strictly smaller than the corresponding character in b. For example, "abcc" is lexicographically smaller than "abcd" because the first position they differ is at the fourth character, and 'c' is smaller than 'd'.
    
    Example 1:
    
    >>> breakPalindrome(palindrome = "abccba")
    >>> "aaccba"
    Explanation: There are many ways to make "abccba" not a palindrome, such as "zbccba", "aaccba", and "abacba".
    Of all the ways, "aaccba" is the lexicographically smallest.
    
    Example 2:
    
    >>> breakPalindrome(palindrome = "a")
    >>> ""
    Explanation: There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
    """
def diagonalSort(mat: List[List[int]]) -> List[List[int]]:
    """
    A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2].
    Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.
    
    Example 1:
    
    
    >>> diagonalSort(mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]])
    >>> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
    
    Example 2:
    
    >>> diagonalSort(mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]])
    >>> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
    """
def maxValueAfterReverse(nums: List[int]) -> int:
    """
    You are given an integer array nums. The value of this array is defined as the sum of |nums[i] - nums[i + 1]| for all 0 <= i < nums.length - 1.
    You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
    Find maximum possible value of the final array.
    
    Example 1:
    
    >>> maxValueAfterReverse(nums = [2,3,1,5,4])
    >>> 10
    Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
    
    Example 2:
    
    >>> maxValueAfterReverse(nums = [2,4,9,24,2,1,10])
    >>> 68
    """
def arrayRankTransform(arr: List[int]) -> List[int]:
    """
    Given an array of integers arr, replace each element with its rank.
    The rank represents how large the element is. The rank has the following rules:
    
    Rank is an integer starting from 1.
    The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
    Rank should be as small as possible.
    
    
    Example 1:
    
    >>> arrayRankTransform(arr = [40,10,20,30])
    >>> [4,1,2,3]
    Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.
    Example 2:
    
    >>> arrayRankTransform(arr = [100,100,100])
    >>> [1,1,1]
    Explanation: Same elements share the same rank.
    
    Example 3:
    
    >>> arrayRankTransform(arr = [37,12,28,9,100,56,80,5,12])
    >>> [5,3,4,2,8,6,7,1,3]
    """
def removePalindromeSub(s: str) -> int:
    """
    You are given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.
    Return the minimum number of steps to make the given string empty.
    A string is a subsequence of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does not necessarily need to be contiguous.
    A string is called palindrome if is one that reads the same backward as well as forward.
    
    Example 1:
    
    >>> removePalindromeSub(s = "ababa")
    >>> 1
    Explanation: s is already a palindrome, so its entirety can be removed in a single step.
    
    Example 2:
    
    >>> removePalindromeSub(s = "abb")
    >>> 2
    Explanation: "abb" -> "bb" -> "".
    Remove palindromic subsequence "a" then "bb".
    
    Example 3:
    
    >>> removePalindromeSub(s = "baabb")
    >>> 2
    Explanation: "baabb" -> "b" -> "".
    Remove palindromic subsequence "baab" then "b".
    """
def filterRestaurants(restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:
    """
    Given the array restaurants where  restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You have to filter the restaurants using three filters.
    The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendlyi set to true) or false (meaning you can include any restaurant). In addition, you have the filters maxPrice and maxDistance which are the maximum value for price and distance of restaurants you should consider respectively.
    Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id from highest to lowest. For simplicity veganFriendlyi and veganFriendly take value 1 when it is true, and 0 when it is false.
    
    Example 1:
    
    >>> filterRestaurants(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10)
    >>> [3,1,5]
    Explanation:
    The restaurants are:
    Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
    Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
    Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
    Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
    Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1]
    After filter restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest).
    
    Example 2:
    
    >>> filterRestaurants(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10)
    >>> [4,3,2,1,5]
    Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
    
    Example 3:
    
    >>> filterRestaurants(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3)
    >>> [4,5]
    """
def findTheCity(n: int, edges: List[List[int]], distanceThreshold: int) -> int:
    """
    There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.
    Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold, If there are multiple such cities, return the city with the greatest number.
    Notice that the distance of a path connecting cities i and j is equal to the sum of the edges' weights along that path.
    
    Example 1:
    
    
    >>> findTheCity(n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4)
    >>> 3
    Explanation: The figure above describes the graph.
    The neighboring cities at a distanceThreshold = 4 for each city are:
    City 0 -> [City 1, City 2]
    City 1 -> [City 0, City 2, City 3]
    City 2 -> [City 0, City 1, City 3]
    City 3 -> [City 1, City 2]
    Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.
    
    Example 2:
    
    
    >>> findTheCity(n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2)
    >>> 0
    Explanation: The figure above describes the graph.
    The neighboring cities at a distanceThreshold = 2 for each city are:
    City 0 -> [City 1]
    City 1 -> [City 0, City 4]
    City 2 -> [City 3, City 4]
    City 3 -> [City 2, City 4]
    City 4 -> [City 1, City 2, City 3]
    The city 0 has 1 neighboring city at a distanceThreshold = 2.
    """
def minDifficulty(jobDifficulty: List[int], d: int) -> int:
    """
    You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).
    You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.
    You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].
    Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.
    
    Example 1:
    
    
    >>> minDifficulty(jobDifficulty = [6,5,4,3,2,1], d = 2)
    >>> 7
    Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
    Second day you can finish the last job, total difficulty = 1.
    The difficulty of the schedule = 6 + 1 = 7
    
    Example 2:
    
    >>> minDifficulty(jobDifficulty = [9,9,9], d = 4)
    >>> -1
    Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
    
    Example 3:
    
    >>> minDifficulty(jobDifficulty = [1,1,1], d = 3)
    >>> 3
    Explanation: The schedule is one job per day. total difficulty will be 3.
    """
def kWeakestRows(mat: List[List[int]], k: int) -> List[int]:
    """
    You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.
    A row i is weaker than a row j if one of the following is true:
    
    The number of soldiers in row i is less than the number of soldiers in row j.
    Both rows have the same number of soldiers and i < j.
    
    Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.
    
    Example 1:
    
    [[1,1,0,0,0],
     [1,1,1,1,0],
     [1,0,0,0,0],
     [1,1,0,0,0],
     [1,1,1,1,1]],
    k = 3
    >>> kWeakestRows(mat =)
    >>> [2,0,3]
    Explanation:
    The number of soldiers in each row is:
    - Row 0: 2
    - Row 1: 4
    - Row 2: 1
    - Row 3: 2
    - Row 4: 5
    The rows ordered from weakest to strongest are [2,0,3,1,4].
    
    Example 2:
    
    [[1,0,0,0],
     [1,1,1,1],
     [1,0,0,0],
     [1,0,0,0]],
    k = 2
    >>> kWeakestRows(mat =)
    >>> [0,2]
    Explanation:
    The number of soldiers in each row is:
    - Row 0: 1
    - Row 1: 4
    - Row 2: 1
    - Row 3: 1
    The rows ordered from weakest to strongest are [0,2,3,1].
    """
def minSetSize(arr: List[int]) -> int:
    """
    You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
    Return the minimum size of the set so that at least half of the integers of the array are removed.
    
    Example 1:
    
    >>> minSetSize(arr = [3,3,3,3,5,5,5,2,2,7])
    >>> 2
    Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
    Possible sets of size 2 are {3,5},{3,2},{5,2}.
    Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array.
    
    Example 2:
    
    >>> minSetSize(arr = [7,7,7,7,7,7])
    >>> 1
    Explanation: The only possible set you can choose is {7}. This will make the new array empty.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxProduct(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.
    Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 109 + 7.
    Note that you need to maximize the answer before taking the mod and not after taking it.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4,5,6])
    >>> 110
    Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
    
    Example 2:
    
    
    >>> __init__(root = [1,null,2,3,4,null,null,5,6])
    >>> 90
    Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
    """