ShwStone/humaneval-leetcode huggingface dataset:
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given the head of a singly linked list, return the middle node of the linked list.
    If there are two middle nodes, return the second middle node.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5])
    >>> [3,4,5]
    Explanation: The middle node of the list is node 3.
    
    Example 2:
    
    
    >>> __init__(head = [1,2,3,4,5,6])
    >>> [4,5,6]
    Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
    """
def stoneGame(piles: List[int]) -> bool:
    """
    Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
    The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.
    Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
    Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins.
    
    Example 1:
    
    >>> stoneGame(piles = [5,3,4,5])
    >>> true
    Explanation:
    Alice starts first, and can only take the first 5 or the last 5.
    Say she takes the first 5, so that the row becomes [3, 4, 5].
    If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
    If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
    This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
    
    Example 2:
    
    >>> stoneGame(piles = [3,7,2,3])
    >>> true
    """
def nthMagicalNumber(n: int, a: int, b: int) -> int:
    """
    A positive integer is magical if it is divisible by either a or b.
    Given the three integers n, a, and b, return the nth magical number. Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> nthMagicalNumber(n = 1, a = 2, b = 3)
    >>> 2
    
    Example 2:
    
    >>> nthMagicalNumber(n = 4, a = 2, b = 3)
    >>> 6
    """
def profitableSchemes(n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
    """
    There is a group of n members, and a list of various crimes they could commit. The ith crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can't participate in another crime.
    Let's call a profitable scheme any subset of these crimes that generates at least minProfit profit, and the total number of members participating in that subset of crimes is at most n.
    Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> profitableSchemes(n = 5, minProfit = 3, group = [2,2], profit = [2,3])
    >>> 2
    Explanation: To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1.
    In total, there are 2 schemes.
    Example 2:
    
    >>> profitableSchemes(n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8])
    >>> 7
    Explanation: To make a profit of at least 5, the group could commit any crimes, as long as they commit one.
    There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).
    """
def decodeAtIndex(s: str, k: int) -> str:
    """
    You are given an encoded string s. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
    
    If the character read is a letter, that letter is written onto the tape.
    If the character read is a digit d, the entire current tape is repeatedly written d - 1 more times in total.
    
    Given an integer k, return the kth letter (1-indexed) in the decoded string.
    
    Example 1:
    
    >>> decodeAtIndex(s = "leet2code3", k = 10)
    >>> "o"
    Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
    The 10th letter in the string is "o".
    
    Example 2:
    
    >>> decodeAtIndex(s = "ha22", k = 5)
    >>> "h"
    Explanation: The decoded string is "hahahaha".
    The 5th letter is "h".
    
    Example 3:
    
    >>> decodeAtIndex(s = "a2345678999999999999999", k = 1)
    >>> "a"
    Explanation: The decoded string is "a" repeated 8301530446056247680 times.
    The 1st letter is "a".
    """
def numRescueBoats(people: List[int], limit: int) -> int:
    """
    You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
    Return the minimum number of boats to carry every given person.
    
    Example 1:
    
    >>> numRescueBoats(people = [1,2], limit = 3)
    >>> 1
    Explanation: 1 boat (1, 2)
    
    Example 2:
    
    >>> numRescueBoats(people = [3,2,2,1], limit = 3)
    >>> 3
    Explanation: 3 boats (1, 2), (2) and (3)
    
    Example 3:
    
    >>> numRescueBoats(people = [3,5,3,4], limit = 5)
    >>> 4
    Explanation: 4 boats (3), (3), (4), (5)
    """
def reachableNodes(edges: List[List[int]], maxMoves: int, n: int) -> int:
    """
    You are given an undirected graph (the "original graph") with n nodes labeled from 0 to n - 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.
    The graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that there is an edge between nodes ui and vi in the original graph, and cnti is the total number of new nodes that you will subdivide the edge into. Note that cnti == 0 means you will not subdivide the edge.
    To subdivide the edge [ui, vi], replace it with (cnti + 1) new edges and cnti new nodes. The new nodes are x1, x2, ..., xcnti, and the new edges are [ui, x1], [x1, x2], [x2, x3], ..., [xcnti-1, xcnti], [xcnti, vi].
    In this new graph, you want to know how many nodes are reachable from the node 0, where a node is reachable if the distance is maxMoves or less.
    Given the original graph and maxMoves, return the number of nodes that are reachable from node 0 in the new graph.
    
    Example 1:
    
    
    >>> reachableNodes(edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3)
    >>> 13
    Explanation: The edge subdivisions are shown in the image above.
    The nodes that are reachable are highlighted in yellow.
    
    Example 2:
    
    >>> reachableNodes(edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4)
    >>> 23
    
    Example 3:
    
    >>> reachableNodes(edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5)
    >>> 1
    Explanation: Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.
    """
def projectionArea(grid: List[List[int]]) -> int:
    """
    You are given an n x n grid where we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z axes.
    Each value v = grid[i][j] represents a tower of v cubes placed on top of the cell (i, j).
    We view the projection of these cubes onto the xy, yz, and zx planes.
    A projection is like a shadow, that maps our 3-dimensional figure to a 2-dimensional plane. We are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
    Return the total area of all three projections.
    
    Example 1:
    
    
    >>> projectionArea(grid = [[1,2],[3,4]])
    >>> 17
    Explanation: Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
    
    Example 2:
    
    >>> projectionArea(grid = [[2]])
    >>> 5
    
    Example 3:
    
    >>> projectionArea(grid = [[1,0],[0,2]])
    >>> 8
    """
def uncommonFromSentences(s1: str, s2: str) -> List[str]:
    """
    A sentence is a string of single-space separated words where each word consists only of lowercase letters.
    A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
    Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.
    
    Example 1:
    
    >>> uncommonFromSentences(s1 = "this apple is sweet", s2 = "this apple is sour")
    >>> ["sweet","sour"]
    Explanation:
    The word "sweet" appears only in s1, while the word "sour" appears only in s2.
    
    Example 2:
    
    >>> uncommonFromSentences(s1 = "apple apple", s2 = "banana")
    >>> ["banana"]
    """
def spiralMatrixIII(rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
    """
    You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
    You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.
    Return an array of coordinates representing the positions of the grid in the order you visited them.
    
    Example 1:
    
    
    >>> spiralMatrixIII(rows = 1, cols = 4, rStart = 0, cStart = 0)
    >>> [[0,0],[0,1],[0,2],[0,3]]
    
    Example 2:
    
    
    >>> spiralMatrixIII(rows = 5, cols = 6, rStart = 1, cStart = 4)
    >>> [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
    """
def possibleBipartition(n: int, dislikes: List[List[int]]) -> bool:
    """
    We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group.
    Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.
    
    Example 1:
    
    >>> possibleBipartition(n = 4, dislikes = [[1,2],[1,3],[2,4]])
    >>> true
    Explanation: The first group has [1,4], and the second group has [2,3].
    
    Example 2:
    
    >>> possibleBipartition(n = 3, dislikes = [[1,2],[1,3],[2,3]])
    >>> false
    Explanation: We need at least 3 groups to divide them. We cannot put them in two groups.
    """
def superEggDrop(k: int, n: int) -> int:
    """
    You are given k identical eggs and you have access to a building with n floors labeled from 1 to n.
    You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break.
    Each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
    Return the minimum number of moves that you need to determine with certainty what the value of f is.
    
    Example 1:
    
    >>> superEggDrop(k = 1, n = 2)
    >>> 2
    Explanation:
    Drop the egg from floor 1. If it breaks, we know that f = 0.
    Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
    If it does not break, then we know f = 2.
    Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
    
    Example 2:
    
    >>> superEggDrop(k = 2, n = 6)
    >>> 3
    
    Example 3:
    
    >>> superEggDrop(k = 3, n = 14)
    >>> 4
    """
def fairCandySwap(aliceSizes: List[int], bobSizes: List[int]) -> List[int]:
    """
    Alice and Bob have a different total number of candies. You are given two integer arrays aliceSizes and bobSizes where aliceSizes[i] is the number of candies of the ith box of candy that Alice has and bobSizes[j] is the number of candies of the jth box of candy that Bob has.
    Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.
    Return an integer array answer where answer[0] is the number of candies in the box that Alice must exchange, and answer[1] is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists.
    
    Example 1:
    
    >>> fairCandySwap(aliceSizes = [1,1], bobSizes = [2,2])
    >>> [1,2]
    
    Example 2:
    
    >>> fairCandySwap(aliceSizes = [1,2], bobSizes = [2,3])
    >>> [1,2]
    
    Example 3:
    
    >>> fairCandySwap(aliceSizes = [2], bobSizes = [1,3])
    >>> [2,3]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def constructFromPrePost(preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
    """
    Given two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the postorder traversal of the same tree, reconstruct and return the binary tree.
    If there exist multiple answers, you can return any of them.
    
    Example 1:
    
    
    >>> __init__(preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1])
    >>> [1,2,3,4,5,6,7]
    
    Example 2:
    
    >>> __init__(preorder = [1], postorder = [1])
    >>> [1]
    """
def findAndReplacePattern(words: List[str], pattern: str) -> List[str]:
    """
    Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.
    A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
    Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
    
    Example 1:
    
    >>> findAndReplacePattern(words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb")
    >>> ["mee","aqq"]
    Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
    "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
    
    Example 2:
    
    >>> findAndReplacePattern(words = ["a","b","c"], pattern = "a")
    >>> ["a","b","c"]
    """
def sumSubseqWidths(nums: List[int]) -> int:
    """
    The width of a sequence is the difference between the maximum and minimum elements in the sequence.
    Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 109 + 7.
    A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
    
    Example 1:
    
    >>> sumSubseqWidths(nums = [2,1,3])
    >>> 6
    Explanation: The subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
    The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
    The sum of these widths is 6.
    
    Example 2:
    
    >>> sumSubseqWidths(nums = [2])
    >>> 0
    """
def surfaceArea(grid: List[List[int]]) -> int:
    """
    You are given an n x n grid where you have placed some 1 x 1 x 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of cell (i, j).
    After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
    Return the total surface area of the resulting shapes.
    Note: The bottom face of each shape counts toward its surface area.
    
    Example 1:
    
    
    >>> surfaceArea(grid = [[1,2],[3,4]])
    >>> 34
    
    Example 2:
    
    
    >>> surfaceArea(grid = [[1,1,1],[1,0,1],[1,1,1]])
    >>> 32
    
    Example 3:
    
    
    >>> surfaceArea(grid = [[2,2,2],[2,1,2],[2,2,2]])
    >>> 46
    """
def numSpecialEquivGroups(words: List[str]) -> int:
    """
    You are given an array of strings of the same length words.
    In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].
    Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].
    
    For example, words[i] = "zzxy" and words[j] = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz".
    
    A group of special-equivalent strings from words is a non-empty subset of words such that:
    
    Every pair of strings in the group are special equivalent, and
    The group is the largest size possible (i.e., there is not a string words[i] not in the group such that words[i] is special-equivalent to every string in the group).
    
    Return the number of groups of special-equivalent strings from words.
    
    Example 1:
    
    >>> numSpecialEquivGroups(words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"])
    >>> 3
    Explanation:
    One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these.
    The other two groups are ["xyzz", "zzxy"] and ["zzyx"].
    Note that in particular, "zzxy" is not special equivalent to "zzyx".
    
    Example 2:
    
    >>> numSpecialEquivGroups(words = ["abc","acb","bac","bca","cab","cba"])
    >>> 3
    """
def isMonotonic(nums: List[int]) -> bool:
    """
    An array is monotonic if it is either monotone increasing or monotone decreasing.
    An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].
    Given an integer array nums, return true if the given array is monotonic, or false otherwise.
    
    Example 1:
    
    >>> isMonotonic(nums = [1,2,2,3])
    >>> true
    
    Example 2:
    
    >>> isMonotonic(nums = [6,5,4,4])
    >>> true
    
    Example 3:
    
    >>> isMonotonic(nums = [1,3,2])
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def increasingBST(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
    
    Example 1:
    
    
    >>> __init__(root = [5,3,6,2,4,null,8,1,null,null,null,7,9])
    >>> [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
    
    Example 2:
    
    
    >>> __init__(root = [5,1,7])
    >>> [1,null,5,null,7]
    """
def subarrayBitwiseORs(arr: List[int]) -> int:
    """
    Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.
    The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
    A subarray is a contiguous non-empty sequence of elements within an array.
    
    Example 1:
    
    >>> subarrayBitwiseORs(arr = [0])
    >>> 1
    Explanation: There is only one possible result: 0.
    
    Example 2:
    
    >>> subarrayBitwiseORs(arr = [1,1,2])
    >>> 3
    Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
    These yield the results 1, 1, 2, 1, 3, 3.
    There are 3 unique values, so the answer is 3.
    
    Example 3:
    
    >>> subarrayBitwiseORs(arr = [1,2,4])
    >>> 6
    Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
    """
def orderlyQueue(s: str, k: int) -> str:
    """
    You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string.
    Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
    
    Example 1:
    
    >>> orderlyQueue(s = "cba", k = 1)
    >>> "acb"
    Explanation:
    In the first move, we move the 1st character 'c' to the end, obtaining the string "bac".
    In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".
    
    Example 2:
    
    >>> orderlyQueue(s = "baaca", k = 3)
    >>> "aaabc"
    Explanation:
    In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab".
    In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".
    """
def atMostNGivenDigitSet(digits: List[str], n: int) -> int:
    """
    Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'.
    Return the number of positive integers that can be generated that are less than or equal to a given integer n.
    
    Example 1:
    
    >>> atMostNGivenDigitSet(digits = ["1","3","5","7"], n = 100)
    >>> 20
    Explanation:
    The 20 numbers that can be written are:
    1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
    
    Example 2:
    
    >>> atMostNGivenDigitSet(digits = ["1","4","9"], n = 1000000000)
    >>> 29523
    Explanation:
    We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
    81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
    2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
    In total, this is 29523 integers that can be written using the digits array.
    
    Example 3:
    
    >>> atMostNGivenDigitSet(digits = ["7"], n = 8)
    >>> 1
    """
def numPermsDISequence(s: str) -> int:
    """
    You are given a string s of length n where s[i] is either:
    
    'D' means decreasing, or
    'I' means increasing.
    
    A permutation perm of n + 1 integers of all the integers in the range [0, n] is called a valid permutation if for all valid i:
    
    If s[i] == 'D', then perm[i] > perm[i + 1], and
    If s[i] == 'I', then perm[i] < perm[i + 1].
    
    Return the number of valid permutations perm. Since the answer may be large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numPermsDISequence(s = "DID")
    >>> 5
    Explanation: The 5 valid permutations of (0, 1, 2, 3) are:
    (1, 0, 3, 2)
    (2, 0, 3, 1)
    (2, 1, 3, 0)
    (3, 0, 2, 1)
    (3, 1, 2, 0)
    
    Example 2:
    
    >>> numPermsDISequence(s = "D")
    >>> 1
    """