ShwStone/humaneval-leetcode huggingface dataset:
def makeFancyString(s: str) -> str:
    """
    A fancy string is a string where no three consecutive characters are equal.
    Given a string s, delete the minimum possible number of characters from s to make it fancy.
    Return the final string after the deletion. It can be shown that the answer will always be unique.
    
    Example 1:
    
    >>> makeFancyString(s = "leeetcode")
    >>> "leetcode"
    Explanation:
    Remove an 'e' from the first group of 'e's to create "leetcode".
    No three consecutive characters are equal, so return "leetcode".
    
    Example 2:
    
    >>> makeFancyString(s = "aaabaaaa")
    >>> "aabaa"
    Explanation:
    Remove an 'a' from the first group of 'a's to create "aabaaaa".
    Remove two 'a's from the second group of 'a's to create "aabaa".
    No three consecutive characters are equal, so return "aabaa".
    
    Example 3:
    
    >>> makeFancyString(s = "aab")
    >>> "aab"
    Explanation: No three consecutive characters are equal, so return "aab".
    """
def checkMove(board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
    """
    You are given a 0-indexed 8 x 8 grid board, where board[r][c] represents the cell (r, c) on a game board. On the board, free cells are represented by '.', white cells are represented by 'W', and black cells are represented by 'B'.
    Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only legal if, after changing it, the cell becomes the endpoint of a good line (horizontal, vertical, or diagonal).
    A good line is a line of three or more cells (including the endpoints) where the endpoints of the line are one color, and the remaining cells in the middle are the opposite color (no cells in the line are free). You can find examples for good lines in the figure below:
    
    Given two integers rMove and cMove and a character color representing the color you are playing as (white or black), return true if changing cell (rMove, cMove) to color color is a legal move, or false if it is not legal.
    
    Example 1:
    
    
    >>> checkMove(board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B")
    >>> true
    Explanation: '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'.
    The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.
    
    Example 2:
    
    
    >>> checkMove(board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W")
    >>> false
    Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.
    """
def minSpaceWastedKResizing(nums: List[int], k: int) -> int:
    """
    You are currently designing a dynamic array. You are given a 0-indexed integer array nums, where nums[i] is the number of elements that will be in the array at time i. In addition, you are given an integer k, the maximum number of times you can resize the array (to any size).
    The size of the array at time t, sizet, must be at least nums[t] because there needs to be enough space in the array to hold all the elements. The space wasted at time t is defined as sizet - nums[t], and the total space wasted is the sum of the space wasted across every time t where 0 <= t < nums.length.
    Return the minimum total space wasted if you can resize the array at most k times.
    Note: The array can have any size at the start and does not count towards the number of resizing operations.
    
    Example 1:
    
    >>> minSpaceWastedKResizing(nums = [10,20], k = 0)
    >>> 10
    Explanation: size = [20,20].
    We can set the initial size to be 20.
    The total wasted space is (20 - 10) + (20 - 20) = 10.
    
    Example 2:
    
    >>> minSpaceWastedKResizing(nums = [10,20,30], k = 1)
    >>> 10
    Explanation: size = [20,20,30].
    We can set the initial size to be 20 and resize to 30 at time 2.
    The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.
    
    Example 3:
    
    >>> minSpaceWastedKResizing(nums = [10,20,15,30,20], k = 2)
    >>> 15
    Explanation: size = [10,20,20,30,30].
    We can set the initial size to 10, resize to 20 at time 1, and resize to 30 at time 3.
    The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 - 20) = 15.
    """
def maxProduct(s: str) -> int:
    """
    You are given a 0-indexed string s and are tasked with finding two non-intersecting palindromic substrings of odd length such that the product of their lengths is maximized.
    More formally, you want to choose four integers i, j, k, l such that 0 <= i <= j < k <= l < s.length and both the substrings s[i...j] and s[k...l] are palindromes and have odd lengths. s[i...j] denotes a substring from index i to index j inclusive.
    Return the maximum possible product of the lengths of the two non-intersecting palindromic substrings.
    A palindrome is a string that is the same forward and backward. A substring is a contiguous sequence of characters in a string.
    
    Example 1:
    
    >>> maxProduct(s = "ababbb")
    >>> 9
    Explanation: Substrings "aba" and "bbb" are palindromes with odd length. product = 3 * 3 = 9.
    
    Example 2:
    
    >>> maxProduct(s = "zaaaxbbby")
    >>> 9
    Explanation: Substrings "aaa" and "bbb" are palindromes with odd length. product = 3 * 3 = 9.
    """
def isPrefixString(s: str, words: List[str]) -> bool:
    """
    Given a string s and an array of strings words, determine whether s is a prefix string of words.
    A string s is a prefix string of words if s can be made by concatenating the first k strings in words for some positive k no larger than words.length.
    Return true if s is a prefix string of words, or false otherwise.
    
    Example 1:
    
    >>> isPrefixString(s = "iloveleetcode", words = ["i","love","leetcode","apples"])
    >>> true
    Explanation:
    s can be made by concatenating "i", "love", and "leetcode" together.
    
    Example 2:
    
    >>> isPrefixString(s = "iloveleetcode", words = ["apples","i","love","leetcode"])
    >>> false
    Explanation:
    It is impossible to make s using a prefix of arr.
    """
def minStoneSum(piles: List[int], k: int) -> int:
    """
    You are given a 0-indexed integer array piles, where piles[i] represents the number of stones in the ith pile, and an integer k. You should apply the following operation exactly k times:
    
    Choose any piles[i] and remove floor(piles[i] / 2) stones from it.
    
    Notice that you can apply the operation on the same pile more than once.
    Return the minimum possible total number of stones remaining after applying the k operations.
    floor(x) is the greatest integer that is smaller than or equal to x (i.e., rounds x down).
    
    Example 1:
    
    >>> minStoneSum(piles = [5,4,9], k = 2)
    >>> 12
    Explanation: Steps of a possible scenario are:
    - Apply the operation on pile 2. The resulting piles are [5,4,5].
    - Apply the operation on pile 0. The resulting piles are [3,4,5].
    The total number of stones in [3,4,5] is 12.
    
    Example 2:
    
    >>> minStoneSum(piles = [4,3,6,7], k = 3)
    >>> 12
    Explanation: Steps of a possible scenario are:
    - Apply the operation on pile 2. The resulting piles are [4,3,3,7].
    - Apply the operation on pile 3. The resulting piles are [4,3,3,4].
    - Apply the operation on pile 0. The resulting piles are [2,3,3,4].
    The total number of stones in [2,3,3,4] is 12.
    """
def minSwaps(s: str) -> int:
    """
    You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.
    A string is called balanced if and only if:
    
    It is the empty string, or
    It can be written as AB, where both A and B are balanced strings, or
    It can be written as [C], where C is a balanced string.
    
    You may swap the brackets at any two indices any number of times.
    Return the minimum number of swaps to make s balanced.
    
    Example 1:
    
    >>> minSwaps(s = "][][")
    >>> 1
    Explanation: You can make the string balanced by swapping index 0 with index 3.
    The resulting string is "[[]]".
    
    Example 2:
    
    >>> minSwaps(s = "]]][[[")
    >>> 2
    Explanation: You can do the following to make the string balanced:
    - Swap index 0 with index 4. s = "[]][][".
    - Swap index 1 with index 5. s = "[[][]]".
    The resulting string is "[[][]]".
    
    Example 3:
    
    >>> minSwaps(s = "[]")
    >>> 0
    Explanation: The string is already balanced.
    """
def longestObstacleCourseAtEachPosition(obstacles: List[int]) -> List[int]:
    """
    You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle.
    For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles such that:
    
    You choose any number of obstacles between 0 and i inclusive.
    You must include the ith obstacle in the course.
    You must put the chosen obstacles in the same order as they appear in obstacles.
    Every obstacle (except the first) is taller than or the same height as the obstacle immediately before it.
    
    Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above.
    
    Example 1:
    
    >>> longestObstacleCourseAtEachPosition(obstacles = [1,2,3,2])
    >>> [1,2,3,3]
    Explanation: The longest valid obstacle course at each position is:
    - i = 0: [1], [1] has length 1.
    - i = 1: [1,2], [1,2] has length 2.
    - i = 2: [1,2,3], [1,2,3] has length 3.
    - i = 3: [1,2,3,2], [1,2,2] has length 3.
    
    Example 2:
    
    >>> longestObstacleCourseAtEachPosition(obstacles = [2,2,1])
    >>> [1,2,1]
    Explanation: The longest valid obstacle course at each position is:
    - i = 0: [2], [2] has length 1.
    - i = 1: [2,2], [2,2] has length 2.
    - i = 2: [2,2,1], [1] has length 1.
    
    Example 3:
    
    >>> longestObstacleCourseAtEachPosition(obstacles = [3,1,5,6,4,2])
    >>> [1,1,2,3,2,2]
    Explanation: The longest valid obstacle course at each position is:
    - i = 0: [3], [3] has length 1.
    - i = 1: [3,1], [1] has length 1.
    - i = 2: [3,1,5], [3,5] has length 2. [1,5] is also valid.
    - i = 3: [3,1,5,6], [3,5,6] has length 3. [1,5,6] is also valid.
    - i = 4: [3,1,5,6,4], [3,4] has length 2. [1,4] is also valid.
    - i = 5: [3,1,5,6,4,2], [1,2] has length 2.
    """
def binarySearchableNumbers(nums: List[int]) -> int:
    """
    Consider a function that implements an algorithm similar to Binary Search. The function has two input parameters: sequence is a sequence of integers, and target is an integer value. The purpose of the function is to find if the target exists in the sequence.
    The pseudocode of the function is as follows:
    
    func(sequence, target)
      while sequence is not empty
        randomly choose an element from sequence as the pivot
        if pivot = target, return true
        else if pivot < target, remove pivot and all elements to its left from the sequence
        else, remove pivot and all elements to its right from the sequence
      end while
      return false
    
    When the sequence is sorted, the function works correctly for all values. When the sequence is not sorted, the function does not work for all values, but may still work for some values.
    Given an integer array nums, representing the sequence, that contains unique numbers and may or may not be sorted, return the number of values that are guaranteed to be found using the function, for every possible pivot selection.
    
    Example 1:
    
    >>> binarySearchableNumbers(nums = [7])
    >>> 1
    Explanation:
    Searching for value 7 is guaranteed to be found.
    Since the sequence has only one element, 7 will be chosen as the pivot. Because the pivot equals the target, the function will return true.
    
    Example 2:
    
    >>> binarySearchableNumbers(nums = [-1,5,2])
    >>> 1
    Explanation:
    Searching for value -1 is guaranteed to be found.
    If -1 was chosen as the pivot, the function would return true.
    If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return true.
    If 2 was chosen as the pivot, 2 would be removed. In the next loop, the sequence would have -1 and 5. No matter which number was chosen as the next pivot, the function would find -1 and return true.
    
    Searching for value 5 is NOT guaranteed to be found.
    If 2 was chosen as the pivot, -1, 5 and 2 would be removed. The sequence would be empty and the function would return false.
    
    Searching for value 2 is NOT guaranteed to be found.
    If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return false.
    
    Because only -1 is guaranteed to be found, you should return 1.
    """
def numOfStrings(patterns: List[str], word: str) -> int:
    """
    Given an array of strings patterns and a string word, return the number of strings in patterns that exist as a substring in word.
    A substring is a contiguous sequence of characters within a string.
    
    Example 1:
    
    >>> numOfStrings(patterns = ["a","abc","bc","d"], word = "abc")
    >>> 3
    Explanation:
    - "a" appears as a substring in "abc".
    - "abc" appears as a substring in "abc".
    - "bc" appears as a substring in "abc".
    - "d" does not appear as a substring in "abc".
    3 of the strings in patterns appear as a substring in word.
    
    Example 2:
    
    >>> numOfStrings(patterns = ["a","b","c"], word = "aaaaabbbbb")
    >>> 2
    Explanation:
    - "a" appears as a substring in "aaaaabbbbb".
    - "b" appears as a substring in "aaaaabbbbb".
    - "c" does not appear as a substring in "aaaaabbbbb".
    2 of the strings in patterns appear as a substring in word.
    
    Example 3:
    
    >>> numOfStrings(patterns = ["a","a","a"], word = "ab")
    >>> 3
    Explanation: Each of the patterns appears as a substring in word "ab".
    """
def rearrangeArray(nums: List[int]) -> List[int]:
    """
    You are given a 0-indexed array nums of distinct integers. You want to rearrange the elements in the array such that every element in the rearranged array is not equal to the average of its neighbors.
    More formally, the rearranged array should have the property such that for every i in the range 1 <= i < nums.length - 1, (nums[i-1] + nums[i+1]) / 2 is not equal to nums[i].
    Return any rearrangement of nums that meets the requirements.
    
    Example 1:
    
    >>> rearrangeArray(nums = [1,2,3,4,5])
    >>> [1,2,4,5,3]
    Explanation:
    When i=1, nums[i] = 2, and the average of its neighbors is (1+4) / 2 = 2.5.
    When i=2, nums[i] = 4, and the average of its neighbors is (2+5) / 2 = 3.5.
    When i=3, nums[i] = 5, and the average of its neighbors is (4+3) / 2 = 3.5.
    
    Example 2:
    
    >>> rearrangeArray(nums = [6,2,0,9,7])
    >>> [9,7,6,2,0]
    Explanation:
    When i=1, nums[i] = 7, and the average of its neighbors is (9+6) / 2 = 7.5.
    When i=2, nums[i] = 6, and the average of its neighbors is (7+2) / 2 = 4.5.
    When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3.
    Note that the original array [6,2,0,9,7] also satisfies the conditions.
    """
def latestDayToCross(row: int, col: int, cells: List[List[int]]) -> int:
    """
    There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively.
    Initially on day 0, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array cells, where cells[i] = [ri, ci] represents that on the ith day, the cell on the rith row and cith column (1-based coordinates) will be covered with water (i.e., changed to 1).
    You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the four cardinal directions (left, right, up, and down).
    Return the last day where it is possible to walk from the top to the bottom by only walking on land cells.
    
    Example 1:
    
    
    >>> latestDayToCross(row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]])
    >>> 2
    Explanation: The above image depicts how the matrix changes each day starting from day 0.
    The last day where it is possible to cross from top to bottom is on day 2.
    
    Example 2:
    
    
    >>> latestDayToCross(row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]])
    >>> 1
    Explanation: The above image depicts how the matrix changes each day starting from day 0.
    The last day where it is possible to cross from top to bottom is on day 1.
    
    Example 3:
    
    
    >>> latestDayToCross(row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]])
    >>> 3
    Explanation: The above image depicts how the matrix changes each day starting from day 0.
    The last day where it is possible to cross from top to bottom is on day 3.
    """
def validPath(n: int, edges: List[List[int]], source: int, destination: int) -> bool:
    """
    There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
    You want to determine if there is a valid path that exists from vertex source to vertex destination.
    Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
    
    Example 1:
    
    
    >>> validPath(n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2)
    >>> true
    Explanation: There are two paths from vertex 0 to vertex 2:
    - 0 → 1 → 2
    - 0 → 2
    
    Example 2:
    
    
    >>> validPath(n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5)
    >>> false
    Explanation: There is no path from vertex 0 to vertex 5.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def equalToDescendants(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the number of nodes where the value of the node is equal to the sum of the values of its descendants.
    A descendant of a node x is any node that is on the path from node x to some leaf node. The sum is considered to be 0 if the node has no descendants.
    
    Example 1:
    
    
    >>> __init__(root = [10,3,4,2,1])
    >>> 2
    Explanation:
    For the node with value 10: The sum of its descendants is 3+4+2+1 = 10.
    For the node with value 3: The sum of its descendants is 2+1 = 3.
    
    Example 2:
    
    
    >>> __init__(root = [2,3,null,2,null])
    >>> 0
    Explanation:
    No node has a value that is equal to the sum of its descendants.
    
    Example 3:
    
    
    >>> __init__(root = [0])
    >>> 1
    For the node with value 0: The sum of its descendants is 0 since it has no descendants.
    """
def minTimeToType(word: str) -> int:
    """
    There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'.
    
    Each second, you may perform one of the following operations:
    
    Move the pointer one character counterclockwise or clockwise.
    Type the character the pointer is currently on.
    
    Given a string word, return the minimum number of seconds to type out the characters in word.
    
    Example 1:
    
    >>> minTimeToType(word = "abc")
    >>> 5
    Explanation:
    The characters are printed as follows:
    - Type the character 'a' in 1 second since the pointer is initially on 'a'.
    - Move the pointer clockwise to 'b' in 1 second.
    - Type the character 'b' in 1 second.
    - Move the pointer clockwise to 'c' in 1 second.
    - Type the character 'c' in 1 second.
    
    Example 2:
    
    >>> minTimeToType(word = "bza")
    >>> 7
    Explanation:
    The characters are printed as follows:
    - Move the pointer clockwise to 'b' in 1 second.
    - Type the character 'b' in 1 second.
    - Move the pointer counterclockwise to 'z' in 2 seconds.
    - Type the character 'z' in 1 second.
    - Move the pointer clockwise to 'a' in 1 second.
    - Type the character 'a' in 1 second.
    
    Example 3:
    
    >>> minTimeToType(word = "zjpc")
    >>> 34
    Explanation:
    The characters are printed as follows:
    - Move the pointer counterclockwise to 'z' in 1 second.
    - Type the character 'z' in 1 second.
    - Move the pointer clockwise to 'j' in 10 seconds.
    - Type the character 'j' in 1 second.
    - Move the pointer clockwise to 'p' in 6 seconds.
    - Type the character 'p' in 1 second.
    - Move the pointer counterclockwise to 'c' in 13 seconds.
    - Type the character 'c' in 1 second.
    """
def maxMatrixSum(matrix: List[List[int]]) -> int:
    """
    You are given an n x n integer matrix. You can do the following operation any number of times:
    
    Choose any two adjacent elements of matrix and multiply each of them by -1.
    
    Two elements are considered adjacent if and only if they share a border.
    Your goal is to maximize the summation of the matrix's elements. Return the maximum sum of the matrix's elements using the operation mentioned above.
    
    Example 1:
    
    
    >>> maxMatrixSum(matrix = [[1,-1],[-1,1]])
    >>> 4
    Explanation: We can follow the following steps to reach sum equals 4:
    - Multiply the 2 elements in the first row by -1.
    - Multiply the 2 elements in the first column by -1.
    
    Example 2:
    
    
    >>> maxMatrixSum(matrix = [[1,2,3],[-1,-2,-3],[1,2,3]])
    >>> 16
    Explanation: We can follow the following step to reach sum equals 16:
    - Multiply the 2 last elements in the second row by -1.
    """