ShwStone/humaneval-leetcode huggingface dataset:
def getCoprimes(nums: List[int], edges: List[List[int]]) -> List[int]:
    """
    There is 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. Each node has a value associated with it, and the root of the tree is node 0.
    To represent this tree, you are given an integer array nums and a 2D array edges. Each nums[i] represents the ith node's value, and each edges[j] = [uj, vj] represents an edge between nodes uj and vj in the tree.
    Two values x and y are coprime if gcd(x, y) == 1 where gcd(x, y) is the greatest common divisor of x and y.
    An ancestor of a node i is any other node on the shortest path from node i to the root. A node is not considered an ancestor of itself.
    Return an array ans of size n, where ans[i] is the closest ancestor to node i such that nums[i] and nums[ans[i]] are coprime, or -1 if there is no such ancestor.
    
    Example 1:
    
    
    >>> getCoprimes(nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]])
    >>> [-1,0,0,1]
    Explanation: In the above figure, each node's value is in parentheses.
    - Node 0 has no coprime ancestors.
    - Node 1 has only one ancestor, node 0. Their values are coprime (gcd(2,3) == 1).
    - Node 2 has two ancestors, nodes 1 and 0. Node 1's value is not coprime (gcd(3,3) == 3), but node 0's
      value is (gcd(2,3) == 1), so node 0 is the closest valid ancestor.
    - Node 3 has two ancestors, nodes 1 and 0. It is coprime with node 1 (gcd(3,2) == 1), so node 1 is its
      closest valid ancestor.
    
    Example 2:
    
    
    >>> getCoprimes(nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]])
    >>> [-1,0,-1,0,0,0,-1]
    """
def mergeAlternately(word1: str, word2: str) -> str:
    """
    You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.\r
    \r
    Return the merged string.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> mergeAlternately(word1 = "abc", word2 = "pqr"\r)
    >>> "apbqcr"\r
    Explanation: The merged string will be merged as so:\r
    word1:  a   b   c\r
    word2:    p   q   r\r
    merged: a p b q c r\r
    \r
    \r
    Example 2:\r
    \r
    \r
    >>> mergeAlternately(word1 = "ab", word2 = "pqrs"\r)
    >>> "apbqrs"\r
    Explanation: Notice that as word2 is longer, "rs" is appended to the end.\r
    word1:  a   b \r
    word2:    p   q   r   s\r
    merged: a p b q   r   s\r
    \r
    \r
    Example 3:\r
    \r
    \r
    >>> mergeAlternately(word1 = "abcd", word2 = "pq"\r)
    >>> "apbqcd"\r
    Explanation: Notice that as word1 is longer, "cd" is appended to the end.\r
    word1:  a   b   c   d\r
    word2:    p   q \r
    merged: a p b q c   d\r
    \r
    \r
     \r
    """
def minOperations(boxes: str) -> List[int]:
    """
    You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty, and '1' if it contains one ball.
    In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes.
    Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.
    Each answer[i] is calculated considering the initial state of the boxes.
    
    Example 1:
    
    >>> minOperations(boxes = "110")
    >>> [1,1,3]
    Explanation: The answer for each box is as follows:
    1) First box: you will have to move one ball from the second box to the first box in one operation.
    2) Second box: you will have to move one ball from the first box to the second box in one operation.
    3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
    
    Example 2:
    
    >>> minOperations(boxes = "001011")
    >>> [11,8,5,4,3,4]
    """
def maximumScore(nums: List[int], multipliers: List[int]) -> int:
    """
    You are given two 0-indexed integer arrays nums and multipliers of size n and m respectively, where n >= m.
    You begin with a score of 0. You want to perform exactly m operations. On the ith operation (0-indexed) you will:
    
    Choose one integer x from either the start or the end of the array nums.
    Add multipliers[i] * x to your score.
    
    Note that multipliers[0] corresponds to the first operation, multipliers[1] to the second operation, and so on.
    
    
    Remove x from nums.
    
    Return the maximum score after performing m operations.
    
    Example 1:
    
    >>> maximumScore(nums = [1,2,3], multipliers = [3,2,1])
    >>> 14
    Explanation: An optimal solution is as follows:
    - Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
    - Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
    - Choose from the end, [1], adding 1 * 1 = 1 to the score.
    The total score is 9 + 4 + 1 = 14.
    Example 2:
    
    >>> maximumScore(nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6])
    >>> 102
    Explanation: An optimal solution is as follows:
    - Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
    - Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
    - Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
    - Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
    - Choose from the end, [-2,7], adding 7 * 6 = 42 to the score.
    The total score is 50 + 15 - 9 + 4 + 42 = 102.
    """
def longestPalindrome(word1: str, word2: str) -> int:
    """
    You are given two strings, word1 and word2. You want to construct a string in the following manner:
    
    Choose some non-empty subsequence subsequence1 from word1.
    Choose some non-empty subsequence subsequence2 from word2.
    Concatenate the subsequences: subsequence1 + subsequence2, to make the string.
    
    Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0.
    A subsequence of a string s is a string that can be made by deleting some (possibly none) characters from s without changing the order of the remaining characters.
    A palindrome is a string that reads the same forward as well as backward.
    
    Example 1:
    
    >>> longestPalindrome(word1 = "cacb", word2 = "cbba")
    >>> 5
    Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.
    Example 2:
    
    >>> longestPalindrome(word1 = "ab", word2 = "ab")
    >>> 3
    Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.
    Example 3:
    
    >>> longestPalindrome(word1 = "aa", word2 = "bb")
    >>> 0
    Explanation: You cannot construct a palindrome from the described method, so return 0.
    """
def sortFeatures(features: List[str], responses: List[str]) -> List[str]:
    """
    You are given a string array features where features[i] is a single word that represents the name of a feature of the latest product you are working on. You have made a survey where users have reported which features they like. You are given a string array responses, where each responses[i] is a string containing space-separated words.
    The popularity of a feature is the number of responses[i] that contain the feature. You want to sort the features in non-increasing order by their popularity. If two features have the same popularity, order them by their original index in features. Notice that one response could contain the same feature multiple times; this feature is only counted once in its popularity.
    Return the features in sorted order.
    
    Example 1:
    
    >>> sortFeatures(features = ["cooler","lock","touch"], responses = ["i like cooler cooler","lock touch cool","locker like touch"])
    >>> ["touch","cooler","lock"]
    Explanation: appearances("cooler") = 1, appearances("lock") = 1, appearances("touch") = 2. Since "cooler" and "lock" both had 1 appearance, "cooler" comes first because "cooler" came first in the features array.
    
    Example 2:
    
    >>> sortFeatures(features = ["a","aa","b","c"], responses = ["a","a aa","a a a a a","b a"])
    >>> ["a","aa","b","c"]
    """
def countMatches(items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
    """
    You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.
    The ith item is said to match the rule if one of the following is true:
    
    ruleKey == "type" and ruleValue == typei.
    ruleKey == "color" and ruleValue == colori.
    ruleKey == "name" and ruleValue == namei.
    
    Return the number of items that match the given rule.
    
    Example 1:
    
    >>> countMatches(items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver")
    >>> 1
    Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].
    
    Example 2:
    
    >>> countMatches(items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone")
    >>> 2
    Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.
    """
def closestCost(baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
    """
    You would like to make dessert and are preparing to buy the ingredients. You have n ice cream base flavors and m types of toppings to choose from. You must follow these rules when making your dessert:
    
    There must be exactly one ice cream base.
    You can add one or more types of topping or have no toppings at all.
    There are at most two of each type of topping.
    
    You are given three inputs:
    
    baseCosts, an integer array of length n, where each baseCosts[i] represents the price of the ith ice cream base flavor.
    toppingCosts, an integer array of length m, where each toppingCosts[i] is the price of one of the ith topping.
    target, an integer representing your target price for dessert.
    
    You want to make a dessert with a total cost as close to target as possible.
    Return the closest possible cost of the dessert to target. If there are multiple, return the lower one.
    
    Example 1:
    
    >>> closestCost(baseCosts = [1,7], toppingCosts = [3,4], target = 10)
    >>> 10
    Explanation: Consider the following combination (all 0-indexed):
    - Choose base 1: cost 7
    - Take 1 of topping 0: cost 1 x 3 = 3
    - Take 0 of topping 1: cost 0 x 4 = 0
    Total: 7 + 3 + 0 = 10.
    
    Example 2:
    
    >>> closestCost(baseCosts = [2,3], toppingCosts = [4,5,100], target = 18)
    >>> 17
    Explanation: Consider the following combination (all 0-indexed):
    - Choose base 1: cost 3
    - Take 1 of topping 0: cost 1 x 4 = 4
    - Take 2 of topping 1: cost 2 x 5 = 10
    - Take 0 of topping 2: cost 0 x 100 = 0
    Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18.
    
    Example 3:
    
    >>> closestCost(baseCosts = [3,10], toppingCosts = [2,5], target = 9)
    >>> 8
    Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost.
    """
def minOperations(nums1: List[int], nums2: List[int]) -> int:
    """
    You are given two arrays of integers nums1 and nums2, possibly of different lengths. The values in the arrays are between 1 and 6, inclusive.
    In one operation, you can change any integer's value in any of the arrays to any value between 1 and 6, inclusive.
    Return the minimum number of operations required to make the sum of values in nums1 equal to the sum of values in nums2. Return -1​​​​​ if it is not possible to make the sum of the two arrays equal.
    
    Example 1:
    
    >>> minOperations(nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2])
    >>> 3
    Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
    - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2].
    - Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2].
    - Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2].
    
    Example 2:
    
    >>> minOperations(nums1 = [1,1,1,1,1,1,1], nums2 = [6])
    >>> -1
    Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
    
    Example 3:
    
    >>> minOperations(nums1 = [6,6], nums2 = [1])
    >>> 3
    Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
    - Change nums1[0] to 2. nums1 = [2,6], nums2 = [1].
    - Change nums1[1] to 2. nums1 = [2,2], nums2 = [1].
    - Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].
    """
def getCollisionTimes(cars: List[List[int]]) -> List[float]:
    """
    There are n cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars of length n, where cars[i] = [positioni, speedi] represents:
    
    positioni is the distance between the ith car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1.
    speedi is the initial speed of the ith car in meters per second.
    
    For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet.
    Return an array answer, where answer[i] is the time, in seconds, at which the ith car collides with the next car, or -1 if the car does not collide with the next car. Answers within 10-5 of the actual answers are accepted.
    
    Example 1:
    
    >>> getCollisionTimes(cars = [[1,2],[2,1],[4,3],[7,2]])
    >>> [1.00000,-1.00000,3.00000,-1.00000]
    Explanation: After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s.
    
    Example 2:
    
    >>> getCollisionTimes(cars = [[3,4],[5,4],[6,3],[9,1]])
    >>> [2.00000,1.00000,1.50000,-1.00000]
    """
def nearestValidPoint(x: int, y: int, points: List[List[int]]) -> int:
    """
    You are given two integers, x and y, which represent your current location on a Cartesian grid: (x, y). You are also given an array points where each points[i] = [ai, bi] represents that a point exists at (ai, bi). A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
    Return the index (0-indexed) of the valid point with the smallest Manhattan distance from your current location. If there are multiple, return the valid point with the smallest index. If there are no valid points, return -1.
    The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
    
    Example 1:
    
    >>> nearestValidPoint(x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]])
    >>> 2
    Explanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.
    Example 2:
    
    >>> nearestValidPoint(x = 3, y = 4, points = [[3,4]])
    >>> 0
    Explanation: The answer is allowed to be on the same location as your current location.
    Example 3:
    
    >>> nearestValidPoint(x = 3, y = 4, points = [[2,3]])
    >>> -1
    Explanation: There are no valid points.
    """
def checkPowersOfThree(n: int) -> bool:
    """
    Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
    An integer y is a power of three if there exists an integer x such that y == 3x.
    
    Example 1:
    
    >>> checkPowersOfThree(n = 12)
    >>> true
    Explanation: 12 = 31 + 32
    
    Example 2:
    
    >>> checkPowersOfThree(n = 91)
    >>> true
    Explanation: 91 = 30 + 32 + 34
    
    Example 3:
    
    >>> checkPowersOfThree(n = 21)
    >>> false
    """
def beautySum(s: str) -> int:
    """
    The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.
    
    For example, the beauty of "abaacc" is 3 - 1 = 2.
    
    Given a string s, return the sum of beauty of all of its substrings.
    
    Example 1:
    
    >>> beautySum(s = "aabcb")
    >>> 5
    Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
    Example 2:
    
    >>> beautySum(s = "aabcbaa")
    >>> 17
    """
def countPairs(n: int, edges: List[List[int]], queries: List[int]) -> List[int]:
    """
    You are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi. You are also given an integer array queries.
    Let incident(a, b) be defined as the number of edges that are connected to either node a or b.
    The answer to the jth query is the number of pairs of nodes (a, b) that satisfy both of the following conditions:
    
    a < b
    incident(a, b) > queries[j]
    
    Return an array answers such that answers.length == queries.length and answers[j] is the answer of the jth query.
    Note that there can be multiple edges between the same two nodes.
    
    Example 1:
    
    
    >>> countPairs(n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3])
    >>> [6,5]
    Explanation: The calculations for incident(a, b) are shown in the table above.
    The answers for each of the queries are as follows:
    - answers[0] = 6. All the pairs have an incident(a, b) value greater than 2.
    - answers[1] = 5. All the pairs except (3, 4) have an incident(a, b) value greater than 3.
    
    Example 2:
    
    >>> countPairs(n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5])
    >>> [10,10,9,8,6]
    """
def checkOnesSegment(s: str) -> bool:
    """
    Given a binary string s ​​​​​without leading zeros, return true​​​ if s contains at most one contiguous segment of ones. Otherwise, return false.
    
    Example 1:
    
    >>> checkOnesSegment(s = "1001")
    >>> false
    Explanation: The ones do not form a contiguous segment.
    
    Example 2:
    
    >>> checkOnesSegment(s = "110")
    >>> true
    """
def minElements(nums: List[int], limit: int, goal: int) -> int:
    """
    You are given an integer array nums and two integers limit and goal. The array nums has an interesting property that abs(nums[i]) <= limit.
    Return the minimum number of elements you need to add to make the sum of the array equal to goal. The array must maintain its property that abs(nums[i]) <= limit.
    Note that abs(x) equals x if x >= 0, and -x otherwise.
    
    Example 1:
    
    >>> minElements(nums = [1,-1,1], limit = 3, goal = -4)
    >>> 2
    Explanation: You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.
    
    Example 2:
    
    >>> minElements(nums = [1,-10,9,1], limit = 100, goal = 0)
    >>> 1
    """
def countRestrictedPaths(n: int, edges: List[List[int]]) -> int:
    """
    There is an undirected weighted connected graph. You are given a positive integer n which denotes that the graph has n nodes labeled from 1 to n, and an array edges where each edges[i] = [ui, vi, weighti] denotes that there is an edge between nodes ui and vi with weight equal to weighti.
    A path from node start to node end is a sequence of nodes [z0, z1, z2, ..., zk] such that z0 = start and zk = end and there is an edge between zi and zi+1 where 0 <= i <= k-1.
    The distance of a path is the sum of the weights on the edges of the path. Let distanceToLastNode(x) denote the shortest distance of a path between node n and node x. A restricted path is a path that also satisfies that distanceToLastNode(zi) > distanceToLastNode(zi+1) where 0 <= i <= k-1.
    Return the number of restricted paths from node 1 to node n. Since that number may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> countRestrictedPaths(n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]])
    >>> 3
    Explanation: Each circle contains the node number in black and its distanceToLastNode value in blue. The three restricted paths are:
    1) 1 --> 2 --> 5
    2) 1 --> 2 --> 3 --> 5
    3) 1 --> 3 --> 5
    
    Example 2:
    
    
    >>> countRestrictedPaths(n = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]])
    >>> 1
    Explanation: Each circle contains the node number in black and its distanceToLastNode value in blue. The only restricted path is 1 --> 3 --> 7.
    """
def minChanges(nums: List[int], k: int) -> int:
    """
    You are given an array nums​​​ and an integer k​​​​​. The XOR of a segment [left, right] where left <= right is the XOR of all the elements with indices between left and right, inclusive: nums[left] XOR nums[left+1] XOR ... XOR nums[right].
    Return the minimum number of elements to change in the array such that the XOR of all segments of size k​​​​​​ is equal to zero.
    
    Example 1:
    
    >>> minChanges(nums = [1,2,0,3,0], k = 1)
    >>> 3
    Explanation: Modify the array from [1,2,0,3,0] to from [0,0,0,0,0].
    
    Example 2:
    
    >>> minChanges(nums = [3,4,5,2,1,7,3,4,7], k = 3)
    >>> 3
    Explanation: Modify the array from [3,4,5,2,1,7,3,4,7] to [3,4,7,3,4,7,3,4,7].
    
    Example 3:
    
    >>> minChanges(nums = [1,2,4,1,2,5,1,2,6], k = 3)
    >>> 3
    Explanation: Modify the array from [1,2,4,1,2,5,1,2,6] to [1,2,3,1,2,3,1,2,3].
    """
def maximumBeauty(flowers: List[int]) -> int:
    """
    There is a garden of n flowers, and each flower has an integer beauty value. The flowers are arranged in a line. You are given an integer array flowers of size n and each flowers[i] represents the beauty of the ith flower.\r
    \r
    A garden is valid if it meets these conditions:\r
    \r
    \r
    	The garden has at least two flowers.\r
    	The first and the last flower of the garden have the same beauty value.\r
    \r
    \r
    As the appointed gardener, you have the ability to remove any (possibly none) flowers from the garden. You want to remove flowers in a way that makes the remaining garden valid. The beauty of the garden is the sum of the beauty of all the remaining flowers.\r
    \r
    Return the maximum possible beauty of some valid garden after you have removed any (possibly none) flowers.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> maximumBeauty(flowers = [1,2,3,1,2]\r)
    >>> 8\r
    Explanation: You can produce the valid garden [2,3,1,2] to have a total beauty of 2 + 3 + 1 + 2 = 8.\r
    \r
    Example 2:\r
    \r
    \r
    >>> maximumBeauty(flowers = [100,1,1,-3,1]\r)
    >>> 3\r
    Explanation: You can produce the valid garden [1,1,1] to have a total beauty of 1 + 1 + 1 = 3.\r
    \r
    \r
    Example 3:\r
    \r
    \r
    >>> maximumBeauty(flowers = [-1,-2,0,-1]\r)
    >>> -2\r
    Explanation: You can produce the valid garden [-1,-1] to have a total beauty of -1 + -1 = -2.\r
    \r
    \r
     \r
    """