ShwStone/humaneval-leetcode huggingface dataset:
def visibleMountains(peaks: List[List[int]]) -> int:
    """
    You are given a 0-indexed 2D integer array peaks where peaks[i] = [xi, yi] states that mountain i has a peak at coordinates (xi, yi). A mountain can be described as a right-angled isosceles triangle, with its base along the x-axis and a right angle at its peak. More formally, the gradients of ascending and descending the mountain are 1 and -1 respectively.
    A mountain is considered visible if its peak does not lie within another mountain (including the border of other mountains).
    Return the number of visible mountains.
    
    Example 1:
    
    
    >>> visibleMountains(peaks = [[2,2],[6,3],[5,4]])
    >>> 2
    Explanation: The diagram above shows the mountains.
    - Mountain 0 is visible since its peak does not lie within another mountain or its sides.
    - Mountain 1 is not visible since its peak lies within the side of mountain 2.
    - Mountain 2 is visible since its peak does not lie within another mountain or its sides.
    There are 2 mountains that are visible.
    Example 2:
    
    
    >>> visibleMountains(peaks = [[1,3],[1,3]])
    >>> 0
    Explanation: The diagram above shows the mountains (they completely overlap).
    Both mountains are not visible since their peaks lie within each other.
    """
def bestHand(ranks: List[int], suits: List[str]) -> str:
    """
    You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i].
    The following are the types of poker hands you can make from best to worst:
    
    "Flush": Five cards of the same suit.
    "Three of a Kind": Three cards of the same rank.
    "Pair": Two cards of the same rank.
    "High Card": Any single card.
    
    Return a string representing the best type of poker hand you can make with the given cards.
    Note that the return values are case-sensitive.
    
    Example 1:
    
    >>> bestHand(ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"])
    >>> "Flush"
    Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
    
    Example 2:
    
    >>> bestHand(ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"])
    >>> "Three of a Kind"
    Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
    Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
    Also note that other cards could be used to make the "Three of a Kind" hand.
    Example 3:
    
    >>> bestHand(ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"])
    >>> "Pair"
    Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
    Note that we cannot make a "Flush" or a "Three of a Kind".
    """
def zeroFilledSubarray(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of subarrays filled with 0.
    A subarray is a contiguous non-empty sequence of elements within an array.
    
    Example 1:
    
    >>> zeroFilledSubarray(nums = [1,3,0,0,2,0,0,4])
    >>> 6
    Explanation:
    There are 4 occurrences of [0] as a subarray.
    There are 2 occurrences of [0,0] as a subarray.
    There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.
    Example 2:
    
    >>> zeroFilledSubarray(nums = [0,0,0,2,0,0])
    >>> 9
    Explanation:
    There are 5 occurrences of [0] as a subarray.
    There are 3 occurrences of [0,0] as a subarray.
    There is 1 occurrence of [0,0,0] as a subarray.
    There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
    
    Example 3:
    
    >>> zeroFilledSubarray(nums = [2,10,2019])
    >>> 0
    Explanation: There is no subarray filled with 0. Therefore, we return 0.
    """
def shortestSequence(rolls: List[int], k: int) -> int:
    """
    You are given an integer array rolls of length n and an integer k. You roll a k sided dice numbered from 1 to k, n times, where the result of the ith roll is rolls[i].
    Return the length of the shortest sequence of rolls so that there's no such subsequence in rolls.
    A sequence of rolls of length len is the result of rolling a k sided dice len times.
    
    Example 1:
    
    >>> shortestSequence(rolls = [4,2,1,2,3,3,2,4,1], k = 4)
    >>> 3
    Explanation: Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls.
    Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls.
    The sequence [1, 4, 2] cannot be taken from rolls, so we return 3.
    Note that there are other sequences that cannot be taken from rolls.
    Example 2:
    
    >>> shortestSequence(rolls = [1,1,2,2], k = 2)
    >>> 2
    Explanation: Every sequence of rolls of length 1, [1], [2], can be taken from rolls.
    The sequence [2, 1] cannot be taken from rolls, so we return 2.
    Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest.
    
    Example 3:
    
    >>> shortestSequence(rolls = [1,1,3,2,2,2,3,3], k = 4)
    >>> 1
    Explanation: The sequence [4] cannot be taken from rolls, so we return 1.
    Note that there are other sequences that cannot be taken from rolls but [4] is the shortest.
    """
def repeatedCharacter(s: str) -> str:
    """
    Given a string s consisting of lowercase English letters, return the first letter to appear twice.
    Note:
    
    A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.
    s will contain at least one letter that appears twice.
    
    
    Example 1:
    
    >>> repeatedCharacter(s = "abccbaacz")
    >>> "c"
    Explanation:
    The letter 'a' appears on the indexes 0, 5 and 6.
    The letter 'b' appears on the indexes 1 and 4.
    The letter 'c' appears on the indexes 2, 3 and 7.
    The letter 'z' appears on the index 8.
    The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
    
    Example 2:
    
    >>> repeatedCharacter(s = "abcdd")
    >>> "d"
    Explanation:
    The only letter that appears twice is 'd' so we return 'd'.
    """
def equalPairs(grid: List[List[int]]) -> int:
    """
    Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.
    A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
    
    Example 1:
    
    
    >>> equalPairs(grid = [[3,2,1],[1,7,6],[2,7,7]])
    >>> 1
    Explanation: There is 1 equal row and column pair:
    - (Row 2, Column 1): [2,7,7]
    
    Example 2:
    
    
    >>> equalPairs(grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]])
    >>> 3
    Explanation: There are 3 equal row and column pairs:
    - (Row 0, Column 0): [3,1,2,2]
    - (Row 2, Column 2): [2,4,2,2]
    - (Row 3, Column 2): [2,4,2,2]
    """
def countExcellentPairs(nums: List[int], k: int) -> int:
    """
    You are given a 0-indexed positive integer array nums and a positive integer k.
    A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied:
    
    Both the numbers num1 and num2 exist in the array nums.
    The sum of the number of set bits in num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation.
    
    Return the number of distinct excellent pairs.
    Two pairs (a, b) and (c, d) are considered distinct if either a != c or b != d. For example, (1, 2) and (2, 1) are distinct.
    Note that a pair (num1, num2) such that num1 == num2 can also be excellent if you have at least one occurrence of num1 in the array.
    
    Example 1:
    
    >>> countExcellentPairs(nums = [1,2,3,1], k = 3)
    >>> 5
    Explanation: The excellent pairs are the following:
    - (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
    - (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
    - (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
    So the number of excellent pairs is 5.
    Example 2:
    
    >>> countExcellentPairs(nums = [5,1,1], k = 10)
    >>> 0
    Explanation: There are no excellent pairs for this array.
    """
def maximumBooks(books: List[int]) -> int:
    """
    You are given a 0-indexed integer array books of length n where books[i] denotes the number of books on the ith shelf of a bookshelf.
    You are going to take books from a contiguous section of the bookshelf spanning from l to r where 0 <= l <= r < n. For each index i in the range l <= i < r, you must take strictly fewer books from shelf i than shelf i + 1.
    Return the maximum number of books you can take from the bookshelf.
    
    Example 1:
    
    >>> maximumBooks(books = [8,5,2,7,9])
    >>> 19
    Explanation:
    - Take 1 book from shelf 1.
    - Take 2 books from shelf 2.
    - Take 7 books from shelf 3.
    - Take 9 books from shelf 4.
    You have taken 19 books, so return 19.
    It can be proven that 19 is the maximum number of books you can take.
    
    Example 2:
    
    >>> maximumBooks(books = [7,0,3,4,5])
    >>> 12
    Explanation:
    - Take 3 books from shelf 2.
    - Take 4 books from shelf 3.
    - Take 5 books from shelf 4.
    You have taken 12 books so return 12.
    It can be proven that 12 is the maximum number of books you can take.
    
    Example 3:
    
    >>> maximumBooks(books = [8,2,3,7,3,4,0,1,4,3])
    >>> 13
    Explanation:
    - Take 1 book from shelf 0.
    - Take 2 books from shelf 1.
    - Take 3 books from shelf 2.
    - Take 7 books from shelf 3.
    You have taken 13 books so return 13.
    It can be proven that 13 is the maximum number of books you can take.
    """
def minimumOperations(nums: List[int]) -> int:
    """
    You are given a non-negative integer array nums. In one operation, you must:
    
    Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums.
    Subtract x from every positive element in nums.
    
    Return the minimum number of operations to make every element in nums equal to 0.
    
    Example 1:
    
    >>> minimumOperations(nums = [1,5,0,3,5])
    >>> 3
    Explanation:
    In the first operation, choose x = 1. Now, nums = [0,4,0,2,4].
    In the second operation, choose x = 2. Now, nums = [0,2,0,0,2].
    In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
    
    Example 2:
    
    >>> minimumOperations(nums = [0])
    >>> 0
    Explanation: Each element in nums is already 0 so no operations are needed.
    """
def maximumGroups(grades: List[int]) -> int:
    """
    You are given a positive integer array grades which represents the grades of students in a university. You would like to enter all these students into a competition in ordered non-empty groups, such that the ordering meets the following conditions:
    
    The sum of the grades of students in the ith group is less than the sum of the grades of students in the (i + 1)th group, for all groups (except the last).
    The total number of students in the ith group is less than the total number of students in the (i + 1)th group, for all groups (except the last).
    
    Return the maximum number of groups that can be formed.
    
    Example 1:
    
    >>> maximumGroups(grades = [10,6,12,7,3,5])
    >>> 3
    Explanation: The following is a possible way to form 3 groups of students:
    - 1st group has the students with grades = [12]. Sum of grades: 12. Student count: 1
    - 2nd group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
    - 3rd group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
    It can be shown that it is not possible to form more than 3 groups.
    
    Example 2:
    
    >>> maximumGroups(grades = [8,8])
    >>> 1
    Explanation: We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.
    """
def closestMeetingNode(edges: List[int], node1: int, node2: int) -> int:
    """
    You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
    The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from i, then edges[i] == -1.
    You are also given two integers node1 and node2.
    Return the index of the node that can be reached from both node1 and node2, such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return -1.
    Note that edges may contain cycles.
    
    Example 1:
    
    
    >>> closestMeetingNode(edges = [2,2,3,-1], node1 = 0, node2 = 1)
    >>> 2
    Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
    The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
    
    Example 2:
    
    
    >>> closestMeetingNode(edges = [1,2,-1], node1 = 0, node2 = 2)
    >>> 2
    Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
    The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
    """
def longestCycle(edges: List[int]) -> int:
    """
    You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
    The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from node i, then edges[i] == -1.
    Return the length of the longest cycle in the graph. If no cycle exists, return -1.
    A cycle is a path that starts and ends at the same node.
    
    Example 1:
    
    
    >>> longestCycle(edges = [3,3,4,2,3])
    >>> 3
    Explanation: The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
    The length of this cycle is 3, so 3 is returned.
    
    Example 2:
    
    
    >>> longestCycle(edges = [2,-1,3,1])
    >>> -1
    Explanation: There are no cycles in this graph.
    """
def minimumCosts(regular: List[int], express: List[int], expressCost: int) -> List[int]:
    """
    A train line going through a city has two routes, the regular route and the express route. Both routes go through the same n + 1 stops labeled from 0 to n. Initially, you start on the regular route at stop 0.
    You are given two 1-indexed integer arrays regular and express, both of length n. regular[i] describes the cost it takes to go from stop i - 1 to stop i using the regular route, and express[i] describes the cost it takes to go from stop i - 1 to stop i using the express route.
    You are also given an integer expressCost which represents the cost to transfer from the regular route to the express route.
    Note that:
    
    There is no cost to transfer from the express route back to the regular route.
    You pay expressCost every time you transfer from the regular route to the express route.
    There is no extra cost to stay on the express route.
    
    Return a 1-indexed array costs of length n, where costs[i] is the minimum cost to reach stop i from stop 0.
    Note that a stop can be counted as reached from either route.
    
    Example 1:
    
    
    >>> minimumCosts(regular = [1,6,9,5], express = [5,2,3,10], expressCost = 8)
    >>> [1,7,14,19]
    Explanation: The diagram above shows how to reach stop 4 from stop 0 with minimum cost.
    - Take the regular route from stop 0 to stop 1, costing 1.
    - Take the express route from stop 1 to stop 2, costing 8 + 2 = 10.
    - Take the express route from stop 2 to stop 3, costing 3.
    - Take the regular route from stop 3 to stop 4, costing 5.
    The total cost is 1 + 10 + 3 + 5 = 19.
    Note that a different route could be taken to reach the other stops with minimum cost.
    
    Example 2:
    
    
    >>> minimumCosts(regular = [11,5,13], express = [7,10,6], expressCost = 3)
    >>> [10,15,24]
    Explanation: The diagram above shows how to reach stop 3 from stop 0 with minimum cost.
    - Take the express route from stop 0 to stop 1, costing 3 + 7 = 10.
    - Take the regular route from stop 1 to stop 2, costing 5.
    - Take the express route from stop 2 to stop 3, costing 3 + 6 = 9.
    The total cost is 10 + 5 + 9 = 24.
    Note that the expressCost is paid again to transfer back to the express route.
    """
def mergeSimilarItems(items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
    """
    You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties:
    
    items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item.
    The value of each item in items is unique.
    
    Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items with value valuei.
    Note: ret should be returned in ascending order by value.
    
    Example 1:
    
    >>> mergeSimilarItems(items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]])
    >>> [[1,6],[3,9],[4,5]]
    Explanation:
    The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.
    The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.
    The item with value = 4 occurs in items1 with weight = 5, total weight = 5.
    Therefore, we return [[1,6],[3,9],[4,5]].
    
    Example 2:
    
    >>> mergeSimilarItems(items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]])
    >>> [[1,4],[2,4],[3,4]]
    Explanation:
    The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.
    The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.
    The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
    Therefore, we return [[1,4],[2,4],[3,4]].
    Example 3:
    
    >>> mergeSimilarItems(items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]])
    >>> [[1,7],[2,4],[7,1]]
    Explanation:
    The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7.
    The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
    The item with value = 7 occurs in items2 with weight = 1, total weight = 1.
    Therefore, we return [[1,7],[2,4],[7,1]].
    """
def countBadPairs(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair if i < j and j - i != nums[j] - nums[i].
    Return the total number of bad pairs in nums.
    
    Example 1:
    
    >>> countBadPairs(nums = [4,1,3,3])
    >>> 5
    Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
    The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
    The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
    The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
    The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
    There are a total of 5 bad pairs, so we return 5.
    
    Example 2:
    
    >>> countBadPairs(nums = [1,2,3,4,5])
    >>> 0
    Explanation: There are no bad pairs.
    """
def taskSchedulerII(tasks: List[int], space: int) -> int:
    """
    You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task.
    You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed.
    Each day, until all tasks have been completed, you must either:
    
    Complete the next task from tasks, or
    Take a break.
    
    Return the minimum number of days needed to complete all tasks.
    
    Example 1:
    
    >>> taskSchedulerII(tasks = [1,2,1,2,3,1], space = 3)
    >>> 9
    Explanation:
    One way to complete all tasks in 9 days is as follows:
    Day 1: Complete the 0th task.
    Day 2: Complete the 1st task.
    Day 3: Take a break.
    Day 4: Take a break.
    Day 5: Complete the 2nd task.
    Day 6: Complete the 3rd task.
    Day 7: Take a break.
    Day 8: Complete the 4th task.
    Day 9: Complete the 5th task.
    It can be shown that the tasks cannot be completed in less than 9 days.
    
    Example 2:
    
    >>> taskSchedulerII(tasks = [5,8,8,5], space = 2)
    >>> 6
    Explanation:
    One way to complete all tasks in 6 days is as follows:
    Day 1: Complete the 0th task.
    Day 2: Complete the 1st task.
    Day 3: Take a break.
    Day 4: Take a break.
    Day 5: Complete the 2nd task.
    Day 6: Complete the 3rd task.
    It can be shown that the tasks cannot be completed in less than 6 days.
    """
def minimumReplacement(nums: List[int]) -> int:
    """
    You are given a 0-indexed integer array nums. In one operation you can replace any element of the array with any two elements that sum to it.
    
    For example, consider nums = [5,6,7]. In one operation, we can replace nums[1] with 2 and 4 and convert nums to [5,2,4,7].
    
    Return the minimum number of operations to make an array that is sorted in non-decreasing order.
    
    Example 1:
    
    >>> minimumReplacement(nums = [3,9,3])
    >>> 2
    Explanation: Here are the steps to sort the array in non-decreasing order:
    - From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
    - From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
    There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
    
    
    Example 2:
    
    >>> minimumReplacement(nums = [1,2,3,4,5])
    >>> 0
    Explanation: The array is already in non-decreasing order. Therefore, we return 0.
    """
def arithmeticTriplets(nums: List[int], diff: int) -> int:
    """
    You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:
    
    i < j < k,
    nums[j] - nums[i] == diff, and
    nums[k] - nums[j] == diff.
    
    Return the number of unique arithmetic triplets.
    
    Example 1:
    
    >>> arithmeticTriplets(nums = [0,1,4,6,7,10], diff = 3)
    >>> 2
    Explanation:
    (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
    (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
    
    Example 2:
    
    >>> arithmeticTriplets(nums = [4,5,6,7,8,9], diff = 2)
    >>> 2
    Explanation:
    (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
    (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
    """