ShwStone/humaneval-leetcode huggingface dataset:
def cherryPickup(grid: List[List[int]]) -> int:
    """
    You are given an n x n grid representing a field of cherries, each cell is one of three possible integers.
    
    0 means the cell is empty, so you can pass through,
    1 means the cell contains a cherry that you can pick up and pass through, or
    -1 means the cell contains a thorn that blocks your way.
    
    Return the maximum number of cherries you can collect by following the rules below:
    
    Starting at the position (0, 0) and reaching (n - 1, n - 1) by moving right or down through valid path cells (cells with value 0 or 1).
    After reaching (n - 1, n - 1), returning to (0, 0) by moving left or up through valid path cells.
    When passing through a path cell containing a cherry, you pick it up, and the cell becomes an empty cell 0.
    If there is no valid path between (0, 0) and (n - 1, n - 1), then no cherries can be collected.
    
    
    Example 1:
    
    
    >>> cherryPickup(grid = [[0,1,-1],[1,0,-1],[1,1,1]])
    >>> 5
    Explanation: The player started at (0, 0) and went down, down, right right to reach (2, 2).
    4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
    Then, the player went left, up, up, left to return home, picking up one more cherry.
    The total number of cherries picked up is 5, and this is the maximum possible.
    
    Example 2:
    
    >>> cherryPickup(grid = [[1,1,-1],[1,-1,1],[-1,1,1]])
    >>> 0
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findClosestLeaf(root: Optional[TreeNode], k: int) -> int:
    """
    Given the root of a binary tree where every node has a unique value and a target integer k, return the value of the nearest leaf node to the target k in the tree.
    Nearest to a leaf means the least number of edges traveled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.
    
    Example 1:
    
    
    >>> __init__(root = [1,3,2], k = 1)
    >>> 2
    Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
    
    Example 2:
    
    
    >>> __init__(root = [1], k = 1)
    >>> 1
    Explanation: The nearest leaf node is the root node itself.
    
    Example 3:
    
    
    >>> __init__(root = [1,2,3,4,null,null,null,5,null,6], k = 2)
    >>> 3
    Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.
    """
def networkDelayTime(times: List[List[int]], n: int, k: int) -> int:
    """
    You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target.
    We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.
    
    Example 1:
    
    
    >>> networkDelayTime(times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2)
    >>> 2
    
    Example 2:
    
    >>> networkDelayTime(times = [[1,2,1]], n = 2, k = 1)
    >>> 1
    
    Example 3:
    
    >>> networkDelayTime(times = [[1,2,1]], n = 2, k = 2)
    >>> -1
    """
def nextGreatestLetter(letters: List[str], target: str) -> str:
    """
    You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
    Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
    
    Example 1:
    
    >>> nextGreatestLetter(letters = ["c","f","j"], target = "a")
    >>> "c"
    Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
    
    Example 2:
    
    >>> nextGreatestLetter(letters = ["c","f","j"], target = "c")
    >>> "f"
    Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
    
    Example 3:
    
    >>> nextGreatestLetter(letters = ["x","x","y","y"], target = "z")
    >>> "x"
    Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
    """
def minCostClimbingStairs(cost: List[int]) -> int:
    """
    You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
    You can either start from the step with index 0, or the step with index 1.
    Return the minimum cost to reach the top of the floor.
    
    Example 1:
    
    >>> minCostClimbingStairs(cost = [10,15,20])
    >>> 15
    Explanation: You will start at index 1.
    - Pay 15 and climb two steps to reach the top.
    The total cost is 15.
    
    Example 2:
    
    >>> minCostClimbingStairs(cost = [1,100,1,1,1,100,1,1,100,1])
    >>> 6
    Explanation: You will start at index 0.
    - Pay 1 and climb two steps to reach index 2.
    - Pay 1 and climb two steps to reach index 4.
    - Pay 1 and climb two steps to reach index 6.
    - Pay 1 and climb one step to reach index 7.
    - Pay 1 and climb two steps to reach index 9.
    - Pay 1 and climb one step to reach the top.
    The total cost is 6.
    """
def dominantIndex(nums: List[int]) -> int:
    """
    You are given an integer array nums where the largest integer is unique.
    Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise.
    
    Example 1:
    
    >>> dominantIndex(nums = [3,6,1,0])
    >>> 1
    Explanation: 6 is the largest integer.
    For every other number in the array x, 6 is at least twice as big as x.
    The index of value 6 is 1, so we return 1.
    
    Example 2:
    
    >>> dominantIndex(nums = [1,2,3,4])
    >>> -1
    Explanation: 4 is less than twice the value of 3, so we return -1.
    """
def shortestCompletingWord(licensePlate: str, words: List[str]) -> str:
    """
    Given a string licensePlate and an array of strings words, find the shortest completing word in words.
    A completing word is a word that contains all the letters in licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more than once in licensePlate, then it must appear in the word the same number of times or more.
    For example, if licensePlate = "aBc 12c", then it contains letters 'a', 'b' (ignoring case), and 'c' twice. Possible completing words are "abccdef", "caaacab", and "cbca".
    Return the shortest completing word in words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the first one that occurs in words.
    
    Example 1:
    
    >>> shortestCompletingWord(licensePlate = "1s3 PSt", words = ["step","steps","stripe","stepple"])
    >>> "steps"
    Explanation: licensePlate contains letters 's', 'p', 's' (ignoring case), and 't'.
    "step" contains 't' and 'p', but only contains 1 's'.
    "steps" contains 't', 'p', and both 's' characters.
    "stripe" is missing an 's'.
    "stepple" is missing an 's'.
    Since "steps" is the only word containing all the letters, that is the answer.
    
    Example 2:
    
    >>> shortestCompletingWord(licensePlate = "1s3 456", words = ["looks","pest","stew","show"])
    >>> "pest"
    Explanation: licensePlate only contains the letter 's'. All the words contain 's', but among these "pest", "stew", and "show" are shortest. The answer is "pest" because it is the word that appears earliest of the 3.
    """
def containVirus(isInfected: List[List[int]]) -> int:
    """
    A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.
    The world is modeled as an m x n binary grid isInfected, where isInfected[i][j] == 0 represents uninfected cells, and isInfected[i][j] == 1 represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary.
    Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region (i.e., the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night). There will never be a tie.
    Return the number of walls used to quarantine all the infected regions. If the world will become fully infected, return the number of walls used.
    
    Example 1:
    
    
    >>> containVirus(isInfected = [[0,1,0,0,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0]])
    >>> 10
    Explanation: There are 2 contaminated regions.
    On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is:
    
    On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
    
    
    Example 2:
    
    
    >>> containVirus(isInfected = [[1,1,1],[1,0,1],[1,1,1]])
    >>> 4
    Explanation: Even though there is only one cell saved, there are 4 walls built.
    Notice that walls are only built on the shared boundary of two different cells.
    
    Example 3:
    
    >>> containVirus(isInfected = [[1,1,1,0,0,0,0,0,0],[1,0,1,0,1,1,1,1,1],[1,1,1,0,0,0,0,0,0]])
    >>> 13
    Explanation: The region on the left only builds two new walls.
    """
def countCornerRectangles(grid: List[List[int]]) -> int:
    """
    Given an m x n integer matrix grid where each entry is only 0 or 1, return the number of corner rectangles.
    A corner rectangle is four distinct 1's on the grid that forms an axis-aligned rectangle. Note that only the corners need to have the value 1. Also, all four 1's used must be distinct.
    
    Example 1:
    
    
    >>> countCornerRectangles(grid = [[1,0,0,1,0],[0,0,1,0,1],[0,0,0,1,0],[1,0,1,0,1]])
    >>> 1
    Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].
    
    Example 2:
    
    
    >>> countCornerRectangles(grid = [[1,1,1],[1,1,1],[1,1,1]])
    >>> 9
    Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
    
    Example 3:
    
    
    >>> countCornerRectangles(grid = [[1,1,1,1]])
    >>> 0
    Explanation: Rectangles must have four distinct corners.
    """
def ipToCIDR(ip: str, n: int) -> List[str]:
    """
    An IP address is a formatted 32-bit unsigned integer where each group of 8 bits is printed as a decimal number and the dot character '.' splits the groups.
    
    For example, the binary number 00001111 10001000 11111111 01101011 (spaces added for clarity) formatted as an IP address would be "15.136.255.107".
    
    A CIDR block is a format used to denote a specific set of IP addresses. It is a string consisting of a base IP address, followed by a slash, followed by a prefix length k. The addresses it covers are all the IPs whose first k bits are the same as the base IP address.
    
    For example, "123.45.67.89/20" is a CIDR block with a prefix length of 20. Any IP address whose binary representation matches 01111011 00101101 0100xxxx xxxxxxxx, where x can be either 0 or 1, is in the set covered by the CIDR block.
    
    You are given a start IP address ip and the number of IP addresses we need to cover n. Your goal is to use as few CIDR blocks as possible to cover all the IP addresses in the inclusive range [ip, ip + n - 1] exactly. No other IP addresses outside of the range should be covered.
    Return the shortest list of CIDR blocks that covers the range of IP addresses. If there are multiple answers, return any of them.
    
    Example 1:
    
    >>> ipToCIDR(ip = "255.0.0.7", n = 10)
    >>> ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"]
    Explanation:
    The IP addresses that need to be covered are:
    - 255.0.0.7  -> 11111111 00000000 00000000 00000111
    - 255.0.0.8  -> 11111111 00000000 00000000 00001000
    - 255.0.0.9  -> 11111111 00000000 00000000 00001001
    - 255.0.0.10 -> 11111111 00000000 00000000 00001010
    - 255.0.0.11 -> 11111111 00000000 00000000 00001011
    - 255.0.0.12 -> 11111111 00000000 00000000 00001100
    - 255.0.0.13 -> 11111111 00000000 00000000 00001101
    - 255.0.0.14 -> 11111111 00000000 00000000 00001110
    - 255.0.0.15 -> 11111111 00000000 00000000 00001111
    - 255.0.0.16 -> 11111111 00000000 00000000 00010000
    The CIDR block "255.0.0.7/32" covers the first address.
    The CIDR block "255.0.0.8/29" covers the middle 8 addresses (binary format of 11111111 00000000 00000000 00001xxx).
    The CIDR block "255.0.0.16/32" covers the last address.
    Note that while the CIDR block "255.0.0.0/28" does cover all the addresses, it also includes addresses outside of the range, so we cannot use it.
    
    Example 2:
    
    >>> ipToCIDR(ip = "117.145.102.62", n = 8)
    >>> ["117.145.102.62/31","117.145.102.64/30","117.145.102.68/31"]
    """
def openLock(deadends: List[str], target: str) -> int:
    """
    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.
    The lock initially starts at '0000', a string representing the state of the 4 wheels.
    You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
    Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
    
    Example 1:
    
    >>> openLock(deadends = ["0201","0101","0102","1212","2002"], target = "0202")
    >>> 6
    Explanation:
    A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
    Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
    because the wheels of the lock become stuck after the display becomes the dead end "0102".
    
    Example 2:
    
    >>> openLock(deadends = ["8888"], target = "0009")
    >>> 1
    Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
    
    Example 3:
    
    >>> openLock(deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888")
    >>> -1
    Explanation: We cannot reach the target without getting stuck.
    """
def crackSafe(n: int, k: int) -> str:
    """
    There is a safe protected by a password. The password is a sequence of n digits where each digit can be in the range [0, k - 1].
    The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the most recent n digits that were entered each time you type a digit.
    
    For example, the correct password is "345" and you enter in "012345":
    
    
    After typing 0, the most recent 3 digits is "0", which is incorrect.
    After typing 1, the most recent 3 digits is "01", which is incorrect.
    After typing 2, the most recent 3 digits is "012", which is incorrect.
    After typing 3, the most recent 3 digits is "123", which is incorrect.
    After typing 4, the most recent 3 digits is "234", which is incorrect.
    After typing 5, the most recent 3 digits is "345", which is correct and the safe unlocks.
    
    
    
    Return any string of minimum length that will unlock the safe at some point of entering it.
    
    Example 1:
    
    >>> crackSafe(n = 1, k = 2)
    >>> "10"
    Explanation: The password is a single digit, so enter each digit. "01" would also unlock the safe.
    
    Example 2:
    
    >>> crackSafe(n = 2, k = 2)
    >>> "01100"
    Explanation: For each possible password:
    - "00" is typed in starting from the 4th digit.
    - "01" is typed in starting from the 1st digit.
    - "10" is typed in starting from the 3rd digit.
    - "11" is typed in starting from the 2nd digit.
    Thus "01100" will unlock the safe. "10011", and "11001" would also unlock the safe.
    """
def reachNumber(target: int) -> int:
    """
    You are standing at position 0 on an infinite number line. There is a destination at position target.
    You can make some number of moves numMoves so that:
    
    On each move, you can either go left or right.
    During the ith move (starting from i == 1 to i == numMoves), you take i steps in the chosen direction.
    
    Given the integer target, return the minimum number of moves required (i.e., the minimum numMoves) to reach the destination.
    
    Example 1:
    
    >>> reachNumber(target = 2)
    >>> 3
    Explanation:
    On the 1st move, we step from 0 to 1 (1 step).
    On the 2nd move, we step from 1 to -1 (2 steps).
    On the 3rd move, we step from -1 to 2 (3 steps).
    
    Example 2:
    
    >>> reachNumber(target = 3)
    >>> 2
    Explanation:
    On the 1st move, we step from 0 to 1 (1 step).
    On the 2nd move, we step from 1 to 3 (2 steps).
    """
def pourWater(heights: List[int], volume: int, k: int) -> List[int]:
    """
    You are given an elevation map represents as an integer array heights where heights[i] representing the height of the terrain at index i. The width at each index is 1. You are also given two integers volume and k. volume units of water will fall at index k.
    Water first drops at the index k and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:
    
    If the droplet would eventually fall by moving left, then move left.
    Otherwise, if the droplet would eventually fall by moving right, then move right.
    Otherwise, rise to its current position.
    
    Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, level means the height of the terrain plus any water in that column.
    We can assume there is infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than one grid block, and each unit of water has to be in exactly one block.
    
    Example 1:
    
    
    >>> pourWater(heights = [2,1,1,2,1,2,2], volume = 4, k = 3)
    >>> [2,2,2,3,2,2,2]
    Explanation:
    The first drop of water lands at index k = 3. When moving left or right, the water can only move to the same level or a lower level. (By level, we mean the total height of the terrain plus any water in that column.)
    Since moving left will eventually make it fall, it moves left. (A droplet "made to fall" means go to a lower height than it was at previously.) Since moving left will not make it fall, it stays in place.
    
    The next droplet falls at index k = 3. Since the new droplet moving left will eventually make it fall, it moves left. Notice that the droplet still preferred to move left, even though it could move right (and moving right makes it fall quicker.)
    
    The third droplet falls at index k = 3. Since moving left would not eventually make it fall, it tries to move right. Since moving right would eventually make it fall, it moves right.
    
    Finally, the fourth droplet falls at index k = 3. Since moving left would not eventually make it fall, it tries to move right. Since moving right would not eventually make it fall, it stays in place.
    
    
    Example 2:
    
    >>> pourWater(heights = [1,2,3,4], volume = 2, k = 2)
    >>> [2,3,3,4]
    Explanation: The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.
    
    Example 3:
    
    >>> pourWater(heights = [3,1,3], volume = 5, k = 1)
    >>> [4,4,4]
    """
def pyramidTransition(bottom: str, allowed: List[str]) -> bool:
    """
    You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains one less block than the row beneath it and is centered on top.
    To make the pyramid aesthetically pleasing, there are only specific triangular patterns that are allowed. A triangular pattern consists of a single block stacked on top of two blocks. The patterns are given as a list of three-letter strings allowed, where the first two characters of a pattern represent the left and right bottom blocks respectively, and the third character is the top block.
    
    For example, "ABC" represents a triangular pattern with a 'C' block stacked on top of an 'A' (left) and 'B' (right) block. Note that this is different from "BAC" where 'B' is on the left bottom and 'A' is on the right bottom.
    
    You start with a bottom row of blocks bottom, given as a single string, that you must use as the base of the pyramid.
    Given bottom and allowed, return true if you can build the pyramid all the way to the top such that every triangular pattern in the pyramid is in allowed, or false otherwise.
    
    Example 1:
    
    
    >>> pyramidTransition(bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"])
    >>> true
    Explanation: The allowed triangular patterns are shown on the right.
    Starting from the bottom (level 3), we can build "CE" on level 2 and then build "A" on level 1.
    There are three triangular patterns in the pyramid, which are "BCC", "CDE", and "CEA". All are allowed.
    
    Example 2:
    
    
    >>> pyramidTransition(bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"])
    >>> false
    Explanation: The allowed triangular patterns are shown on the right.
    Starting from the bottom (level 4), there are multiple ways to build level 3, but trying all the possibilites, you will get always stuck before building level 1.
    """
def intersectionSizeTwo(intervals: List[List[int]]) -> int:
    """
    You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively.
    A containing set is an array nums where each interval from intervals has at least two integers in nums.
    
    For example, if intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9] are containing sets.
    
    Return the minimum possible size of a containing set.
    
    Example 1:
    
    >>> intersectionSizeTwo(intervals = [[1,3],[3,7],[8,9]])
    >>> 5
    Explanation: let nums = [2, 3, 4, 8, 9].
    It can be shown that there cannot be any containing array of size 4.
    
    Example 2:
    
    >>> intersectionSizeTwo(intervals = [[1,3],[1,4],[2,5],[3,5]])
    >>> 3
    Explanation: let nums = [2, 3, 4].
    It can be shown that there cannot be any containing array of size 2.
    
    Example 3:
    
    >>> intersectionSizeTwo(intervals = [[1,2],[2,3],[2,4],[4,5]])
    >>> 5
    Explanation: let nums = [1, 2, 3, 4, 5].
    It can be shown that there cannot be any containing array of size 4.
    """
def boldWords(words: List[str], s: str) -> str:
    """
    Given an array of keywords words and a string s, make all appearances of all keywords words[i] in s bold. Any letters between  and  tags become bold.
    Return s after adding the bold tags. The returned string should use the least number of tags possible, and the tags should form a valid combination.
    
    Example 1:
    
    >>> boldWords(words = ["ab","bc"], s = "aabcd")
    >>> "aabcd"
    Explanation: Note that returning "aabcd" would use more tags, so it is incorrect.
    
    Example 2:
    
    >>> boldWords(words = ["ab","cb"], s = "aabcd")
    >>> "aabcd"
    """