ShwStone/humaneval-leetcode huggingface dataset:
def finalPrices(prices: List[int]) -> List[int]:
    """
    You are given an integer array prices where prices[i] is the price of the ith item in a shop.
    There is a special discount for items in the shop. If you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will not receive any discount at all.
    Return an integer array answer where answer[i] is the final price you will pay for the ith item of the shop, considering the special discount.
    
    Example 1:
    
    >>> finalPrices(prices = [8,4,6,2,3])
    >>> [4,2,4,2,3]
    Explanation:
    For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
    For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
    For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
    For items 3 and 4 you will not receive any discount at all.
    
    Example 2:
    
    >>> finalPrices(prices = [1,2,3,4,5])
    >>> [1,2,3,4,5]
    Explanation: In this case, for all items, you will not receive any discount at all.
    
    Example 3:
    
    >>> finalPrices(prices = [10,1,1,6])
    >>> [9,0,1,6]
    """
def minSumOfLengths(arr: List[int], target: int) -> int:
    """
    You are given an array of integers arr and an integer target.
    You have to find two non-overlapping sub-arrays of arr each with a sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
    Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
    
    Example 1:
    
    >>> minSumOfLengths(arr = [3,2,2,4,3], target = 3)
    >>> 2
    Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
    
    Example 2:
    
    >>> minSumOfLengths(arr = [7,3,4,7], target = 7)
    >>> 2
    Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
    
    Example 3:
    
    >>> minSumOfLengths(arr = [4,3,2,6,2,3,4], target = 6)
    >>> -1
    Explanation: We have only one sub-array of sum = 6.
    """
def minDistance(houses: List[int], k: int) -> int:
    """
    Given the array houses where houses[i] is the location of the ith house along a street and an integer k, allocate k mailboxes in the street.
    Return the minimum total distance between each house and its nearest mailbox.
    The test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    
    >>> minDistance(houses = [1,4,8,10,20], k = 3)
    >>> 5
    Explanation: Allocate mailboxes in position 3, 9 and 20.
    Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5
    
    Example 2:
    
    
    >>> minDistance(houses = [2,3,5,12,18], k = 2)
    >>> 9
    Explanation: Allocate mailboxes in position 3 and 14.
    Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9.
    """
def runningSum(nums: List[int]) -> List[int]:
    """
    Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
    Return the running sum of nums.
    
    Example 1:
    
    >>> runningSum(nums = [1,2,3,4])
    >>> [1,3,6,10]
    Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
    Example 2:
    
    >>> runningSum(nums = [1,1,1,1,1])
    >>> [1,2,3,4,5]
    Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
    Example 3:
    
    >>> runningSum(nums = [3,1,2,10,1])
    >>> [3,4,6,16,17]
    """
def findLeastNumOfUniqueInts(arr: List[int], k: int) -> int:
    """
    Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.\r
    \r
    \r
    \r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> findLeastNumOfUniqueInts(arr = [5,5,4], k = 1\r)
    >>> 1\r
    Explanation: Remove the single 4, only 5 is left.\r
    \r
    Example 2:\r
    \r
    \r
    >>> findLeastNumOfUniqueInts(arr = [4,3,1,1,3,3,2], k = 3\r)
    >>> 2\r
    Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.\r
    \r
     \r
    """
def minDays(bloomDay: List[int], m: int, k: int) -> int:
    """
    You are given an integer array bloomDay, an integer m and an integer k.
    You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
    The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
    Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
    
    Example 1:
    
    >>> minDays(bloomDay = [1,10,3,10,2], m = 3, k = 1)
    >>> 3
    Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.
    We need 3 bouquets each should contain 1 flower.
    After day 1: [x, _, _, _, _]   // we can only make one bouquet.
    After day 2: [x, _, _, _, x]   // we can only make two bouquets.
    After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.
    
    Example 2:
    
    >>> minDays(bloomDay = [1,10,3,10,2], m = 3, k = 2)
    >>> -1
    Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
    
    Example 3:
    
    >>> minDays(bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3)
    >>> 12
    Explanation: We need 2 bouquets each should have 3 flowers.
    Here is the garden after the 7 and 12 days:
    After day 7: [x, x, x, x, _, x, x]
    We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
    After day 12: [x, x, x, x, x, x, x]
    It is obvious that we can make two bouquets in different ways.
    """
# class Node:
#     def __init__(val=0, left=None, right=None, random=None):
#         self.val = val
#         self.left = left
#         self.right = right
#         self.random = random

class Solution:
    def copyRandomBinaryTree(root: 'Optional[Node]') -> 'Optional[NodeCopy]':
    """
    A binary tree is given such that each node contains an additional random pointer which could point to any node in the tree or null.
    Return a deep copy of the tree.
    The tree is represented in the same input/output way as normal binary trees where each node is represented as a pair of [val, random_index] where:
    
    val: an integer representing Node.val
    random_index: the index of the node (in the input) where the random pointer points to, or null if it does not point to any node.
    
    You will be given the tree in class Node and you should return the cloned tree in class NodeCopy. NodeCopy class is just a clone of Node class with the same attributes and constructors.
    
    Example 1:
    
    
    >>> __init__(root = [[1,null],null,[4,3],[7,0]])
    >>> [[1,null],null,[4,3],[7,0]]
    Explanation: The original binary tree is [1,null,4,7].
    The random pointer of node one is null, so it is represented as [1, null].
    The random pointer of node 4 is node 7, so it is represented as [4, 3] where 3 is the index of node 7 in the array representing the tree.
    The random pointer of node 7 is node 1, so it is represented as [7, 0] where 0 is the index of node 1 in the array representing the tree.
    
    Example 2:
    
    
    >>> __init__(root = [[1,4],null,[1,0],null,[1,5],[1,5]])
    >>> [[1,4],null,[1,0],null,[1,5],[1,5]]
    Explanation: The random pointer of a node can be the node itself.
    
    Example 3:
    
    
    >>> __init__(root = [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]])
    >>> [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
    """
def xorOperation(n: int, start: int) -> int:
    """
    You are given an integer n and an integer start.
    Define an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length.
    Return the bitwise XOR of all elements of nums.
    
    Example 1:
    
    >>> xorOperation(n = 5, start = 0)
    >>> 8
    Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
    Where "^" corresponds to bitwise XOR operator.
    
    Example 2:
    
    >>> xorOperation(n = 4, start = 3)
    >>> 8
    Explanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
    """
def getFolderNames(names: List[str]) -> List[str]:
    """
    Given an array of strings names of size n. You will create n folders in your file system such that, at the ith minute, you will create a folder with the name names[i].
    Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.
    Return an array of strings of length n where ans[i] is the actual name the system will assign to the ith folder when you create it.
    
    Example 1:
    
    >>> getFolderNames(names = ["pes","fifa","gta","pes(2019)"])
    >>> ["pes","fifa","gta","pes(2019)"]
    Explanation: Let's see how the file system creates folder names:
    "pes" --> not assigned before, remains "pes"
    "fifa" --> not assigned before, remains "fifa"
    "gta" --> not assigned before, remains "gta"
    "pes(2019)" --> not assigned before, remains "pes(2019)"
    
    Example 2:
    
    >>> getFolderNames(names = ["gta","gta(1)","gta","avalon"])
    >>> ["gta","gta(1)","gta(2)","avalon"]
    Explanation: Let's see how the file system creates folder names:
    "gta" --> not assigned before, remains "gta"
    "gta(1)" --> not assigned before, remains "gta(1)"
    "gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
    "avalon" --> not assigned before, remains "avalon"
    
    Example 3:
    
    >>> getFolderNames(names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"])
    >>> ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
    Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".
    """
def avoidFlood(rains: List[int]) -> List[int]:
    """
    Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake that is full of water, there will be a flood. Your goal is to avoid floods in any lake.
    Given an integer array rains where:
    
    rains[i] > 0 means there will be rains over the rains[i] lake.
    rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.
    
    Return an array ans where:
    
    ans.length == rains.length
    ans[i] == -1 if rains[i] > 0.
    ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.
    
    If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
    Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.
    
    Example 1:
    
    >>> avoidFlood(rains = [1,2,3,4])
    >>> [-1,-1,-1,-1]
    Explanation: After the first day full lakes are [1]
    After the second day full lakes are [1,2]
    After the third day full lakes are [1,2,3]
    After the fourth day full lakes are [1,2,3,4]
    There's no day to dry any lake and there is no flood in any lake.
    
    Example 2:
    
    >>> avoidFlood(rains = [1,2,0,0,2,1])
    >>> [-1,-1,2,1,-1,-1]
    Explanation: After the first day full lakes are [1]
    After the second day full lakes are [1,2]
    After the third day, we dry lake 2. Full lakes are [1]
    After the fourth day, we dry lake 1. There is no full lakes.
    After the fifth day, full lakes are [2].
    After the sixth day, full lakes are [1,2].
    It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
    
    Example 3:
    
    >>> avoidFlood(rains = [1,2,0,1,2])
    >>> []
    Explanation: After the second day, full lakes are  [1,2]. We have to dry one lake in the third day.
    After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
    """
def findCriticalAndPseudoCriticalEdges(n: int, edges: List[List[int]]) -> List[List[int]]:
    """
    Given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted edge between nodes ai and bi. A minimum spanning tree (MST) is a subset of the graph's edges that connects all vertices without cycles and with the minimum possible total edge weight.
    Find all the critical and pseudo-critical edges in the given graph's minimum spanning tree (MST). An MST edge whose deletion from the graph would cause the MST weight to increase is called a critical edge. On the other hand, a pseudo-critical edge is that which can appear in some MSTs but not all.
    Note that you can return the indices of the edges in any order.
    
    Example 1:
    
    
    >>> findCriticalAndPseudoCriticalEdges(n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]])
    >>> [[0,1],[2,3,4,5]]
    Explanation: The figure above describes the graph.
    The following figure shows all the possible MSTs:
    
    Notice that the two edges 0 and 1 appear in all MSTs, therefore they are critical edges, so we return them in the first list of the output.
    The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges. We add them to the second list of the output.
    
    Example 2:
    
    
    >>> findCriticalAndPseudoCriticalEdges(n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]])
    >>> [[],[0,1,2,3]]
    Explanation: We can observe that since all 4 edges have equal weight, choosing any 3 edges from the given 4 will yield an MST. Therefore all 4 edges are pseudo-critical.
    """
def average(salary: List[int]) -> float:
    """
    You are given an array of unique integers salary where salary[i] is the salary of the ith employee.
    Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.
    
    Example 1:
    
    >>> average(salary = [4000,3000,1000,2000])
    >>> 2500.00000
    Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
    Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500
    
    Example 2:
    
    >>> average(salary = [1000,2000,3000])
    >>> 2000.00000
    Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
    Average salary excluding minimum and maximum salary is (2000) / 1 = 2000
    """
def kthFactor(n: int, k: int) -> int:
    """
    You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.
    Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.
    
    Example 1:
    
    >>> kthFactor(n = 12, k = 3)
    >>> 3
    Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
    
    Example 2:
    
    >>> kthFactor(n = 7, k = 2)
    >>> 7
    Explanation: Factors list is [1, 7], the 2nd factor is 7.
    
    Example 3:
    
    >>> kthFactor(n = 4, k = 4)
    >>> -1
    Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
    """
def longestSubarray(nums: List[int]) -> int:
    """
    Given a binary array nums, you should delete one element from it.
    Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.
    
    Example 1:
    
    >>> longestSubarray(nums = [1,1,0,1])
    >>> 3
    Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
    
    Example 2:
    
    >>> longestSubarray(nums = [0,1,1,1,0,1,1,0,1])
    >>> 5
    Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
    
    Example 3:
    
    >>> longestSubarray(nums = [1,1,1])
    >>> 2
    Explanation: You must delete one element.
    """
def minNumberOfSemesters(n: int, relations: List[List[int]], k: int) -> int:
    """
    You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei. Also, you are given the integer k.
    In one semester, you can take at most k courses as long as you have taken all the prerequisites in the previous semesters for the courses you are taking.
    Return the minimum number of semesters needed to take all courses. The testcases will be generated such that it is possible to take every course.
    
    Example 1:
    
    
    >>> minNumberOfSemesters(n = 4, relations = [[2,1],[3,1],[1,4]], k = 2)
    >>> 3
    Explanation: The figure above represents the given graph.
    In the first semester, you can take courses 2 and 3.
    In the second semester, you can take course 1.
    In the third semester, you can take course 4.
    
    Example 2:
    
    
    >>> minNumberOfSemesters(n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2)
    >>> 4
    Explanation: The figure above represents the given graph.
    In the first semester, you can only take courses 2 and 3 since you cannot take more than two per semester.
    In the second semester, you can take course 4.
    In the third semester, you can take course 1.
    In the fourth semester, you can take course 5.
    """
def isPathCrossing(path: str) -> bool:
    """
    Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.
    Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.
    
    Example 1:
    
    
    >>> isPathCrossing(path = "NES")
    >>> false
    Explanation: Notice that the path doesn't cross any point more than once.
    
    Example 2:
    
    
    >>> isPathCrossing(path = "NESWW")
    >>> true
    Explanation: Notice that the path visits the origin twice.
    """
def canArrange(arr: List[int], k: int) -> bool:
    """
    Given an array of integers arr of even length n and an integer k.
    We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.
    Return true If you can find a way to do that or false otherwise.
    
    Example 1:
    
    >>> canArrange(arr = [1,2,3,4,5,10,6,7,8,9], k = 5)
    >>> true
    Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
    
    Example 2:
    
    >>> canArrange(arr = [1,2,3,4,5,6], k = 7)
    >>> true
    Explanation: Pairs are (1,6),(2,5) and(3,4).
    
    Example 3:
    
    >>> canArrange(arr = [1,2,3,4,5,6], k = 10)
    >>> false
    Explanation: You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10.
    """
def numSubseq(nums: List[int], target: int) -> int:
    """
    You are given an array of integers nums and an integer target.
    Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numSubseq(nums = [3,5,6,7], target = 9)
    >>> 4
    Explanation: There are 4 subsequences that satisfy the condition.
    [3] -> Min value + max value <= target (3 + 3 <= 9)
    [3,5] -> (3 + 5 <= 9)
    [3,5,6] -> (3 + 6 <= 9)
    [3,6] -> (3 + 6 <= 9)
    
    Example 2:
    
    >>> numSubseq(nums = [3,3,6,8], target = 10)
    >>> 6
    Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
    [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
    
    Example 3:
    
    >>> numSubseq(nums = [2,3,3,4,6,7], target = 12)
    >>> 61
    Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
    Number of valid subsequences (63 - 2 = 61).
    """
def findMaxValueOfEquation(points: List[List[int]], k: int) -> int:
    """
    You are given an array points containing the coordinates of points on a 2D plane, sorted by the x-values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also given an integer k.
    Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k and 1 <= i < j <= points.length.
    It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi - xj| <= k.
    
    Example 1:
    
    >>> findMaxValueOfEquation(points = [[1,3],[2,0],[5,10],[6,-10]], k = 1)
    >>> 4
    Explanation: The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 + -10 + |5 - 6| = 1.
    No other pairs satisfy the condition, so we return the max of 4 and 1.
    
    Example 2:
    
    >>> findMaxValueOfEquation(points = [[0,0],[3,0],[9,2]], k = 3)
    >>> 3
    Explanation: Only the first two points have an absolute difference of 3 or less in the x-values, and give the value of 0 + 0 + |0 - 3| = 3.
    """
def canMakeArithmeticProgression(arr: List[int]) -> bool:
    """
    A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
    Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.
    
    Example 1:
    
    >>> canMakeArithmeticProgression(arr = [3,5,1])
    >>> true
    Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
    
    Example 2:
    
    >>> canMakeArithmeticProgression(arr = [1,2,4])
    >>> false
    Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
    """