ShwStone/humaneval-leetcode huggingface dataset:
def houseOfCards(n: int) -> int:
    """
    You are given an integer n representing the number of playing cards you have. A house of cards meets the following conditions:
    
    A house of cards consists of one or more rows of triangles and horizontal cards.
    Triangles are created by leaning two cards against each other.
    One card must be placed horizontally between all adjacent triangles in a row.
    Any triangle on a row higher than the first must be placed on a horizontal card from the previous row.
    Each triangle is placed in the leftmost available spot in the row.
    
    Return the number of distinct house of cards you can build using all n cards. Two houses of cards are considered distinct if there exists a row where the two houses contain a different number of cards.
    
    Example 1:
    
    
    >>> houseOfCards(n = 16)
    >>> 2
    Explanation: The two valid houses of cards are shown.
    The third house of cards in the diagram is not valid because the rightmost triangle on the top row is not placed on top of a horizontal card.
    
    Example 2:
    
    
    >>> houseOfCards(n = 2)
    >>> 1
    Explanation: The one valid house of cards is shown.
    
    Example 3:
    
    
    >>> houseOfCards(n = 4)
    >>> 0
    Explanation: The three houses of cards in the diagram are not valid.
    The first house of cards needs a horizontal card placed between the two triangles.
    The second house of cards uses 5 cards.
    The third house of cards uses 2 cards.
    """
def mostFrequent(nums: List[int], key: int) -> int:
    """
    You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums.
    For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that:
    
    0 <= i <= nums.length - 2,
    nums[i] == key and,
    nums[i + 1] == target.
    
    Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique.
    
    Example 1:
    
    >>> mostFrequent(nums = [1,100,200,1,100], key = 1)
    >>> 100
    Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
    No other integers follow an occurrence of key, so we return 100.
    
    Example 2:
    
    >>> mostFrequent(nums = [2,2,2,2,3], key = 2)
    >>> 2
    Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
    For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
    target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
    """
def sortJumbled(mapping: List[int], nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.
    The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.
    You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.
    Notes:
    
    Elements with the same mapped values should appear in the same relative order as in the input.
    The elements of nums should only be sorted based on their mapped values and not be replaced by them.
    
    
    Example 1:
    
    >>> sortJumbled(mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38])
    >>> [338,38,991]
    Explanation:
    Map the number 991 as follows:
    1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
    2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
    Therefore, the mapped value of 991 is 669.
    338 maps to 007, or 7 after removing the leading zeros.
    38 maps to 07, which is also 7 after removing leading zeros.
    Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
    Thus, the sorted array is [338,38,991].
    
    Example 2:
    
    >>> sortJumbled(mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123])
    >>> [123,456,789]
    Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
    """
def getAncestors(n: int, edges: List[List[int]]) -> List[List[int]]:
    """
    You are given a positive integer n representing the number of nodes of a Directed Acyclic Graph (DAG). The nodes are numbered from 0 to n - 1 (inclusive).
    You are also given a 2D integer array edges, where edges[i] = [fromi, toi] denotes that there is a unidirectional edge from fromi to toi in the graph.
    Return a list answer, where answer[i] is the list of ancestors of the ith node, sorted in ascending order.
    A node u is an ancestor of another node v if u can reach v via a set of edges.
    
    Example 1:
    
    
    >>> getAncestors(n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]])
    >>> [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
    Explanation:
    The above diagram represents the input graph.
    - Nodes 0, 1, and 2 do not have any ancestors.
    - Node 3 has two ancestors 0 and 1.
    - Node 4 has two ancestors 0 and 2.
    - Node 5 has three ancestors 0, 1, and 3.
    - Node 6 has five ancestors 0, 1, 2, 3, and 4.
    - Node 7 has four ancestors 0, 1, 2, and 3.
    
    Example 2:
    
    
    >>> getAncestors(n = 5, edgeList = [[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]])
    >>> [[],[0],[0,1],[0,1,2],[0,1,2,3]]
    Explanation:
    The above diagram represents the input graph.
    - Node 0 does not have any ancestor.
    - Node 1 has one ancestor 0.
    - Node 2 has two ancestors 0 and 1.
    - Node 3 has three ancestors 0, 1, and 2.
    - Node 4 has four ancestors 0, 1, 2, and 3.
    """
def minMovesToMakePalindrome(s: str) -> int:
    """
    You are given a string s consisting only of lowercase English letters.
    In one move, you can select any two adjacent characters of s and swap them.
    Return the minimum number of moves needed to make s a palindrome.
    Note that the input will be generated such that s can always be converted to a palindrome.
    
    Example 1:
    
    >>> minMovesToMakePalindrome(s = "aabb")
    >>> 2
    Explanation:
    We can obtain two palindromes from s, "abba" and "baab".
    - We can obtain "abba" from s in 2 moves: "aabb" -> "abab" -> "abba".
    - We can obtain "baab" from s in 2 moves: "aabb" -> "abab" -> "baab".
    Thus, the minimum number of moves needed to make s a palindrome is 2.
    
    Example 2:
    
    >>> minMovesToMakePalindrome(s = "letelt")
    >>> 2
    Explanation:
    One of the palindromes we can obtain from s in 2 moves is "lettel".
    One of the ways we can obtain it is "letelt" -> "letetl" -> "lettel".
    Other palindromes such as "tleelt" can also be obtained in 2 moves.
    It can be shown that it is not possible to obtain a palindrome in less than 2 moves.
    """
def cellsInRange(s: str) -> List[str]:
    """
    A cell (r, c) of an excel sheet is represented as a string "" where:
    
     denotes the column number c of the cell. It is represented by alphabetical letters.
    
    
    For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.
    
    
     is the row number r of the cell. The rth row is represented by the integer r.
    
    You are given a string s in the format ":", where  represents the column c1,  represents the row r1,  represents the column c2, and  represents the row r2, such that r1 <= r2 and c1 <= c2.
    Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.
    
    Example 1:
    
    
    >>> cellsInRange(s = "K1:L2")
    >>> ["K1","K2","L1","L2"]
    Explanation:
    The above diagram shows the cells which should be present in the list.
    The red arrows denote the order in which the cells should be presented.
    
    Example 2:
    
    
    >>> cellsInRange(s = "A1:F1")
    >>> ["A1","B1","C1","D1","E1","F1"]
    Explanation:
    The above diagram shows the cells which should be present in the list.
    The red arrow denotes the order in which the cells should be presented.
    """
def minimalKSum(nums: List[int], k: int) -> int:
    """
    You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum.
    Return the sum of the k integers appended to nums.
    
    Example 1:
    
    >>> minimalKSum(nums = [1,4,25,10,25], k = 2)
    >>> 5
    Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3.
    The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
    The sum of the two integers appended is 2 + 3 = 5, so we return 5.
    Example 2:
    
    >>> minimalKSum(nums = [5,6], k = 6)
    >>> 25
    Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
    The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum.
    The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def createBinaryTree(descriptions: List[List[int]]) -> Optional[TreeNode]:
    """
    You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore,
    
    If isLefti == 1, then childi is the left child of parenti.
    If isLefti == 0, then childi is the right child of parenti.
    
    Construct the binary tree described by descriptions and return its root.
    The test cases will be generated such that the binary tree is valid.
    
    Example 1:
    
    
    >>> __init__(descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]])
    >>> [50,20,80,15,17,19]
    Explanation: The root node is the node with value 50 since it has no parent.
    The resulting binary tree is shown in the diagram.
    
    Example 2:
    
    
    >>> __init__(descriptions = [[1,2,1],[2,3,0],[3,4,1]])
    >>> [1,2,null,null,3,4]
    Explanation: The root node is the node with value 1 since it has no parent.
    The resulting binary tree is shown in the diagram.
    """
def replaceNonCoprimes(nums: List[int]) -> List[int]:
    """
    You are given an array of integers nums. Perform the following steps:
    
    Find any two adjacent numbers in nums that are non-coprime.
    If no such numbers are found, stop the process.
    Otherwise, delete the two numbers and replace them with their LCM (Least Common Multiple).
    Repeat this process as long as you keep finding two adjacent non-coprime numbers.
    
    Return the final modified array. It can be shown that replacing adjacent non-coprime numbers in any arbitrary order will lead to the same result.
    The test cases are generated such that the values in the final array are less than or equal to 108.
    Two values x and y are non-coprime if GCD(x, y) > 1 where GCD(x, y) is the Greatest Common Divisor of x and y.
    
    Example 1:
    
    >>> replaceNonCoprimes(nums = [6,4,3,2,7,6,2])
    >>> [12,7,6]
    Explanation:
    - (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [12,3,2,7,6,2].
    - (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [12,2,7,6,2].
    - (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [12,7,6,2].
    - (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,6].
    There are no more adjacent non-coprime numbers in nums.
    Thus, the final modified array is [12,7,6].
    Note that there are other ways to obtain the same resultant array.
    
    Example 2:
    
    >>> replaceNonCoprimes(nums = [2,2,1,1,3,3,3])
    >>> [2,1,1,3]
    Explanation:
    - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3,3].
    - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3].
    - (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [2,1,1,3].
    There are no more adjacent non-coprime numbers in nums.
    Thus, the final modified array is [2,1,1,3].
    Note that there are other ways to obtain the same resultant array.
    """
def singleDivisorTriplet(nums: List[int]) -> int:
    """
    You are given a 0-indexed array of positive integers nums. A triplet of three distinct indices (i, j, k) is called a single divisor triplet of nums if nums[i] + nums[j] + nums[k] is divisible by exactly one of nums[i], nums[j], or nums[k].
    Return the number of single divisor triplets of nums.
    
    Example 1:
    
    >>> singleDivisorTriplet(nums = [4,6,7,3,2])
    >>> 12
    Explanation:
    The triplets (0, 3, 4), (0, 4, 3), (3, 0, 4), (3, 4, 0), (4, 0, 3), and (4, 3, 0) have the values of [4, 3, 2] (or a permutation of [4, 3, 2]).
    4 + 3 + 2 = 9 which is only divisible by 3, so all such triplets are single divisor triplets.
    The triplets (0, 2, 3), (0, 3, 2), (2, 0, 3), (2, 3, 0), (3, 0, 2), and (3, 2, 0) have the values of [4, 7, 3] (or a permutation of [4, 7, 3]).
    4 + 7 + 3 = 14 which is only divisible by 7, so all such triplets are single divisor triplets.
    There are 12 single divisor triplets in total.
    
    Example 2:
    
    >>> singleDivisorTriplet(nums = [1,2,2])
    >>> 6
    Explanation:
    The triplets (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), and (2, 1, 0) have the values of [1, 2, 2] (or a permutation of [1, 2, 2]).
    1 + 2 + 2 = 5 which is only divisible by 1, so all such triplets are single divisor triplets.
    There are 6 single divisor triplets in total.
    
    Example 3:
    
    >>> singleDivisorTriplet(nums = [1,1,1])
    >>> 0
    Explanation:
    There are no single divisor triplets.
    Note that (0, 1, 2) is not a single divisor triplet because nums[0] + nums[1] + nums[2] = 3 and 3 is divisible by nums[0], nums[1], and nums[2].
    """
def findKDistantIndices(nums: List[int], key: int, k: int) -> List[int]:
    """
    You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key.
    Return a list of all k-distant indices sorted in increasing order.
    
    Example 1:
    
    >>> findKDistantIndices(nums = [3,4,9,1,3,9,5], key = 9, k = 1)
    >>> [1,2,3,4,5,6]
    Explanation: Here, nums[2] == key and nums[5] == key.
    - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index.
    - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
    - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
    - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
    - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
    - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
    - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
    Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
    
    Example 2:
    
    >>> findKDistantIndices(nums = [2,2,2,2,2], key = 2, k = 2)
    >>> [0,1,2,3,4]
    Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
    Hence, we return [0,1,2,3,4].
    """
def digArtifacts(n: int, artifacts: List[List[int]], dig: List[List[int]]) -> int:
    """
    There is an n x n 0-indexed grid with some artifacts buried in it. You are given the integer n and a 0-indexed 2D integer array artifacts describing the positions of the rectangular artifacts where artifacts[i] = [r1i, c1i, r2i, c2i] denotes that the ith artifact is buried in the subgrid where:
    
    (r1i, c1i) is the coordinate of the top-left cell of the ith artifact and
    (r2i, c2i) is the coordinate of the bottom-right cell of the ith artifact.
    
    You will excavate some cells of the grid and remove all the mud from them. If the cell has a part of an artifact buried underneath, it will be uncovered. If all the parts of an artifact are uncovered, you can extract it.
    Given a 0-indexed 2D integer array dig where dig[i] = [ri, ci] indicates that you will excavate the cell (ri, ci), return the number of artifacts that you can extract.
    The test cases are generated such that:
    
    No two artifacts overlap.
    Each artifact only covers at most 4 cells.
    The entries of dig are unique.
    
    
    Example 1:
    
    
    >>> digArtifacts(n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]])
    >>> 1
    Explanation:
    The different colors represent different artifacts. Excavated cells are labeled with a 'D' in the grid.
    There is 1 artifact that can be extracted, namely the red artifact.
    The blue artifact has one part in cell (1,1) which remains uncovered, so we cannot extract it.
    Thus, we return 1.
    
    Example 2:
    
    
    >>> digArtifacts(n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1],[1,1]])
    >>> 2
    Explanation: Both the red and blue artifacts have all parts uncovered (labeled with a 'D') and can be extracted, so we return 2.
    """
def maximumTop(nums: List[int], k: int) -> int:
    """
    You are given a 0-indexed integer array nums representing the contents of a pile, where nums[0] is the topmost element of the pile.
    In one move, you can perform either of the following:
    
    If the pile is not empty, remove the topmost element of the pile.
    If there are one or more removed elements, add any one of them back onto the pile. This element becomes the new topmost element.
    
    You are also given an integer k, which denotes the total number of moves to be made.
    Return the maximum value of the topmost element of the pile possible after exactly k moves. In case it is not possible to obtain a non-empty pile after k moves, return -1.
    
    Example 1:
    
    >>> maximumTop(nums = [5,2,2,4,0,6], k = 4)
    >>> 5
    Explanation:
    One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:
    - Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6].
    - Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6].
    - Step 3: Remove the topmost element = 2. The pile becomes [4,0,6].
    - Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].
    Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.
    
    Example 2:
    
    >>> maximumTop(nums = [2], k = 1)
    >>> -1
    Explanation:
    In the first move, our only option is to pop the topmost element of the pile.
    Since it is not possible to obtain a non-empty pile after one move, we return -1.
    """
def minimumWeight(n: int, edges: List[List[int]], src1: int, src2: int, dest: int) -> int:
    """
    You are given an integer n denoting the number of nodes of a weighted directed graph. The nodes are numbered from 0 to n - 1.
    You are also given a 2D integer array edges where edges[i] = [fromi, toi, weighti] denotes that there exists a directed edge from fromi to toi with weight weighti.
    Lastly, you are given three distinct integers src1, src2, and dest denoting three distinct nodes of the graph.
    Return the minimum weight of a subgraph of the graph such that it is possible to reach dest from both src1 and src2 via a set of edges of this subgraph. In case such a subgraph does not exist, return -1.
    A subgraph is a graph whose vertices and edges are subsets of the original graph. The weight of a subgraph is the sum of weights of its constituent edges.
    
    Example 1:
    
    
    >>> minimumWeight(n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5)
    >>> 9
    Explanation:
    The above figure represents the input graph.
    The blue edges represent one of the subgraphs that yield the optimal answer.
    Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.
    
    Example 2:
    
    
    >>> minimumWeight(n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2)
    >>> -1
    Explanation:
    The above figure represents the input graph.
    It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.
    """
def distanceToCycle(n: int, edges: List[List[int]]) -> List[int]:
    """
    You are given a positive integer n representing the number of nodes in a connected undirected graph containing exactly one cycle. The nodes are numbered from 0 to n - 1 (inclusive).
    You are also given a 2D integer array edges, where edges[i] = [node1i, node2i] denotes that there is a bidirectional edge connecting node1i and node2i in the graph.
    The distance between two nodes a and b is defined to be the minimum number of edges that are needed to go from a to b.
    Return an integer array answer of size n, where answer[i] is the minimum distance between the ith node and any node in the cycle.
    
    Example 1:
    
    
    >>> distanceToCycle(n = 7, edges = [[1,2],[2,4],[4,3],[3,1],[0,1],[5,2],[6,5]])
    >>> [1,0,0,0,0,1,2]
    Explanation:
    The nodes 1, 2, 3, and 4 form the cycle.
    The distance from 0 to 1 is 1.
    The distance from 1 to 1 is 0.
    The distance from 2 to 2 is 0.
    The distance from 3 to 3 is 0.
    The distance from 4 to 4 is 0.
    The distance from 5 to 2 is 1.
    The distance from 6 to 2 is 2.
    
    Example 2:
    
    
    >>> distanceToCycle(n = 9, edges = [[0,1],[1,2],[0,2],[2,6],[6,7],[6,8],[0,3],[3,4],[3,5]])
    >>> [0,0,0,1,2,2,1,2,2]
    Explanation:
    The nodes 0, 1, and 2 form the cycle.
    The distance from 0 to 0 is 0.
    The distance from 1 to 1 is 0.
    The distance from 2 to 2 is 0.
    The distance from 3 to 1 is 1.
    The distance from 4 to 1 is 2.
    The distance from 5 to 1 is 2.
    The distance from 6 to 2 is 1.
    The distance from 7 to 2 is 2.
    The distance from 8 to 2 is 2.
    """
def divideArray(nums: List[int]) -> bool:
    """
    You are given an integer array nums consisting of 2 * n integers.
    You need to divide nums into n pairs such that:
    
    Each element belongs to exactly one pair.
    The elements present in a pair are equal.
    
    Return true if nums can be divided into n pairs, otherwise return false.
    
    Example 1:
    
    >>> divideArray(nums = [3,2,3,2,2,2])
    >>> true
    Explanation:
    There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
    If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
    
    Example 2:
    
    >>> divideArray(nums = [1,2,3,4])
    >>> false
    Explanation:
    There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
    """