ShwStone/humaneval-leetcode huggingface dataset:
def maxScore(cardPoints: List[int], k: int) -> int:
    """
    There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.
    In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
    Your score is the sum of the points of the cards you have taken.
    Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
    
    Example 1:
    
    >>> maxScore(cardPoints = [1,2,3,4,5,6,1], k = 3)
    >>> 12
    Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
    
    Example 2:
    
    >>> maxScore(cardPoints = [2,2,2], k = 2)
    >>> 4
    Explanation: Regardless of which two cards you take, your score will always be 4.
    
    Example 3:
    
    >>> maxScore(cardPoints = [9,7,7,9,7,7,9], k = 7)
    >>> 55
    Explanation: You have to take all the cards. Your score is the sum of points of all cards.
    """
def findDiagonalOrder(nums: List[List[int]]) -> List[int]:
    """
    Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.
    
    Example 1:
    
    
    >>> findDiagonalOrder(nums = [[1,2,3],[4,5,6],[7,8,9]])
    >>> [1,4,2,7,5,3,8,6,9]
    
    Example 2:
    
    
    >>> findDiagonalOrder(nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]])
    >>> [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
    """
def constrainedSubsetSum(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.
    A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.
    
    Example 1:
    
    >>> constrainedSubsetSum(nums = [10,2,-10,5,20], k = 2)
    >>> 37
    Explanation: The subsequence is [10, 2, 5, 20].
    
    Example 2:
    
    >>> constrainedSubsetSum(nums = [-1,-2,-3], k = 1)
    >>> -1
    Explanation: The subsequence must be non-empty, so we choose the largest number.
    
    Example 3:
    
    >>> constrainedSubsetSum(nums = [10,-2,-10,-5,20], k = 2)
    >>> 23
    Explanation: The subsequence is [10, -2, -5, 20].
    """
def countElements(arr: List[int]) -> int:
    """
    Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them separately.
    
    Example 1:
    
    >>> countElements(arr = [1,2,3])
    >>> 2
    Explanation: 1 and 2 are counted cause 2 and 3 are in arr.
    
    Example 2:
    
    >>> countElements(arr = [1,1,3,3,5,5,7,7])
    >>> 0
    Explanation: No numbers are counted, cause there is no 2, 4, 6, or 8 in arr.
    """
def stringShift(s: str, shift: List[List[int]]) -> str:
    """
    You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [directioni, amounti]:
    
    directioni can be 0 (for left shift) or 1 (for right shift).
    amounti is the amount by which string s is to be shifted.
    A left shift by 1 means remove the first character of s and append it to the end.
    Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.
    
    Return the final string after all operations.
    
    Example 1:
    
    >>> stringShift(s = "abc", shift = [[0,1],[1,2]])
    >>> "cab"
    Explanation:
    [0,1] means shift to left by 1. "abc" -> "bca"
    [1,2] means shift to right by 2. "bca" -> "cab"
    Example 2:
    
    >>> stringShift(s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]])
    >>> "efgabcd"
    Explanation:
    [1,1] means shift to right by 1. "abcdefg" -> "gabcdef"
    [1,1] means shift to right by 1. "gabcdef" -> "fgabcde"
    [0,2] means shift to left by 2. "fgabcde" -> "abcdefg"
    [1,3] means shift to right by 3. "abcdefg" -> "efgabcd"
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidSequence(root: Optional[TreeNode], arr: List[int]) -> bool:
    """
    Given a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such binary tree.
    We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree.
    
    Example 1:
    
    
    >>> __init__(root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1])
    >>> true
    Explanation:
    The path 0 -> 1 -> 0 -> 1 is a valid sequence (green color in the figure).
    Other valid sequences are:
    0 -> 1 -> 1 -> 0
    0 -> 0 -> 0
    
    Example 2:
    
    
    >>> __init__(root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,0,1])
    >>> false
    Explanation: The path 0 -> 0 -> 1 does not exist, therefore it is not even a sequence.
    
    Example 3:
    
    
    >>> __init__(root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1])
    >>> false
    Explanation: The path 0 -> 1 -> 1 is a sequence, but it is not a valid sequence.
    """
def kidsWithCandies(candies: List[int], extraCandies: int) -> List[bool]:
    """
    There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.
    Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.
    Note that multiple kids can have the greatest number of candies.
    
    Example 1:
    
    >>> kidsWithCandies(candies = [2,3,5,1,3], extraCandies = 3)
    >>> [true,true,true,false,true]
    Explanation: If you give all extraCandies to:
    - Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
    - Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
    - Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
    - Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
    - Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
    
    Example 2:
    
    >>> kidsWithCandies(candies = [4,2,1,1,2], extraCandies = 1)
    >>> [true,false,false,false,false]
    Explanation: There is only 1 extra candy.
    Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
    
    Example 3:
    
    >>> kidsWithCandies(candies = [12,1,12], extraCandies = 10)
    >>> [true,false,true]
    """
def maxDiff(num: int) -> int:
    """
    You are given an integer num. You will apply the following steps exactly two times:
    
    Pick a digit x (0 <= x <= 9).
    Pick another digit y (0 <= y <= 9). The digit y can be equal to x.
    Replace all the occurrences of x in the decimal representation of num by y.
    The new integer cannot have any leading zeros, also the new integer cannot be 0.
    
    Let a and b be the results of applying the operations to num the first and second times, respectively.
    Return the max difference between a and b.
    
    Example 1:
    
    >>> maxDiff(num = 555)
    >>> 888
    Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
    The second time pick x = 5 and y = 1 and store the new integer in b.
    We have now a = 999 and b = 111 and max difference = 888
    
    Example 2:
    
    >>> maxDiff(num = 9)
    >>> 8
    Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
    The second time pick x = 9 and y = 1 and store the new integer in b.
    We have now a = 9 and b = 1 and max difference = 8
    """
def checkIfCanBreak(s1: str, s2: str) -> bool:
    """
    Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa. In other words s2 can break s1 or vice-versa.
    A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
    
    Example 1:
    
    >>> checkIfCanBreak(s1 = "abc", s2 = "xya")
    >>> true
    Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
    
    Example 2:
    
    >>> checkIfCanBreak(s1 = "abe", s2 = "acd")
    >>> false
    Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
    
    Example 3:
    
    >>> checkIfCanBreak(s1 = "leetcodee", s2 = "interview")
    >>> true
    """
def numberWays(hats: List[List[int]]) -> int:
    """
    There are n people and 40 types of hats labeled from 1 to 40.
    Given a 2D integer array hats, where hats[i] is a list of all hats preferred by the ith person.
    Return the number of ways that n people can wear different hats from each other.
    Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    >>> numberWays(hats = [[3,4],[4,5],[5]])
    >>> 1
    Explanation: There is only one way to choose hats given the conditions.
    First person choose hat 3, Second person choose hat 4 and last one hat 5.
    
    Example 2:
    
    >>> numberWays(hats = [[3,5,1],[3,5]])
    >>> 4
    Explanation: There are 4 ways to choose hats:
    (3,5), (5,3), (1,3) and (1,5)
    
    Example 3:
    
    >>> numberWays(hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])
    >>> 24
    Explanation: Each person can choose hats labeled from 1 to 4.
    Number of Permutations of (1,2,3,4) = 24.
    """
def destCity(paths: List[List[str]]) -> str:
    """
    You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
    It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
    
    Example 1:
    
    >>> destCity(paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]])
    >>> "Sao Paulo"
    Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
    
    Example 2:
    
    >>> destCity(paths = [["B","C"],["D","B"],["C","A"]])
    >>> "A"
    Explanation: All possible trips are:
    "D" -> "B" -> "C" -> "A".
    "B" -> "C" -> "A".
    "C" -> "A".
    "A".
    Clearly the destination city is "A".
    
    Example 3:
    
    >>> destCity(paths = [["A","Z"]])
    >>> "Z"
    """
def kLengthApart(nums: List[int], k: int) -> bool:
    """
    Given an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.
    
    Example 1:
    
    
    >>> kLengthApart(nums = [1,0,0,0,1,0,0,1], k = 2)
    >>> true
    Explanation: Each of the 1s are at least 2 places away from each other.
    
    Example 2:
    
    
    >>> kLengthApart(nums = [1,0,0,1,0,1], k = 2)
    >>> false
    Explanation: The second 1 and third 1 are only one apart from each other.
    """
def longestSubarray(nums: List[int], limit: int) -> int:
    """
    Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
    
    Example 1:
    
    >>> longestSubarray(nums = [8,2,4,7], limit = 4)
    >>> 2
    Explanation: All subarrays are:
    [8] with maximum absolute diff |8-8| = 0 <= 4.
    [8,2] with maximum absolute diff |8-2| = 6 > 4.
    [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
    [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
    [2] with maximum absolute diff |2-2| = 0 <= 4.
    [2,4] with maximum absolute diff |2-4| = 2 <= 4.
    [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
    [4] with maximum absolute diff |4-4| = 0 <= 4.
    [4,7] with maximum absolute diff |4-7| = 3 <= 4.
    [7] with maximum absolute diff |7-7| = 0 <= 4.
    Therefore, the size of the longest subarray is 2.
    
    Example 2:
    
    >>> longestSubarray(nums = [10,1,2,4,7,2], limit = 5)
    >>> 4
    Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
    
    Example 3:
    
    >>> longestSubarray(nums = [4,2,2,2,4,4,2,2], limit = 0)
    >>> 3
    """
def kthSmallest(mat: List[List[int]], k: int) -> int:
    """
    You are given an m x n matrix mat that has its rows sorted in non-decreasing order and an integer k.
    You are allowed to choose exactly one element from each row to form an array.
    Return the kth smallest array sum among all possible arrays.
    
    Example 1:
    
    >>> kthSmallest(mat = [[1,3,11],[2,4,6]], k = 5)
    >>> 7
    Explanation: Choosing one element from each row, the first k smallest sum are:
    [1,2], [1,4], [3,2], [3,4], [1,6]. Where the 5th sum is 7.
    
    Example 2:
    
    >>> kthSmallest(mat = [[1,3,11],[2,4,6]], k = 9)
    >>> 17
    
    Example 3:
    
    >>> kthSmallest(mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7)
    >>> 9
    Explanation: Choosing one element from each row, the first k smallest sum are:
    [1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]. Where the 7th sum is 9.
    """
def buildArray(target: List[int], n: int) -> List[str]:
    """
    You are given an integer array target and an integer n.
    You have an empty stack with the two following operations:
    
    "Push": pushes an integer to the top of the stack.
    "Pop": removes the integer on the top of the stack.
    
    You also have a stream of the integers in the range [1, n].
    Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
    
    If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
    If the stack is not empty, pop the integer at the top of the stack.
    If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
    
    Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.
    
    Example 1:
    
    >>> buildArray(target = [1,3], n = 3)
    >>> ["Push","Push","Pop","Push"]
    Explanation: Initially the stack s is empty. The last element is the top of the stack.
    Read 1 from the stream and push it to the stack. s = [1].
    Read 2 from the stream and push it to the stack. s = [1,2].
    Pop the integer on the top of the stack. s = [1].
    Read 3 from the stream and push it to the stack. s = [1,3].
    
    Example 2:
    
    >>> buildArray(target = [1,2,3], n = 3)
    >>> ["Push","Push","Push"]
    Explanation: Initially the stack s is empty. The last element is the top of the stack.
    Read 1 from the stream and push it to the stack. s = [1].
    Read 2 from the stream and push it to the stack. s = [1,2].
    Read 3 from the stream and push it to the stack. s = [1,2,3].
    
    Example 3:
    
    >>> buildArray(target = [1,2], n = 4)
    >>> ["Push","Push"]
    Explanation: Initially the stack s is empty. The last element is the top of the stack.
    Read 1 from the stream and push it to the stack. s = [1].
    Read 2 from the stream and push it to the stack. s = [1,2].
    Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
    The answers that read integer 3 from the stream are not accepted.
    """
def countTriplets(arr: List[int]) -> int:
    """
    Given an array of integers arr.
    We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
    Let's define a and b as follows:
    
    a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
    b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
    
    Note that ^ denotes the bitwise-xor operation.
    Return the number of triplets (i, j and k) Where a == b.
    
    Example 1:
    
    >>> countTriplets(arr = [2,3,1,6,7])
    >>> 4
    Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
    
    Example 2:
    
    >>> countTriplets(arr = [1,1,1,1,1])
    >>> 10
    """
def ways(pizza: List[str], k: int) -> int:
    """
    Given a rectangular pizza represented as a rows x cols matrix containing the following characters: 'A' (an apple) and '.' (empty cell) and given the integer k. You have to cut the pizza into k pieces using k-1 cuts.
    For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.
    Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.
    
    Example 1:
    
    
    >>> ways(pizza = ["A..","AAA","..."], k = 3)
    >>> 3
    Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
    
    Example 2:
    
    >>> ways(pizza = ["A..","AA.","..."], k = 3)
    >>> 1
    
    Example 3:
    
    >>> ways(pizza = ["A..","A..","..."], k = 1)
    >>> 1
    """
def maxPower(s: str) -> int:
    """
    The power of the string is the maximum length of a non-empty substring that contains only one unique character.
    Given a string s, return the power of s.
    
    Example 1:
    
    >>> maxPower(s = "leetcode")
    >>> 2
    Explanation: The substring "ee" is of length 2 with the character 'e' only.
    
    Example 2:
    
    >>> maxPower(s = "abbcccddddeeeeedcba")
    >>> 5
    Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
    """
def simplifiedFractions(n: int) -> List[str]:
    """
    Given an integer n, return a list of all simplified fractions between 0 and 1 (exclusive) such that the denominator is less-than-or-equal-to n. You can return the answer in any order.
    
    Example 1:
    
    >>> simplifiedFractions(n = 2)
    >>> ["1/2"]
    Explanation: "1/2" is the only unique fraction with a denominator less-than-or-equal-to 2.
    
    Example 2:
    
    >>> simplifiedFractions(n = 3)
    >>> ["1/2","1/3","2/3"]
    
    Example 3:
    
    >>> simplifiedFractions(n = 4)
    >>> ["1/2","1/3","1/4","2/3","3/4"]
    Explanation: "2/4" is not a simplified fraction because it can be simplified to "1/2".
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def goodNodes(root: Optional[TreeNode]) -> int:
    """
    Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.\r
    \r
    Return the number of good nodes in the binary tree.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    \r
    \r
    >>> __init__(root = [3,1,4,3,null,1,5]\r)
    >>> 4\r
    Explanation: Nodes in blue are good.\r
    Root Node (3) is always a good node.\r
    Node 4 -> (3,4) is the maximum value in the path starting from the root.\r
    Node 5 -> (3,4,5) is the maximum value in the path\r
    Node 3 -> (3,1,3) is the maximum value in the path.\r
    \r
    Example 2:\r
    \r
    \r
    \r
    \r
    >>> __init__(root = [3,3,null,4,2]\r)
    >>> 3\r
    Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.\r
    \r
    Example 3:\r
    \r
    \r
    >>> __init__(root = [1]\r)
    >>> 1\r
    Explanation: Root is considered as good.\r
    \r
     \r
    """
def largestNumber(cost: List[int], target: int) -> str:
    """
    Given an array of integers cost and an integer target, return the maximum integer you can paint under the following rules:
    
    The cost of painting a digit (i + 1) is given by cost[i] (0-indexed).
    The total cost used must be equal to target.
    The integer does not have 0 digits.
    
    Since the answer may be very large, return it as a string. If there is no way to paint any integer given the condition, return "0".
    
    Example 1:
    
    >>> largestNumber(cost = [4,3,2,5,6,7,2,5,5], target = 9)
    >>> "7772"
    Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number.
    Digit    cost
      1  ->   4
      2  ->   3
      3  ->   2
      4  ->   5
      5  ->   6
      6  ->   7
      7  ->   2
      8  ->   5
      9  ->   5
    
    Example 2:
    
    >>> largestNumber(cost = [7,6,5,5,5,6,8,7,8], target = 12)
    >>> "85"
    Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12.
    
    Example 3:
    
    >>> largestNumber(cost = [2,4,6,2,4,6,4,4,4], target = 5)
    >>> "0"
    Explanation: It is impossible to paint any integer with total cost equal to target.
    """
def busyStudent(startTime: List[int], endTime: List[int], queryTime: int) -> int:
    """
    Given two integer arrays startTime and endTime and given an integer queryTime.
    The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].
    Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.
    
    Example 1:
    
    >>> busyStudent(startTime = [1,2,3], endTime = [3,2,7], queryTime = 4)
    >>> 1
    Explanation: We have 3 students where:
    The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.
    The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.
    The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.
    
    Example 2:
    
    >>> busyStudent(startTime = [4], endTime = [4], queryTime = 4)
    >>> 1
    Explanation: The only student was doing their homework at the queryTime.
    """