ShwStone/humaneval-leetcode huggingface dataset:
def slowestKey(releaseTimes: List[int], keysPressed: str) -> str:
    """
    A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.
    You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed. The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released.
    The tester wants to know the key of the keypress that had the longest duration. The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0].
    Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key may not have had the same duration.
    Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.
    
    Example 1:
    
    >>> slowestKey(releaseTimes = [9,29,49,50], keysPressed = "cbcd")
    >>> "c"
    Explanation: The keypresses were as follows:
    Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
    Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).
    Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).
    Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).
    The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
    'c' is lexicographically larger than 'b', so the answer is 'c'.
    
    Example 2:
    
    >>> slowestKey(releaseTimes = [12,23,36,46,62], keysPressed = "spuda")
    >>> "a"
    Explanation: The keypresses were as follows:
    Keypress for 's' had a duration of 12.
    Keypress for 'p' had a duration of 23 - 12 = 11.
    Keypress for 'u' had a duration of 36 - 23 = 13.
    Keypress for 'd' had a duration of 46 - 36 = 10.
    Keypress for 'a' had a duration of 62 - 46 = 16.
    The longest of these was the keypress for 'a' with duration 16.
    """
def checkArithmeticSubarrays(nums: List[int], l: List[int], r: List[int]) -> List[bool]:
    """
    A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
    For example, these are arithmetic sequences:
    
    1, 3, 5, 7, 9
    7, 7, 7, 7
    3, -1, -5, -9
    The following sequence is not arithmetic:
    
    1, 1, 2, 5, 7
    You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
    Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
    
    Example 1:
    
    >>> checkArithmeticSubarrays(nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5])
    >>> [true,false,true]
    Explanation:
    In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
    In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
    In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.
    Example 2:
    
    >>> checkArithmeticSubarrays(nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10])
    >>> [false,true,false,false,true,true]
    """
def minimumEffortPath(heights: List[List[int]]) -> int:
    """
    You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
    A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
    Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
    
    Example 1:
    
    
    >>> minimumEffortPath(heights = [[1,2,2],[3,8,2],[5,3,5]])
    >>> 2
    Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
    This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
    
    Example 2:
    
    
    >>> minimumEffortPath(heights = [[1,2,3],[3,8,4],[5,3,5]])
    >>> 1
    Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
    
    Example 3:
    
    
    >>> minimumEffortPath(heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]])
    >>> 0
    Explanation: This route does not require any effort.
    """
def matrixRankTransform(matrix: List[List[int]]) -> List[List[int]]:
    """
    Given an m x n matrix, return a new matrix answer where answer[row][col] is the rank of matrix[row][col].
    The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules:
    
    The rank is an integer starting from 1.
    If two elements p and q are in the same row or column, then:
    
    If p < q then rank(p) < rank(q)
    If p == q then rank(p) == rank(q)
    If p > q then rank(p) > rank(q)
    
    
    The rank should be as small as possible.
    
    The test cases are generated so that answer is unique under the given rules.
    
    Example 1:
    
    
    >>> matrixRankTransform(matrix = [[1,2],[3,4]])
    >>> [[1,2],[2,3]]
    Explanation:
    The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
    The rank of matrix[0][1] is 2 because matrix[0][1] > matrix[0][0] and matrix[0][0] is rank 1.
    The rank of matrix[1][0] is 2 because matrix[1][0] > matrix[0][0] and matrix[0][0] is rank 1.
    The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1] > matrix[1][0], and both matrix[0][1] and matrix[1][0] are rank 2.
    
    Example 2:
    
    
    >>> matrixRankTransform(matrix = [[7,7],[7,7]])
    >>> [[1,1],[1,1]]
    
    Example 3:
    
    
    >>> matrixRankTransform(matrix = [[20,-21,14],[-19,4,19],[22,-47,24],[-19,4,19]])
    >>> [[4,2,3],[1,3,4],[5,1,6],[1,3,4]]
    """
def frequencySort(nums: List[int]) -> List[int]:
    """
    Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
    Return the sorted array.
    
    Example 1:
    
    >>> frequencySort(nums = [1,1,2,2,2,3])
    >>> [3,1,1,2,2,2]
    Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
    
    Example 2:
    
    >>> frequencySort(nums = [2,3,1,3,2])
    >>> [1,3,3,2,2]
    Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
    
    Example 3:
    
    >>> frequencySort(nums = [-1,1,-6,4,5,-6,1,4,1])
    >>> [5,-1,4,4,-6,-6,1,1,1]
    """
def maxWidthOfVerticalArea(points: List[List[int]]) -> int:
    """
    Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.
    A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.
    Note that points on the edge of a vertical area are not considered included in the area.
    
    Example 1:
    ​
    
    >>> maxWidthOfVerticalArea(points = [[8,7],[9,9],[7,4],[9,7]])
    >>> 1
    Explanation: Both the red and the blue area are optimal.
    
    Example 2:
    
    >>> maxWidthOfVerticalArea(points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]])
    >>> 3
    """
def countSubstrings(s: str, t: str) -> int:
    """
    Given two strings s and t, find the number of ways you can choose a non-empty substring of s and replace a single character by a different character such that the resulting substring is a substring of t. In other words, find the number of substrings in s that differ from some substring in t by exactly one character.
    For example, the underlined substrings in "computer" and "computation" only differ by the 'e'/'a', so this is a valid way.
    Return the number of substrings that satisfy the condition above.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> countSubstrings(s = "aba", t = "baba")
    >>> 6
    Explanation: The following are the pairs of substrings from s and t that differ by exactly 1 character:
    ("aba", "baba")
    ("aba", "baba")
    ("aba", "baba")
    ("aba", "baba")
    ("aba", "baba")
    ("aba", "baba")
    The underlined portions are the substrings that are chosen from s and t.
    
    ​​Example 2:
    
    >>> countSubstrings(s = "ab", t = "bb")
    >>> 3
    Explanation: The following are the pairs of substrings from s and t that differ by 1 character:
    ("ab", "bb")
    ("ab", "bb")
    ("ab", "bb")
    ​​​​The underlined portions are the substrings that are chosen from s and t.
    """
def numWays(words: List[str], target: str) -> int:
    """
    You are given a list of strings of the same length words and a string target.
    Your task is to form target using the given words under the following rules:
    
    target should be formed from left to right.
    To form the ith character (0-indexed) of target, you can choose the kth character of the jth string in words if target[i] = words[j][k].
    Once you use the kth character of the jth string of words, you can no longer use the xth character of any string in words where x <= k. In other words, all characters to the left of or at index k become unusuable for every string.
    Repeat the process until you form the string target.
    
    Notice that you can use multiple characters from the same string in words provided the conditions above are met.
    Return the number of ways to form target from words. Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numWays(words = ["acca","bbbb","caca"], target = "aba")
    >>> 6
    Explanation: There are 6 ways to form target.
    "aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("caca")
    "aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("caca")
    "aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("acca")
    "aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("acca")
    "aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("acca")
    "aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("caca")
    
    Example 2:
    
    >>> numWays(words = ["abba","baab"], target = "bab")
    >>> 4
    Explanation: There are 4 ways to form target.
    "bab" -> index 0 ("baab"), index 1 ("baab"), index 2 ("abba")
    "bab" -> index 0 ("baab"), index 1 ("baab"), index 3 ("baab")
    "bab" -> index 0 ("baab"), index 2 ("baab"), index 3 ("baab")
    "bab" -> index 1 ("abba"), index 2 ("baab"), index 3 ("baab")
    """
def canFormArray(arr: List[int], pieces: List[List[int]]) -> bool:
    """
    You are given an array of distinct integers arr and an array of integer arrays pieces, where the integers in pieces are distinct. Your goal is to form arr by concatenating the arrays in pieces in any order. However, you are not allowed to reorder the integers in each array pieces[i].
    Return true if it is possible to form the array arr from pieces. Otherwise, return false.
    
    Example 1:
    
    >>> canFormArray(arr = [15,88], pieces = [[88],[15]])
    >>> true
    Explanation: Concatenate [15] then [88]
    
    Example 2:
    
    >>> canFormArray(arr = [49,18,16], pieces = [[16,18,49]])
    >>> false
    Explanation: Even though the numbers match, we cannot reorder pieces[0].
    
    Example 3:
    
    >>> canFormArray(arr = [91,4,64,78], pieces = [[78],[4,64],[91]])
    >>> true
    Explanation: Concatenate [91] then [4,64] then [78]
    """
def countVowelStrings(n: int) -> int:
    """
    Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.
    A string s is lexicographically sorted if for all valid i, s[i] is the same as or comes before s[i+1] in the alphabet.
    
    Example 1:
    
    >>> countVowelStrings(n = 1)
    >>> 5
    Explanation: The 5 sorted strings that consist of vowels only are ["a","e","i","o","u"].
    
    Example 2:
    
    >>> countVowelStrings(n = 2)
    >>> 15
    Explanation: The 15 sorted strings that consist of vowels only are
    ["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"].
    Note that "ea" is not a valid string since 'e' comes after 'a' in the alphabet.
    
    Example 3:
    
    >>> countVowelStrings(n = 33)
    >>> 66045
    """
def furthestBuilding(heights: List[int], bricks: int, ladders: int) -> int:
    """
    You are given an integer array heights representing the heights of buildings, some bricks, and some ladders.
    You start your journey from building 0 and move to the next building by possibly using bricks or ladders.
    While moving from building i to building i+1 (0-indexed),
    
    If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
    If the current building's height is less than the next building's height, you can either use one ladder or (h[i+1] - h[i]) bricks.
    
    Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
    
    Example 1:
    
    
    >>> furthestBuilding(heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1)
    >>> 4
    Explanation: Starting at building 0, you can follow these steps:
    - Go to building 1 without using ladders nor bricks since 4 >= 2.
    - Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
    - Go to building 3 without using ladders nor bricks since 7 >= 6.
    - Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
    It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
    
    Example 2:
    
    >>> furthestBuilding(heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2)
    >>> 7
    
    Example 3:
    
    >>> furthestBuilding(heights = [14,3,19,3], bricks = 17, ladders = 0)
    >>> 3
    """
def kthSmallestPath(destination: List[int], k: int) -> str:
    """
    Bob is standing at cell (0, 0), and he wants to reach destination: (row, column). He can only travel right and down. You are going to help Bob by providing instructions for him to reach destination.
    The instructions are represented as a string, where each character is either:
    
    'H', meaning move horizontally (go right), or
    'V', meaning move vertically (go down).
    
    Multiple instructions will lead Bob to destination. For example, if destination is (2, 3), both "HHHVV" and "HVHVH" are valid instructions.
    However, Bob is very picky. Bob has a lucky number k, and he wants the kth lexicographically smallest instructions that will lead him to destination. k is 1-indexed.
    Given an integer array destination and an integer k, return the kth lexicographically smallest instructions that will take Bob to destination.
    
    Example 1:
    
    
    >>> kthSmallestPath(destination = [2,3], k = 1)
    >>> "HHHVV"
    Explanation: All the instructions that reach (2, 3) in lexicographic order are as follows:
    ["HHHVV", "HHVHV", "HHVVH", "HVHHV", "HVHVH", "HVVHH", "VHHHV", "VHHVH", "VHVHH", "VVHHH"].
    
    Example 2:
    
    
    >>> kthSmallestPath(destination = [2,3], k = 2)
    >>> "HHVHV"
    
    Example 3:
    
    
    >>> kthSmallestPath(destination = [2,3], k = 3)
    >>> "HHVVH"
    """
# class TreeNode:
#     def __init__(x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    """
    Given the root of a binary tree, return the lowest common ancestor (LCA) of two given nodes, p and q. If either node p or q does not exist in the tree, return null. All values of the nodes in the tree are unique.
    According to the definition of LCA on Wikipedia: "The lowest common ancestor of two nodes p and q in a binary tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)". A descendant of a node x is a node y that is on the path from node x to some leaf node.
    
    Example 1:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1)
    >>> 3
    Explanation: The LCA of nodes 5 and 1 is 3.
    Example 2:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4)
    >>> 5
    Explanation: The LCA of nodes 5 and 4 is 5. A node can be a descendant of itself according to the definition of LCA.
    Example 3:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 10)
    >>> null
    Explanation: Node 10 does not exist in the tree, so return null.
    """
def getMaximumGenerated(n: int) -> int:
    """
    You are given an integer n. A 0-indexed integer array nums of length n + 1 is generated in the following way:
    
    nums[0] = 0
    nums[1] = 1
    nums[2 * i] = nums[i] when 2 <= 2 * i <= n
    nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n
    
    Return the maximum integer in the array nums​​​.
    
    Example 1:
    
    >>> getMaximumGenerated(n = 7)
    >>> 3
    Explanation: According to the given rules:
      nums[0] = 0
      nums[1] = 1
      nums[(1 * 2) = 2] = nums[1] = 1
      nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
      nums[(2 * 2) = 4] = nums[2] = 1
      nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
      nums[(3 * 2) = 6] = nums[3] = 2
      nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
    Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is max(0,1,1,2,1,3,2,3) = 3.
    
    Example 2:
    
    >>> getMaximumGenerated(n = 2)
    >>> 1
    Explanation: According to the given rules, nums = [0,1,1]. The maximum is max(0,1,1) = 1.
    
    Example 3:
    
    >>> getMaximumGenerated(n = 3)
    >>> 2
    Explanation: According to the given rules, nums = [0,1,1,2]. The maximum is max(0,1,1,2) = 2.
    """
def minDeletions(s: str) -> int:
    """
    A string s is called good if there are no two different characters in s that have the same frequency.
    Given a string s, return the minimum number of characters you need to delete to make s good.
    The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.
    
    Example 1:
    
    >>> minDeletions(s = "aab")
    >>> 0
    Explanation: s is already good.
    
    Example 2:
    
    >>> minDeletions(s = "aaabbbcc")
    >>> 2
    Explanation: You can delete two 'b's resulting in the good string "aaabcc".
    Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
    Example 3:
    
    >>> minDeletions(s = "ceabaacb")
    >>> 2
    Explanation: You can delete both 'c's resulting in the good string "eabaab".
    Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
    """
def maxProfit(inventory: List[int], orders: int) -> int:
    """
    You have an inventory of different colored balls, and there is a customer that wants orders balls of any color.
    The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your inventory. For example, if you own 6 yellow balls, the customer would pay 6 for the first yellow ball. After the transaction, there are only 5 yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls decreases as you sell more to the customer).
    You are given an integer array, inventory, where inventory[i] represents the number of balls of the ith color that you initially own. You are also given an integer orders, which represents the total number of balls that the customer wants. You can sell the balls in any order.
    Return the maximum total value that you can attain after selling orders colored balls. As the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> maxProfit(inventory = [2,5], orders = 4)
    >>> 14
    Explanation: Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).
    The maximum total value is 2 + 5 + 4 + 3 = 14.
    
    Example 2:
    
    >>> maxProfit(inventory = [3,5], orders = 6)
    >>> 19
    Explanation: Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).
    The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.
    """