ShwStone/humaneval-leetcode huggingface dataset:
def decodeMessage(key: str, message: str) -> str:
    """
    You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
    
    Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
    Align the substitution table with the regular English alphabet.
    Each letter in message is then substituted using the table.
    Spaces ' ' are transformed to themselves.
    
    
    For example, given key = "happy boy" (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
    
    Return the decoded message.
    
    Example 1:
    
    
    >>> decodeMessage(key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv")
    >>> "this is a secret"
    Explanation: The diagram above shows the substitution table.
    It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog".
    
    Example 2:
    
    
    >>> decodeMessage(key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb")
    >>> "the five boxing wizards jump quickly"
    Explanation: The diagram above shows the substitution table.
    It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo".
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def spiralMatrix(m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
    """
    You are given two integers m and n, which represent the dimensions of a matrix.
    You are also given the head of a linked list of integers.
    Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1.
    Return the generated matrix.
    
    Example 1:
    
    
    >>> __init__(m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0])
    >>> [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
    Explanation: The diagram above shows how the values are printed in the matrix.
    Note that the remaining spaces in the matrix are filled with -1.
    
    Example 2:
    
    
    >>> __init__(m = 1, n = 4, head = [0,1,2])
    >>> [[0,1,2,-1]]
    Explanation: The diagram above shows how the values are printed from left to right in the matrix.
    The last space in the matrix is set to -1.
    """
def peopleAwareOfSecret(n: int, delay: int, forget: int) -> int:
    """
    On day 1, one person discovers a secret.
    You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, which means that each person will forget the secret forget days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.
    Given an integer n, return the number of people who know the secret at the end of day n. Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> peopleAwareOfSecret(n = 6, delay = 2, forget = 4)
    >>> 5
    Explanation:
    Day 1: Suppose the first person is named A. (1 person)
    Day 2: A is the only person who knows the secret. (1 person)
    Day 3: A shares the secret with a new person, B. (2 people)
    Day 4: A shares the secret with a new person, C. (3 people)
    Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
    Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
    
    Example 2:
    
    >>> peopleAwareOfSecret(n = 4, delay = 1, forget = 3)
    >>> 6
    Explanation:
    Day 1: The first person is named A. (1 person)
    Day 2: A shares the secret with B. (2 people)
    Day 3: A and B share the secret with 2 new people, C and D. (4 people)
    Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
    """
def countPaths(grid: List[List[int]]) -> int:
    """
    You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.
    Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.
    Two paths are considered different if they do not have exactly the same sequence of visited cells.
    
    Example 1:
    
    
    >>> countPaths(grid = [[1,1],[3,4]])
    >>> 8
    Explanation: The strictly increasing paths are:
    - Paths with length 1: [1], [1], [3], [4].
    - Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
    - Paths with length 3: [1 -> 3 -> 4].
    The total number of paths is 4 + 3 + 1 = 8.
    
    Example 2:
    
    >>> countPaths(grid = [[1],[2]])
    >>> 3
    Explanation: The strictly increasing paths are:
    - Paths with length 1: [1], [2].
    - Paths with length 2: [1 -> 2].
    The total number of paths is 2 + 1 = 3.
    """
def makePalindrome(s: str) -> bool:
    """
    You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character.
    Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.
    
    Example 1:
    
    >>> makePalindrome(s = "abcdba")
    >>> true
    Explanation: One way to make s a palindrome using 1 operation is:
    - Change s[2] to 'd'. Now, s = "abddba".
    One operation could be performed to make s a palindrome so return true.
    
    Example 2:
    
    >>> makePalindrome(s = "aa")
    >>> true
    Explanation: One way to make s a palindrome using 2 operations is:
    - Change s[0] to 'b'. Now, s = "ba".
    - Change s[1] to 'b'. Now, s = "bb".
    Two operations could be performed to make s a palindrome so return true.
    
    Example 3:
    
    >>> makePalindrome(s = "abcdef")
    >>> false
    Explanation: It is not possible to make s a palindrome using one or two operations so return false.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def evaluateTree(root: Optional[TreeNode]) -> bool:
    """
    You are given the root of a full binary tree with the following properties:
    
    Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.
    Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.
    
    The evaluation of a node is as follows:
    
    If the node is a leaf node, the evaluation is the value of the node, i.e. True or False.
    Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
    
    Return the boolean result of evaluating the root node.
    A full binary tree is a binary tree where each node has either 0 or 2 children.
    A leaf node is a node that has zero children.
    
    Example 1:
    
    
    >>> __init__(root = [2,1,3,null,null,0,1])
    >>> true
    Explanation: The above diagram illustrates the evaluation process.
    The AND node evaluates to False AND True = False.
    The OR node evaluates to True OR False = True.
    The root node evaluates to True, so we return true.
    Example 2:
    
    >>> __init__(root = [0])
    >>> false
    Explanation: The root node is a leaf node and it evaluates to false, so we return false.
    """
def latestTimeCatchTheBus(buses: List[int], passengers: List[int], capacity: int) -> int:
    """
    You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth passenger. All bus departure times are unique. All passenger arrival times are unique.
    You are given an integer capacity, which represents the maximum number of passengers that can get on each bus.
    When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.
    More formally when a bus arrives, either:
    
    If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or
    The capacity passengers with the earliest arrival times will get on the bus.
    
    Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger.
    Note: The arrays buses and passengers are not necessarily sorted.
    
    Example 1:
    
    >>> latestTimeCatchTheBus(buses = [10,20], passengers = [2,17,18,19], capacity = 2)
    >>> 16
    Explanation: Suppose you arrive at time 16.
    At time 10, the first bus departs with the 0th passenger.
    At time 20, the second bus departs with you and the 1st passenger.
    Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1st passenger to catch the bus.
    Example 2:
    
    >>> latestTimeCatchTheBus(buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2)
    >>> 20
    Explanation: Suppose you arrive at time 20.
    At time 10, the first bus departs with the 3rd passenger.
    At time 20, the second bus departs with the 5th and 1st passengers.
    At time 30, the third bus departs with the 0th passenger and you.
    Notice if you had arrived any later, then the 6th passenger would have taken your seat on the third bus.
    """
def minSumSquareDiff(nums1: List[int], nums2: List[int], k1: int, k2: int) -> int:
    """
    You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
    The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])2 for each 0 <= i < n.
    You are also given two positive integers k1 and k2. You can modify any of the elements of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements of nums2 by +1 or -1 at most k2 times.
    Return the minimum sum of squared difference after modifying array nums1 at most k1 times and modifying array nums2 at most k2 times.
    Note: You are allowed to modify the array elements to become negative integers.
    
    Example 1:
    
    >>> minSumSquareDiff(nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0)
    >>> 579
    Explanation: The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0.
    The sum of square difference will be: (1 - 2)2 + (2 - 10)2 + (3 - 20)2 + (4 - 19)2 = 579.
    
    Example 2:
    
    >>> minSumSquareDiff(nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1)
    >>> 43
    Explanation: One way to obtain the minimum sum of square difference is:
    - Increase nums1[0] once.
    - Increase nums2[2] once.
    The minimum of the sum of square difference will be:
    (2 - 5)2 + (4 - 8)2 + (10 - 7)2 + (12 - 9)2 = 43.
    Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.
    """
def validSubarraySize(nums: List[int], threshold: int) -> int:
    """
    You are given an integer array nums and an integer threshold.
    Find any subarray of nums of length k such that every element in the subarray is greater than threshold / k.
    Return the size of any such subarray. If there is no such subarray, return -1.
    A subarray is a contiguous non-empty sequence of elements within an array.
    
    Example 1:
    
    >>> validSubarraySize(nums = [1,3,4,3,1], threshold = 6)
    >>> 3
    Explanation: The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
    Note that this is the only valid subarray.
    
    Example 2:
    
    >>> validSubarraySize(nums = [6,5,6,5,8], threshold = 7)
    >>> 1
    Explanation: The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
    Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5.
    Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
    Therefore, 2, 3, 4, or 5 may also be returned.
    """
def fillCups(amount: List[int]) -> int:
    """
    You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up 2 cups with different types of water, or 1 cup of any type of water.
    You are given a 0-indexed integer array amount of length 3 where amount[0], amount[1], and amount[2] denote the number of cold, warm, and hot water cups you need to fill respectively. Return the minimum number of seconds needed to fill up all the cups.
    
    Example 1:
    
    >>> fillCups(amount = [1,4,2])
    >>> 4
    Explanation: One way to fill up the cups is:
    Second 1: Fill up a cold cup and a warm cup.
    Second 2: Fill up a warm cup and a hot cup.
    Second 3: Fill up a warm cup and a hot cup.
    Second 4: Fill up a warm cup.
    It can be proven that 4 is the minimum number of seconds needed.
    
    Example 2:
    
    >>> fillCups(amount = [5,4,4])
    >>> 7
    Explanation: One way to fill up the cups is:
    Second 1: Fill up a cold cup, and a hot cup.
    Second 2: Fill up a cold cup, and a warm cup.
    Second 3: Fill up a cold cup, and a warm cup.
    Second 4: Fill up a warm cup, and a hot cup.
    Second 5: Fill up a cold cup, and a hot cup.
    Second 6: Fill up a cold cup, and a warm cup.
    Second 7: Fill up a hot cup.
    
    Example 3:
    
    >>> fillCups(amount = [5,0,0])
    >>> 5
    Explanation: Every second, we fill up a cold cup.
    """
def canChange(start: str, target: str) -> bool:
    """
    You are given two strings start and target, both of length n. Each string consists only of the characters 'L', 'R', and '_' where:
    
    The characters 'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space directly to its left, and a piece 'R' can move to the right only if there is a blank space directly to its right.
    The character '_' represents a blank space that can be occupied by any of the 'L' or 'R' pieces.
    
    Return true if it is possible to obtain the string target by moving the pieces of the string start any number of times. Otherwise, return false.
    
    Example 1:
    
    >>> canChange(start = "_L__R__R_", target = "L______RR")
    >>> true
    Explanation: We can obtain the string target from start by doing the following moves:
    - Move the first piece one step to the left, start becomes equal to "L___R__R_".
    - Move the last piece one step to the right, start becomes equal to "L___R___R".
    - Move the second piece three steps to the right, start becomes equal to "L______RR".
    Since it is possible to get the string target from start, we return true.
    
    Example 2:
    
    >>> canChange(start = "R_L_", target = "__LR")
    >>> false
    Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_".
    After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
    
    Example 3:
    
    >>> canChange(start = "_R", target = "R_")
    >>> false
    Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.
    """
def idealArrays(n: int, maxValue: int) -> int:
    """
    You are given two integers n and maxValue, which are used to describe an ideal array.
    A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:
    
    Every arr[i] is a value from 1 to maxValue, for 0 <= i < n.
    Every arr[i] is divisible by arr[i - 1], for 0 < i < n.
    
    Return the number of distinct ideal arrays of length n. Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> idealArrays(n = 2, maxValue = 5)
    >>> 10
    Explanation: The following are the possible ideal arrays:
    - Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
    - Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
    - Arrays starting with the value 3 (1 array): [3,3]
    - Arrays starting with the value 4 (1 array): [4,4]
    - Arrays starting with the value 5 (1 array): [5,5]
    There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
    
    Example 2:
    
    >>> idealArrays(n = 5, maxValue = 3)
    >>> 11
    Explanation: The following are the possible ideal arrays:
    - Arrays starting with the value 1 (9 arrays):
       - With no other distinct values (1 array): [1,1,1,1,1]
       - With 2nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
       - With 2nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
    - Arrays starting with the value 2 (1 array): [2,2,2,2,2]
    - Arrays starting with the value 3 (1 array): [3,3,3,3,3]
    There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
    """
def minimumSwaps(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums.
    Swaps of adjacent elements are able to be performed on nums.
    A valid array meets the following conditions:
    
    The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.
    The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.
    
    Return the minimum swaps required to make nums a valid array.
    
    Example 1:
    
    >>> minimumSwaps(nums = [3,4,5,5,3,1])
    >>> 6
    Explanation: Perform the following swaps:
    - Swap 1: Swap the 3rd and 4th elements, nums is then [3,4,5,3,5,1].
    - Swap 2: Swap the 4th and 5th elements, nums is then [3,4,5,3,1,5].
    - Swap 3: Swap the 3rd and 4th elements, nums is then [3,4,5,1,3,5].
    - Swap 4: Swap the 2nd and 3rd elements, nums is then [3,4,1,5,3,5].
    - Swap 5: Swap the 1st and 2nd elements, nums is then [3,1,4,5,3,5].
    - Swap 6: Swap the 0th and 1st elements, nums is then [1,3,4,5,3,5].
    It can be shown that 6 swaps is the minimum swaps required to make a valid array.
    
    Example 2:
    
    >>> minimumSwaps(nums = [9])
    >>> 0
    Explanation: The array is already valid, so we return 0.
    """
def numberOfPairs(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed integer array nums. In one operation, you may do the following:
    
    Choose two integers in nums that are equal.
    Remove both integers from nums, forming a pair.
    
    The operation is done on nums as many times as possible.
    Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.
    
    Example 1:
    
    >>> numberOfPairs(nums = [1,3,2,1,3,2,2])
    >>> [3,1]
    Explanation:
    Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
    Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
    Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
    No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
    
    Example 2:
    
    >>> numberOfPairs(nums = [1,1])
    >>> [1,0]
    Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
    No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
    
    Example 3:
    
    >>> numberOfPairs(nums = [0])
    >>> [0,1]
    Explanation: No pairs can be formed, and there is 1 number leftover in nums.
    """
def maximumSum(nums: List[int]) -> int:
    """
    You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].
    Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions. If no such pair of indices exists, return -1.
    
    Example 1:
    
    >>> maximumSum(nums = [18,43,36,13,7])
    >>> 54
    Explanation: The pairs (i, j) that satisfy the conditions are:
    - (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
    - (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
    So the maximum sum that we can obtain is 54.
    
    Example 2:
    
    >>> maximumSum(nums = [10,12,19,14])
    >>> -1
    Explanation: There are no two numbers that satisfy the conditions, so we return -1.
    """
def smallestTrimmedNumbers(nums: List[str], queries: List[List[int]]) -> List[int]:
    """
    You are given a 0-indexed array of strings nums, where each string is of equal length and consists of only digits.
    You are also given a 0-indexed 2D integer array queries where queries[i] = [ki, trimi]. For each queries[i], you need to:
    
    Trim each number in nums to its rightmost trimi digits.
    Determine the index of the kith smallest trimmed number in nums. If two trimmed numbers are equal, the number with the lower index is considered to be smaller.
    Reset each number in nums to its original length.
    
    Return an array answer of the same length as queries, where answer[i] is the answer to the ith query.
    Note:
    
    To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.
    Strings in nums may contain leading zeros.
    
    
    Example 1:
    
    >>> smallestTrimmedNumbers(nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]])
    >>> [2,2,1,0]
    Explanation:
    1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
    2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2.
    3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4th smallest number is 73.
    4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
       Note that the trimmed number "02" is evaluated as 2.
    
    Example 2:
    
    >>> smallestTrimmedNumbers(nums = ["24","37","96","04"], queries = [[2,1],[2,2]])
    >>> [3,0]
    Explanation:
    1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2nd smallest number is 4 at index 3.
       There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
    2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24.
    """
def minOperations(nums: List[int], numsDivide: List[int]) -> int:
    """
    You are given two positive integer arrays nums and numsDivide. You can delete any number of elements from nums.
    Return the minimum number of deletions such that the smallest element in nums divides all the elements of numsDivide. If this is not possible, return -1.
    Note that an integer x divides y if y % x == 0.
    
    Example 1:
    
    >>> minOperations(nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15])
    >>> 2
    Explanation:
    The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.
    We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3].
    The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.
    It can be shown that 2 is the minimum number of deletions needed.
    
    Example 2:
    
    >>> minOperations(nums = [4,3,6], numsDivide = [8,2,6,10])
    >>> -1
    Explanation:
    We want the smallest element in nums to divide all the elements of numsDivide.
    There is no way to delete elements from nums to allow this.
    """