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 maxAncestorDiff(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.
    A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.
    
    Example 1:
    
    
    >>> __init__(root = [8,3,10,1,6,null,14,null,null,4,7,13])
    >>> 7
    Explanation: We have various ancestor-node differences, some of which are given below :
    |8 - 3| = 5
    |3 - 7| = 4
    |8 - 1| = 7
    |10 - 13| = 3
    Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
    Example 2:
    
    
    >>> __init__(root = [1,null,2,null,0,3])
    >>> 3
    """
def longestArithSeqLength(nums: List[int]) -> int:
    """
    Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
    Note that:
    
    A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
    A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
    
    
    Example 1:
    
    >>> longestArithSeqLength(nums = [3,6,9,12])
    >>> 4
    Explanation:  The whole array is an arithmetic sequence with steps of length = 3.
    
    Example 2:
    
    >>> longestArithSeqLength(nums = [9,4,7,2,10])
    >>> 3
    Explanation:  The longest arithmetic subsequence is [4,7,10].
    
    Example 3:
    
    >>> longestArithSeqLength(nums = [20,1,15,3,10,5,8])
    >>> 4
    Explanation:  The longest arithmetic subsequence is [20,15,10,5].
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverFromPreorder(traversal: str) -> Optional[TreeNode]:
    """
    We run a preorder depth-first search (DFS) on the root of a binary tree.
    At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  If the depth of a node is D, the depth of its immediate child is D + 1.  The depth of the root node is 0.
    If a node has only one child, that child is guaranteed to be the left child.
    Given the output traversal of this traversal, recover the tree and return its root.
    
    Example 1:
    
    
    >>> __init__(traversal = "1-2--3--4-5--6--7")
    >>> [1,2,5,3,4,6,7]
    
    Example 2:
    
    
    >>> __init__(traversal = "1-2--3---4-5--6---7")
    >>> [1,2,5,3,null,6,null,4,null,7]
    
    Example 3:
    
    
    >>> __init__(traversal = "1-401--349---90--88")
    >>> [1,401,null,349,88,90]
    """
def twoCitySchedCost(costs: List[List[int]]) -> int:
    """
    A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti], the cost of flying the ith person to city a is aCosti, and the cost of flying the ith person to city b is bCosti.
    Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.
    
    Example 1:
    
    >>> twoCitySchedCost(costs = [[10,20],[30,200],[400,50],[30,20]])
    >>> 110
    Explanation:
    The first person goes to city A for a cost of 10.
    The second person goes to city A for a cost of 30.
    The third person goes to city B for a cost of 50.
    The fourth person goes to city B for a cost of 20.
    
    The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
    
    Example 2:
    
    >>> twoCitySchedCost(costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]])
    >>> 1859
    
    Example 3:
    
    >>> twoCitySchedCost(costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]])
    >>> 3086
    """
def allCellsDistOrder(rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
    """
    You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter).
    Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. You may return the answer in any order that satisfies this condition.
    The distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.
    
    Example 1:
    
    >>> allCellsDistOrder(rows = 1, cols = 2, rCenter = 0, cCenter = 0)
    >>> [[0,0],[0,1]]
    Explanation: The distances from (0, 0) to other cells are: [0,1]
    
    Example 2:
    
    >>> allCellsDistOrder(rows = 2, cols = 2, rCenter = 0, cCenter = 1)
    >>> [[0,1],[0,0],[1,1],[1,0]]
    Explanation: The distances from (0, 1) to other cells are: [0,1,1,2]
    The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
    
    Example 3:
    
    >>> allCellsDistOrder(rows = 2, cols = 3, rCenter = 1, cCenter = 2)
    >>> [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
    Explanation: The distances from (1, 2) to other cells are: [0,1,1,2,2,3]
    There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
    """
def maxSumTwoNoOverlap(nums: List[int], firstLen: int, secondLen: int) -> int:
    """
    Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths firstLen and secondLen.
    The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.
    A subarray is a contiguous part of an array.
    
    Example 1:
    
    >>> maxSumTwoNoOverlap(nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2)
    >>> 20
    Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
    
    Example 2:
    
    >>> maxSumTwoNoOverlap(nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2)
    >>> 29
    Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
    
    Example 3:
    
    >>> maxSumTwoNoOverlap(nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3)
    >>> 31
    Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
    """
def numMovesStones(a: int, b: int, c: int) -> List[int]:
    """
    There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.
    In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions x, y, and z with x < y < z. You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.
    The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
    Return an integer array answer of length 2 where:
    
    answer[0] is the minimum number of moves you can play, and
    answer[1] is the maximum number of moves you can play.
    
    
    Example 1:
    
    >>> numMovesStones(a = 1, b = 2, c = 5)
    >>> [1,2]
    Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
    
    Example 2:
    
    >>> numMovesStones(a = 4, b = 3, c = 2)
    >>> [0,0]
    Explanation: We cannot make any moves.
    
    Example 3:
    
    >>> numMovesStones(a = 3, b = 5, c = 1)
    >>> [1,2]
    Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
    """
def colorBorder(grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]:
    """
    You are given an m x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the color of the grid square at that location.
    Two squares are called adjacent if they are next to each other in any of the 4 directions.
    Two squares belong to the same connected component if they have the same color and they are adjacent.
    The border of a connected component is all the squares in the connected component that are either adjacent to (at least) a square not in the component, or on the boundary of the grid (the first or last row or column).
    You should color the border of the connected component that contains the square grid[row][col] with color.
    Return the final grid.
    
    Example 1:
    >>> colorBorder(grid = [[1,1],[1,2]], row = 0, col = 0, color = 3)
    >>> [[3,3],[3,2]]
    Example 2:
    >>> colorBorder(grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3)
    >>> [[1,3,3],[2,3,3]]
    Example 3:
    >>> colorBorder(grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2)
    >>> [[2,2,2],[2,1,2],[2,2,2]]
    """
def maxUncrossedLines(nums1: List[int], nums2: List[int]) -> int:
    """
    You are given two integer arrays nums1 and nums2. We write the integers of nums1 and nums2 (in the order they are given) on two separate horizontal lines.
    We may draw connecting lines: a straight line connecting two numbers nums1[i] and nums2[j] such that:
    
    nums1[i] == nums2[j], and
    the line we draw does not intersect any other connecting (non-horizontal) line.
    
    Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).
    Return the maximum number of connecting lines we can draw in this way.
    
    Example 1:
    
    
    >>> maxUncrossedLines(nums1 = [1,4,2], nums2 = [1,2,4])
    >>> 2
    Explanation: We can draw 2 uncrossed lines as in the diagram.
    We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2] = 4 will intersect the line from nums1[2]=2 to nums2[1]=2.
    
    Example 2:
    
    >>> maxUncrossedLines(nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2])
    >>> 3
    
    Example 3:
    
    >>> maxUncrossedLines(nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1])
    >>> 2
    """
def isEscapePossible(blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
    """
    There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are (x, y).
    We start at the source = [sx, sy] square and want to reach the target = [tx, ty] square. There is also an array of blocked squares, where each blocked[i] = [xi, yi] represents a blocked square with coordinates (xi, yi).
    Each move, we can walk one square north, east, south, or west if the square is not in the array of blocked squares. We are also not allowed to walk outside of the grid.
    Return true if and only if it is possible to reach the target square from the source square through a sequence of valid moves.
    
    Example 1:
    
    >>> isEscapePossible(blocked = [[0,1],[1,0]], source = [0,0], target = [0,2])
    >>> false
    Explanation: The target square is inaccessible starting from the source square because we cannot move.
    We cannot move north or east because those squares are blocked.
    We cannot move south or west because we cannot go outside of the grid.
    
    Example 2:
    
    >>> isEscapePossible(blocked = [], source = [0,0], target = [999999,999999])
    >>> true
    Explanation: Because there are no blocked cells, it is possible to reach the target square.
    """
def isBoomerang(points: List[List[int]]) -> bool:
    """
    Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, return true if these points are a boomerang.
    A boomerang is a set of three points that are all distinct and not in a straight line.
    
    Example 1:
    >>> isBoomerang(points = [[1,1],[2,3],[3,2]])
    >>> true
    Example 2:
    >>> isBoomerang(points = [[1,1],[2,2],[3,3]])
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def bstToGst(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 minScoreTriangulation(values: List[int]) -> int:
    """
    You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex in clockwise order.
    Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in n - 2 triangles.
    You will triangulate the polygon. For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all n - 2 triangles.
    Return the minimum possible score that you can achieve with some triangulation of the polygon.
    
    Example 1:
    
    
    >>> minScoreTriangulation(values = [1,2,3])
    >>> 6
    Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
    
    Example 2:
    
    
    >>> minScoreTriangulation(values = [3,7,4,5])
    >>> 144
    Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
    The minimum score is 144.
    
    Example 3:
    
    
    >>> minScoreTriangulation(values = [1,3,1,4,1,5])
    >>> 13
    Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
    """
def numMovesStonesII(stones: List[int]) -> List[int]:
    """
    There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.
    Call a stone an endpoint stone if it has the smallest or largest position. In one move, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.
    
    In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.
    
    The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
    Return an integer array answer of length 2 where:
    
    answer[0] is the minimum number of moves you can play, and
    answer[1] is the maximum number of moves you can play.
    
    
    Example 1:
    
    >>> numMovesStonesII(stones = [7,4,9])
    >>> [1,2]
    Explanation: We can move 4 -> 8 for one move to finish the game.
    Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.
    
    Example 2:
    
    >>> numMovesStonesII(stones = [6,5,4,3,10])
    >>> [2,3]
    Explanation: We can move 3 -> 8 then 10 -> 7 to finish the game.
    Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
    Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
    """
def isRobotBounded(instructions: str) -> bool:
    """
    On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:
    
    The north direction is the positive direction of the y-axis.
    The south direction is the negative direction of the y-axis.
    The east direction is the positive direction of the x-axis.
    The west direction is the negative direction of the x-axis.
    
    The robot can receive one of three instructions:
    
    "G": go straight 1 unit.
    "L": turn 90 degrees to the left (i.e., anti-clockwise direction).
    "R": turn 90 degrees to the right (i.e., clockwise direction).
    
    The robot performs the instructions given in order, and repeats them forever.
    Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
    
    Example 1:
    
    >>> isRobotBounded(instructions = "GGLLGG")
    >>> true
    Explanation: The robot is initially at (0, 0) facing the north direction.
    "G": move one step. Position: (0, 1). Direction: North.
    "G": move one step. Position: (0, 2). Direction: North.
    "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
    "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
    "G": move one step. Position: (0, 1). Direction: South.
    "G": move one step. Position: (0, 0). Direction: South.
    Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
    Based on that, we return true.
    
    Example 2:
    
    >>> isRobotBounded(instructions = "GG")
    >>> false
    Explanation: The robot is initially at (0, 0) facing the north direction.
    "G": move one step. Position: (0, 1). Direction: North.
    "G": move one step. Position: (0, 2). Direction: North.
    Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
    Based on that, we return false.
    
    Example 3:
    
    >>> isRobotBounded(instructions = "GL")
    >>> true
    Explanation: The robot is initially at (0, 0) facing the north direction.
    "G": move one step. Position: (0, 1). Direction: North.
    "L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
    "G": move one step. Position: (-1, 1). Direction: West.
    "L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
    "G": move one step. Position: (-1, 0). Direction: South.
    "L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
    "G": move one step. Position: (0, 0). Direction: East.
    "L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
    Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
    Based on that, we return true.
    """
def gardenNoAdj(n: int, paths: List[List[int]]) -> List[int]:
    """
    You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers.
    All gardens have at most 3 paths coming into or leaving it.
    Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
    Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
    
    Example 1:
    
    >>> gardenNoAdj(n = 3, paths = [[1,2],[2,3],[3,1]])
    >>> [1,2,3]
    Explanation:
    Gardens 1 and 2 have different types.
    Gardens 2 and 3 have different types.
    Gardens 3 and 1 have different types.
    Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].
    
    Example 2:
    
    >>> gardenNoAdj(n = 4, paths = [[1,2],[3,4]])
    >>> [1,2,1,2]
    
    Example 3:
    
    >>> gardenNoAdj(n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]])
    >>> [1,2,3,4]
    """
def maxSumAfterPartitioning(arr: List[int], k: int) -> int:
    """
    Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.
    Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    >>> maxSumAfterPartitioning(arr = [1,15,7,9,2,5,10], k = 3)
    >>> 84
    Explanation: arr becomes [15,15,15,9,10,10,10]
    
    Example 2:
    
    >>> maxSumAfterPartitioning(arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4)
    >>> 83
    
    Example 3:
    
    >>> maxSumAfterPartitioning(arr = [1], k = 1)
    >>> 1
    """
def longestDupSubstring(s: str) -> str:
    """
    Given a string s, consider all duplicated substrings: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap.
    Return any duplicated substring that has the longest possible length. If s does not have a duplicated substring, the answer is "".
    
    Example 1:
    >>> longestDupSubstring(s = "banana")
    >>> "ana"
    Example 2:
    >>> longestDupSubstring(s = "abcd")
    >>> ""
    """
def lastStoneWeight(stones: List[int]) -> int:
    """
    You are given an array of integers stones where stones[i] is the weight of the ith stone.
    We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:
    
    If x == y, both stones are destroyed, and
    If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
    
    At the end of the game, there is at most one stone left.
    Return the weight of the last remaining stone. If there are no stones left, return 0.
    
    Example 1:
    
    >>> lastStoneWeight(stones = [2,7,4,1,8,1])
    >>> 1
    Explanation:
    We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
    we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
    we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
    we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
    
    Example 2:
    
    >>> lastStoneWeight(stones = [1])
    >>> 1
    """
def removeDuplicates(s: str) -> str:
    """
    You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
    We repeatedly make duplicate removals on s until we no longer can.
    Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
    
    Example 1:
    
    >>> removeDuplicates(s = "abbaca")
    >>> "ca"
    Explanation:
    For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
    
    Example 2:
    
    >>> removeDuplicates(s = "azxxzy")
    >>> "ay"
    """