ShwStone/humaneval-leetcode huggingface dataset:
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findFrequentTreeSum(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.
    The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
    
    Example 1:
    
    
    >>> __init__(root = [5,2,-3])
    >>> [2,-3,4]
    
    Example 2:
    
    
    >>> __init__(root = [5,2,-5])
    >>> [2]
    """
def fib(n: int) -> int:
    """
    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
    
    F(0) = 0, F(1) = 1
    F(n) = F(n - 1) + F(n - 2), for n > 1.
    
    Given n, calculate F(n).
    
    Example 1:
    
    >>> fib(n = 2)
    >>> 1
    Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
    
    Example 2:
    
    >>> fib(n = 3)
    >>> 2
    Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
    
    Example 3:
    
    >>> fib(n = 4)
    >>> 3
    Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findBottomLeftValue(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the leftmost value in the last row of the tree.
    
    Example 1:
    
    
    >>> __init__(root = [2,1,3])
    >>> 1
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3,4,null,5,6,null,null,7])
    >>> 7
    """
def findRotateSteps(ring: str, key: str) -> int:
    """
    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring" and use the dial to spell a specific keyword to open the door.
    Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.
    Initially, the first character of the ring is aligned at the "12:00" direction. You should spell all the characters in key one by one by rotating ring clockwise or anticlockwise to make each character of the string key aligned at the "12:00" direction and then by pressing the center button.
    At the stage of rotating the ring to spell the key character key[i]:
    
    You can rotate the ring clockwise or anticlockwise by one place, which counts as one step. The final purpose of the rotation is to align one of ring's characters at the "12:00" direction, where this character must equal key[i].
    If the character key[i] has been aligned at the "12:00" direction, press the center button to spell, which also counts as one step. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
    
    
    Example 1:
    
    
    >>> findRotateSteps(ring = "godding", key = "gd")
    >>> 4
    Explanation:
    For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
    For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
    Also, we need 1 more step for spelling.
    So the final output is 4.
    
    Example 2:
    
    >>> findRotateSteps(ring = "godding", key = "godding")
    >>> 13
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def largestValues(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
    
    Example 1:
    
    
    >>> __init__(root = [1,3,2,5,3,null,9])
    >>> [1,3,9]
    
    Example 2:
    
    >>> __init__(root = [1,2,3])
    >>> [1,3]
    """
def longestPalindromeSubseq(s: str) -> int:
    """
    Given a string s, find the longest palindromic subsequence's length in s.
    A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
    
    Example 1:
    
    >>> longestPalindromeSubseq(s = "bbbab")
    >>> 4
    Explanation: One possible longest palindromic subsequence is "bbbb".
    
    Example 2:
    
    >>> longestPalindromeSubseq(s = "cbbd")
    >>> 2
    Explanation: One possible longest palindromic subsequence is "bb".
    """
def findMinMoves(machines: List[int]) -> int:
    """
    You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
    For each move, you could choose any m (1 <= m <= n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
    Given an integer array machines representing the number of dresses in each washing machine from left to right on the line, return the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
    
    Example 1:
    
    >>> findMinMoves(machines = [1,0,5])
    >>> 3
    Explanation:
    1st move:    1     0 <-- 5    =>    1     1     4
    2nd move:    1 <-- 1 <-- 4    =>    2     1     3
    3rd move:    2     1 <-- 3    =>    2     2     2
    
    Example 2:
    
    >>> findMinMoves(machines = [0,3,0])
    >>> 2
    Explanation:
    1st move:    0 <-- 3     0    =>    1     2     0
    2nd move:    1     2 --> 0    =>    1     1     1
    
    Example 3:
    
    >>> findMinMoves(machines = [0,2,0])
    >>> -1
    Explanation:
    It's impossible to make all three washing machines have the same number of dresses.
    """
def change(amount: int, coins: List[int]) -> int:
    """
    You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
    Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.
    You may assume that you have an infinite number of each kind of coin.
    The answer is guaranteed to fit into a signed 32-bit integer.
    
    Example 1:
    
    >>> change(amount = 5, coins = [1,2,5])
    >>> 4
    Explanation: there are four ways to make up the amount:
    5=5
    5=2+2+1
    5=2+1+1+1
    5=1+1+1+1+1
    
    Example 2:
    
    >>> change(amount = 3, coins = [2])
    >>> 0
    Explanation: the amount of 3 cannot be made up just with coins of 2.
    
    Example 3:
    
    >>> change(amount = 10, coins = [10])
    >>> 1
    """
def detectCapitalUse(word: str) -> bool:
    """
    We define the usage of capitals in a word to be right when one of the following cases holds:
    
    All letters in this word are capitals, like "USA".
    All letters in this word are not capitals, like "leetcode".
    Only the first letter in this word is capital, like "Google".
    
    Given a string word, return true if the usage of capitals in it is right.
    
    Example 1:
    >>> detectCapitalUse(word = "USA")
    >>> true
    Example 2:
    >>> detectCapitalUse(word = "FlaG")
    >>> false
    """
def findLUSlength(a: str, b: str) -> int:
    """
    Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If no such uncommon subsequence exists, return -1.
    An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.
    
    Example 1:
    
    >>> findLUSlength(a = "aba", b = "cdc")
    >>> 3
    Explanation: One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc".
    Note that "cdc" is also a longest uncommon subsequence.
    
    Example 2:
    
    >>> findLUSlength(a = "aaa", b = "bbb")
    >>> 3
    Explanation: The longest uncommon subsequences are "aaa" and "bbb".
    
    Example 3:
    
    >>> findLUSlength(a = "aaa", b = "aaa")
    >>> -1
    Explanation: Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a. So the answer would be -1.
    """
def findLUSlength(strs: List[str]) -> int:
    """
    Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.
    An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
    A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
    
    For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).
    
    
    Example 1:
    >>> findLUSlength(strs = ["aba","cdc","eae"])
    >>> 3
    Example 2:
    >>> findLUSlength(strs = ["aaa","aaa","aa"])
    >>> -1
    """
def checkSubarraySum(nums: List[int], k: int) -> bool:
    """
    Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.
    A good subarray is a subarray where:
    
    its length is at least two, and
    the sum of the elements of the subarray is a multiple of k.
    
    Note that:
    
    A subarray is a contiguous part of the array.
    An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
    
    
    Example 1:
    
    >>> checkSubarraySum(nums = [23,2,4,6,7], k = 6)
    >>> true
    Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
    
    Example 2:
    
    >>> checkSubarraySum(nums = [23,2,6,4,7], k = 6)
    >>> true
    Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
    42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
    
    Example 3:
    
    >>> checkSubarraySum(nums = [23,2,6,4,7], k = 13)
    >>> false
    """
def findLongestWord(s: str, dictionary: List[str]) -> str:
    """
    Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
    
    Example 1:
    
    >>> findLongestWord(s = "abpcplea", dictionary = ["ale","apple","monkey","plea"])
    >>> "apple"
    
    Example 2:
    
    >>> findLongestWord(s = "abpcplea", dictionary = ["a","b","c"])
    >>> "a"
    """
def findMaxLength(nums: List[int]) -> int:
    """
    Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
    
    Example 1:
    
    >>> findMaxLength(nums = [0,1])
    >>> 2
    Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
    
    Example 2:
    
    >>> findMaxLength(nums = [0,1,0])
    >>> 2
    Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
    
    Example 3:
    
    >>> findMaxLength(nums = [0,1,1,1,1,1,0,0,0])
    >>> 6
    Explanation: [1,1,1,0,0,0] is the longest contiguous subarray with equal number of 0 and 1.
    """
def countArrangement(n: int) -> int:
    """
    Suppose you have n integers labeled 1 through n. A permutation of those n integers perm (1-indexed) is considered a beautiful arrangement if for every i (1 <= i <= n), either of the following is true:
    
    perm[i] is divisible by i.
    i is divisible by perm[i].
    
    Given an integer n, return the number of the beautiful arrangements that you can construct.
    
    Example 1:
    
    >>> countArrangement(n = 2)
    >>> 2
    Explanation:
    The first beautiful arrangement is [1,2]:
        - perm[1] = 1 is divisible by i = 1
        - perm[2] = 2 is divisible by i = 2
    The second beautiful arrangement is [2,1]:
        - perm[1] = 2 is divisible by i = 1
        - i = 2 is divisible by perm[2] = 1
    
    Example 2:
    
    >>> countArrangement(n = 1)
    >>> 1
    """
def wordsAbbreviation(words: List[str]) -> List[str]:
    """
    Given an array of distinct strings words, return the minimal possible abbreviations for every word.
    The following are the rules for a string abbreviation:
    
    The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
    If more than one word shares the same abbreviation, then perform the following operation:
    
    Increase the prefix (characters in the first part) of each of their abbreviations by 1.
    
    For example, say you start with the words ["abcdef","abndef"] both initially abbreviated as "a4f". Then, a sequence of operations would be ["a4f","a4f"] -> ["ab3f","ab3f"] -> ["abc2f","abn2f"].
    
    
    This operation is repeated until every abbreviation is unique.
    
    
    At the end, if an abbreviation did not make a word shorter, then keep it as the original word.
    
    
    Example 1:
    >>> wordsAbbreviation(words = ["like","god","internal","me","internet","interval","intension","face","intrusion"])
    >>> ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
    Example 2:
    >>> wordsAbbreviation(words = ["aa","aaa"])
    >>> ["aa","aaa"]
    """
def updateBoard(board: List[List[str]], click: List[int]) -> List[List[str]]:
    """
    Let's play the minesweeper game (Wikipedia, online game)!
    You are given an m x n char matrix board representing the game board where:
    
    'M' represents an unrevealed mine,
    'E' represents an unrevealed empty square,
    'B' represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
    digit ('1' to '8') represents how many mines are adjacent to this revealed square, and
    'X' represents a revealed mine.
    
    You are also given an integer array click where click = [clickr, clickc] represents the next click position among all the unrevealed squares ('M' or 'E').
    Return the board after revealing this position according to the following rules:
    
    If a mine 'M' is revealed, then the game is over. You should change it to 'X'.
    If an empty square 'E' with no adjacent mines is revealed, then change it to a revealed blank 'B' and all of its adjacent unrevealed squares should be revealed recursively.
    If an empty square 'E' with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
    Return the board when no more squares will be revealed.
    
    
    Example 1:
    
    
    >>> updateBoard(board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0])
    >>> [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
    
    Example 2:
    
    
    >>> updateBoard(board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2])
    >>> [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def getMinimumDifference(root: Optional[TreeNode]) -> int:
    """
    Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
    
    Example 1:
    
    
    >>> __init__(root = [4,2,6,1,3])
    >>> 1
    
    Example 2:
    
    
    >>> __init__(root = [1,0,48,null,null,12,49])
    >>> 1
    """
def findLonelyPixel(picture: List[List[str]]) -> int:
    """
    Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of black lonely pixels.
    A black lonely pixel is a character 'B' that located at a specific position where the same row and same column don't have any other black pixels.
    
    Example 1:
    
    
    >>> findLonelyPixel(picture = [["W","W","B"],["W","B","W"],["B","W","W"]])
    >>> 3
    Explanation: All the three 'B's are black lonely pixels.
    
    Example 2:
    
    
    >>> findLonelyPixel(picture = [["B","B","B"],["B","B","W"],["B","B","B"]])
    >>> 0
    """
def findPairs(nums: List[int], k: int) -> int:
    """
    Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.
    A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:
    
    0 <= i, j < nums.length
    i != j
    |nums[i] - nums[j]| == k
    
    Notice that |val| denotes the absolute value of val.
    
    Example 1:
    
    >>> findPairs(nums = [3,1,4,1,5], k = 2)
    >>> 2
    Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
    Although we have two 1s in the input, we should only return the number of unique pairs.
    
    Example 2:
    
    >>> findPairs(nums = [1,2,3,4,5], k = 1)
    >>> 4
    Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
    
    Example 3:
    
    >>> findPairs(nums = [1,3,1,5,4], k = 0)
    >>> 1
    Explanation: There is one 0-diff pair in the array, (1, 1).
    """
def findBlackPixel(picture: List[List[str]], target: int) -> int:
    """
    Given an m x n picture consisting of black 'B' and white 'W' pixels and an integer target, return the number of black lonely pixels.
    A black lonely pixel is a character 'B' that located at a specific position (r, c) where:
    
    Row r and column c both contain exactly target black pixels.
    For all rows that have a black pixel at column c, they should be exactly the same as row r.
    
    
    Example 1:
    
    
    >>> findBlackPixel(picture = [["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","W","B","W","B","W"]], target = 3)
    >>> 6
    Explanation: All the green 'B' are the black pixels we need (all 'B's at column 1 and 3).
    Take 'B' at row r = 0 and column c = 1 as an example:
     - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels.
     - Rule 2, the rows have black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.
    
    Example 2:
    
    
    >>> findBlackPixel(picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1)
    >>> 0
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def str2tree(s: str) -> Optional[TreeNode]:
    """
    You need to construct a binary tree from a string consisting of parenthesis and integers.
    The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
    You always start to construct the left child node of the parent first if it exists.
    
    Example 1:
    
    
    >>> __init__(s = "4(2(3)(1))(6(5))")
    >>> [4,2,6,3,1,5]
    
    Example 2:
    
    >>> __init__(s = "4(2(3)(1))(6(5)(7))")
    >>> [4,2,6,3,1,5,7]
    
    Example 3:
    
    >>> __init__(s = "-4(2(3)(1))(6(5)(7))")
    >>> [-4,2,6,3,1,5,7]
    """
def complexNumberMultiply(num1: str, num2: str) -> str:
    """
    A complex number can be represented as a string on the form "real+imaginaryi" where:
    
    real is the real part and is an integer in the range [-100, 100].
    imaginary is the imaginary part and is an integer in the range [-100, 100].
    i2 == -1.
    
    Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
    
    Example 1:
    
    >>> complexNumberMultiply(num1 = "1+1i", num2 = "1+1i")
    >>> "0+2i"
    Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
    
    Example 2:
    
    >>> complexNumberMultiply(num1 = "1+-1i", num2 = "1+-1i")
    >>> "0+-2i"
    Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def convertBST(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.
    As a reminder, a binary search tree is a tree that satisfies these constraints:
    
    The left subtree of a node contains only nodes with keys less than the node's key.
    The right subtree of a node contains only nodes with keys greater than the node's key.
    Both the left and right subtrees must also be binary search trees.
    
    
    Example 1:
    
    
    >>> __init__(root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8])
    >>> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
    
    Example 2:
    
    >>> __init__(root = [0,null,1])
    >>> [1,null,1]
    """
def findMinDifference(timePoints: List[str]) -> int:
    """
    Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
    
    Example 1:
    >>> findMinDifference(timePoints = ["23:59","00:00"])
    >>> 1
    Example 2:
    >>> findMinDifference(timePoints = ["00:00","23:59","00:00"])
    >>> 0
    """
def singleNonDuplicate(nums: List[int]) -> int:
    """
    You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
    Return the single element that appears only once.
    Your solution must run in O(log n) time and O(1) space.
    
    Example 1:
    >>> singleNonDuplicate(nums = [1,1,2,3,3,4,4,8,8])
    >>> 2
    Example 2:
    >>> singleNonDuplicate(nums = [3,3,7,7,10,11,11])
    >>> 10
    """
def reverseStr(s: str, k: int) -> str:
    """
    Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
    If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
    
    Example 1:
    >>> reverseStr(s = "abcdefg", k = 2)
    >>> "bacdfeg"
    Example 2:
    >>> reverseStr(s = "abcd", k = 2)
    >>> "bacd"
    """
def updateMatrix(mat: List[List[int]]) -> List[List[int]]:
    """
    Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
    The distance between two cells sharing a common edge is 1.
    
    Example 1:
    
    
    >>> updateMatrix(mat = [[0,0,0],[0,1,0],[0,0,0]])
    >>> [[0,0,0],[0,1,0],[0,0,0]]
    
    Example 2:
    
    
    >>> updateMatrix(mat = [[0,0,0],[0,1,0],[1,1,1]])
    >>> [[0,0,0],[0,1,0],[1,2,1]]
    """