ShwStone/humaneval-leetcode huggingface dataset:
def bestClosingTime(customers: str) -> int:
    """
    You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':
    
    if the ith character is 'Y', it means that customers come at the ith hour
    whereas 'N' indicates that no customers come at the ith hour.
    
    If the shop closes at the jth hour (0 <= j <= n), the penalty is calculated as follows:
    
    For every hour when the shop is open and no customers come, the penalty increases by 1.
    For every hour when the shop is closed and customers come, the penalty increases by 1.
    
    Return the earliest hour at which the shop must be closed to incur a minimum penalty.
    Note that if a shop closes at the jth hour, it means the shop is closed at the hour j.
    
    Example 1:
    
    >>> bestClosingTime(customers = "YYNY")
    >>> 2
    Explanation:
    - Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
    - Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
    - Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
    - Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
    - Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
    Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
    
    Example 2:
    
    >>> bestClosingTime(customers = "NNNNN")
    >>> 0
    Explanation: It is best to close the shop at the 0th hour as no customers arrive.
    Example 3:
    
    >>> bestClosingTime(customers = "YYYY")
    >>> 4
    Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.
    """
def countPalindromes(s: str) -> int:
    """
    Given a string of digits s, return the number of palindromic subsequences of s having length 5. Since the answer may be very large, return it modulo 109 + 7.
    Note:
    
    A string is palindromic if it reads the same forward and backward.
    A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
    
    
    Example 1:
    
    >>> countPalindromes(s = "103301")
    >>> 2
    Explanation:
    There are 6 possible subsequences of length 5: "10330","10331","10301","10301","13301","03301".
    Two of them (both equal to "10301") are palindromic.
    
    Example 2:
    
    >>> countPalindromes(s = "0000000")
    >>> 21
    Explanation: All 21 subsequences are "00000", which is palindromic.
    
    Example 3:
    
    >>> countPalindromes(s = "9999900000")
    >>> 2
    Explanation: The only two palindromic subsequences are "99999" and "00000".
    """
def pivotInteger(n: int) -> int:
    """
    Given a positive integer n, find the pivot integer x such that:
    
    The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.
    
    Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.
    
    Example 1:
    
    >>> pivotInteger(n = 8)
    >>> 6
    Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
    
    Example 2:
    
    >>> pivotInteger(n = 1)
    >>> 1
    Explanation: 1 is the pivot integer since: 1 = 1.
    
    Example 3:
    
    >>> pivotInteger(n = 4)
    >>> -1
    Explanation: It can be proved that no such integer exist.
    """
def appendCharacters(s: str, t: str) -> int:
    """
    You are given two strings s and t consisting of only lowercase English letters.
    Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.
    A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
    
    Example 1:
    
    >>> appendCharacters(s = "coaching", t = "coding")
    >>> 4
    Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
    Now, t is a subsequence of s ("coachingding").
    It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
    
    Example 2:
    
    >>> appendCharacters(s = "abcde", t = "a")
    >>> 0
    Explanation: t is already a subsequence of s ("abcde").
    
    Example 3:
    
    >>> appendCharacters(s = "z", t = "abcde")
    >>> 5
    Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
    Now, t is a subsequence of s ("zabcde").
    It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNodes(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given the head of a linked list.
    Remove every node which has a node with a greater value anywhere to the right side of it.
    Return the head of the modified linked list.
    
    Example 1:
    
    
    >>> __init__(head = [5,2,13,3,8])
    >>> [13,8]
    Explanation: The nodes that should be removed are 5, 2 and 3.
    - Node 13 is to the right of node 5.
    - Node 13 is to the right of node 2.
    - Node 8 is to the right of node 3.
    
    Example 2:
    
    >>> __init__(head = [1,1,1,1])
    >>> [1,1,1,1]
    Explanation: Every node has value 1, so no nodes are removed.
    """
def countSubarrays(nums: List[int], k: int) -> int:
    """
    You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k.
    Return the number of non-empty subarrays in nums that have a median equal to k.
    Note:
    
    The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element.
    
    
    For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4.
    
    
    A subarray is a contiguous part of an array.
    
    
    Example 1:
    
    >>> countSubarrays(nums = [3,2,1,4,5], k = 4)
    >>> 3
    Explanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
    
    Example 2:
    
    >>> countSubarrays(nums = [2,3,1], k = 3)
    >>> 1
    Explanation: [3] is the only subarray that has a median equal to 3.
    """
def fixedRatio(s: str, num1: int, num2: int) -> int:
    """
    You are given a binary string s, and two integers num1 and num2. num1 and num2 are coprime numbers.
    A ratio substring is a substring of s where the ratio between the number of 0's and the number of 1's in the substring is exactly num1 : num2.
    
    For example, if num1 = 2 and num2 = 3, then "01011" and "1110000111" are ratio substrings, while "11000" is not.
    
    Return the number of non-empty ratio substrings of s.
    Note that:
    
    A substring is a contiguous sequence of characters within a string.
    Two values x and y are coprime if gcd(x, y) == 1 where gcd(x, y) is the greatest common divisor of x and y.
    
    
    Example 1:
    
    >>> fixedRatio(s = "0110011", num1 = 1, num2 = 2)
    >>> 4
    Explanation: There exist 4 non-empty ratio substrings.
    - The substring s[0..2]: "0110011". It contains one 0 and two 1's. The ratio is 1 : 2.
    - The substring s[1..4]: "0110011". It contains one 0 and two 1's. The ratio is 1 : 2.
    - The substring s[4..6]: "0110011". It contains one 0 and two 1's. The ratio is 1 : 2.
    - The substring s[1..6]: "0110011". It contains two 0's and four 1's. The ratio is 2 : 4 == 1 : 2.
    It can be shown that there are no more ratio substrings.
    
    Example 2:
    
    >>> fixedRatio(s = "10101", num1 = 3, num2 = 1)
    >>> 0
    Explanation: There is no ratio substrings of s. We return 0.
    """
def isCircularSentence(sentence: str) -> bool:
    """
    A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
    
    For example, "Hello World", "HELLO", "hello world hello world" are all sentences.
    
    Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
    A sentence is circular if:
    
    The last character of each word in the sentence is equal to the first character of its next word.
    The last character of the last word is equal to the first character of the first word.
    
    For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.
    Given a string sentence, return true if it is circular. Otherwise, return false.
    
    Example 1:
    
    >>> isCircularSentence(sentence = "leetcode exercises sound delightful")
    >>> true
    Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"].
    - leetcode's last character is equal to exercises's first character.
    - exercises's last character is equal to sound's first character.
    - sound's last character is equal to delightful's first character.
    - delightful's last character is equal to leetcode's first character.
    The sentence is circular.
    Example 2:
    
    >>> isCircularSentence(sentence = "eetcode")
    >>> true
    Explanation: The words in sentence are ["eetcode"].
    - eetcode's last character is equal to eetcode's first character.
    The sentence is circular.
    Example 3:
    
    >>> isCircularSentence(sentence = "Leetcode is cool")
    >>> false
    Explanation: The words in sentence are ["Leetcode", "is", "cool"].
    - Leetcode's last character is not equal to is's first character.
    The sentence is not circular.
    """
def dividePlayers(skill: List[int]) -> int:
    """
    You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.
    The chemistry of a team is equal to the product of the skills of the players on that team.
    Return the sum of the chemistry of all the teams, or return -1 if there is no way to divide the players into teams such that the total skill of each team is equal.
    
    Example 1:
    
    >>> dividePlayers(skill = [3,2,5,1,3,4])
    >>> 22
    Explanation:
    Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
    The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
    
    Example 2:
    
    >>> dividePlayers(skill = [3,4])
    >>> 12
    Explanation:
    The two players form a team with a total skill of 7.
    The chemistry of the team is 3 * 4 = 12.
    
    Example 3:
    
    >>> dividePlayers(skill = [1,1,2,3])
    >>> -1
    Explanation:
    There is no way to divide the players into teams such that the total skill of each team is equal.
    """
def minScore(n: int, roads: List[List[int]]) -> int:
    """
    You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected.
    The score of a path between two cities is defined as the minimum distance of a road in this path.
    Return the minimum possible score of a path between cities 1 and n.
    Note:
    
    A path is a sequence of roads between two cities.
    It is allowed for a path to contain the same road multiple times, and you can visit cities 1 and n multiple times along the path.
    The test cases are generated such that there is at least one path between 1 and n.
    
    
    Example 1:
    
    
    >>> minScore(n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]])
    >>> 5
    Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
    It can be shown that no other path has less score.
    
    Example 2:
    
    
    >>> minScore(n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]])
    >>> 2
    Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
    """
def magnificentSets(n: int, edges: List[List[int]]) -> int:
    """
    You are given a positive integer n representing the number of nodes in an undirected graph. The nodes are labeled from 1 to n.
    You are also given a 2D integer array edges, where edges[i] = [ai, bi] indicates that there is a bidirectional edge between nodes ai and bi. Notice that the given graph may be disconnected.
    Divide the nodes of the graph into m groups (1-indexed) such that:
    
    Each node in the graph belongs to exactly one group.
    For every pair of nodes in the graph that are connected by an edge [ai, bi], if ai belongs to the group with index x, and bi belongs to the group with index y, then |y - x| = 1.
    
    Return the maximum number of groups (i.e., maximum m) into which you can divide the nodes. Return -1 if it is impossible to group the nodes with the given conditions.
    
    Example 1:
    
    
    >>> magnificentSets(n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]])
    >>> 4
    Explanation: As shown in the image we:
    - Add node 5 to the first group.
    - Add node 1 to the second group.
    - Add nodes 2 and 4 to the third group.
    - Add nodes 3 and 6 to the fourth group.
    We can see that every edge is satisfied.
    It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
    
    Example 2:
    
    >>> magnificentSets(n = 3, edges = [[1,2],[2,3],[3,1]])
    >>> -1
    Explanation: If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
    It can be shown that no grouping is possible.
    """
def evenProduct(nums: List[int]) -> int:
    """
    Given a 0-indexed integer array nums, return the number of subarrays of nums having an even product.
    
    Example 1:
    
    >>> evenProduct(nums = [9,6,7,13])
    >>> 6
    Explanation: There are 6 subarrays with an even product:
    - nums[0..1] = 9 * 6 = 54.
    - nums[0..2] = 9 * 6 * 7 = 378.
    - nums[0..3] = 9 * 6 * 7 * 13 = 4914.
    - nums[1..1] = 6.
    - nums[1..2] = 6 * 7 = 42.
    - nums[1..3] = 6 * 7 * 13 = 546.
    
    Example 2:
    
    >>> evenProduct(nums = [7,3,5])
    >>> 0
    Explanation: There are no subarrays with an even product.
    """
def maximumValue(strs: List[str]) -> int:
    """
    The value of an alphanumeric string can be defined as:
    
    The numeric representation of the string in base 10, if it comprises of digits only.
    The length of the string, otherwise.
    
    Given an array strs of alphanumeric strings, return the maximum value of any string in strs.
    
    Example 1:
    
    >>> maximumValue(strs = ["alic3","bob","3","4","00000"])
    >>> 5
    Explanation:
    - "alic3" consists of both letters and digits, so its value is its length, i.e. 5.
    - "bob" consists only of letters, so its value is also its length, i.e. 3.
    - "3" consists only of digits, so its value is its numeric equivalent, i.e. 3.
    - "4" also consists only of digits, so its value is 4.
    - "00000" consists only of digits, so its value is 0.
    Hence, the maximum value is 5, of "alic3".
    
    Example 2:
    
    >>> maximumValue(strs = ["1","01","001","0001"])
    >>> 1
    Explanation:
    Each string in the array has value 1. Hence, we return 1.
    """
def maxStarSum(vals: List[int], edges: List[List[int]], k: int) -> int:
    """
    There is an undirected graph consisting of n nodes numbered from 0 to n - 1. You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node.
    You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.
    A star graph is a subgraph of the given graph having a center node containing 0 or more neighbors. In other words, it is a subset of edges of the given graph such that there exists a common node for all edges.
    The image below shows star graphs with 3 and 4 neighbors respectively, centered at the blue node.
    
    The star sum is the sum of the values of all the nodes present in the star graph.
    Given an integer k, return the maximum star sum of a star graph containing at most k edges.
    
    Example 1:
    
    
    >>> maxStarSum(vals = [1,2,3,4,10,-10,-20], edges = [[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], k = 2)
    >>> 16
    Explanation: The above diagram represents the input graph.
    The star graph with the maximum star sum is denoted by blue. It is centered at 3 and includes its neighbors 1 and 4.
    It can be shown it is not possible to get a star graph with a sum greater than 16.
    
    Example 2:
    
    >>> maxStarSum(vals = [-5], edges = [], k = 0)
    >>> -5
    Explanation: There is only one possible star graph, which is node 0 itself.
    Hence, we return -5.
    """
def maxJump(stones: List[int]) -> int:
    """
    You are given a 0-indexed integer array stones sorted in strictly increasing order representing the positions of stones in a river.
    A frog, initially on the first stone, wants to travel to the last stone and then return to the first stone. However, it can jump to any stone at most once.
    The length of a jump is the absolute difference between the position of the stone the frog is currently on and the position of the stone to which the frog jumps.
    
    More formally, if the frog is at stones[i] and is jumping to stones[j], the length of the jump is |stones[i] - stones[j]|.
    
    The cost of a path is the maximum length of a jump among all jumps in the path.
    Return the minimum cost of a path for the frog.
    
    Example 1:
    
    
    >>> maxJump(stones = [0,2,5,6,7])
    >>> 5
    Explanation: The above figure represents one of the optimal paths the frog can take.
    The cost of this path is 5, which is the maximum length of a jump.
    Since it is not possible to achieve a cost of less than 5, we return it.
    
    Example 2:
    
    
    >>> maxJump(stones = [0,3,9])
    >>> 9
    Explanation:
    The frog can jump directly to the last stone and come back to the first stone.
    In this case, the length of each jump will be 9. The cost for the path will be max(9, 9) = 9.
    It can be shown that this is the minimum achievable cost.
    """
def minimumTotalCost(nums1: List[int], nums2: List[int]) -> int:
    """
    You are given two 0-indexed integer arrays nums1 and nums2, of equal length n.
    In one operation, you can swap the values of any two indices of nums1. The cost of this operation is the sum of the indices.
    Find the minimum total cost of performing the given operation any number of times such that nums1[i] != nums2[i] for all 0 <= i <= n - 1 after performing all the operations.
    Return the minimum total cost such that nums1 and nums2 satisfy the above condition. In case it is not possible, return -1.
    
    Example 1:
    
    >>> minimumTotalCost(nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5])
    >>> 10
    Explanation:
    One of the ways we can perform the operations is:
    - Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]
    - Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].
    - Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].
    We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.
    Note that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10.
    
    Example 2:
    
    >>> minimumTotalCost(nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3])
    >>> 10
    Explanation:
    One of the ways we can perform the operations is:
    - Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].
    - Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].
    The total cost needed here is 10, which is the minimum possible.
    
    Example 3:
    
    >>> minimumTotalCost(nums1 = [1,2,2], nums2 = [1,2,2])
    >>> -1
    Explanation:
    It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.
    Hence, we return -1.
    """
def deleteGreatestValue(grid: List[List[int]]) -> int:
    """
    You are given an m x n matrix grid consisting of positive integers.
    Perform the following operation until grid becomes empty:
    
    Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them.
    Add the maximum of deleted elements to the answer.
    
    Note that the number of columns decreases by one after each operation.
    Return the answer after performing the operations described above.
    
    Example 1:
    
    
    >>> deleteGreatestValue(grid = [[1,2,4],[3,3,1]])
    >>> 8
    Explanation: The diagram above shows the removed values in each step.
    - In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer.
    - In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer.
    - In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer.
    The final answer = 4 + 3 + 1 = 8.
    
    Example 2:
    
    
    >>> deleteGreatestValue(grid = [[10]])
    >>> 10
    Explanation: The diagram above shows the removed values in each step.
    - In the first operation, we remove 10 from the first row. We add 10 to the answer.
    The final answer = 10.
    """
def longestSquareStreak(nums: List[int]) -> int:
    """
    You are given an integer array nums. A subsequence of nums is called a square streak if:
    
    The length of the subsequence is at least 2, and
    after sorting the subsequence, each element (except the first element) is the square of the previous number.
    
    Return the length of the longest square streak in nums, or return -1 if there is no square streak.
    A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
    
    Example 1:
    
    >>> longestSquareStreak(nums = [4,3,6,16,8,2])
    >>> 3
    Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
    - 4 = 2 * 2.
    - 16 = 4 * 4.
    Therefore, [4,16,2] is a square streak.
    It can be shown that every subsequence of length 4 is not a square streak.
    
    Example 2:
    
    >>> longestSquareStreak(nums = [2,3,5,6,7])
    >>> -1
    Explanation: There is no square streak in nums so return -1.
    """