ShwStone/humaneval-leetcode huggingface dataset:
def findPermutation(s: str) -> List[int]:
    """
    A permutation perm of n integers of all the integers in the range [1, n] can be represented as a string s of length n - 1 where:
    
    s[i] == 'I' if perm[i] < perm[i + 1], and
    s[i] == 'D' if perm[i] > perm[i + 1].
    
    Given a string s, reconstruct the lexicographically smallest permutation perm and return it.
    
    Example 1:
    
    >>> findPermutation(s = "I")
    >>> [1,2]
    Explanation: [1,2] is the only legal permutation that can represented by s, where the number 1 and 2 construct an increasing relationship.
    
    Example 2:
    
    >>> findPermutation(s = "DI")
    >>> [2,1,3]
    Explanation: Both [2,1,3] and [3,1,2] can be represented as "DI", but since we want to find the smallest lexicographical permutation, you should return [2,1,3]
    """
def findMaxConsecutiveOnes(nums: List[int]) -> int:
    """
    Given a binary array nums, return the maximum number of consecutive 1's in the array.
    
    Example 1:
    
    >>> findMaxConsecutiveOnes(nums = [1,1,0,1,1,1])
    >>> 3
    Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
    
    Example 2:
    
    >>> findMaxConsecutiveOnes(nums = [1,0,1,1,0,1])
    >>> 2
    """
def predictTheWinner(nums: List[int]) -> bool:
    """
    You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.
    Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.
    Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.
    
    Example 1:
    
    >>> predictTheWinner(nums = [1,5,2])
    >>> false
    Explanation: Initially, player 1 can choose between 1 and 2.
    If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
    So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
    Hence, player 1 will never be the winner and you need to return false.
    
    Example 2:
    
    >>> predictTheWinner(nums = [1,5,233,7])
    >>> true
    Explanation: Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
    Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
    """
def findMaxConsecutiveOnes(nums: List[int]) -> int:
    """
    Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.
    
    Example 1:
    
    >>> findMaxConsecutiveOnes(nums = [1,0,1,1,0])
    >>> 4
    Explanation:
    - If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.
    - If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.
    The max number of consecutive ones is 4.
    
    Example 2:
    
    >>> findMaxConsecutiveOnes(nums = [1,0,1,1,0,1])
    >>> 4
    Explanation:
    - If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.
    - If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.
    The max number of consecutive ones is 4.
    """
def findMinStep(board: str, hand: str) -> int:
    """
    You are playing a variation of the game Zuma.
    In this variation of Zuma, there is a single row of colored balls on a board, where each ball can be colored red 'R', yellow 'Y', blue 'B', green 'G', or white 'W'. You also have several colored balls in your hand.
    Your goal is to clear all of the balls from the board. On each turn:
    
    Pick any ball from your hand and insert it in between two balls in the row or on either end of the row.
    If there is a group of three or more consecutive balls of the same color, remove the group of balls from the board.
    
    If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.
    
    
    If there are no more balls on the board, then you win the game.
    Repeat this process until you either win or do not have any more balls in your hand.
    
    Given a string board, representing the row of balls on the board, and a string hand, representing the balls in your hand, return the minimum number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return -1.
    
    Example 1:
    
    >>> findMinStep(board = "WRRBBW", hand = "RB")
    >>> -1
    Explanation: It is impossible to clear all the balls. The best you can do is:
    - Insert 'R' so the board becomes WRRRBBW. WRRRBBW -> WBBW.
    - Insert 'B' so the board becomes WBBBW. WBBBW -> WW.
    There are still balls remaining on the board, and you are out of balls to insert.
    Example 2:
    
    >>> findMinStep(board = "WWRRBBWW", hand = "WRBRW")
    >>> 2
    Explanation: To make the board empty:
    - Insert 'R' so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW.
    - Insert 'B' so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty.
    2 balls from your hand were needed to clear the board.
    
    Example 3:
    
    >>> findMinStep(board = "G", hand = "GGGGG")
    >>> 2
    Explanation: To make the board empty:
    - Insert 'G' so the board becomes GG.
    - Insert 'G' so the board becomes GGG. GGG -> empty.
    2 balls from your hand were needed to clear the board.
    """
def hasPath(maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
    """
    There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
    Given the m x n maze, the ball's start position and the destination, where start = [startrow, startcol] and destination = [destinationrow, destinationcol], return true if the ball can stop at the destination, otherwise return false.
    You may assume that the borders of the maze are all walls (see examples).
    
    Example 1:
    
    
    >>> hasPath(maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4])
    >>> true
    Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
    
    Example 2:
    
    
    >>> hasPath(maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2])
    >>> false
    Explanation: There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.
    
    Example 3:
    
    >>> hasPath(maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1])
    >>> false
    """
def findSubsequences(nums: List[int]) -> List[List[int]]:
    """
    Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.
    
    Example 1:
    
    >>> findSubsequences(nums = [4,6,7,7])
    >>> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
    
    Example 2:
    
    >>> findSubsequences(nums = [4,4,3,2,1])
    >>> [[4,4]]
    """
def constructRectangle(area: int) -> List[int]:
    """
    A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
    
    The area of the rectangular web page you designed must equal to the given target area.
    The width W should not be larger than the length L, which means L >= W.
    The difference between length L and width W should be as small as possible.
    
    Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
    
    Example 1:
    
    >>> constructRectangle(area = 4)
    >>> [2,2]
    Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
    But according to requirement 2, [1,4] is illegal; according to requirement 3,  [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
    
    Example 2:
    
    >>> constructRectangle(area = 37)
    >>> [37,1]
    
    Example 3:
    
    >>> constructRectangle(area = 122122)
    >>> [427,286]
    """
def reversePairs(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of reverse pairs in the array.
    A reverse pair is a pair (i, j) where:
    
    0 <= i < j < nums.length and
    nums[i] > 2 * nums[j].
    
    
    Example 1:
    
    >>> reversePairs(nums = [1,3,2,3,1])
    >>> 2
    Explanation: The reverse pairs are:
    (1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
    (3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1
    
    Example 2:
    
    >>> reversePairs(nums = [2,4,3,5,1])
    >>> 3
    Explanation: The reverse pairs are:
    (1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
    (2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
    (3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1
    """
def findTargetSumWays(nums: List[int], target: int) -> int:
    """
    You are given an integer array nums and an integer target.
    You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.
    
    For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".
    
    Return the number of different expressions that you can build, which evaluates to target.
    
    Example 1:
    
    >>> findTargetSumWays(nums = [1,1,1,1,1], target = 3)
    >>> 5
    Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
    -1 + 1 + 1 + 1 + 1 = 3
    +1 - 1 + 1 + 1 + 1 = 3
    +1 + 1 - 1 + 1 + 1 = 3
    +1 + 1 + 1 - 1 + 1 = 3
    +1 + 1 + 1 + 1 - 1 = 3
    
    Example 2:
    
    >>> findTargetSumWays(nums = [1], target = 1)
    >>> 1
    """
def findPoisonedDuration(timeSeries: List[int], duration: int) -> int:
    """
    Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.
    You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.
    Return the total number of seconds that Ashe is poisoned.
    
    Example 1:
    
    >>> findPoisonedDuration(timeSeries = [1,4], duration = 2)
    >>> 4
    Explanation: Teemo's attacks on Ashe go as follows:
    - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
    - At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
    Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
    
    Example 2:
    
    >>> findPoisonedDuration(timeSeries = [1,2], duration = 2)
    >>> 3
    Explanation: Teemo's attacks on Ashe go as follows:
    - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
    - At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
    Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.
    """
def nextGreaterElement(nums1: List[int], nums2: List[int]) -> List[int]:
    """
    The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
    You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
    For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
    Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
    
    Example 1:
    
    >>> nextGreaterElement(nums1 = [4,1,2], nums2 = [1,3,4,2])
    >>> [-1,3,-1]
    Explanation: The next greater element for each value of nums1 is as follows:
    - 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
    - 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
    - 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
    
    Example 2:
    
    >>> nextGreaterElement(nums1 = [2,4], nums2 = [1,2,3,4])
    >>> [3,-1]
    Explanation: The next greater element for each value of nums1 is as follows:
    - 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
    - 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
    """
def findDiagonalOrder(mat: List[List[int]]) -> List[int]:
    """
    Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.
    
    Example 1:
    
    
    >>> findDiagonalOrder(mat = [[1,2,3],[4,5,6],[7,8,9]])
    >>> [1,2,4,7,5,3,6,8,9]
    
    Example 2:
    
    >>> findDiagonalOrder(mat = [[1,2],[3,4]])
    >>> [1,2,3,4]
    """
def findShortestWay(maze: List[List[int]], ball: List[int], hole: List[int]) -> str:
    """
    There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.
    Given the m x n maze, the ball's position ball and the hole's position hole, where ball = [ballrow, ballcol] and hole = [holerow, holecol], return a string instructions of all the instructions that the ball should follow to drop in the hole with the shortest distance possible. If there are multiple valid instructions, return the lexicographically minimum one. If the ball can't drop in the hole, return "impossible".
    If there is a way for the ball to drop in the hole, the answer instructions should contain the characters 'u' (i.e., up), 'd' (i.e., down), 'l' (i.e., left), and 'r' (i.e., right).
    The distance is the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included).
    You may assume that the borders of the maze are all walls (see examples).
    
    Example 1:
    
    
    >>> findShortestWay(maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], ball = [4,3], hole = [0,1])
    >>> "lul"
    Explanation: There are two shortest ways for the ball to drop into the hole.
    The first way is left -> up -> left, represented by "lul".
    The second way is up -> left, represented by 'ul'.
    Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".
    
    Example 2:
    
    
    >>> findShortestWay(maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], ball = [4,3], hole = [3,0])
    >>> "impossible"
    Explanation: The ball cannot reach the hole.
    
    Example 3:
    
    >>> findShortestWay(maze = [[0,0,0,0,0,0,0],[0,0,1,0,0,1,0],[0,0,0,0,1,0,0],[0,0,0,0,0,0,1]], ball = [0,4], hole = [3,5])
    >>> "dldr"
    """
def findWords(words: List[str]) -> List[str]:
    """
    Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
    Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.
    In the American keyboard:
    
    the first row consists of the characters "qwertyuiop",
    the second row consists of the characters "asdfghjkl", and
    the third row consists of the characters "zxcvbnm".
    
    
    
    Example 1:
    
    >>> findWords(words = ["Hello","Alaska","Dad","Peace"])
    >>> ["Alaska","Dad"]
    Explanation:
    Both "a" and "A" are in the 2nd row of the American keyboard due to case insensitivity.
    
    Example 2:
    
    >>> findWords(words = ["omk"])
    >>> []
    
    Example 3:
    
    >>> findWords(words = ["adsdf","sfd"])
    >>> ["adsdf","sfd"]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findMode(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
    If the tree has more than one mode, return them in any order.
    Assume a BST is defined as follows:
    
    The left subtree of a node contains only nodes with keys less than or equal to the node's key.
    The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
    Both the left and right subtrees must also be binary search trees.
    
    
    Example 1:
    
    
    >>> __init__(root = [1,null,2,2])
    >>> [2]
    
    Example 2:
    
    >>> __init__(root = [0])
    >>> [0]
    """
def findMaximizedCapital(k: int, w: int, profits: List[int], capital: List[int]) -> int:
    """
    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.
    You are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.
    Initially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.
    Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.
    The answer is guaranteed to fit in a 32-bit signed integer.
    
    Example 1:
    
    >>> findMaximizedCapital(k = 2, w = 0, profits = [1,2,3], capital = [0,1,1])
    >>> 4
    Explanation: Since your initial capital is 0, you can only start the project indexed 0.
    After finishing it you will obtain profit 1 and your capital becomes 1.
    With capital 1, you can either start the project indexed 1 or the project indexed 2.
    Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
    Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
    
    Example 2:
    
    >>> findMaximizedCapital(k = 3, w = 0, profits = [1,2,3], capital = [0,1,2])
    >>> 6
    """
def nextGreaterElements(nums: List[int]) -> List[int]:
    """
    Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.
    The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.
    
    Example 1:
    
    >>> nextGreaterElements(nums = [1,2,1])
    >>> [2,-1,2]
    Explanation: The first 1's next greater number is 2;
    The number 2 can't find next greater number.
    The second 1's next greater number needs to search circularly, which is also 2.
    
    Example 2:
    
    >>> nextGreaterElements(nums = [1,2,3,4,3])
    >>> [2,3,4,-1,4]
    """
def convertToBase7(num: int) -> str:
    """
    Given an integer num, return a string of its base 7 representation.
    
    Example 1:
    >>> convertToBase7(num = 100)
    >>> "202"
    Example 2:
    >>> convertToBase7(num = -7)
    >>> "-10"
    """
def shortestDistance(maze: List[List[int]], start: List[int], destination: List[int]) -> int:
    """
    There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
    Given the m x n maze, the ball's start position and the destination, where start = [startrow, startcol] and destination = [destinationrow, destinationcol], return the shortest distance for the ball to stop at the destination. If the ball cannot stop at destination, return -1.
    The distance is the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included).
    You may assume that the borders of the maze are all walls (see examples).
    
    Example 1:
    
    
    >>> shortestDistance(maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4])
    >>> 12
    Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
    The length of the path is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
    
    Example 2:
    
    
    >>> shortestDistance(maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2])
    >>> -1
    Explanation: There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.
    
    Example 3:
    
    >>> shortestDistance(maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1])
    >>> -1
    """
def findRelativeRanks(score: List[int]) -> List[str]:
    """
    You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
    The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
    
    The 1st place athlete's rank is "Gold Medal".
    The 2nd place athlete's rank is "Silver Medal".
    The 3rd place athlete's rank is "Bronze Medal".
    For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x").
    
    Return an array answer of size n where answer[i] is the rank of the ith athlete.
    
    Example 1:
    
    >>> findRelativeRanks(score = [5,4,3,2,1])
    >>> ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
    Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
    Example 2:
    
    >>> findRelativeRanks(score = [10,3,8,9,4])
    >>> ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
    Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
    """
def checkPerfectNumber(num: int) -> bool:
    """
    A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.
    Given an integer n, return true if n is a perfect number, otherwise return false.
    
    Example 1:
    
    >>> checkPerfectNumber(num = 28)
    >>> true
    Explanation: 28 = 1 + 2 + 4 + 7 + 14
    1, 2, 4, 7, and 14 are all divisors of 28.
    
    Example 2:
    
    >>> checkPerfectNumber(num = 7)
    >>> false
    """