ShwStone/humaneval-leetcode huggingface dataset:
def distributeCandies(candyType: List[int]) -> int:
    """
    Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.
    The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.
    Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them.
    
    Example 1:
    
    >>> distributeCandies(candyType = [1,1,2,2,3,3])
    >>> 3
    Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
    
    Example 2:
    
    >>> distributeCandies(candyType = [1,1,2,3])
    >>> 2
    Explanation: Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.
    
    Example 3:
    
    >>> distributeCandies(candyType = [6,6,6,6])
    >>> 1
    Explanation: Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
    """
def findPaths(m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
    """
    There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.
    Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> findPaths(m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0)
    >>> 6
    
    Example 2:
    
    
    >>> findPaths(m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1)
    >>> 12
    """
def findUnsortedSubarray(nums: List[int]) -> int:
    """
    Given an integer array nums, you need to find one continuous subarray such that if you only sort this subarray in non-decreasing order, then the whole array will be sorted in non-decreasing order.
    Return the shortest such subarray and output its length.
    
    Example 1:
    
    >>> findUnsortedSubarray(nums = [2,6,4,8,10,9,15])
    >>> 5
    Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
    
    Example 2:
    
    >>> findUnsortedSubarray(nums = [1,2,3,4])
    >>> 0
    
    Example 3:
    
    >>> findUnsortedSubarray(nums = [1])
    >>> 0
    """
def killProcess(pid: List[int], ppid: List[int], kill: int) -> List[int]:
    """
    You have n processes forming a rooted tree structure. You are given two integer arrays pid and ppid, where pid[i] is the ID of the ith process and ppid[i] is the ID of the ith process's parent process.
    Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree).
    When a process is killed, all of its children processes will also be killed.
    Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.
    
    Example 1:
    
    
    >>> killProcess(pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5)
    >>> [5,10]
    Explanation: The processes colored in red are the processes that should be killed.
    
    Example 2:
    
    >>> killProcess(pid = [1], ppid = [0], kill = 1)
    >>> [1]
    """
def minDistance(word1: str, word2: str) -> int:
    """
    Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.
    In one step, you can delete exactly one character in either string.
    
    Example 1:
    
    >>> minDistance(word1 = "sea", word2 = "eat")
    >>> 2
    Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
    
    Example 2:
    
    >>> minDistance(word1 = "leetcode", word2 = "etco")
    >>> 4
    """
def outerTrees(trees: List[List[int]]) -> List[List[int]]:
    """
    You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden.
    Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if all the trees are enclosed.
    Return the coordinates of trees that are exactly located on the fence perimeter. You may return the answer in any order.
    
    Example 1:
    
    
    >>> outerTrees(trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]])
    >>> [[1,1],[2,0],[4,2],[3,3],[2,4]]
    Explanation: All the trees will be on the perimeter of the fence except the tree at [2, 2], which will be inside the fence.
    
    Example 2:
    
    
    >>> outerTrees(trees = [[1,2],[2,2],[4,2]])
    >>> [[4,2],[2,2],[1,2]]
    Explanation: The fence forms a line that passes through all the trees.
    """
def isValid(code: str) -> bool:
    """
    Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
    A code snippet is valid if all the following rules hold:
    
    The code must be wrapped in a valid closed tag. Otherwise, the code is invalid.
    A closed tag (not necessarily valid) has exactly the following format : TAG_CONTENT. Among them,  is the start tag, and  is the end tag. The TAG_NAME in start and end tags should be the same. A closed tag is valid if and only if the TAG_NAME and TAG_CONTENT are valid.
    A valid TAG_NAME only contain upper-case letters, and has length in range [1,9]. Otherwise, the TAG_NAME is invalid.
    A valid TAG_CONTENT may contain other valid closed tags, cdata and any characters (see note1) EXCEPT unmatched <, unmatched start and end tag, and unmatched or closed tags with invalid TAG_NAME. Otherwise, the TAG_CONTENT is invalid.
    A start tag is unmatched if no end tag exists with the same TAG_NAME, and vice versa. However, you also need to consider the issue of unbalanced when tags are nested.
    A < is unmatched if you cannot find a subsequent >. And when you find a < or , all the subsequent characters until the next > should be parsed as TAG_NAME (not necessarily valid).
    The cdata has the following format : CDATA_CONTENT. The range of CDATA_CONTENT is defined as the characters between </code> and the <b>first subsequent</b> <code>.
    CDATA_CONTENT may contain any characters. The function of cdata is to forbid the validator to parse CDATA_CONTENT, so even it has some characters that can be parsed as tag (no matter valid or invalid), you should treat it as regular characters.
    
    
    Example 1:
    
    >>> isValid(code = "This is the first line <div>")
    >>> true
    Explanation:
    The code is wrapped in a closed tag :  and .
    The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
    Although CDATA_CONTENT has an unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as a tag.
    So TAG_CONTENT is valid, and then the code is valid. Thus return true.
    
    Example 2:
    
    >>> isValid(code = ">>  ![cdata[]] <div>]>]]>>]")
    >>> true
    Explanation:
    We first separate the code into : start_tag|tag_content|end_tag.
    start_tag -> ""
    end_tag -> ""
    tag_content could also be separated into : text1|cdata|text2.
    text1 -> ">>  ![cdata[]] "
    cdata -> "<div>]>", where the CDATA_CONTENT is "]>"
    text2 -> "]]>>]"
    The reason why start_tag is NOT ">>" is because of the rule 6.
    The reason why cdata is NOT "<div>]>]]>" is because of the rule 7.
    
    Example 3:
    
    >>> isValid(code = "      ")
    >>> false
    Explanation: Unbalanced. If "" is closed, then "" must be unmatched, and vice versa.
    """
def fractionAddition(expression: str) -> str:
    """
    Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.
    The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.
    
    Example 1:
    
    >>> fractionAddition(expression = "-1/2+1/2")
    >>> "0/1"
    
    Example 2:
    
    >>> fractionAddition(expression = "-1/2+1/2+1/3")
    >>> "1/3"
    
    Example 3:
    
    >>> fractionAddition(expression = "1/3-1/2")
    >>> "-1/6"
    """
def validSquare(p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
    """
    Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square.
    The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
    A valid square has four equal sides with positive length and four equal angles (90-degree angles).
    
    Example 1:
    
    >>> validSquare(p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1])
    >>> true
    
    Example 2:
    
    >>> validSquare(p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12])
    >>> false
    
    Example 3:
    
    >>> validSquare(p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1])
    >>> true
    """
def findLHS(nums: List[int]) -> int:
    """
    We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.
    Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.
    
    Example 1:
    
    >>> findLHS(nums = [1,3,2,2,5,2,3,7])
    >>> 5
    Explanation:
    The longest harmonious subsequence is [3,2,2,2,3].
    
    Example 2:
    
    >>> findLHS(nums = [1,2,3,4])
    >>> 2
    Explanation:
    The longest harmonious subsequences are [1,2], [2,3], and [3,4], all of which have a length of 2.
    
    Example 3:
    
    >>> findLHS(nums = [1,1,1,1])
    >>> 0
    Explanation:
    No harmonic subsequence exists.
    """
def maxCount(m: int, n: int, ops: List[List[int]]) -> int:
    """
    You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i] = [ai, bi] means M[x][y] should be incremented by one for all 0 <= x < ai and 0 <= y < bi.
    Count and return the number of maximum integers in the matrix after performing all the operations.
    
    Example 1:
    
    
    >>> maxCount(m = 3, n = 3, ops = [[2,2],[3,3]])
    >>> 4
    Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.
    
    Example 2:
    
    >>> maxCount(m = 3, n = 3, ops = [[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3]])
    >>> 4
    
    Example 3:
    
    >>> maxCount(m = 3, n = 3, ops = [])
    >>> 9
    """
def findRestaurant(list1: List[str], list2: List[str]) -> List[str]:
    """
    Given two arrays of strings list1 and list2, find the common strings with the least index sum.
    A common string is a string that appeared in both list1 and list2.
    A common string with the least index sum is a common string such that if it appeared at list1[i] and list2[j] then i + j should be the minimum value among all the other common strings.
    Return all the common strings with the least index sum. Return the answer in any order.
    
    Example 1:
    
    >>> findRestaurant(list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"])
    >>> ["Shogun"]
    Explanation: The only common string is "Shogun".
    
    Example 2:
    
    >>> findRestaurant(list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"])
    >>> ["Shogun"]
    Explanation: The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
    
    Example 3:
    
    >>> findRestaurant(list1 = ["happy","sad","good"], list2 = ["sad","happy","good"])
    >>> ["sad","happy"]
    Explanation: There are three common strings:
    "happy" with index sum = (0 + 1) = 1.
    "sad" with index sum = (1 + 0) = 1.
    "good" with index sum = (2 + 2) = 4.
    The strings with the least index sum are "sad" and "happy".
    """
def findIntegers(n: int) -> int:
    """
    Given a positive integer n, return the number of the integers in the range [0, n] whose binary representations do not contain consecutive ones.
    
    Example 1:
    
    >>> findIntegers(n = 5)
    >>> 5
    Explanation:
    Here are the non-negative integers <= 5 with their corresponding binary representations:
    0 : 0
    1 : 1
    2 : 10
    3 : 11
    4 : 100
    5 : 101
    Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
    
    Example 2:
    
    >>> findIntegers(n = 1)
    >>> 2
    
    Example 3:
    
    >>> findIntegers(n = 2)
    >>> 3
    """
def canPlaceFlowers(flowerbed: List[int], n: int) -> bool:
    """
    You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
    Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
    
    Example 1:
    >>> canPlaceFlowers(flowerbed = [1,0,0,0,1], n = 1)
    >>> true
    Example 2:
    >>> canPlaceFlowers(flowerbed = [1,0,0,0,1], n = 2)
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def tree2str(root: Optional[TreeNode]) -> str:
    """
    Given the root node of a binary tree, your task is to create a string representation of the tree following a specific set of formatting rules. The representation should be based on a preorder traversal of the binary tree and must adhere to the following guidelines:
    
    
    Node Representation: Each node in the tree should be represented by its integer value.
    
    
    Parentheses for Children: If a node has at least one child (either left or right), its children should be represented inside parentheses. Specifically:
    
    If a node has a left child, the value of the left child should be enclosed in parentheses immediately following the node's value.
    If a node has a right child, the value of the right child should also be enclosed in parentheses. The parentheses for the right child should follow those of the left child.
    
    
    
    Omitting Empty Parentheses: Any empty parentheses pairs (i.e., ()) should be omitted from the final string representation of the tree, with one specific exception: when a node has a right child but no left child. In such cases, you must include an empty pair of parentheses to indicate the absence of the left child. This ensures that the one-to-one mapping between the string representation and the original binary tree structure is maintained.
    In summary, empty parentheses pairs should be omitted when a node has only a left child or no children. However, when a node has a right child but no left child, an empty pair of parentheses must precede the representation of the right child to reflect the tree's structure accurately.
    
    
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4])
    >>> "1(2(4))(3)"
    Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the empty parenthesis pairs. And it will be "1(2(4))(3)".
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3,null,4])
    >>> "1(2()(4))(3)"
    Explanation: Almost the same as the first example, except the () after 2 is necessary to indicate the absence of a left child for 2 and the presence of a right child.
    """
def findDuplicate(paths: List[str]) -> List[List[str]]:
    """
    Given a list paths of directory info, including the directory path, and all the files with contents in this directory, return all the duplicate files in the file system in terms of their paths. You may return the answer in any order.
    A group of duplicate files consists of at least two files that have the same content.
    A single directory info string in the input list has the following format:
    
    "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
    
    It means there are n files (f1.txt, f2.txt ... fn.txt) with content (f1_content, f2_content ... fn_content) respectively in the directory "root/d1/d2/.../dm". Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.
    The output is a list of groups of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:
    
    "directory_path/file_name.txt"
    
    
    Example 1:
    >>> findDuplicate(paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"])
    >>> [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
    Example 2:
    >>> findDuplicate(paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"])
    >>> [["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]]
    """
def triangleNumber(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
    
    Example 1:
    
    >>> triangleNumber(nums = [2,2,3,4])
    >>> 3
    Explanation: Valid combinations are:
    2,3,4 (using the first 2)
    2,3,4 (using the second 2)
    2,2,3
    
    Example 2:
    
    >>> triangleNumber(nums = [4,2,3,4])
    >>> 4
    """
def addBoldTag(s: str, words: List[str]) -> str:
    """
    You are given a string s and an array of strings words.
    You should add a closed pair of bold tag  and  to wrap the substrings in s that exist in words.
    
    If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag.
    If two substrings wrapped by bold tags are consecutive, you should combine them.
    
    Return s after adding the bold tags.
    
    Example 1:
    
    >>> addBoldTag(s = "abcxyz123", words = ["abc","123"])
    >>> "abcxyz123"
    Explanation: The two strings of words are substrings of s as following: "abcxyz123".
    We add  before each substring and  after each substring.
    
    Example 2:
    
    >>> addBoldTag(s = "aaabbb", words = ["aa","b"])
    >>> "aaabbb"
    Explanation:
    "aa" appears as a substring two times: "aaabbb" and "aaabbb".
    "b" appears as a substring three times: "aaabbb", "aaabbb", and "aaabbb".
    We add  before each substring and  after each substring: "aaabbb".
    Since the first two 's overlap, we merge them: "aaabbb".
    Since now the four 's are consecutive, we merge them: "aaabbb".
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def mergeTrees(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    You are given two binary trees root1 and root2.
    Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
    Return the merged tree.
    Note: The merging process must start from the root nodes of both trees.
    
    Example 1:
    
    
    >>> __init__(root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7])
    >>> [3,4,5,5,4,null,7]
    
    Example 2:
    
    >>> __init__(root1 = [1], root2 = [1,2])
    >>> [2,2]
    """
def leastInterval(tasks: List[str], n: int) -> int:
    """
    You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of at least n intervals between two tasks with the same label.
    Return the minimum number of CPU intervals required to complete all tasks.
    
    Example 1:
    
    >>> leastInterval(tasks = ["A","A","A","B","B","B"], n = 2)
    >>> 8
    Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.
    After completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th interval, you can do A again as 2 intervals have passed.
    
    Example 2:
    
    >>> leastInterval(tasks = ["A","C","A","B","D","B"], n = 1)
    >>> 6
    Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.
    With a cooling interval of 1, you can repeat a task after just one other task.
    
    Example 3:
    
    >>> leastInterval(tasks = ["A","A","A", "B","B","B"], n = 3)
    >>> 10
    Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.
    There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def addOneRow(root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
    """
    Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.
    Note that the root node is at depth 1.
    The adding rule is:
    
    Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
    cur's original left subtree should be the left subtree of the new left subtree root.
    cur's original right subtree should be the right subtree of the new right subtree root.
    If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
    
    
    Example 1:
    
    
    >>> __init__(root = [4,2,6,3,1,5], val = 1, depth = 2)
    >>> [4,1,1,2,null,null,6,3,1,5]
    
    Example 2:
    
    
    >>> __init__(root = [4,2,null,3,1], val = 1, depth = 3)
    >>> [4,2,null,1,1,3,null,null,1]
    """
def maxDistance(arrays: List[List[int]]) -> int:
    """
    You are given m arrays, where each array is sorted in ascending order.
    You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a - b|.
    Return the maximum distance.
    
    Example 1:
    
    >>> maxDistance(arrays = [[1,2,3],[4,5],[1,2,3]])
    >>> 4
    Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
    
    Example 2:
    
    >>> maxDistance(arrays = [[1],[1]])
    >>> 0
    """
def smallestFactorization(num: int) -> int:
    """
    Given a positive integer num, return the smallest positive integer x whose multiplication of each digit equals num. If there is no answer or the answer is not fit in 32-bit signed integer, return 0.
    
    Example 1:
    >>> smallestFactorization(num = 48)
    >>> 68
    Example 2:
    >>> smallestFactorization(num = 15)
    >>> 35
    """