ShwStone/humaneval-leetcode huggingface dataset:
def maxJumps(arr: List[int], d: int) -> int:
    """
    Given an array of integers arr and an integer d. In one step you can jump from index i to index:
    
    i + x where: i + x < arr.length and  0 < x <= d.
    i - x where: i - x >= 0 and  0 < x <= d.
    
    In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
    You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
    Notice that you can not jump outside of the array at any time.
    
    Example 1:
    
    
    >>> maxJumps(arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2)
    >>> 4
    Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
    Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
    Similarly You cannot jump from index 3 to index 2 or index 1.
    
    Example 2:
    
    >>> maxJumps(arr = [3,3,3,3,3], d = 3)
    >>> 1
    Explanation: You can start at any index. You always cannot jump to any index.
    
    Example 3:
    
    >>> maxJumps(arr = [7,6,5,4,3,2,1], d = 1)
    >>> 7
    Explanation: Start at index 0. You can visit all the indicies.
    """
def numberOfSteps(num: int) -> int:
    """
    Given an integer num, return the number of steps to reduce it to zero.
    In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
    
    Example 1:
    
    >>> numberOfSteps(num = 14)
    >>> 6
    Explanation:
    Step 1) 14 is even; divide by 2 and obtain 7.
    Step 2) 7 is odd; subtract 1 and obtain 6.
    Step 3) 6 is even; divide by 2 and obtain 3.
    Step 4) 3 is odd; subtract 1 and obtain 2.
    Step 5) 2 is even; divide by 2 and obtain 1.
    Step 6) 1 is odd; subtract 1 and obtain 0.
    
    Example 2:
    
    >>> numberOfSteps(num = 8)
    >>> 4
    Explanation:
    Step 1) 8 is even; divide by 2 and obtain 4.
    Step 2) 4 is even; divide by 2 and obtain 2.
    Step 3) 2 is even; divide by 2 and obtain 1.
    Step 4) 1 is odd; subtract 1 and obtain 0.
    
    Example 3:
    
    >>> numberOfSteps(num = 123)
    >>> 12
    """
def numOfSubarrays(arr: List[int], k: int, threshold: int) -> int:
    """
    Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold.
    
    Example 1:
    
    >>> numOfSubarrays(arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4)
    >>> 3
    Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
    
    Example 2:
    
    >>> numOfSubarrays(arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5)
    >>> 6
    Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
    """
def angleClock(hour: int, minutes: int) -> float:
    """
    Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
    Answers within 10-5 of the actual value will be accepted as correct.
    
    Example 1:
    
    
    >>> angleClock(hour = 12, minutes = 30)
    >>> 165
    
    Example 2:
    
    
    >>> angleClock(hour = 3, minutes = 30)
    >>> 75
    
    Example 3:
    
    
    >>> angleClock(hour = 3, minutes = 15)
    >>> 7.5
    """
def minJumps(arr: List[int]) -> int:
    """
    Given an array of integers arr, you are initially positioned at the first index of the array.
    In one step you can jump from index i to index:
    
    i + 1 where: i + 1 < arr.length.
    i - 1 where: i - 1 >= 0.
    j where: arr[i] == arr[j] and i != j.
    
    Return the minimum number of steps to reach the last index of the array.
    Notice that you can not jump outside of the array at any time.
    
    Example 1:
    
    >>> minJumps(arr = [100,-23,-23,404,100,23,23,23,3,404])
    >>> 3
    Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
    
    Example 2:
    
    >>> minJumps(arr = [7])
    >>> 0
    Explanation: Start index is the last index. You do not need to jump.
    
    Example 3:
    
    >>> minJumps(arr = [7,6,9,6,9,6,9,7])
    >>> 1
    Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
    """
def checkIfExist(arr: List[int]) -> bool:
    """
    Given an array arr of integers, check if there exist two indices i and j such that :
    
    i != j
    0 <= i, j < arr.length
    arr[i] == 2 * arr[j]
    
    
    Example 1:
    
    >>> checkIfExist(arr = [10,2,5,3])
    >>> true
    Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
    
    Example 2:
    
    >>> checkIfExist(arr = [3,1,7,11])
    >>> false
    Explanation: There is no i and j that satisfy the conditions.
    """
def minSteps(s: str, t: str) -> int:
    """
    You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.
    Return the minimum number of steps to make t an anagram of s.
    An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
    
    Example 1:
    
    >>> minSteps(s = "bab", t = "aba")
    >>> 1
    Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
    
    Example 2:
    
    >>> minSteps(s = "leetcode", t = "practice")
    >>> 5
    Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
    
    Example 3:
    
    >>> minSteps(s = "anagram", t = "mangaar")
    >>> 0
    Explanation: "anagram" and "mangaar" are anagrams.
    """
def maxStudents(seats: List[List[str]]) -> int:
    """
    Given a m * n matrix seats  that represent seats distributions in a classroom. If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.
    Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible.
    Students must be placed in seats in good condition.
    
    Example 1:
    
    
                    [".","#","#","#","#","."],
                    ["#",".","#","#",".","#"]]
    >>> maxStudents(seats = [["#",".","#","#",".","#"],)
    >>> 4
    Explanation: Teacher can place 4 students in available seats so they don't cheat on the exam.
    
    Example 2:
    
                    ["#","#"],
                    ["#","."],
                    ["#","#"],
                    [".","#"]]
    >>> maxStudents(seats = [[".","#"],)
    >>> 3
    Explanation: Place all students in available seats.
    
    
    Example 3:
    
                    [".","#",".","#","."],
                    [".",".","#",".","."],
                    [".","#",".","#","."],
                    ["#",".",".",".","#"]]
    >>> maxStudents(seats = [["#",".",".",".","#"],)
    >>> 10
    Explanation: Place students in available seats in column 1, 3 and 5.
    """
def countNegatives(grid: List[List[int]]) -> int:
    """
    Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.
    
    Example 1:
    
    >>> countNegatives(grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]])
    >>> 8
    Explanation: There are 8 negatives number in the matrix.
    
    Example 2:
    
    >>> countNegatives(grid = [[3,2],[1,0]])
    >>> 0
    """
def maxEvents(events: List[List[int]]) -> int:
    """
    You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.
    You can attend an event i at any day d where startTimei <= d <= endTimei. You can only attend one event at any time d.
    Return the maximum number of events you can attend.
    
    Example 1:
    
    
    >>> maxEvents(events = [[1,2],[2,3],[3,4]])
    >>> 3
    Explanation: You can attend all the three events.
    One way to attend them all is as shown.
    Attend the first event on day 1.
    Attend the second event on day 2.
    Attend the third event on day 3.
    
    Example 2:
    
    >>> maxEvents(events= [[1,2],[2,3],[3,4],[1,2]])
    >>> 4
    """
def isPossible(target: List[int]) -> bool:
    """
    You are given an array target of n integers. From a starting array arr consisting of n 1's, you may perform the following procedure :
    
    let x be the sum of all elements currently in your array.
    choose index i, such that 0 <= i < n and set the value of arr at index i to x.
    You may repeat this procedure as many times as needed.
    
    Return true if it is possible to construct the target array from arr, otherwise, return false.
    
    Example 1:
    
    >>> isPossible(target = [9,3,5])
    >>> true
    Explanation: Start with arr = [1, 1, 1]
    [1, 1, 1], sum = 3 choose index 1
    [1, 3, 1], sum = 5 choose index 2
    [1, 3, 5], sum = 9 choose index 0
    [9, 3, 5] Done
    
    Example 2:
    
    >>> isPossible(target = [1,1,1,2])
    >>> false
    Explanation: Impossible to create target array from [1,1,1,1].
    
    Example 3:
    
    >>> isPossible(target = [8,5])
    >>> true
    """
def sortByBits(arr: List[int]) -> List[int]:
    """
    You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.
    Return the array after sorting it.
    
    Example 1:
    
    >>> sortByBits(arr = [0,1,2,3,4,5,6,7,8])
    >>> [0,1,2,4,8,3,5,6,7]
    Explantion: [0] is the only integer with 0 bits.
    [1,2,4,8] all have 1 bit.
    [3,5,6] have 2 bits.
    [7] has 3 bits.
    The sorted array by bits is [0,1,2,4,8,3,5,6,7]
    
    Example 2:
    
    >>> sortByBits(arr = [1024,512,256,128,64,32,16,8,4,2,1])
    >>> [1,2,4,8,16,32,64,128,256,512,1024]
    Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
    """
def numberOfSubstrings(s: str) -> int:
    """
    Given a string s consisting only of characters a, b and c.
    Return the number of substrings containing at least one occurrence of all these characters a, b and c.
    
    Example 1:
    
    >>> numberOfSubstrings(s = "abcabc")
    >>> 10
    Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).
    
    Example 2:
    
    >>> numberOfSubstrings(s = "aaacb")
    >>> 3
    Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".
    
    Example 3:
    
    >>> numberOfSubstrings(s = "abc")
    >>> 1
    """
def countOrders(n: int) -> int:
    """
    Given n orders, each order consists of a pickup and a delivery service.
    Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).
    Since the answer may be too large, return it modulo 10^9 + 7.
    
    Example 1:
    
    >>> countOrders(n = 1)
    >>> 1
    Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.
    
    Example 2:
    
    >>> countOrders(n = 2)
    >>> 6
    Explanation: All possible orders:
    (P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
    This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.
    
    Example 3:
    
    >>> countOrders(n = 3)
    >>> 90
    """
def daysBetweenDates(date1: str, date2: str) -> int:
    """
    Write a program to count the number of days between two dates.
    The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
    
    Example 1:
    >>> daysBetweenDates(date1 = "2019-06-29", date2 = "2019-06-30")
    >>> 1
    Example 2:
    >>> daysBetweenDates(date1 = "2020-01-15", date2 = "2019-12-31")
    >>> 15
    """
def validateBinaryTreeNodes(n: int, leftChild: List[int], rightChild: List[int]) -> bool:
    """
    You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.
    If node i has no left child then leftChild[i] will equal -1, similarly for the right child.
    Note that the nodes have no values and that we only use the node numbers in this problem.
    
    Example 1:
    
    
    >>> validateBinaryTreeNodes(n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1])
    >>> true
    
    Example 2:
    
    
    >>> validateBinaryTreeNodes(n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1])
    >>> false
    
    Example 3:
    
    
    >>> validateBinaryTreeNodes(n = 2, leftChild = [1,0], rightChild = [-1,-1])
    >>> false
    """
def closestDivisors(num: int) -> List[int]:
    """
    Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.
    Return the two integers in any order.
    
    Example 1:
    
    >>> closestDivisors(num = 8)
    >>> [3,3]
    Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.
    
    Example 2:
    
    >>> closestDivisors(num = 123)
    >>> [5,25]
    
    Example 3:
    
    >>> closestDivisors(num = 999)
    >>> [40,25]
    """
def largestMultipleOfThree(digits: List[int]) -> str:
    """
    Given an array of digits digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. If there is no answer return an empty string.
    Since the answer may not fit in an integer data type, return the answer as a string. Note that the returning answer must not contain unnecessary leading zeros.
    
    Example 1:
    
    >>> largestMultipleOfThree(digits = [8,1,9])
    >>> "981"
    
    Example 2:
    
    >>> largestMultipleOfThree(digits = [8,6,7,1,0])
    >>> "8760"
    
    Example 3:
    
    >>> largestMultipleOfThree(digits = [1])
    >>> ""
    """
def smallerNumbersThanCurrent(nums: List[int]) -> List[int]:
    """
    Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
    Return the answer in an array.
    
    Example 1:
    
    >>> smallerNumbersThanCurrent(nums = [8,1,2,2,3])
    >>> [4,0,1,1,3]
    Explanation:
    For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
    For nums[1]=1 does not exist any smaller number than it.
    For nums[2]=2 there exist one smaller number than it (1).
    For nums[3]=2 there exist one smaller number than it (1).
    For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
    
    Example 2:
    
    >>> smallerNumbersThanCurrent(nums = [6,5,4,8])
    >>> [2,1,0,3]
    
    Example 3:
    
    >>> smallerNumbersThanCurrent(nums = [7,7,7,7])
    >>> [0,0,0,0]
    """
def rankTeams(votes: List[str]) -> str:
    """
    In a special ranking system, each voter gives a rank from highest to lowest to all teams participating in the competition.
    The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.
    You are given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.
    Return a string of all teams sorted by the ranking system.
    
    Example 1:
    
    >>> rankTeams(votes = ["ABC","ACB","ABC","ACB","ACB"])
    >>> "ACB"
    Explanation:
    Team A was ranked first place by 5 voters. No other team was voted as first place, so team A is the first team.
    Team B was ranked second by 2 voters and ranked third by 3 voters.
    Team C was ranked second by 3 voters and ranked third by 2 voters.
    As most of the voters ranked C second, team C is the second team, and team B is the third.
    
    Example 2:
    
    >>> rankTeams(votes = ["WXYZ","XYZW"])
    >>> "XWYZ"
    Explanation:
    X is the winner due to the tie-breaking rule. X has the same votes as W for the first position, but X has one vote in the second position, while W does not have any votes in the second position.
    
    Example 3:
    
    >>> rankTeams(votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"])
    >>> "ZMNAGUEDSJYLBOPHRQICWFXTVK"
    Explanation: Only one voter, so their votes are used for the ranking.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSubPath(head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
    """
    Given a binary tree root and a linked list with head as the first node.
    Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.
    In this context downward path means a path that starts at some node and goes downwards.
    
    Example 1:
    
    
    >>> __init__(head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3])
    >>> true
    Explanation: Nodes in blue form a subpath in the binary Tree.
    
    Example 2:
    
    
    >>> __init__(head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3])
    >>> true
    
    Example 3:
    
    >>> __init__(head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3])
    >>> false
    Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.
    """
def minCost(grid: List[List[int]]) -> int:
    """
    Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:
    
    1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1])
    2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1])
    3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j])
    4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j])
    
    Notice that there could be some signs on the cells of the grid that point outside the grid.
    You will initially start at the upper left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path does not have to be the shortest.
    You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only.
    Return the minimum cost to make the grid have at least one valid path.
    
    Example 1:
    
    
    >>> minCost(grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]])
    >>> 3
    Explanation: You will start at point (0, 0).
    The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
    The total cost = 3.
    
    Example 2:
    
    
    >>> minCost(grid = [[1,1,3],[3,2,2],[1,1,4]])
    >>> 0
    Explanation: You can follow the path from (0, 0) to (2, 2).
    
    Example 3:
    
    
    >>> minCost(grid = [[1,2],[4,3]])
    >>> 1
    """
def sortString(s: str) -> str:
    """
    You are given a string s. Reorder the string using the following algorithm:
    
    Remove the smallest character from s and append it to the result.
    Remove the smallest character from s that is greater than the last appended character, and append it to the result.
    Repeat step 2 until no more characters can be removed.
    Remove the largest character from s and append it to the result.
    Remove the largest character from s that is smaller than the last appended character, and append it to the result.
    Repeat step 5 until no more characters can be removed.
    Repeat steps 1 through 6 until all characters from s have been removed.
    
    If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.
    Return the resulting string after reordering s using this algorithm.
    
    Example 1:
    
    >>> sortString(s = "aaaabbbbcccc")
    >>> "abccbaabccba"
    Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
    After steps 4, 5 and 6 of the first iteration, result = "abccba"
    First iteration is done. Now s = "aabbcc" and we go back to step 1
    After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
    After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
    
    Example 2:
    
    >>> sortString(s = "rat")
    >>> "art"
    Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.
    """
def findTheLongestSubstring(s: str) -> int:
    """
    Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.
    
    Example 1:
    
    >>> findTheLongestSubstring(s = "eleetminicoworoep")
    >>> 13
    Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
    
    Example 2:
    
    >>> findTheLongestSubstring(s = "leetcodeisgreat")
    >>> 5
    Explanation: The longest substring is "leetc" which contains two e's.
    
    Example 3:
    
    >>> findTheLongestSubstring(s = "bcbcbc")
    >>> 6
    Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def longestZigZag(root: Optional[TreeNode]) -> int:
    """
    You are given the root of a binary tree.
    A ZigZag path for a binary tree is defined as follow:
    
    Choose any node in the binary tree and a direction (right or left).
    If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
    Change the direction from right to left or from left to right.
    Repeat the second and third steps until you can't move in the tree.
    
    Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).
    Return the longest ZigZag path contained in that tree.
    
    Example 1:
    
    
    >>> __init__(root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1])
    >>> 3
    Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
    
    Example 2:
    
    
    >>> __init__(root = [1,1,1,null,1,null,null,1,1,null,1])
    >>> 4
    Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).
    
    Example 3:
    
    >>> __init__(root = [1])
    >>> 0
    """