ShwStone/humaneval-leetcode huggingface dataset:
def maximumProduct(nums: List[int]) -> int:
    """
    Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
    
    Example 1:
    >>> maximumProduct(nums = [1,2,3])
    >>> 6
    Example 2:
    >>> maximumProduct(nums = [1,2,3,4])
    >>> 24
    Example 3:
    >>> maximumProduct(nums = [-1,-2,-3])
    >>> -6
    """
def kInversePairs(n: int, k: int) -> int:
    """
    For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].
    Given two integers n and k, return the number of different arrays consisting of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.
    
    Example 1:
    
    >>> kInversePairs(n = 3, k = 0)
    >>> 1
    Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
    
    Example 2:
    
    >>> kInversePairs(n = 3, k = 1)
    >>> 2
    Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
    """
def scheduleCourse(courses: List[List[int]]) -> int:
    """
    There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi.
    You will start on the 1st day and you cannot take two or more courses simultaneously.
    Return the maximum number of courses that you can take.
    
    Example 1:
    
    >>> scheduleCourse(courses = [[100,200],[200,1300],[1000,1250],[2000,3200]])
    >>> 3
    Explanation:
    There are totally 4 courses, but you can take 3 courses at most:
    First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.
    Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day.
    Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
    The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
    
    Example 2:
    
    >>> scheduleCourse(courses = [[1,2]])
    >>> 1
    
    Example 3:
    
    >>> scheduleCourse(courses = [[3,2],[4,3]])
    >>> 0
    """
def smallestRange(nums: List[List[int]]) -> List[int]:
    """
    You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.
    We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.
    
    Example 1:
    
    >>> smallestRange(nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]])
    >>> [20,24]
    Explanation:
    List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
    List 2: [0, 9, 12, 20], 20 is in range [20,24].
    List 3: [5, 18, 22, 30], 22 is in range [20,24].
    
    Example 2:
    
    >>> smallestRange(nums = [[1,2,3],[1,2,3],[1,2,3]])
    >>> [1,1]
    """
def judgeSquareSum(c: int) -> bool:
    """
    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
    
    Example 1:
    
    >>> judgeSquareSum(c = 5)
    >>> true
    Explanation: 1 * 1 + 2 * 2 = 5
    
    Example 2:
    
    >>> judgeSquareSum(c = 3)
    >>> false
    """
def findDerangement(n: int) -> int:
    """
    In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position.
    You are given an integer n. There is originally an array consisting of n integers from 1 to n in ascending order, return the number of derangements it can generate. Since the answer may be huge, return it modulo 109 + 7.
    
    Example 1:
    
    >>> findDerangement(n = 3)
    >>> 2
    Explanation: The original array is [1,2,3]. The two derangements are [2,3,1] and [3,1,2].
    
    Example 2:
    
    >>> findDerangement(n = 2)
    >>> 1
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def averageOfLevels(root: Optional[TreeNode]) -> List[float]:
    """
    Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [3.00000,14.50000,11.00000]
    Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
    Hence return [3, 14.5, 11].
    
    Example 2:
    
    
    >>> __init__(root = [3,9,20,15,7])
    >>> [3.00000,14.50000,11.00000]
    """
def shoppingOffers(price: List[int], special: List[List[int]], needs: List[int]) -> int:
    """
    In LeetCode Store, there are n items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
    You are given an integer array price where price[i] is the price of the ith item, and an integer array needs where needs[i] is the number of pieces of the ith item you want to buy.
    You are also given an array special where special[i] is of size n + 1 where special[i][j] is the number of pieces of the jth item in the ith offer and special[i][n] (i.e., the last integer in the array) is the price of the ith offer.
    Return the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.
    
    Example 1:
    
    >>> shoppingOffers(price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2])
    >>> 14
    Explanation: There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
    In special offer 1, you can pay $5 for 3A and 0B
    In special offer 2, you can pay $10 for 1A and 2B.
    You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
    
    Example 2:
    
    >>> shoppingOffers(price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1])
    >>> 11
    Explanation: The price of A is $2, and $3 for B, $4 for C.
    You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
    You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C.
    You cannot add more items, though only $9 for 2A ,2B and 1C.
    """
def numDecodings(s: str) -> int:
    """
    A message containing letters from A-Z can be encoded into numbers using the following mapping:
    
    'A' -> "1"
    'B' -> "2"
    ...
    'Z' -> "26"
    
    To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:
    
    "AAJF" with the grouping (1 1 10 6)
    "KJF" with the grouping (11 10 6)
    
    Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".
    In addition to the mapping above, an encoded message may contain the '*' character, which can represent any digit from '1' to '9' ('0' is excluded). For example, the encoded message "1*" may represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19". Decoding "1*" is equivalent to decoding any of the encoded messages it can represent.
    Given a string s consisting of digits and '*' characters, return the number of ways to decode it.
    Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numDecodings(s = "*")
    >>> 9
    Explanation: The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9".
    Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively.
    Hence, there are a total of 9 ways to decode "*".
    
    Example 2:
    
    >>> numDecodings(s = "1*")
    >>> 18
    Explanation: The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19".
    Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K").
    Hence, there are a total of 9 * 2 = 18 ways to decode "1*".
    
    Example 3:
    
    >>> numDecodings(s = "2*")
    >>> 15
    Explanation: The encoded message can represent any of the encoded messages "21", "22", "23", "24", "25", "26", "27", "28", or "29".
    "21", "22", "23", "24", "25", and "26" have 2 ways of being decoded, but "27", "28", and "29" only have 1 way.
    Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode "2*".
    """
def solveEquation(equation: str) -> str:
    """
    Solve a given equation and return the value of 'x' in the form of a string "x=#value". The equation contains only '+', '-' operation, the variable 'x' and its coefficient. You should return "No solution" if there is no solution for the equation, or "Infinite solutions" if there are infinite solutions for the equation.
    If there is exactly one solution for the equation, we ensure that the value of 'x' is an integer.
    
    Example 1:
    
    >>> solveEquation(equation = "x+5-3+x=6+x-2")
    >>> "x=2"
    
    Example 2:
    
    >>> solveEquation(equation = "x=x")
    >>> "Infinite solutions"
    
    Example 3:
    
    >>> solveEquation(equation = "2x=x")
    >>> "x=0"
    """
def findMaxAverage(nums: List[int], k: int) -> float:
    """
    You are given an integer array nums consisting of n elements, and an integer k.
    Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
    
    Example 1:
    
    >>> findMaxAverage(nums = [1,12,-5,-6,50,3], k = 4)
    >>> 12.75000
    Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
    
    Example 2:
    
    >>> findMaxAverage(nums = [5], k = 1)
    >>> 5.00000
    """
def findMaxAverage(nums: List[int], k: int) -> float:
    """
    You are given an integer array nums consisting of n elements, and an integer k.
    Find a contiguous subarray whose length is greater than or equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
    
    Example 1:
    
    >>> findMaxAverage(nums = [1,12,-5,-6,50,3], k = 4)
    >>> 12.75000
    Explanation:
    - When the length is 4, averages are [0.5, 12.75, 10.5] and the maximum average is 12.75
    - When the length is 5, averages are [10.4, 10.8] and the maximum average is 10.8
    - When the length is 6, averages are [9.16667] and the maximum average is 9.16667
    The maximum average is when we choose a subarray of length 4 (i.e., the sub array [12, -5, -6, 50]) which has the max average 12.75, so we return 12.75
    Note that we do not consider the subarrays of length < 4.
    
    Example 2:
    
    >>> findMaxAverage(nums = [5], k = 1)
    >>> 5.00000
    """
def findErrorNums(nums: List[int]) -> List[int]:
    """
    You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
    You are given an integer array nums representing the data status of this set after the error.
    Find the number that occurs twice and the number that is missing and return them in the form of an array.
    
    Example 1:
    >>> findErrorNums(nums = [1,2,2,4])
    >>> [2,3]
    Example 2:
    >>> findErrorNums(nums = [1,1])
    >>> [1,2]
    """
def findLongestChain(pairs: List[List[int]]) -> int:
    """
    You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
    A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
    Return the length longest chain which can be formed.
    You do not need to use up all the given intervals. You can select pairs in any order.
    
    Example 1:
    
    >>> findLongestChain(pairs = [[1,2],[2,3],[3,4]])
    >>> 2
    Explanation: The longest chain is [1,2] -> [3,4].
    
    Example 2:
    
    >>> findLongestChain(pairs = [[1,2],[7,8],[4,5]])
    >>> 3
    Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
    """
def countSubstrings(s: str) -> int:
    """
    Given a string s, return the number of palindromic substrings in it.
    A string is a palindrome when it reads the same backward as forward.
    A substring is a contiguous sequence of characters within the string.
    
    Example 1:
    
    >>> countSubstrings(s = "abc")
    >>> 3
    Explanation: Three palindromic strings: "a", "b", "c".
    
    Example 2:
    
    >>> countSubstrings(s = "aaa")
    >>> 6
    Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
    """
def replaceWords(dictionary: List[str], sentence: str) -> str:
    """
    In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".
    Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.
    Return the sentence after the replacement.
    
    Example 1:
    
    >>> replaceWords(dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery")
    >>> "the cat was rat by the bat"
    
    Example 2:
    
    >>> replaceWords(dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs")
    >>> "a a b c"
    """
def predictPartyVictory(senate: str) -> str:
    """
    In the world of Dota2, there are two parties: the Radiant and the Dire.
    The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
    
    Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
    Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
    
    Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.
    The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
    Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".
    
    Example 1:
    
    >>> predictPartyVictory(senate = "RD")
    >>> "Radiant"
    Explanation:
    The first senator comes from Radiant and he can just ban the next senator's right in round 1.
    And the second senator can't exercise any rights anymore since his right has been banned.
    And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
    
    Example 2:
    
    >>> predictPartyVictory(senate = "RDD")
    >>> "Dire"
    Explanation:
    The first senator comes from Radiant and he can just ban the next senator's right in round 1.
    And the second senator can't exercise any rights anymore since his right has been banned.
    And the third senator comes from Dire and he can ban the first senator's right in round 1.
    And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
    """
def minSteps(n: int) -> int:
    """
    There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:
    
    Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
    Paste: You can paste the characters which are copied last time.
    
    Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen.
    
    Example 1:
    
    >>> minSteps(n = 3)
    >>> 3
    Explanation: Initially, we have one character 'A'.
    In step 1, we use Copy All operation.
    In step 2, we use Paste operation to get 'AA'.
    In step 3, we use Paste operation to get 'AAA'.
    
    Example 2:
    
    >>> minSteps(n = 1)
    >>> 0
    """
def maxA(n: int) -> int:
    """
    Imagine you have a special keyboard with the following keys:
    
    A: Print one 'A' on the screen.
    Ctrl-A: Select the whole screen.
    Ctrl-C: Copy selection to buffer.
    Ctrl-V: Print buffer on screen appending it after what has already been printed.
    
    Given an integer n, return the maximum number of 'A' you can print on the screen with at most n presses on the keys.
    
    Example 1:
    
    >>> maxA(n = 3)
    >>> 3
    Explanation: We can at most get 3 A's on screen by pressing the following key sequence:
    A, A, A
    
    Example 2:
    
    >>> maxA(n = 7)
    >>> 9
    Explanation: We can at most get 9 A's on screen by pressing following key sequence:
    A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findTarget(root: Optional[TreeNode], k: int) -> bool:
    """
    Given the root of a binary search tree and an integer k, return true if there exist two elements in the BST such that their sum is equal to k, or false otherwise.
    
    Example 1:
    
    
    >>> __init__(root = [5,3,6,2,4,null,7], k = 9)
    >>> true
    
    Example 2:
    
    
    >>> __init__(root = [5,3,6,2,4,null,7], k = 28)
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def constructMaximumBinaryTree(nums: List[int]) -> Optional[TreeNode]:
    """
    You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:
    
    Create a root node whose value is the maximum value in nums.
    Recursively build the left subtree on the subarray prefix to the left of the maximum value.
    Recursively build the right subtree on the subarray suffix to the right of the maximum value.
    
    Return the maximum binary tree built from nums.
    
    Example 1:
    
    
    >>> __init__(nums = [3,2,1,6,0,5])
    >>> [6,3,5,null,2,0,null,null,1]
    Explanation: The recursive calls are as follow:
    - The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
        - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
            - Empty array, so no child.
            - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
                - Empty array, so no child.
                - Only one element, so child is a node with value 1.
        - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
            - Only one element, so child is a node with value 0.
            - Empty array, so no child.
    
    Example 2:
    
    
    >>> __init__(nums = [3,2,1])
    >>> [3,null,2,null,1]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def printTree(root: Optional[TreeNode]) -> List[List[str]]:
    """
    Given the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree. The formatted layout matrix should be constructed using the following rules:
    
    The height of the tree is height and the number of rows m should be equal to height + 1.
    The number of columns n should be equal to 2height+1 - 1.
    Place the root node in the middle of the top row (more formally, at location res[0][(n-1)/2]).
    For each node that has been placed in the matrix at position res[r][c], place its left child at res[r+1][c-2height-r-1] and its right child at res[r+1][c+2height-r-1].
    Continue this process until all the nodes in the tree have been placed.
    Any empty cells should contain the empty string "".
    
    Return the constructed matrix res.
    
    Example 1:
    
    
    >>> __init__(root = [1,2])
    >>>
    [["","1",""],
     ["2","",""]]
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3,null,4])
    >>>
    [["","","","1","","",""],
     ["","2","","","","3",""],
     ["","","4","","","",""]]
    """
def cheapestJump(coins: List[int], maxJump: int) -> List[int]:
    """
    You are given an integer array coins (1-indexed) of length n and an integer maxJump. You can jump to any index i of the array coins if coins[i] != -1 and you have to pay coins[i] when you visit index i. In addition to that, if you are currently at index i, you can only jump to any index i + k where i + k <= n and k is a value in the range [1, maxJump].
    You are initially positioned at index 1 (coins[1] is not -1). You want to find the path that reaches index n with the minimum cost.
    Return an integer array of the indices that you will visit in order so that you can reach index n with the minimum cost. If there are multiple paths with the same cost, return the lexicographically smallest such path. If it is not possible to reach index n, return an empty array.
    A path p1 = [Pa1, Pa2, ..., Pax] of length x is lexicographically smaller than p2 = [Pb1, Pb2, ..., Pbx] of length y, if and only if at the first j where Paj and Pbj differ, Paj < Pbj; when no such j exists, then x < y.
    
    Example 1:
    >>> cheapestJump(coins = [1,2,4,-1,2], maxJump = 2)
    >>> [1,3,5]
    Example 2:
    >>> cheapestJump(coins = [1,2,4,-1,2], maxJump = 1)
    >>> []
    """
def judgeCircle(moves: str) -> bool:
    """
    There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
    You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).
    Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.
    Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
    
    Example 1:
    
    >>> judgeCircle(moves = "UD")
    >>> true
    Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
    
    Example 2:
    
    >>> judgeCircle(moves = "LL")
    >>> false
    Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
    """