ShwStone/humaneval-leetcode huggingface dataset:
def wallsAndGates(rooms: List[List[int]]) -> None:
        """
        Do not return anything, modify rooms in-place instead.
        """
    """
    You are given an m x n grid rooms initialized with these three possible values.
    
    -1 A wall or an obstacle.
    0 A gate.
    INF Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
    
    Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
    
    Example 1:
    
    
    >>> wallsAndGates(rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]])
    >>> [[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
    
    Example 2:
    
    >>> wallsAndGates(rooms = [[-1]])
    >>> [[-1]]
    """
def findDuplicate(nums: List[int]) -> int:
    """
    Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
    There is only one repeated number in nums, return this repeated number.
    You must solve the problem without modifying the array nums and using only constant extra space.
    
    Example 1:
    
    >>> findDuplicate(nums = [1,3,4,2,2])
    >>> 2
    
    Example 2:
    
    >>> findDuplicate(nums = [3,1,3,4,2])
    >>> 3
    
    Example 3:
    
    >>> findDuplicate(nums = [3,3,3,3,3])
    >>> 3
    """
def gameOfLife(board: List[List[int]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
    """
    According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
    The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
    
    Any live cell with fewer than two live neighbors dies as if caused by under-population.
    Any live cell with two or three live neighbors lives on to the next generation.
    Any live cell with more than three live neighbors dies, as if by over-population.
    Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
    
    The next state of the board is determined by applying the above rules simultaneously to every cell in the current state of the m x n grid board. In this process, births and deaths occur simultaneously.
    Given the current state of the board, update the board to reflect its next state.
    Note that you do not need to return anything.
    
    Example 1:
    
    
    >>> gameOfLife(board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]])
    >>> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
    
    Example 2:
    
    
    >>> gameOfLife(board = [[1,1],[1,0]])
    >>> [[1,1],[1,1]]
    """
def wordPattern(pattern: str, s: str) -> bool:
    """
    Given a pattern and a string s, find if s follows the same pattern.
    Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Specifically:
    
    Each letter in pattern maps to exactly one unique word in s.
    Each unique word in s maps to exactly one letter in pattern.
    No two letters map to the same word, and no two words map to the same letter.
    
    
    Example 1:
    
    >>> wordPattern(pattern = "abba", s = "dog cat cat dog")
    >>> true
    Explanation:
    The bijection can be established as:
    
    'a' maps to "dog".
    'b' maps to "cat".
    
    
    Example 2:
    
    >>> wordPattern(pattern = "abba", s = "dog cat cat fish")
    >>> false
    
    Example 3:
    
    >>> wordPattern(pattern = "aaaa", s = "dog cat cat dog")
    >>> false
    """
def wordPatternMatch(pattern: str, s: str) -> bool:
    """
    Given a pattern and a string s, return true if s matches the pattern.
    A string s matches a pattern if there is some bijective mapping of single characters to non-empty strings such that if each character in pattern is replaced by the string it maps to, then the resulting string is s. A bijective mapping means that no two characters map to the same string, and no character maps to two different strings.
    
    Example 1:
    
    >>> wordPatternMatch(pattern = "abab", s = "redblueredblue")
    >>> true
    Explanation: One possible mapping is as follows:
    'a' -> "red"
    'b' -> "blue"
    Example 2:
    
    >>> wordPatternMatch(pattern = "aaaa", s = "asdasdasdasd")
    >>> true
    Explanation: One possible mapping is as follows:
    'a' -> "asd"
    
    Example 3:
    
    >>> wordPatternMatch(pattern = "aabb", s = "xyzabcxzyabc")
    >>> false
    """
def canWinNim(n: int) -> bool:
    """
    You are playing the following Nim Game with your friend:
    
    Initially, there is a heap of stones on the table.
    You and your friend will alternate taking turns, and you go first.
    On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
    The one who removes the last stone is the winner.
    
    Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false.
    
    Example 1:
    
    >>> canWinNim(n = 4)
    >>> false
    Explanation: These are the possible outcomes:
    1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
    2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
    3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
    In all outcomes, your friend wins.
    
    Example 2:
    
    >>> canWinNim(n = 1)
    >>> true
    
    Example 3:
    
    >>> canWinNim(n = 2)
    >>> true
    """
def generatePossibleNextMoves(currentState: str) -> List[str]:
    """
    You are playing a Flip Game with your friend.
    You are given a string currentState that contains only '+' and '-'. You and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move, and therefore the other person will be the winner.
    Return all possible states of the string currentState after one valid move. You may return the answer in any order. If there is no valid move, return an empty list [].
    
    Example 1:
    
    >>> generatePossibleNextMoves(currentState = "++++")
    >>> ["--++","+--+","++--"]
    
    Example 2:
    
    >>> generatePossibleNextMoves(currentState = "+")
    >>> []
    """
def canWin(currentState: str) -> bool:
    """
    You are playing a Flip Game with your friend.
    You are given a string currentState that contains only '+' and '-'. You and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move, and therefore the other person will be the winner.
    Return true if the starting player can guarantee a win, and false otherwise.
    
    Example 1:
    
    >>> canWin(currentState = "++++")
    >>> true
    Explanation: The starting player can guarantee a win by flipping the middle "++" to become "+--+".
    
    Example 2:
    
    >>> canWin(currentState = "+")
    >>> false
    """
def minTotalDistance(grid: List[List[int]]) -> int:
    """
    Given an m x n binary grid grid where each 1 marks the home of one friend, return the minimal total travel distance.
    The total travel distance is the sum of the distances between the houses of the friends and the meeting point.
    The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
    
    Example 1:
    
    
    >>> minTotalDistance(grid = [[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]])
    >>> 6
    Explanation: Given three friends living at (0,0), (0,4), and (2,2).
    The point (0,2) is an ideal meeting point, as the total travel distance of 2 + 2 + 2 = 6 is minimal.
    So return 6.
    
    Example 2:
    
    >>> minTotalDistance(grid = [[1,1]])
    >>> 1
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def longestConsecutive(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the length of the longest consecutive sequence path.
    A consecutive sequence path is a path where the values increase by one along the path.
    Note that the path can start at any node in the tree, and you cannot go from a node to its parent in the path.
    
    Example 1:
    
    
    >>> __init__(root = [1,null,3,2,4,null,null,null,5])
    >>> 3
    Explanation: Longest consecutive sequence path is 3-4-5, so return 3.
    
    Example 2:
    
    
    >>> __init__(root = [2,null,3,2,null,1])
    >>> 2
    Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.
    """
def getHint(secret: str, guess: str) -> str:
    """
    You are playing the Bulls and Cows game with your friend.
    You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:
    
    The number of "bulls", which are digits in the guess that are in the correct position.
    The number of "cows", which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
    
    Given the secret number secret and your friend's guess guess, return the hint for your friend's guess.
    The hint should be formatted as "xAyB", where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.
    
    Example 1:
    
    >>> getHint(secret = "1807", guess = "7810")
    >>> "1A3B"
    Explanation: Bulls are connected with a '|' and cows are underlined:
    "1807"
      |
    "7810"
    Example 2:
    
    >>> getHint(secret = "1123", guess = "0111")
    >>> "1A1B"
    Explanation: Bulls are connected with a '|' and cows are underlined:
    "1123"        "1123"
      |      or     |
    "0111"        "0111"
    Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
    """
def lengthOfLIS(nums: List[int]) -> int:
    """
    Given an integer array nums, return the length of the longest strictly increasing subsequence.
    
    Example 1:
    
    >>> lengthOfLIS(nums = [10,9,2,5,3,7,101,18])
    >>> 4
    Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
    
    Example 2:
    
    >>> lengthOfLIS(nums = [0,1,0,3,2,3])
    >>> 4
    
    Example 3:
    
    >>> lengthOfLIS(nums = [7,7,7,7,7,7,7])
    >>> 1
    """
def minArea(image: List[List[str]], x: int, y: int) -> int:
    """
    You are given an m x n binary matrix image where 0 represents a white pixel and 1 represents a black pixel.
    The black pixels are connected (i.e., there is only one black region). Pixels are connected horizontally and vertically.
    Given two integers x and y that represents the location of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.
    You must write an algorithm with less than O(mn) runtime complexity
    
    Example 1:
    
    
    >>> minArea(image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2)
    >>> 6
    
    Example 2:
    
    >>> minArea(image = [["1"]], x = 0, y = 0)
    >>> 1
    """
def numIslands2(m: int, n: int, positions: List[List[int]]) -> List[int]:
    """
    You are given an empty 2D binary grid grid of size m x n. The grid represents a map where 0's represent water and 1's represent land. Initially, all the cells of grid are water cells (i.e., all the cells are 0's).
    We may perform an add land operation which turns the water at position into a land. You are given an array positions where positions[i] = [ri, ci] is the position (ri, ci) at which we should operate the ith operation.
    Return an array of integers answer where answer[i] is the number of islands after turning the cell (ri, ci) into a land.
    An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
    
    Example 1:
    
    
    >>> numIslands2(m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]])
    >>> [1,1,2,3]
    Explanation:
    Initially, the 2d grid is filled with water.
    - Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land. We have 1 island.
    - Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land. We still have 1 island.
    - Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land. We have 2 islands.
    - Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land. We have 3 islands.
    
    Example 2:
    
    >>> numIslands2(m = 1, n = 1, positions = [[0,0]])
    >>> [1]
    """
def isAdditiveNumber(num: str) -> bool:
    """
    An additive number is a string whose digits can form an additive sequence.
    A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.
    Given a string containing only digits, return true if it is an additive number or false otherwise.
    Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
    
    Example 1:
    
    >>> isAdditiveNumber("112358")
    >>> true
    Explanation:
    The digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
    1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
    
    Example 2:
    
    >>> isAdditiveNumber("199100199")
    >>> true
    Explanation:
    The additive sequence is: 1, 99, 100, 199.
    1 + 99 = 100, 99 + 100 = 199
    """
def maxProfit(prices: List[int]) -> int:
    """
    You are given an array prices where prices[i] is the price of a given stock on the ith day.
    Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
    
    After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
    
    Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
    
    Example 1:
    
    >>> maxProfit(prices = [1,2,3,0,2])
    >>> 3
    Explanation: transactions = [buy, sell, cooldown, buy, sell]
    
    Example 2:
    
    >>> maxProfit(prices = [1])
    >>> 0
    """
def findMinHeightTrees(n: int, edges: List[List[int]]) -> List[int]:
    """
    A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
    Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h))  are called minimum height trees (MHTs).
    Return a list of all MHTs' root labels. You can return the answer in any order.
    The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
    
    Example 1:
    
    
    >>> findMinHeightTrees(n = 4, edges = [[1,0],[1,2],[1,3]])
    >>> [1]
    Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
    
    Example 2:
    
    
    >>> findMinHeightTrees(n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]])
    >>> [3,4]
    """
def multiply(mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
    """
    Given two sparse matrices mat1 of size m x k and mat2 of size k x n, return the result of mat1 x mat2. You may assume that multiplication is always possible.
    
    Example 1:
    
    
    >>> multiply(mat1 = [[1,0,0],[-1,0,3]], mat2 = [[7,0,0],[0,0,0],[0,0,1]])
    >>> [[7,0,0],[-7,0,3]]
    
    Example 2:
    
    >>> multiply(mat1 = [[0]], mat2 = [[0]])
    >>> [[0]]
    """
def maxCoins(nums: List[int]) -> int:
    """
    You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.
    If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.
    Return the maximum coins you can collect by bursting the balloons wisely.
    
    Example 1:
    
    >>> maxCoins(nums = [3,1,5,8])
    >>> 167
    Explanation:
    nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
    coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167
    Example 2:
    
    >>> maxCoins(nums = [1,5])
    >>> 10
    """
def nthSuperUglyNumber(n: int, primes: List[int]) -> int:
    """
    A super ugly number is a positive integer whose prime factors are in the array primes.
    Given an integer n and an array of integers primes, return the nth super ugly number.
    The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
    
    Example 1:
    
    >>> nthSuperUglyNumber(n = 12, primes = [2,7,13,19])
    >>> 32
    Explanation: [1,2,4,7,8,13,14,16,19,26,28,32] is the sequence of the first 12 super ugly numbers given primes = [2,7,13,19].
    
    Example 2:
    
    >>> nthSuperUglyNumber(n = 1, primes = [2,3,5])
    >>> 1
    Explanation: 1 has no prime factors, therefore all of its prime factors are in the array primes = [2,3,5].
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def verticalOrder(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, return the vertical order traversal of its nodes' values. (i.e., from top to bottom, column by column).
    If two nodes are in the same row and column, the order should be from left to right.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [[9],[3,15],[20],[7]]
    
    Example 2:
    
    
    >>> __init__(root = [3,9,8,4,0,1,7])
    >>> [[4],[9],[3,0,1],[8],[7]]
    
    Example 3:
    
    
    >>> __init__(root = [1,2,3,4,10,9,11,null,5,null,null,null,null,null,null,null,6])
    >>> [[4],[2,5],[1,10,9,6],[3],[11]]
    """
def countSmaller(nums: List[int]) -> List[int]:
    """
    Given an integer array nums, return an integer array counts where counts[i] is the number of smaller elements to the right of nums[i].
    
    Example 1:
    
    >>> countSmaller(nums = [5,2,6,1])
    >>> [2,1,1,0]
    Explanation:
    To the right of 5 there are 2 smaller elements (2 and 1).
    To the right of 2 there is only 1 smaller element (1).
    To the right of 6 there is 1 smaller element (1).
    To the right of 1 there is 0 smaller element.
    
    Example 2:
    
    >>> countSmaller(nums = [-1])
    >>> [0]
    
    Example 3:
    
    >>> countSmaller(nums = [-1,-1])
    >>> [0,0]
    """
def removeDuplicateLetters(s: str) -> str:
    """
    Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
    
    Example 1:
    
    >>> removeDuplicateLetters(s = "bcabc")
    >>> "abc"
    
    Example 2:
    
    >>> removeDuplicateLetters(s = "cbacdcbc")
    >>> "acdb"
    """
def shortestDistance(grid: List[List[int]]) -> int:
    """
    You are given an m x n grid grid of values 0, 1, or 2, where:
    
    each 0 marks an empty land that you can pass by freely,
    each 1 marks a building that you cannot pass through, and
    each 2 marks an obstacle that you cannot pass through.
    
    You want to build a house on an empty land that reaches all buildings in the shortest total travel distance. You can only move up, down, left, and right.
    Return the shortest travel distance for such a house. If it is not possible to build such a house according to the above rules, return -1.
    The total travel distance is the sum of the distances between the houses of the friends and the meeting point.
    The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
    
    Example 1:
    
    
    >>> shortestDistance(grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]])
    >>> 7
    Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2).
    The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal.
    So return 7.
    
    Example 2:
    
    >>> shortestDistance(grid = [[1,0]])
    >>> 1
    
    Example 3:
    
    >>> shortestDistance(grid = [[1]])
    >>> -1
    """
def maxProduct(words: List[str]) -> int:
    """
    Given a string array words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0.
    
    Example 1:
    
    >>> maxProduct(words = ["abcw","baz","foo","bar","xtfn","abcdef"])
    >>> 16
    Explanation: The two words can be "abcw", "xtfn".
    
    Example 2:
    
    >>> maxProduct(words = ["a","ab","abc","d","cd","bcd","abcd"])
    >>> 4
    Explanation: The two words can be "ab", "cd".
    
    Example 3:
    
    >>> maxProduct(words = ["a","aa","aaa","aaaa"])
    >>> 0
    Explanation: No such pair of words.
    """
def bulbSwitch(n: int) -> int:
    """
    There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.
    On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
    Return the number of bulbs that are on after n rounds.
    
    Example 1:
    
    
    >>> bulbSwitch(n = 3)
    >>> 1
    Explanation: At first, the three bulbs are [off, off, off].
    After the first round, the three bulbs are [on, on, on].
    After the second round, the three bulbs are [on, off, on].
    After the third round, the three bulbs are [on, off, off].
    So you should return 1 because there is only one bulb is on.
    Example 2:
    
    >>> bulbSwitch(n = 0)
    >>> 0
    
    Example 3:
    
    >>> bulbSwitch(n = 1)
    >>> 1
    """
def generateAbbreviations(word: str) -> List[str]:
    """
    A word's generalized abbreviation can be constructed by taking any number of non-overlapping and non-adjacent substrings and replacing them with their respective lengths.
    
    For example, "abcde" can be abbreviated into:
    
    
    "a3e" ("bcd" turned into "3")
    "1bcd1" ("a" and "e" both turned into "1")
    "5" ("abcde" turned into "5")
    "abcde" (no substrings replaced)
    
    
    However, these abbreviations are invalid:
    
    "23" ("ab" turned into "2" and "cde" turned into "3") is invalid as the substrings chosen are adjacent.
    "22de" ("ab" turned into "2" and "bc" turned into "2") is invalid as the substring chosen overlap.
    
    
    
    Given a string word, return a list of all the possible generalized abbreviations of word. Return the answer in any order.
    
    Example 1:
    >>> generateAbbreviations(word = "word")
    >>> ["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]
    Example 2:
    >>> generateAbbreviations(word = "a")
    >>> ["1","a"]
    """