ShwStone/humaneval-leetcode huggingface dataset:
def platesBetweenCandles(s: str, queries: List[List[int]]) -> List[int]:
    """
    There is a long table with a line of plates and candles arranged on top of it. You are given a 0-indexed string s consisting of characters '*' and '|' only, where a '*' represents a plate and a '|' represents a candle.
    You are also given a 0-indexed 2D integer array queries where queries[i] = [lefti, righti] denotes the substring s[lefti...righti] (inclusive). For each query, you need to find the number of plates between candles that are in the substring. A plate is considered between candles if there is at least one candle to its left and at least one candle to its right in the substring.
    
    For example, s = "||**||**|*", and a query [3, 8] denotes the substring "*||**|". The number of plates between candles in this substring is 2, as each of the two plates has at least one candle in the substring to its left and right.
    
    Return an integer array answer where answer[i] is the answer to the ith query.
    
    Example 1:
    
    
    >>> platesBetweenCandles(s = "**|**|***|", queries = [[2,5],[5,9]])
    >>> [2,3]
    Explanation:
    - queries[0] has two plates between candles.
    - queries[1] has three plates between candles.
    
    Example 2:
    
    
    >>> platesBetweenCandles(s = "***|**|*****|**||**|*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]])
    >>> [9,0,0,0,0]
    Explanation:
    - queries[0] has nine plates between candles.
    - The other queries have zero plates between candles.
    """
def countCombinations(pieces: List[str], positions: List[List[int]]) -> int:
    """
    There is an 8 x 8 chessboard containing n pieces (rooks, queens, or bishops). You are given a string array pieces of length n, where pieces[i] describes the type (rook, queen, or bishop) of the ith piece. In addition, you are given a 2D integer array positions also of length n, where positions[i] = [ri, ci] indicates that the ith piece is currently at the 1-based coordinate (ri, ci) on the chessboard.
    When making a move for a piece, you choose a destination square that the piece will travel toward and stop on.
    
    A rook can only travel horizontally or vertically from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), or (r, c-1).
    A queen can only travel horizontally, vertically, or diagonally from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), (r, c-1), (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1).
    A bishop can only travel diagonally from (r, c) to the direction of (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1).
    
    You must make a move for every piece on the board simultaneously. A move combination consists of all the moves performed on all the given pieces. Every second, each piece will instantaneously travel one square towards their destination if they are not already at it. All pieces start traveling at the 0th second. A move combination is invalid if, at a given time, two or more pieces occupy the same square.
    Return the number of valid move combinations​​​​​.
    Notes:
    
    No two pieces will start in the same square.
    You may choose the square a piece is already on as its destination.
    If two pieces are directly adjacent to each other, it is valid for them to move past each other and swap positions in one second.
    
    
    Example 1:
    
    
    >>> countCombinations(pieces = ["rook"], positions = [[1,1]])
    >>> 15
    Explanation: The image above shows the possible squares the piece can move to.
    
    Example 2:
    
    
    >>> countCombinations(pieces = ["queen"], positions = [[1,1]])
    >>> 22
    Explanation: The image above shows the possible squares the piece can move to.
    
    Example 3:
    
    
    >>> countCombinations(pieces = ["bishop"], positions = [[4,3]])
    >>> 12
    Explanation: The image above shows the possible squares the piece can move to.
    """
def smallestEqual(nums: List[int]) -> int:
    """
    Given a 0-indexed integer array nums, return the smallest index i of nums such that i mod 10 == nums[i], or -1 if such index does not exist.
    x mod y denotes the remainder when x is divided by y.
    
    Example 1:
    
    >>> smallestEqual(nums = [0,1,2])
    >>> 0
    Explanation:
    i=0: 0 mod 10 = 0 == nums[0].
    i=1: 1 mod 10 = 1 == nums[1].
    i=2: 2 mod 10 = 2 == nums[2].
    All indices have i mod 10 == nums[i], so we return the smallest index 0.
    
    Example 2:
    
    >>> smallestEqual(nums = [4,3,2,1])
    >>> 2
    Explanation:
    i=0: 0 mod 10 = 0 != nums[0].
    i=1: 1 mod 10 = 1 != nums[1].
    i=2: 2 mod 10 = 2 == nums[2].
    i=3: 3 mod 10 = 3 != nums[3].
    2 is the only index which has i mod 10 == nums[i].
    
    Example 3:
    
    >>> smallestEqual(nums = [1,2,3,4,5,6,7,8,9,0])
    >>> -1
    Explanation: No index satisfies i mod 10 == nums[i].
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nodesBetweenCriticalPoints(head: Optional[ListNode]) -> List[int]:
    """
    A critical point in a linked list is defined as either a local maxima or a local minima.
    A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.
    A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.
    Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.
    Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].
    
    Example 1:
    
    
    >>> __init__(head = [3,1])
    >>> [-1,-1]
    Explanation: There are no critical points in [3,1].
    
    Example 2:
    
    
    >>> __init__(head = [5,3,1,2,5,1,2])
    >>> [1,3]
    Explanation: There are three critical points:
    - [5,3,1,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.
    - [5,3,1,2,5,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.
    - [5,3,1,2,5,1,2]: The sixth node is a local minima because 1 is less than 5 and 2.
    The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.
    The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.
    
    Example 3:
    
    
    >>> __init__(head = [1,3,2,2,3,2,2,2,7])
    >>> [3,3]
    Explanation: There are two critical points:
    - [1,3,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.
    - [1,3,2,2,3,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.
    Both the minimum and maximum distances are between the second and the fifth node.
    Thus, minDistance and maxDistance is 5 - 2 = 3.
    Note that the last node is not considered a local maxima because it does not have a next node.
    """
def minimumOperations(nums: List[int], start: int, goal: int) -> int:
    """
    You are given a 0-indexed integer array nums containing distinct numbers, an integer start, and an integer goal. There is an integer x that is initially set to start, and you want to perform operations on x such that it is converted to goal. You can perform the following operation repeatedly on the number x:
    If 0 <= x <= 1000, then for any index i in the array (0 <= i < nums.length), you can set x to any of the following:
    
    x + nums[i]
    x - nums[i]
    x ^ nums[i] (bitwise-XOR)
    
    Note that you can use each nums[i] any number of times in any order. Operations that set x to be out of the range 0 <= x <= 1000 are valid, but no more operations can be done afterward.
    Return the minimum number of operations needed to convert x = start into goal, and -1 if it is not possible.
    
    Example 1:
    
    >>> minimumOperations(nums = [2,4,12], start = 2, goal = 12)
    >>> 2
    Explanation: We can go from 2 → 14 → 12 with the following 2 operations.
    - 2 + 12 = 14
    - 14 - 2 = 12
    
    Example 2:
    
    >>> minimumOperations(nums = [3,5,7], start = 0, goal = -4)
    >>> 2
    Explanation: We can go from 0 → 3 → -4 with the following 2 operations.
    - 0 + 3 = 3
    - 3 - 7 = -4
    Note that the last operation sets x out of the range 0 <= x <= 1000, which is valid.
    
    Example 3:
    
    >>> minimumOperations(nums = [2,8,16], start = 0, goal = 1)
    >>> -1
    Explanation: There is no way to convert 0 into 1.
    """
def possiblyEquals(s1: str, s2: str) -> bool:
    """
    An original string, consisting of lowercase English letters, can be encoded by the following steps:
    
    Arbitrarily split it into a sequence of some number of non-empty substrings.
    Arbitrarily choose some elements (possibly none) of the sequence, and replace each with its length (as a numeric string).
    Concatenate the sequence as the encoded string.
    
    For example, one way to encode an original string "abcdefghijklmnop" might be:
    
    Split it as a sequence: ["ab", "cdefghijklmn", "o", "p"].
    Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes ["ab", "12", "1", "p"].
    Concatenate the elements of the sequence to get the encoded string: "ab121p".
    
    Given two encoded strings s1 and s2, consisting of lowercase English letters and digits 1-9 (inclusive), return true if there exists an original string that could be encoded as both s1 and s2. Otherwise, return false.
    Note: The test cases are generated such that the number of consecutive digits in s1 and s2 does not exceed 3.
    
    Example 1:
    
    >>> possiblyEquals(s1 = "internationalization", s2 = "i18n")
    >>> true
    Explanation: It is possible that "internationalization" was the original string.
    - "internationalization"
      -> Split:       ["internationalization"]
      -> Do not replace any element
      -> Concatenate:  "internationalization", which is s1.
    - "internationalization"
      -> Split:       ["i", "nternationalizatio", "n"]
      -> Replace:     ["i", "18",                 "n"]
      -> Concatenate:  "i18n", which is s2
    
    Example 2:
    
    >>> possiblyEquals(s1 = "l123e", s2 = "44")
    >>> true
    Explanation: It is possible that "leetcode" was the original string.
    - "leetcode"
      -> Split:      ["l", "e", "et", "cod", "e"]
      -> Replace:    ["l", "1", "2",  "3",   "e"]
      -> Concatenate: "l123e", which is s1.
    - "leetcode"
      -> Split:      ["leet", "code"]
      -> Replace:    ["4",    "4"]
      -> Concatenate: "44", which is s2.
    
    Example 3:
    
    >>> possiblyEquals(s1 = "a5b", s2 = "c5b")
    >>> false
    Explanation: It is impossible.
    - The original string encoded as s1 must start with the letter 'a'.
    - The original string encoded as s2 must start with the letter 'c'.
    """
def numberOfCleanRooms(room: List[List[int]]) -> int:
    """
    A room is represented by a 0-indexed 2D binary matrix room where a 0 represents an empty space and a 1 represents a space with an object. The top left corner of the room will be empty in all test cases.
    A cleaning robot starts at the top left corner of the room and is facing right. The robot will continue heading straight until it reaches the edge of the room or it hits an object, after which it will turn 90 degrees clockwise and repeat this process. The starting space and all spaces that the robot visits are cleaned by it.
    Return the number of clean spaces in the room if the robot runs indefinitely.
    
    Example 1:
    
    
    
    >>> numberOfCleanRooms(room = [[0,0,0],[1,1,0],[0,0,0]])
    >>> 7
    Explanation:
    
    ​​​​​​​The robot cleans the spaces at (0, 0), (0, 1), and (0, 2).
    The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces down.
    The robot cleans the spaces at (1, 2), and (2, 2).
    The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces left.
    The robot cleans the spaces at (2, 1), and (2, 0).
    The robot has cleaned all 7 empty spaces, so return 7.
    
    
    Example 2:
    
    
    
    >>> numberOfCleanRooms(room = [[0,1,0],[1,0,0],[0,0,0]])
    >>> 1
    Explanation:
    
    The robot cleans the space at (0, 0).
    The robot hits an object, so it turns 90 degrees clockwise and now faces down.
    The robot hits an object, so it turns 90 degrees clockwise and now faces left.
    The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces up.
    The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces right.
    The robot is back at its starting position.
    The robot has cleaned 1 space, so return 1.
    
    
    Example 3:
    
    >>> numberOfCleanRooms(room = [[0,0,0],[0,0,0],[0,0,0]])
    >>> 8​​​​​​​
    """
def countVowelSubstrings(word: str) -> int:
    """
    A substring is a contiguous (non-empty) sequence of characters within a string.
    A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it.
    Given a string word, return the number of vowel substrings in word.
    
    Example 1:
    
    >>> countVowelSubstrings(word = "aeiouu")
    >>> 2
    Explanation: The vowel substrings of word are as follows (underlined):
    - "aeiouu"
    - "aeiouu"
    
    Example 2:
    
    >>> countVowelSubstrings(word = "unicornarihan")
    >>> 0
    Explanation: Not all 5 vowels are present, so there are no vowel substrings.
    
    Example 3:
    
    >>> countVowelSubstrings(word = "cuaieuouac")
    >>> 7
    Explanation: The vowel substrings of word are as follows (underlined):
    - "cuaieuouac"
    - "cuaieuouac"
    - "cuaieuouac"
    - "cuaieuouac"
    - "cuaieuouac"
    - "cuaieuouac"
    - "cuaieuouac"
    """
def countVowels(word: str) -> int:
    """
    Given a string word, return the sum of the number of vowels ('a', 'e', 'i', 'o', and 'u') in every substring of word.
    A substring is a contiguous (non-empty) sequence of characters within a string.
    Note: Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.
    
    Example 1:
    
    >>> countVowels(word = "aba")
    >>> 6
    Explanation:
    All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
    - "b" has 0 vowels in it
    - "a", "ab", "ba", and "a" have 1 vowel each
    - "aba" has 2 vowels in it
    Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6.
    
    Example 2:
    
    >>> countVowels(word = "abc")
    >>> 3
    Explanation:
    All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
    - "a", "ab", and "abc" have 1 vowel each
    - "b", "bc", and "c" have 0 vowels each
    Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3.
    
    Example 3:
    
    >>> countVowels(word = "ltcd")
    >>> 0
    Explanation: There are no vowels in any substring of "ltcd".
    """
def minimizedMaximum(n: int, quantities: List[int]) -> int:
    """
    You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.
    You need to distribute all products to the retail stores following these rules:
    
    A store can only be given at most one product type but can be given any amount of it.
    After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.
    
    Return the minimum possible x.
    
    Example 1:
    
    >>> minimizedMaximum(n = 6, quantities = [11,6])
    >>> 3
    Explanation: One optimal way is:
    - The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
    - The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
    The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
    
    Example 2:
    
    >>> minimizedMaximum(n = 7, quantities = [15,10,10])
    >>> 5
    Explanation: One optimal way is:
    - The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
    - The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
    - The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
    The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
    
    Example 3:
    
    >>> minimizedMaximum(n = 1, quantities = [100000])
    >>> 100000
    Explanation: The only optimal way is:
    - The 100000 products of type 0 are distributed to the only store.
    The maximum number of products given to any store is max(100000) = 100000.
    """
def maximalPathQuality(values: List[int], edges: List[List[int]], maxTime: int) -> int:
    """
    There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the ith node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and vj, and it takes timej seconds to travel between the two nodes. Finally, you are given an integer maxTime.
    A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum).
    Return the maximum quality of a valid path.
    Note: There are at most four edges connected to each node.
    
    Example 1:
    
    
    >>> maximalPathQuality(values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49)
    >>> 75
    Explanation:
    One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
    The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.
    
    Example 2:
    
    
    >>> maximalPathQuality(values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30)
    >>> 25
    Explanation:
    One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30.
    The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.
    
    Example 3:
    
    
    >>> maximalPathQuality(values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50)
    >>> 7
    Explanation:
    One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
    The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.
    """
def equalCountSubstrings(s: str, count: int) -> int:
    """
    You are given a 0-indexed string s consisting of only lowercase English letters, and an integer count. A substring of s is said to be an equal count substring if, for each unique letter in the substring, it appears exactly count times in the substring.
    Return the number of equal count substrings in s.
    A substring is a contiguous non-empty sequence of characters within a string.
    
    Example 1:
    
    >>> equalCountSubstrings(s = "aaabcbbcc", count = 3)
    >>> 3
    Explanation:
    The substring that starts at index 0 and ends at index 2 is "aaa".
    The letter 'a' in the substring appears exactly 3 times.
    The substring that starts at index 3 and ends at index 8 is "bcbbcc".
    The letters 'b' and 'c' in the substring appear exactly 3 times.
    The substring that starts at index 0 and ends at index 8 is "aaabcbbcc".
    The letters 'a', 'b', and 'c' in the substring appear exactly 3 times.
    
    Example 2:
    
    >>> equalCountSubstrings(s = "abcd", count = 2)
    >>> 0
    Explanation:
    The number of times each letter appears in s is less than count.
    Therefore, no substrings in s are equal count substrings, so return 0.
    
    Example 3:
    
    >>> equalCountSubstrings(s = "a", count = 5)
    >>> 0
    Explanation:
    The number of times each letter appears in s is less than count.
    Therefore, no substrings in s are equal count substrings, so return 0
    """
def checkAlmostEquivalent(word1: str, word2: str) -> bool:
    """
    Two strings word1 and word2 are considered almost equivalent if the differences between the frequencies of each letter from 'a' to 'z' between word1 and word2 is at most 3.
    Given two strings word1 and word2, each of length n, return true if word1 and word2 are almost equivalent, or false otherwise.
    The frequency of a letter x is the number of times it occurs in the string.
    
    Example 1:
    
    >>> checkAlmostEquivalent(word1 = "aaaa", word2 = "bccb")
    >>> false
    Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb".
    The difference is 4, which is more than the allowed 3.
    
    Example 2:
    
    >>> checkAlmostEquivalent(word1 = "abcdeef", word2 = "abaaacc")
    >>> true
    Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
    - 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
    - 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
    - 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
    - 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
    - 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
    - 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.
    
    Example 3:
    
    >>> checkAlmostEquivalent(word1 = "cccddabba", word2 = "babababab")
    >>> true
    Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
    - 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
    - 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
    - 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
    - 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.
    """
def maximumBeauty(items: List[List[int]], queries: List[int]) -> List[int]:
    """
    You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
    You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is less than or equal to queries[j]. If no such item exists, then the answer to this query is 0.
    Return an array answer of the same length as queries where answer[j] is the answer to the jth query.
    
    Example 1:
    
    >>> maximumBeauty(items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6])
    >>> [2,4,5,5,6,6]
    Explanation:
    - For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
    - For queries[1]=2, the items which can be considered are [1,2] and [2,4].
      The maximum beauty among them is 4.
    - For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
      The maximum beauty among them is 5.
    - For queries[4]=5 and queries[5]=6, all items can be considered.
      Hence, the answer for them is the maximum beauty of all items, i.e., 6.
    
    Example 2:
    
    >>> maximumBeauty(items = [[1,2],[1,2],[1,3],[1,4]], queries = [1])
    >>> [4]
    Explanation:
    The price of every item is equal to 1, so we choose the item with the maximum beauty 4.
    Note that multiple items can have the same price and/or beauty.
    
    Example 3:
    
    >>> maximumBeauty(items = [[10,1000]], queries = [5])
    >>> [0]
    Explanation:
    No item has a price less than or equal to 5, so no item can be chosen.
    Hence, the answer to the query is 0.
    """