ShwStone/humaneval-leetcode huggingface dataset:
def getLastMoment(n: int, left: List[int], right: List[int]) -> int:
    """
    We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.
    When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.
    When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.
    Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.
    
    Example 1:
    
    
    >>> getLastMoment(n = 4, left = [4,3], right = [0,1])
    >>> 4
    Explanation: In the image above:
    -The ant at index 0 is named A and going to the right.
    -The ant at index 1 is named B and going to the right.
    -The ant at index 3 is named C and going to the left.
    -The ant at index 4 is named D and going to the left.
    The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).
    
    Example 2:
    
    
    >>> getLastMoment(n = 7, left = [], right = [0,1,2,3,4,5,6,7])
    >>> 7
    Explanation: All ants are going to the right, the ant at index 0 needs 7 seconds to fall.
    
    Example 3:
    
    
    >>> getLastMoment(n = 7, left = [0,1,2,3,4,5,6,7], right = [])
    >>> 7
    Explanation: All ants are going to the left, the ant at index 7 needs 7 seconds to fall.
    """
def numSubmat(mat: List[List[int]]) -> int:
    """
    Given an m x n binary matrix mat, return the number of submatrices that have all ones.
    
    Example 1:
    
    
    >>> numSubmat(mat = [[1,0,1],[1,1,0],[1,1,0]])
    >>> 13
    Explanation:
    There are 6 rectangles of side 1x1.
    There are 2 rectangles of side 1x2.
    There are 3 rectangles of side 2x1.
    There is 1 rectangle of side 2x2.
    There is 1 rectangle of side 3x1.
    Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
    
    Example 2:
    
    
    >>> numSubmat(mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]])
    >>> 24
    Explanation:
    There are 8 rectangles of side 1x1.
    There are 5 rectangles of side 1x2.
    There are 2 rectangles of side 1x3.
    There are 4 rectangles of side 2x1.
    There are 2 rectangles of side 2x2.
    There are 2 rectangles of side 3x1.
    There is 1 rectangle of side 3x2.
    Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
    """
def minInteger(num: str, k: int) -> str:
    """
    You are given a string num representing the digits of a very large integer and an integer k. You are allowed to swap any two adjacent digits of the integer at most k times.
    Return the minimum integer you can obtain also as a string.
    
    Example 1:
    
    
    >>> minInteger(num = "4321", k = 4)
    >>> "1342"
    Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
    
    Example 2:
    
    >>> minInteger(num = "100", k = 1)
    >>> "010"
    Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
    
    Example 3:
    
    >>> minInteger(num = "36789", k = 1000)
    >>> "36789"
    Explanation: We can keep the number without any swaps.
    """
def reformatDate(date: str) -> str:
    """
    Given a date string in the form Day Month Year, where:
    
    Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.
    Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.
    Year is in the range [1900, 2100].
    
    Convert the date string to the format YYYY-MM-DD, where:
    
    YYYY denotes the 4 digit year.
    MM denotes the 2 digit month.
    DD denotes the 2 digit day.
    
    
    Example 1:
    
    >>> reformatDate(date = "20th Oct 2052")
    >>> "2052-10-20"
    
    Example 2:
    
    >>> reformatDate(date = "6th Jun 1933")
    >>> "1933-06-06"
    
    Example 3:
    
    >>> reformatDate(date = "26th May 1960")
    >>> "1960-05-26"
    """
def rangeSum(nums: List[int], n: int, left: int, right: int) -> int:
    """
    You are given the array nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of n * (n + 1) / 2 numbers.
    Return the sum of the numbers from index left to index right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 109 + 7.
    
    Example 1:
    
    >>> rangeSum(nums = [1,2,3,4], n = 4, left = 1, right = 5)
    >>> 13
    Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
    
    Example 2:
    
    >>> rangeSum(nums = [1,2,3,4], n = 4, left = 3, right = 4)
    >>> 6
    Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
    
    Example 3:
    
    >>> rangeSum(nums = [1,2,3,4], n = 4, left = 1, right = 10)
    >>> 50
    """
def minDifference(nums: List[int]) -> int:
    """
    You are given an integer array nums.
    In one move, you can choose one element of nums and change it to any value.
    Return the minimum difference between the largest and smallest value of nums after performing at most three moves.
    
    Example 1:
    
    >>> minDifference(nums = [5,3,2,4])
    >>> 0
    Explanation: We can make at most 3 moves.
    In the first move, change 2 to 3. nums becomes [5,3,3,4].
    In the second move, change 4 to 3. nums becomes [5,3,3,3].
    In the third move, change 5 to 3. nums becomes [3,3,3,3].
    After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
    
    Example 2:
    
    >>> minDifference(nums = [1,5,0,10,14])
    >>> 1
    Explanation: We can make at most 3 moves.
    In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
    In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
    In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
    After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
    It can be shown that there is no way to make the difference 0 in 3 moves.
    Example 3:
    
    >>> minDifference(nums = [3,100,20])
    >>> 0
    Explanation: We can make at most 3 moves.
    In the first move, change 100 to 7. nums becomes [3,7,20].
    In the second move, change 20 to 7. nums becomes [3,7,7].
    In the third move, change 3 to 7. nums becomes [7,7,7].
    After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
    """
def winnerSquareGame(n: int) -> bool:
    """
    Alice and Bob take turns playing a game, with Alice starting first.
    Initially, there are n stones in a pile. On each player's turn, that player makes a move consisting of removing any non-zero square number of stones in the pile.
    Also, if a player cannot make a move, he/she loses the game.
    Given a positive integer n, return true if and only if Alice wins the game otherwise return false, assuming both players play optimally.
    
    Example 1:
    
    >>> winnerSquareGame(n = 1)
    >>> true
    Explanation: Alice can remove 1 stone winning the game because Bob doesn't have any moves.
    Example 2:
    
    >>> winnerSquareGame(n = 2)
    >>> false
    Explanation: Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -> 1 -> 0).
    
    Example 3:
    
    >>> winnerSquareGame(n = 4)
    >>> true
    Explanation: n is already a perfect square, Alice can win with one move, removing 4 stones (4 -> 0).
    """
def numIdenticalPairs(nums: List[int]) -> int:
    """
    Given an array of integers nums, return the number of good pairs.
    A pair (i, j) is called good if nums[i] == nums[j] and i < j.
    
    Example 1:
    
    >>> numIdenticalPairs(nums = [1,2,3,1,1,3])
    >>> 4
    Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
    
    Example 2:
    
    >>> numIdenticalPairs(nums = [1,1,1,1])
    >>> 6
    Explanation: Each pair in the array are good.
    
    Example 3:
    
    >>> numIdenticalPairs(nums = [1,2,3])
    >>> 0
    """
def numSub(s: str) -> int:
    """
    Given a binary string s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numSub(s = "0110111")
    >>> 9
    Explanation: There are 9 substring in total with only 1's characters.
    "1" -> 5 times.
    "11" -> 3 times.
    "111" -> 1 time.
    Example 2:
    
    >>> numSub(s = "101")
    >>> 2
    Explanation: Substring "1" is shown 2 times in s.
    
    Example 3:
    
    >>> numSub(s = "111111")
    >>> 21
    Explanation: Each substring contains only 1's characters.
    """
def maxProbability(n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
    """
    You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].
    Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.
    If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.
    
    Example 1:
    
    
    >>> maxProbability(n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2)
    >>> 0.25000
    Explanation: There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
    
    Example 2:
    
    
    >>> maxProbability(n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2)
    >>> 0.30000
    
    Example 3:
    
    
    >>> maxProbability(n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2)
    >>> 0.00000
    Explanation: There is no path between 0 and 2.
    """
def getMinDistSum(positions: List[List[int]]) -> float:
    """
    A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new center in a position such that the sum of the euclidean distances to all customers is minimum.
    Given an array positions where positions[i] = [xi, yi] is the position of the ith customer on the map, return the minimum sum of the euclidean distances to all customers.
    In other words, you need to choose the position of the service center [xcentre, ycentre] such that the following formula is minimized:
    
    Answers within 10-5 of the actual value will be accepted.
    
    Example 1:
    
    
    >>> getMinDistSum(positions = [[0,1],[1,0],[1,2],[2,1]])
    >>> 4.00000
    Explanation: As shown, you can see that choosing [xcentre, ycentre] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
    
    Example 2:
    
    
    >>> getMinDistSum(positions = [[1,1],[3,3]])
    >>> 2.82843
    Explanation: The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
    """
def numWaterBottles(numBottles: int, numExchange: int) -> int:
    """
    There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.
    The operation of drinking a full water bottle turns it into an empty bottle.
    Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
    
    Example 1:
    
    
    >>> numWaterBottles(numBottles = 9, numExchange = 3)
    >>> 13
    Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
    Number of water bottles you can drink: 9 + 3 + 1 = 13.
    
    Example 2:
    
    
    >>> numWaterBottles(numBottles = 15, numExchange = 4)
    >>> 19
    Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
    Number of water bottles you can drink: 15 + 3 + 1 = 19.
    """
def countSubTrees(n: int, edges: List[List[int]], labels: str) -> List[int]:
    """
    You are given a tree (i.e. a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. The root of the tree is the node 0, and each node of the tree has a label which is a lower-case character given in the string labels (i.e. The node with the number i has the label labels[i]).
    The edges array is given on the form edges[i] = [ai, bi], which means there is an edge between nodes ai and bi in the tree.
    Return an array of size n where ans[i] is the number of nodes in the subtree of the ith node which have the same label as node i.
    A subtree of a tree T is the tree consisting of a node in T and all of its descendant nodes.
    
    Example 1:
    
    
    >>> countSubTrees(n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd")
    >>> [2,1,1,1,1,1,1]
    Explanation: Node 0 has label 'a' and its sub-tree has node 2 with label 'a' as well, thus the answer is 2. Notice that any node is part of its sub-tree.
    Node 1 has a label 'b'. The sub-tree of node 1 contains nodes 1,4 and 5, as nodes 4 and 5 have different labels than node 1, the answer is just 1 (the node itself).
    
    Example 2:
    
    
    >>> countSubTrees(n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb")
    >>> [4,2,1,1]
    Explanation: The sub-tree of node 2 contains only node 2, so the answer is 1.
    The sub-tree of node 3 contains only node 3, so the answer is 1.
    The sub-tree of node 1 contains nodes 1 and 2, both have label 'b', thus the answer is 2.
    The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label 'b', thus the answer is 4.
    
    Example 3:
    
    
    >>> countSubTrees(n = 5, edges = [[0,1],[0,2],[1,3],[0,4]], labels = "aabab")
    >>> [3,2,1,1,1]
    """
def maxNumOfSubstrings(s: str) -> List[str]:
    """
    Given a string s of lowercase letters, you need to find the maximum number of non-empty substrings of s that meet the following conditions:
    
    The substrings do not overlap, that is for any two substrings s[i..j] and s[x..y], either j < x or i > y is true.
    A substring that contains a certain character c must also contain all occurrences of c.
    
    Find the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length. It can be shown that there exists a unique solution of minimum total length.
    Notice that you can return the substrings in any order.
    
    Example 1:
    
    >>> maxNumOfSubstrings(s = "adefaddaccc")
    >>> ["e","f","ccc"]
    Explanation: The following are all the possible substrings that meet the conditions:
    [
      "adefaddaccc"
      "adefadda",
      "ef",
      "e",
      "f",
      "ccc",
    ]
    If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist.
    
    Example 2:
    
    >>> maxNumOfSubstrings(s = "abbaccd")
    >>> ["d","bb","cc"]
    Explanation: Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length.
    """
def closestToTarget(arr: List[int], target: int) -> int:
    """
    Winston was given the above mysterious function func. He has an integer array arr and an integer target and he wants to find the values l and r that make the value |func(arr, l, r) - target| minimum possible.
    Return the minimum possible value of |func(arr, l, r) - target|.
    Notice that func should be called with the values l and r where 0 <= l, r < arr.length.
    
    Example 1:
    
    >>> closestToTarget(arr = [9,12,3,7,15], target = 5)
    >>> 2
    Explanation: Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.
    
    Example 2:
    
    >>> closestToTarget(arr = [1000000,1000000,1000000], target = 1)
    >>> 999999
    Explanation: Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.
    
    Example 3:
    
    >>> closestToTarget(arr = [1,2,4,8,16], target = 0)
    >>> 0
    """
def countOdds(low: int, high: int) -> int:
    """
    Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> countOdds(low = 3, high = 7\r)
    >>> 3\r
    Explanation: The odd numbers between 3 and 7 are [3,5,7].\r
    \r
    Example 2:\r
    \r
    \r
    >>> countOdds(low = 8, high = 10\r)
    >>> 1\r
    Explanation: The odd numbers between 8 and 10 are [9].\r
    \r
     \r
    """
def numOfSubarrays(arr: List[int]) -> int:
    """
    Given an array of integers arr, return the number of subarrays with an odd sum.
    Since the answer can be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numOfSubarrays(arr = [1,3,5])
    >>> 4
    Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
    All sub-arrays sum are [1,4,9,3,8,5].
    Odd sums are [1,9,3,5] so the answer is 4.
    
    Example 2:
    
    >>> numOfSubarrays(arr = [2,4,6])
    >>> 0
    Explanation: All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
    All sub-arrays sum are [2,6,12,4,10,6].
    All sub-arrays have even sum and the answer is 0.
    
    Example 3:
    
    >>> numOfSubarrays(arr = [1,2,3,4,5,6,7])
    >>> 16
    """
def numSplits(s: str) -> int:
    """
    You are given a string s.
    A split is called good if you can split s into two non-empty strings sleft and sright where their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters in sleft and sright is the same.
    Return the number of good splits you can make in s.
    
    Example 1:
    
    >>> numSplits(s = "aacaba")
    >>> 2
    Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
    ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
    ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
    ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
    ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
    ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
    
    Example 2:
    
    >>> numSplits(s = "abcd")
    >>> 1
    Explanation: Split the string as follows ("ab", "cd").
    """
def minNumberOperations(target: List[int]) -> int:
    """
    You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.
    In one operation you can choose any subarray from initial and increment each value by one.
    Return the minimum number of operations to form a target array from initial.
    The test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    >>> minNumberOperations(target = [1,2,3,2,1])
    >>> 3
    Explanation: We need at least 3 operations to form the target array from the initial array.
    [0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
    [1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
    [1,2,2,2,1] increment 1 at index 2.
    [1,2,3,2,1] target array is formed.
    
    Example 2:
    
    >>> minNumberOperations(target = [3,1,1,2])
    >>> 4
    Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
    
    Example 3:
    
    >>> minNumberOperations(target = [3,1,5,4,2])
    >>> 7
    Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
    """
def restoreString(s: str, indices: List[int]) -> str:
    """
    You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
    Return the shuffled string.
    
    Example 1:
    
    
    >>> restoreString(s = "codeleet", indices = [4,5,6,7,0,2,1,3])
    >>> "leetcode"
    Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
    
    Example 2:
    
    >>> restoreString(s = "abc", indices = [0,1,2])
    >>> "abc"
    Explanation: After shuffling, each character remains in its position.
    """
def minFlips(target: str) -> int:
    """
    You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target.
    In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing '0' to '1' and '1' to '0'.
    Return the minimum number of operations needed to make s equal to target.
    
    Example 1:
    
    >>> minFlips(target = "10111")
    >>> 3
    Explanation: Initially, s = "00000".
    Choose index i = 2: "00000" -> "00111"
    Choose index i = 0: "00111" -> "11000"
    Choose index i = 1: "11000" -> "10111"
    We need at least 3 flip operations to form target.
    
    Example 2:
    
    >>> minFlips(target = "101")
    >>> 3
    Explanation: Initially, s = "000".
    Choose index i = 0: "000" -> "111"
    Choose index i = 1: "111" -> "100"
    Choose index i = 2: "100" -> "101"
    We need at least 3 flip operations to form target.
    
    Example 3:
    
    >>> minFlips(target = "00000")
    >>> 0
    Explanation: We do not need any operations since the initial s already equals target.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def countPairs(root: Optional[TreeNode], distance: int) -> int:
    """
    You are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.
    Return the number of good leaf node pairs in the tree.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,null,4], distance = 3)
    >>> 1
    Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3,4,5,6,7], distance = 3)
    >>> 2
    Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.
    
    Example 3:
    
    >>> __init__(root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3)
    >>> 1
    Explanation: The only good pair is [2,5].
    """