ShwStone/humaneval-leetcode huggingface dataset:
def minFallingPathSum(matrix: List[List[int]]) -> int:
    """
    Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix.
    A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
    
    Example 1:
    
    
    >>> minFallingPathSum(matrix = [[2,1,3],[6,5,4],[7,8,9]])
    >>> 13
    Explanation: There are two falling paths with a minimum sum as shown.
    
    Example 2:
    
    
    >>> minFallingPathSum(matrix = [[-19,57],[-40,-5]])
    >>> -59
    Explanation: The falling path with a minimum sum is shown.
    """
def beautifulArray(n: int) -> List[int]:
    """
    An array nums of length n is beautiful if:
    
    nums is a permutation of the integers in the range [1, n].
    For every 0 <= i < j < n, there is no index k with i < k < j where 2 * nums[k] == nums[i] + nums[j].
    
    Given the integer n, return any beautiful array nums of length n. There will be at least one valid answer for the given n.
    
    Example 1:
    >>> beautifulArray(n = 4)
    >>> [2,1,4,3]
    Example 2:
    >>> beautifulArray(n = 5)
    >>> [3,1,2,5,4]
    """
def shortestBridge(grid: List[List[int]]) -> int:
    """
    You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
    An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.
    You may change 0's to 1's to connect the two islands to form one island.
    Return the smallest number of 0's you must flip to connect the two islands.
    
    Example 1:
    
    >>> shortestBridge(grid = [[0,1],[1,0]])
    >>> 1
    
    Example 2:
    
    >>> shortestBridge(grid = [[0,1,0],[0,0,0],[0,0,1]])
    >>> 2
    
    Example 3:
    
    >>> shortestBridge(grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]])
    >>> 1
    """
def knightDialer(n: int) -> int:
    """
    The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram:
    A chess knight can move as indicated in the chess diagram below:
    
    We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
    
    Given an integer n, return how many distinct phone numbers of length n we can dial.
    You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
    As the answer may be very large, return the answer modulo 109 + 7.
    
    Example 1:
    
    >>> knightDialer(n = 1)
    >>> 10
    Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
    
    Example 2:
    
    >>> knightDialer(n = 2)
    >>> 20
    Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
    
    Example 3:
    
    >>> knightDialer(n = 3131)
    >>> 136006598
    Explanation: Please take care of the mod.
    """
def movesToStamp(stamp: str, target: str) -> List[int]:
    """
    You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'.
    In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp.
    
    For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:
    
    
    place stamp at index 0 of s to obtain "abc??",
    place stamp at index 1 of s to obtain "?abc?", or
    place stamp at index 2 of s to obtain "??abc".
    
    	Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s).
    
    We want to convert s to target using at most 10 * target.length turns.
    Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 * target.length turns, return an empty array.
    
    Example 1:
    
    >>> movesToStamp(stamp = "abc", target = "ababc")
    >>> [0,2]
    Explanation: Initially s = "?????".
    - Place stamp at index 0 to get "abc??".
    - Place stamp at index 2 to get "ababc".
    [1,0,2] would also be accepted as an answer, as well as some other answers.
    
    Example 2:
    
    >>> movesToStamp(stamp = "abca", target = "aabcaca")
    >>> [3,0,1]
    Explanation: Initially s = "???????".
    - Place stamp at index 3 to get "???abca".
    - Place stamp at index 0 to get "abcabca".
    - Place stamp at index 1 to get "aabcaca".
    """
def reorderLogFiles(logs: List[str]) -> List[str]:
    """
    You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
    There are two types of logs:
    
    Letter-logs: All words (except the identifier) consist of lowercase English letters.
    Digit-logs: All words (except the identifier) consist of digits.
    
    Reorder these logs so that:
    
    The letter-logs come before all digit-logs.
    The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
    The digit-logs maintain their relative ordering.
    
    Return the final order of the logs.
    
    Example 1:
    
    >>> reorderLogFiles(logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"])
    >>> ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
    Explanation:
    The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
    The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".
    
    Example 2:
    
    >>> reorderLogFiles(logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"])
    >>> ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rangeSumBST(root: Optional[TreeNode], low: int, high: int) -> int:
    """
    Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
    
    Example 1:
    
    
    >>> __init__(root = [10,5,15,3,7,null,18], low = 7, high = 15)
    >>> 32
    Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
    
    Example 2:
    
    
    >>> __init__(root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10)
    >>> 23
    Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
    """
def minAreaRect(points: List[List[int]]) -> int:
    """
    You are given an array of points in the X-Y plane points where points[i] = [xi, yi].
    Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.
    
    Example 1:
    
    
    >>> minAreaRect(points = [[1,1],[1,3],[3,1],[3,3],[2,2]])
    >>> 4
    
    Example 2:
    
    
    >>> minAreaRect(points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]])
    >>> 2
    """
def distinctSubseqII(s: str) -> int:
    """
    Given a string s, return the number of distinct non-empty subsequences of s. Since the answer may be very large, return it modulo 109 + 7.
    A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not.
    
    Example 1:
    
    >>> distinctSubseqII(s = "abc")
    >>> 7
    Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
    
    Example 2:
    
    >>> distinctSubseqII(s = "aba")
    >>> 6
    Explanation: The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
    
    Example 3:
    
    >>> distinctSubseqII(s = "aaa")
    >>> 3
    Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".
    """
def validMountainArray(arr: List[int]) -> bool:
    """
    Given an array of integers arr, return true if and only if it is a valid mountain array.
    Recall that arr is a mountain array if and only if:
    
    arr.length >= 3
    There exists some i with 0 < i < arr.length - 1 such that:
    
    arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    
    
    
    
    
    Example 1:
    >>> validMountainArray(arr = [2,1])
    >>> false
    Example 2:
    >>> validMountainArray(arr = [3,5,5])
    >>> false
    Example 3:
    >>> validMountainArray(arr = [0,3,2,1])
    >>> true
    """
def diStringMatch(s: str) -> List[int]:
    """
    A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:
    
    s[i] == 'I' if perm[i] < perm[i + 1], and
    s[i] == 'D' if perm[i] > perm[i + 1].
    
    Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
    
    Example 1:
    >>> diStringMatch(s = "IDID")
    >>> [0,4,1,3,2]
    Example 2:
    >>> diStringMatch(s = "III")
    >>> [0,1,2,3]
    Example 3:
    >>> diStringMatch(s = "DDI")
    >>> [3,2,0,1]
    """
def shortestSuperstring(words: List[str]) -> str:
    """
    Given an array of strings words, return the smallest string that contains each string in words as a substring. If there are multiple valid strings of the smallest length, return any of them.
    You may assume that no string in words is a substring of another string in words.
    
    Example 1:
    
    >>> shortestSuperstring(words = ["alex","loves","leetcode"])
    >>> "alexlovesleetcode"
    Explanation: All permutations of "alex","loves","leetcode" would also be accepted.
    
    Example 2:
    
    >>> shortestSuperstring(words = ["catg","ctaagt","gcta","ttca","atgcatc"])
    >>> "gctaagttcatgcatc"
    """
def minDeletionSize(strs: List[str]) -> int:
    """
    You are given an array of n strings strs, all of the same length.
    The strings can be arranged such that there is one on each line, making a grid.
    
    For example, strs = ["abc", "bce", "cae"] can be arranged as follows:
    
    
    abc
    bce
    cae
    
    You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted, while column 1 ('b', 'c', 'a') is not, so you would delete column 1.
    Return the number of columns that you will delete.
    
    Example 1:
    
    >>> minDeletionSize(strs = ["cba","daf","ghi"])
    >>> 1
    Explanation: The grid looks as follows:
      cba
      daf
      ghi
    Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
    
    Example 2:
    
    >>> minDeletionSize(strs = ["a","b"])
    >>> 0
    Explanation: The grid looks as follows:
      a
      b
    Column 0 is the only column and is sorted, so you will not delete any columns.
    
    Example 3:
    
    >>> minDeletionSize(strs = ["zyx","wvu","tsr"])
    >>> 3
    Explanation: The grid looks as follows:
      zyx
      wvu
      tsr
    All 3 columns are not sorted, so you will delete all 3.
    """
def minIncrementForUnique(nums: List[int]) -> int:
    """
    You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.
    Return the minimum number of moves to make every value in nums unique.
    The test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    >>> minIncrementForUnique(nums = [1,2,2])
    >>> 1
    Explanation: After 1 move, the array could be [1, 2, 3].
    
    Example 2:
    
    >>> minIncrementForUnique(nums = [3,2,1,2,1,7])
    >>> 6
    Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
    It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
    """
def validateStackSequences(pushed: List[int], popped: List[int]) -> bool:
    """
    Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
    
    Example 1:
    
    >>> validateStackSequences(pushed = [1,2,3,4,5], popped = [4,5,3,2,1])
    >>> true
    Explanation: We might do the following sequence:
    push(1), push(2), push(3), push(4),
    pop() -> 4,
    push(5),
    pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
    
    Example 2:
    
    >>> validateStackSequences(pushed = [1,2,3,4,5], popped = [4,3,5,1,2])
    >>> false
    Explanation: 1 cannot be popped before 2.
    """
def removeStones(stones: List[List[int]]) -> int:
    """
    On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
    A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
    Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.
    
    Example 1:
    
    >>> removeStones(stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]])
    >>> 5
    Explanation: One way to remove 5 stones is as follows:
    1. Remove stone [2,2] because it shares the same row as [2,1].
    2. Remove stone [2,1] because it shares the same column as [0,1].
    3. Remove stone [1,2] because it shares the same row as [1,0].
    4. Remove stone [1,0] because it shares the same column as [0,0].
    5. Remove stone [0,1] because it shares the same row as [0,0].
    Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
    
    Example 2:
    
    >>> removeStones(stones = [[0,0],[0,2],[1,1],[2,0],[2,2]])
    >>> 3
    Explanation: One way to make 3 moves is as follows:
    1. Remove stone [2,2] because it shares the same row as [2,0].
    2. Remove stone [2,0] because it shares the same column as [0,0].
    3. Remove stone [0,2] because it shares the same row as [0,0].
    Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
    
    Example 3:
    
    >>> removeStones(stones = [[0,0]])
    >>> 0
    Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
    """
def bagOfTokensScore(tokens: List[int], power: int) -> int:
    """
    You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] denotes the value of tokeni.
    Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):
    
    Face-up: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
    Face-down: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.
    
    Return the maximum possible score you can achieve after playing any number of tokens.
    
    Example 1:
    
    >>> bagOfTokensScore(tokens = [100], power = 50)
    >>> 0
    Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).
    
    Example 2:
    
    >>> bagOfTokensScore(tokens = [200,100], power = 150)
    >>> 1
    Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.
    There is no need to play token0, since you cannot play it face-up to add to your score. The maximum score achievable is 1.
    
    Example 3:
    
    >>> bagOfTokensScore(tokens = [100,200,300,400], power = 200)
    >>> 2
    Explanation: Play the tokens in this order to get a score of 2:
    
    Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
    Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
    Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
    Play token2 (300) face-up, reducing power to 0 and increasing score to 2.
    
    The maximum score achievable is 2.
    """
def largestTimeFromDigits(arr: List[int]) -> str:
    """
    Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.
    24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
    Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.
    
    Example 1:
    
    >>> largestTimeFromDigits(arr = [1,2,3,4])
    >>> "23:41"
    Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.
    
    Example 2:
    
    >>> largestTimeFromDigits(arr = [5,5,5,5])
    >>> ""
    Explanation: There are no valid 24-hour times as "55:55" is not valid.
    """
def deckRevealedIncreasing(deck: List[int]) -> List[int]:
    """
    You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].
    You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
    You will do the following steps repeatedly until all cards are revealed:
    
    Take the top card of the deck, reveal it, and take it out of the deck.
    If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
    If there are still unrevealed cards, go back to step 1. Otherwise, stop.
    
    Return an ordering of the deck that would reveal the cards in increasing order.
    Note that the first entry in the answer is considered to be the top of the deck.
    
    Example 1:
    
    >>> deckRevealedIncreasing(deck = [17,13,11,2,3,5,7])
    >>> [2,13,3,11,5,17,7]
    Explanation:
    We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
    After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
    We reveal 2, and move 13 to the bottom.  The deck is now [3,11,5,17,7,13].
    We reveal 3, and move 11 to the bottom.  The deck is now [5,17,7,13,11].
    We reveal 5, and move 17 to the bottom.  The deck is now [7,13,11,17].
    We reveal 7, and move 13 to the bottom.  The deck is now [11,17,13].
    We reveal 11, and move 17 to the bottom.  The deck is now [13,17].
    We reveal 13, and move 17 to the bottom.  The deck is now [17].
    We reveal 17.
    Since all the cards revealed are in increasing order, the answer is correct.
    
    Example 2:
    
    >>> deckRevealedIncreasing(deck = [1,1000])
    >>> [1,1000]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flipEquiv(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    """
    For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
    A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
    Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise.
    
    Example 1:
    
    
    >>> __init__(root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7])
    >>> true
    Explanation: We flipped at nodes with values 1, 3, and 5.
    
    Example 2:
    
    >>> __init__(root1 = [], root2 = [])
    >>> true
    
    Example 3:
    
    >>> __init__(root1 = [], root2 = [1])
    >>> false
    """
def largestComponentSize(nums: List[int]) -> int:
    """
    You are given an integer array of unique positive integers nums. Consider the following graph:
    
    There are nums.length nodes, labeled nums[0] to nums[nums.length - 1],
    There is an undirected edge between nums[i] and nums[j] if nums[i] and nums[j] share a common factor greater than 1.
    
    Return the size of the largest connected component in the graph.
    
    Example 1:
    
    
    >>> largestComponentSize(nums = [4,6,15,35])
    >>> 4
    
    Example 2:
    
    
    >>> largestComponentSize(nums = [20,50,9,63])
    >>> 2
    
    Example 3:
    
    
    >>> largestComponentSize(nums = [2,3,6,7,4,12,21,39])
    >>> 8
    """
def isAlienSorted(words: List[str], order: str) -> bool:
    """
    In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
    Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.
    
    Example 1:
    
    >>> isAlienSorted(words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz")
    >>> true
    Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
    
    Example 2:
    
    >>> isAlienSorted(words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz")
    >>> false
    Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
    
    Example 3:
    
    >>> isAlienSorted(words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz")
    >>> false
    Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
    """
def canReorderDoubled(arr: List[int]) -> bool:
    """
    Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.
    
    Example 1:
    
    >>> canReorderDoubled(arr = [3,1,3,6])
    >>> false
    
    Example 2:
    
    >>> canReorderDoubled(arr = [2,1,2,6])
    >>> false
    
    Example 3:
    
    >>> canReorderDoubled(arr = [4,-2,2,-4])
    >>> true
    Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
    """