ShwStone/humaneval-leetcode huggingface dataset:
def oddString(words: List[str]) -> str:
    """
    You are given an array of equal-length strings words. Assume that the length of each string is n.
    Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.
    
    For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].
    
    All the strings in words have the same difference integer array, except one. You should find that string.
    Return the string in words that has different difference integer array.
    
    Example 1:
    
    >>> oddString(words = ["adc","wzy","abc"])
    >>> "abc"
    Explanation:
    - The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
    - The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
    - The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
    The odd array out is [1, 1], so we return the corresponding string, "abc".
    
    Example 2:
    
    >>> oddString(words = ["aaa","bob","ccc","ddd"])
    >>> "bob"
    Explanation: All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
    """
def twoEditWords(queries: List[str], dictionary: List[str]) -> List[str]:
    """
    You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length.
    In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary.
    Return a list of all words from queries, that match with some word from dictionary after a maximum of two edits. Return the words in the same order they appear in queries.
    
    Example 1:
    
    >>> twoEditWords(queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"])
    >>> ["word","note","wood"]
    Explanation:
    - Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
    - Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
    - It would take more than 2 edits for "ants" to equal a dictionary word.
    - "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
    Thus, we return ["word","note","wood"].
    
    Example 2:
    
    >>> twoEditWords(queries = ["yes"], dictionary = ["not"])
    >>> []
    Explanation:
    Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
    """
def destroyTargets(nums: List[int], space: int) -> int:
    """
    You are given a 0-indexed array nums consisting of positive integers, representing targets on a number line. You are also given an integer space.
    You have a machine which can destroy targets. Seeding the machine with some nums[i] allows it to destroy all targets with values that can be represented as nums[i] + c * space, where c is any non-negative integer. You want to destroy the maximum number of targets in nums.
    Return the minimum value of nums[i] you can seed the machine with to destroy the maximum number of targets.
    
    Example 1:
    
    >>> destroyTargets(nums = [3,7,8,1,1,5], space = 2)
    >>> 1
    Explanation: If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,...
    In this case, we would destroy 5 total targets (all except for nums[2]).
    It is impossible to destroy more than 5 targets, so we return nums[3].
    
    Example 2:
    
    >>> destroyTargets(nums = [1,3,5,2,4,6], space = 2)
    >>> 1
    Explanation: Seeding the machine with nums[0], or nums[3] destroys 3 targets.
    It is not possible to destroy more than 3 targets.
    Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
    
    Example 3:
    
    >>> destroyTargets(nums = [6,2,5], space = 100)
    >>> 2
    Explanation: Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
    """
def secondGreaterElement(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed array of non-negative integers nums. For each integer in nums, you must find its respective second greater integer.
    The second greater integer of nums[i] is nums[j] such that:
    
    j > i
    nums[j] > nums[i]
    There exists exactly one index k such that nums[k] > nums[i] and i < k < j.
    
    If there is no such nums[j], the second greater integer is considered to be -1.
    
    For example, in the array [1, 2, 4, 3], the second greater integer of 1 is 4, 2 is 3, and that of 3 and 4 is -1.
    
    Return an integer array answer, where answer[i] is the second greater integer of nums[i].
    
    Example 1:
    
    >>> secondGreaterElement(nums = [2,4,0,9,6])
    >>> [9,6,6,-1,-1]
    Explanation:
    0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
    1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
    2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
    3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
    4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
    Thus, we return [9,6,6,-1,-1].
    
    Example 2:
    
    >>> secondGreaterElement(nums = [3,3])
    >>> [-1,-1]
    Explanation:
    We return [-1,-1] since neither integer has any integer greater than it.
    """
def averageValue(nums: List[int]) -> int:
    """
    Given an integer array nums of positive integers, return the average value of all even integers that are divisible by 3.
    Note that the average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
    
    Example 1:
    
    >>> averageValue(nums = [1,3,6,10,12,15])
    >>> 9
    Explanation: 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.
    
    Example 2:
    
    >>> averageValue(nums = [1,2,4,7,10])
    >>> 0
    Explanation: There is no single number that satisfies the requirement, so return 0.
    """
def mostPopularCreator(creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
    """
    You are given two string arrays creators and ids, and an integer array views, all of length n. The ith video on a platform was created by creators[i], has an id of ids[i], and has views[i] views.
    The popularity of a creator is the sum of the number of views on all of the creator's videos. Find the creator with the highest popularity and the id of their most viewed video.
    
    If multiple creators have the highest popularity, find all of them.
    If multiple videos have the highest view count for a creator, find the lexicographically smallest id.
    
    Note: It is possible for different videos to have the same id, meaning that ids do not uniquely identify a video. For example, two videos with the same ID are considered as distinct videos with their own viewcount.
    Return a 2D array of strings answer where answer[i] = [creatorsi, idi] means that creatorsi has the highest popularity and idi is the id of their most popular video. The answer can be returned in any order.
    
    Example 1:
    
    >>> mostPopularCreator(creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4])
    >>> [["alice","one"],["bob","two"]]
    Explanation:
    The popularity of alice is 5 + 5 = 10.
    The popularity of bob is 10.
    The popularity of chris is 4.
    alice and bob are the most popular creators.
    For bob, the video with the highest view count is "two".
    For alice, the videos with the highest view count are "one" and "three". Since "one" is lexicographically smaller than "three", it is included in the answer.
    
    Example 2:
    
    >>> mostPopularCreator(creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2])
    >>> [["alice","b"]]
    Explanation:
    The videos with id "b" and "c" have the highest view count.
    Since "b" is lexicographically smaller than "c", it is included in the answer.
    """
def makeIntegerBeautiful(n: int, target: int) -> int:
    """
    You are given two positive integers n and target.
    An integer is considered beautiful if the sum of its digits is less than or equal to target.
    Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.
    
    Example 1:
    
    >>> makeIntegerBeautiful(n = 16, target = 6)
    >>> 4
    Explanation: Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
    
    Example 2:
    
    >>> makeIntegerBeautiful(n = 467, target = 6)
    >>> 33
    Explanation: Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
    
    Example 3:
    
    >>> makeIntegerBeautiful(n = 1, target = 1)
    >>> 0
    Explanation: Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def treeQueries(root: Optional[TreeNode], queries: List[int]) -> List[int]:
    """
    You are given the root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m.
    You have to perform m independent queries on the tree where in the ith query you do the following:
    
    Remove the subtree rooted at the node with the value queries[i] from the tree. It is guaranteed that queries[i] will not be equal to the value of the root.
    
    Return an array answer of size m where answer[i] is the height of the tree after performing the ith query.
    Note:
    
    The queries are independent, so the tree returns to its initial state after each query.
    The height of a tree is the number of edges in the longest simple path from the root to some node in the tree.
    
    
    Example 1:
    
    
    >>> __init__(root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4])
    >>> [2]
    Explanation: The diagram above shows the tree after removing the subtree rooted at node with value 4.
    The height of the tree is 2 (The path 1 -> 3 -> 2).
    
    Example 2:
    
    
    >>> __init__(root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8])
    >>> [3,2,3,2]
    Explanation: We have the following queries:
    - Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
    - Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
    - Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
    - Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
    """
def sortArray(nums: List[int]) -> int:
    """
    You are given an integer array nums of size n containing each element from 0 to n - 1 (inclusive). Each of the elements from 1 to n - 1 represents an item, and the element 0 represents an empty space.
    In one operation, you can move any item to the empty space. nums is considered to be sorted if the numbers of all the items are in ascending order and the empty space is either at the beginning or at the end of the array.
    For example, if n = 4, nums is sorted if:
    
    nums = [0,1,2,3] or
    nums = [1,2,3,0]
    
    ...and considered to be unsorted otherwise.
    Return the minimum number of operations needed to sort nums.
    
    Example 1:
    
    >>> sortArray(nums = [4,2,0,3,1])
    >>> 3
    Explanation:
    - Move item 2 to the empty space. Now, nums = [4,0,2,3,1].
    - Move item 1 to the empty space. Now, nums = [4,1,2,3,0].
    - Move item 4 to the empty space. Now, nums = [0,1,2,3,4].
    It can be proven that 3 is the minimum number of operations needed.
    
    Example 2:
    
    >>> sortArray(nums = [1,2,3,4,0])
    >>> 0
    Explanation: nums is already sorted so return 0.
    
    Example 3:
    
    >>> sortArray(nums = [1,0,2,4,3])
    >>> 2
    Explanation:
    - Move item 2 to the empty space. Now, nums = [1,2,0,4,3].
    - Move item 3 to the empty space. Now, nums = [1,2,3,4,0].
    It can be proven that 2 is the minimum number of operations needed.
    """
def applyOperations(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed array nums of size n consisting of non-negative integers.
    You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:
    
    If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.
    
    After performing all the operations, shift all the 0's to the end of the array.
    
    For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
    
    Return the resulting array.
    Note that the operations are applied sequentially, not all at once.
    
    Example 1:
    
    >>> applyOperations(nums = [1,2,2,1,1,0])
    >>> [1,4,2,0,0,0]
    Explanation: We do the following operations:
    - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
    - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
    - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
    - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
    - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
    After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
    
    Example 2:
    
    >>> applyOperations(nums = [0,1])
    >>> [1,0]
    Explanation: No operation can be applied, we just shift the 0 to the end.
    """
def maximumSubarraySum(nums: List[int], k: int) -> int:
    """
    You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:
    
    The length of the subarray is k, and
    All the elements of the subarray are distinct.
    
    Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.
    A subarray is a contiguous non-empty sequence of elements within an array.
    
    Example 1:
    
    >>> maximumSubarraySum(nums = [1,5,4,2,9,9,9], k = 3)
    >>> 15
    Explanation: The subarrays of nums with length 3 are:
    - [1,5,4] which meets the requirements and has a sum of 10.
    - [5,4,2] which meets the requirements and has a sum of 11.
    - [4,2,9] which meets the requirements and has a sum of 15.
    - [2,9,9] which does not meet the requirements because the element 9 is repeated.
    - [9,9,9] which does not meet the requirements because the element 9 is repeated.
    We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
    
    Example 2:
    
    >>> maximumSubarraySum(nums = [4,4,4], k = 3)
    >>> 0
    Explanation: The subarrays of nums with length 3 are:
    - [4,4,4] which does not meet the requirements because the element 4 is repeated.
    We return 0 because no subarrays meet the conditions.
    """
def totalCost(costs: List[int], k: int, candidates: int) -> int:
    """
    You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker.
    You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules:
    
    You will run k sessions and hire exactly one worker in each session.
    In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index.
    
    For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].
    In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.
    
    
    If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.
    A worker can only be chosen once.
    
    Return the total cost to hire exactly k workers.
    
    Example 1:
    
    >>> totalCost(costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4)
    >>> 11
    Explanation: We hire 3 workers in total. The total cost is initially 0.
    - In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
    - In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
    - In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
    The total hiring cost is 11.
    
    Example 2:
    
    >>> totalCost(costs = [1,2,4,1], k = 3, candidates = 3)
    >>> 4
    Explanation: We hire 3 workers in total. The total cost is initially 0.
    - In the first hiring round we choose the worker from [1,2,4,1]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
    - In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
    - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
    The total hiring cost is 4.
    """
def minimumTotalDistance(robot: List[int], factory: List[List[int]]) -> int:
    """
    There are some robots and factories on the X-axis. You are given an integer array robot where robot[i] is the position of the ith robot. You are also given a 2D integer array factory where factory[j] = [positionj, limitj] indicates that positionj is the position of the jth factory and that the jth factory can repair at most limitj robots.
    The positions of each robot are unique. The positions of each factory are also unique. Note that a robot can be in the same position as a factory initially.
    All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.
    At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots.
    Return the minimum total distance traveled by all the robots. The test cases are generated such that all the robots can be repaired.
    Note that
    
    All robots move at the same speed.
    If two robots move in the same direction, they will never collide.
    If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.
    If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.
    If the robot moved from a position x to a position y, the distance it moved is |y - x|.
    
    
    Example 1:
    
    
    >>> minimumTotalDistance(robot = [0,4,6], factory = [[2,2],[6,2]])
    >>> 4
    Explanation: As shown in the figure:
    - The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
    - The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
    - The third robot at position 6 will be repaired at the second factory. It does not need to move.
    The limit of the first factory is 2, and it fixed 2 robots.
    The limit of the second factory is 2, and it fixed 1 robot.
    The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
    
    Example 2:
    
    
    >>> minimumTotalDistance(robot = [1,-1], factory = [[-2,1],[2,1]])
    >>> 2
    Explanation: As shown in the figure:
    - The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
    - The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
    The limit of the first factory is 1, and it fixed 1 robot.
    The limit of the second factory is 1, and it fixed 1 robot.
    The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
    """
def validSubarraySplit(nums: List[int]) -> int:
    """
    You are given an integer array nums.
    Splitting of an integer array nums into subarrays is valid if:
    
    the greatest common divisor of the first and last elements of each subarray is greater than 1, and
    each element of nums belongs to exactly one subarray.
    
    Return the minimum number of subarrays in a valid subarray splitting of nums. If a valid subarray splitting is not possible, return -1.
    Note that:
    
    The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
    A subarray is a contiguous non-empty part of an array.
    
    
    Example 1:
    
    >>> validSubarraySplit(nums = [2,6,3,4,3])
    >>> 2
    Explanation: We can create a valid split in the following way: [2,6] | [3,4,3].
    - The starting element of the 1st subarray is 2 and the ending is 6. Their greatest common divisor is 2, which is greater than 1.
    - The starting element of the 2nd subarray is 3 and the ending is 3. Their greatest common divisor is 3, which is greater than 1.
    It can be proved that 2 is the minimum number of subarrays that we can obtain in a valid split.
    
    Example 2:
    
    >>> validSubarraySplit(nums = [3,5])
    >>> 2
    Explanation: We can create a valid split in the following way: [3] | [5].
    - The starting element of the 1st subarray is 3 and the ending is 3. Their greatest common divisor is 3, which is greater than 1.
    - The starting element of the 2nd subarray is 5 and the ending is 5. Their greatest common divisor is 5, which is greater than 1.
    It can be proved that 2 is the minimum number of subarrays that we can obtain in a valid split.
    
    Example 3:
    
    >>> validSubarraySplit(nums = [1,2,1])
    >>> -1
    Explanation: It is impossible to create valid split.
    """