ShwStone/humaneval-leetcode huggingface dataset:
def maximumAlternatingSubarraySum(nums: List[int]) -> int:
    """
    A subarray of a 0-indexed integer array is a contiguous non-empty sequence of elements within an array.
    The alternating subarray sum of a subarray that ranges from index i to j (inclusive, 0 <= i <= j < nums.length) is nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j].
    Given a 0-indexed integer array nums, return the maximum alternating subarray sum of any subarray of nums.
    
    Example 1:
    
    >>> maximumAlternatingSubarraySum(nums = [3,-1,1,2])
    >>> 5
    Explanation:
    The subarray [3,-1,1] has the largest alternating subarray sum.
    The alternating subarray sum is 3 - (-1) + 1 = 5.
    
    Example 2:
    
    >>> maximumAlternatingSubarraySum(nums = [2,2,2,2,2])
    >>> 2
    Explanation:
    The subarrays [2], [2,2,2], and [2,2,2,2,2] have the largest alternating subarray sum.
    The alternating subarray sum of [2] is 2.
    The alternating subarray sum of [2,2,2] is 2 - 2 + 2 = 2.
    The alternating subarray sum of [2,2,2,2,2] is 2 - 2 + 2 - 2 + 2 = 2.
    
    Example 3:
    
    >>> maximumAlternatingSubarraySum(nums = [1])
    >>> 1
    Explanation:
    There is only one non-empty subarray, which is [1].
    The alternating subarray sum is 1.
    """
def minMovesToSeat(seats: List[int], students: List[int]) -> int:
    """
    There are n availabe seats and n students standing in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.
    You may perform the following move any number of times:
    
    Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)
    
    Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
    Note that there may be multiple seats or students in the same position at the beginning.
    
    Example 1:
    
    >>> minMovesToSeat(seats = [3,1,5], students = [2,7,4])
    >>> 4
    Explanation: The students are moved as follows:
    - The first student is moved from position 2 to position 1 using 1 move.
    - The second student is moved from position 7 to position 5 using 2 moves.
    - The third student is moved from position 4 to position 3 using 1 move.
    In total, 1 + 2 + 1 = 4 moves were used.
    
    Example 2:
    
    >>> minMovesToSeat(seats = [4,1,5,9], students = [1,3,2,6])
    >>> 7
    Explanation: The students are moved as follows:
    - The first student is not moved.
    - The second student is moved from position 3 to position 4 using 1 move.
    - The third student is moved from position 2 to position 5 using 3 moves.
    - The fourth student is moved from position 6 to position 9 using 3 moves.
    In total, 0 + 1 + 3 + 3 = 7 moves were used.
    
    Example 3:
    
    >>> minMovesToSeat(seats = [2,2,6,6], students = [1,3,2,6])
    >>> 4
    Explanation: Note that there are two seats at position 2 and two seats at position 6.
    The students are moved as follows:
    - The first student is moved from position 1 to position 2 using 1 move.
    - The second student is moved from position 3 to position 6 using 3 moves.
    - The third student is not moved.
    - The fourth student is not moved.
    In total, 1 + 3 + 0 + 0 = 4 moves were used.
    """
def winnerOfGame(colors: str) -> bool:
    """
    There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.
    Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.
    
    Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
    Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
    Alice and Bob cannot remove pieces from the edge of the line.
    If a player cannot make a move on their turn, that player loses and the other player wins.
    
    Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.
    
    Example 1:
    
    >>> winnerOfGame(colors = "AAABABB")
    >>> true
    Explanation:
    AAABABB -> AABABB
    Alice moves first.
    She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
    
    Now it's Bob's turn.
    Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
    Thus, Alice wins, so return true.
    
    Example 2:
    
    >>> winnerOfGame(colors = "AA")
    >>> false
    Explanation:
    Alice has her turn first.
    There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
    Thus, Bob wins, so return false.
    
    Example 3:
    
    >>> winnerOfGame(colors = "ABBBBBBBAAA")
    >>> false
    Explanation:
    ABBBBBBBAAA -> ABBBBBBBAA
    Alice moves first.
    Her only option is to remove the second to last 'A' from the right.
    
    ABBBBBBBAA -> ABBBBBBAA
    Next is Bob's turn.
    He has many options for which 'B' piece to remove. He can pick any.
    
    On Alice's second turn, she has no more pieces that she can remove.
    Thus, Bob wins, so return false.
    """
def kthSmallestProduct(nums1: List[int], nums2: List[int], k: int) -> int:
    """
    Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return the kth (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.
    
    Example 1:
    
    >>> kthSmallestProduct(nums1 = [2,5], nums2 = [3,4], k = 2)
    >>> 8
    Explanation: The 2 smallest products are:
    - nums1[0] * nums2[0] = 2 * 3 = 6
    - nums1[0] * nums2[1] = 2 * 4 = 8
    The 2nd smallest product is 8.
    
    Example 2:
    
    >>> kthSmallestProduct(nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6)
    >>> 0
    Explanation: The 6 smallest products are:
    - nums1[0] * nums2[1] = (-4) * 4 = -16
    - nums1[0] * nums2[0] = (-4) * 2 = -8
    - nums1[1] * nums2[1] = (-2) * 4 = -8
    - nums1[1] * nums2[0] = (-2) * 2 = -4
    - nums1[2] * nums2[0] = 0 * 2 = 0
    - nums1[2] * nums2[1] = 0 * 4 = 0
    The 6th smallest product is 0.
    
    Example 3:
    
    >>> kthSmallestProduct(nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3)
    >>> -6
    Explanation: The 3 smallest products are:
    - nums1[0] * nums2[4] = (-2) * 5 = -10
    - nums1[0] * nums2[3] = (-2) * 4 = -8
    - nums1[4] * nums2[0] = 2 * (-3) = -6
    The 3rd smallest product is -6.
    """
def areNumbersAscending(s: str) -> bool:
    """
    A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters.
    
    For example, "a puppy has 2 eyes 4 legs" is a sentence with seven tokens: "2" and "4" are numbers and the other tokens such as "puppy" are words.
    
    Given a string s representing a sentence, you need to check if all the numbers in s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s).
    Return true if so, or false otherwise.
    
    Example 1:
    
    
    >>> areNumbersAscending(s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles")
    >>> true
    Explanation: The numbers in s are: 1, 3, 4, 6, 12.
    They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
    
    Example 2:
    
    >>> areNumbersAscending(s = "hello world 5 x 5")
    >>> false
    Explanation: The numbers in s are: 5, 5. They are not strictly increasing.
    
    Example 3:
    
    
    >>> areNumbersAscending(s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s")
    >>> false
    Explanation: The numbers in s are: 7, 51, 50, 60. They are not strictly increasing.
    """
def countMaxOrSubsets(nums: List[int]) -> int:
    """
    Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.
    An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.
    The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
    
    Example 1:
    
    >>> countMaxOrSubsets(nums = [3,1])
    >>> 2
    Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
    - [3]
    - [3,1]
    
    Example 2:
    
    >>> countMaxOrSubsets(nums = [2,2,2])
    >>> 7
    Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 23 - 1 = 7 total subsets.
    
    Example 3:
    
    >>> countMaxOrSubsets(nums = [3,2,1,5])
    >>> 6
    Explanation: The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
    - [3,5]
    - [3,1,5]
    - [3,2,5]
    - [3,2,1,5]
    - [2,5]
    - [2,1,5]
    """
def secondMinimum(n: int, edges: List[List[int]], time: int, change: int) -> int:
    """
    A city is represented as a bi-directional connected graph with n vertices where each vertex is labeled from 1 to n (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. The time taken to traverse any edge is time minutes.
    Each vertex has a traffic signal which changes its color from green to red and vice versa every change minutes. All signals change at the same time. You can enter a vertex at any time, but can leave a vertex only when the signal is green. You cannot wait at a vertex if the signal is green.
    The second minimum value is defined as the smallest value strictly larger than the minimum value.
    
    For example the second minimum value of [2, 3, 4] is 3, and the second minimum value of [2, 2, 4] is 4.
    
    Given n, edges, time, and change, return the second minimum time it will take to go from vertex 1 to vertex n.
    Notes:
    
    You can go through any vertex any number of times, including 1 and n.
    You can assume that when the journey starts, all signals have just turned green.
    
    
    Example 1:
    
    
    >>> secondMinimum(n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5)
    >>> 13
    Explanation:
    The figure on the left shows the given graph.
    The blue path in the figure on the right is the minimum time path.
    The time taken is:
    - Start at 1, time elapsed=0
    - 1 -> 4: 3 minutes, time elapsed=3
    - 4 -> 5: 3 minutes, time elapsed=6
    Hence the minimum time needed is 6 minutes.
    
    The red path shows the path to get the second minimum time.
    - Start at 1, time elapsed=0
    - 1 -> 3: 3 minutes, time elapsed=3
    - 3 -> 4: 3 minutes, time elapsed=6
    - Wait at 4 for 4 minutes, time elapsed=10
    - 4 -> 5: 3 minutes, time elapsed=13
    Hence the second minimum time is 13 minutes.
    
    Example 2:
    
    
    >>> secondMinimum(n = 2, edges = [[1,2]], time = 3, change = 2)
    >>> 11
    Explanation:
    The minimum time path is 1 -> 2 with time = 3 minutes.
    The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortLinkedList(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given the head of a singly linked list that is sorted in non-decreasing order using the absolute values of its nodes, return the list sorted in non-decreasing order using the actual values of its nodes.
    
    Example 1:
    
    
    >>> __init__(head = [0,2,-5,5,10,-10])
    >>> [-10,-5,0,2,5,10]
    Explanation:
    The list sorted in non-descending order using the absolute values of the nodes is [0,2,-5,5,10,-10].
    The list sorted in non-descending order using the actual values is [-10,-5,0,2,5,10].
    
    Example 2:
    
    
    >>> __init__(head = [0,1,2])
    >>> [0,1,2]
    Explanation:
    The linked list is already sorted in non-decreasing order.
    
    Example 3:
    
    >>> __init__(head = [1])
    >>> [1]
    Explanation:
    The linked list is already sorted in non-decreasing order.
    """
def countValidWords(sentence: str) -> int:
    """
    A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '.
    A token is a valid word if all three of the following are true:
    
    It only contains lowercase letters, hyphens, and/or punctuation (no digits).
    There is at most one hyphen '-'. If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid).
    There is at most one punctuation mark. If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid).
    
    Examples of valid words include "a-b.", "afad", "ba-c", "a!", and "!".
    Given a string sentence, return the number of valid words in sentence.
    
    Example 1:
    
    >>> countValidWords(sentence = "cat and  dog")
    >>> 3
    Explanation: The valid words in the sentence are "cat", "and", and "dog".
    
    Example 2:
    
    >>> countValidWords(sentence = "!this  1-s b8d!")
    >>> 0
    Explanation: There are no valid words in the sentence.
    "!this" is invalid because it starts with a punctuation mark.
    "1-s" and "b8d" are invalid because they contain digits.
    
    Example 3:
    
    >>> countValidWords(sentence = "alice and  bob are playing stone-game10")
    >>> 5
    Explanation: The valid words in the sentence are "alice", "and", "bob", "are", and "playing".
    "stone-game10" is invalid because it contains digits.
    """
def nextBeautifulNumber(n: int) -> int:
    """
    An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x.
    Given an integer n, return the smallest numerically balanced number strictly greater than n.
    
    Example 1:
    
    >>> nextBeautifulNumber(n = 1)
    >>> 22
    Explanation:
    22 is numerically balanced since:
    - The digit 2 occurs 2 times.
    It is also the smallest numerically balanced number strictly greater than 1.
    
    Example 2:
    
    >>> nextBeautifulNumber(n = 1000)
    >>> 1333
    Explanation:
    1333 is numerically balanced since:
    - The digit 1 occurs 1 time.
    - The digit 3 occurs 3 times.
    It is also the smallest numerically balanced number strictly greater than 1000.
    Note that 1022 cannot be the answer because 0 appeared more than 0 times.
    
    Example 3:
    
    >>> nextBeautifulNumber(n = 3000)
    >>> 3133
    Explanation:
    3133 is numerically balanced since:
    - The digit 1 occurs 1 time.
    - The digit 3 occurs 3 times.
    It is also the smallest numerically balanced number strictly greater than 3000.
    """
def countHighestScoreNodes(parents: List[int]) -> int:
    """
    There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, where parents[i] is the parent of node i. Since node 0 is the root, parents[0] == -1.
    Each node has a score. To find the score of a node, consider if the node and the edges connected to it were removed. The tree would become one or more non-empty subtrees. The size of a subtree is the number of the nodes in it. The score of the node is the product of the sizes of all those subtrees.
    Return the number of nodes that have the highest score.
    
    Example 1:
    
    
    >>> countHighestScoreNodes(parents = [-1,2,0,2,0])
    >>> 3
    Explanation:
    - The score of node 0 is: 3 * 1 = 3
    - The score of node 1 is: 4 = 4
    - The score of node 2 is: 1 * 1 * 2 = 2
    - The score of node 3 is: 4 = 4
    - The score of node 4 is: 4 = 4
    The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
    
    Example 2:
    
    
    >>> countHighestScoreNodes(parents = [-1,2,0])
    >>> 2
    Explanation:
    - The score of node 0 is: 2 = 2
    - The score of node 1 is: 2 = 2
    - The score of node 2 is: 1 * 1 = 1
    The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
    """
def minimumTime(n: int, relations: List[List[int]], time: List[int]) -> int:
    """
    You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that course prevCoursej has to be completed before course nextCoursej (prerequisite relationship). Furthermore, you are given a 0-indexed integer array time where time[i] denotes how many months it takes to complete the (i+1)th course.
    You must find the minimum number of months needed to complete all the courses following these rules:
    
    You may start taking a course at any time if the prerequisites are met.
    Any number of courses can be taken at the same time.
    
    Return the minimum number of months needed to complete all the courses.
    Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).
    
    Example 1:
    
    
    >>> minimumTime(n = 3, relations = [[1,3],[2,3]], time = [3,2,5])
    >>> 8
    Explanation: The figure above represents the given graph and the time required to complete each course.
    We start course 1 and course 2 simultaneously at month 0.
    Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
    Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.
    
    Example 2:
    
    
    >>> minimumTime(n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5])
    >>> 12
    Explanation: The figure above represents the given graph and the time required to complete each course.
    You can start courses 1, 2, and 3 at month 0.
    You can complete them after 1, 2, and 3 months respectively.
    Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
    Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
    Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
    """
def minimumCost(sentence: str, k: int) -> int:
    """
    You are given a string sentence containing words separated by spaces, and an integer k. Your task is to separate sentence into rows where the number of characters in each row is at most k. You may assume that sentence does not begin or end with a space, and the words in sentence are separated by a single space.
    You can split sentence into rows by inserting line breaks between words in sentence. A word cannot be split between two rows. Each word must be used exactly once, and the word order cannot be rearranged. Adjacent words in a row should be separated by a single space, and rows should not begin or end with spaces.
    The cost of a row with length n is (k - n)2, and the total cost is the sum of the costs for all rows except the last one.
    
    For example if sentence = "i love leetcode" and k = 12:
    
    
    Separating sentence into "i", "love", and "leetcode" has a cost of (12 - 1)2 + (12 - 4)2 = 185.
    Separating sentence into "i love", and "leetcode" has a cost of (12 - 6)2 = 36.
    Separating sentence into "i", and "love leetcode" is not possible because the length of "love leetcode" is greater than k.
    
    
    
    Return the minimum possible total cost of separating sentence into rows.
    
    Example 1:
    
    >>> minimumCost(sentence = "i love leetcode", k = 12)
    >>> 36
    Explanation:
    Separating sentence into "i", "love", and "leetcode" has a cost of (12 - 1)2 + (12 - 4)2 = 185.
    Separating sentence into "i love", and "leetcode" has a cost of (12 - 6)2 = 36.
    Separating sentence into "i", "love leetcode" is not possible because "love leetcode" has length 13.
    36 is the minimum possible total cost so return it.
    
    Example 2:
    
    >>> minimumCost(sentence = "apples and bananas taste great", k = 7)
    >>> 21
    Explanation
    Separating sentence into "apples", "and", "bananas", "taste", and "great" has a cost of (7 - 6)2 + (7 - 3)2 + (7 - 7)2 + (7 - 5)2 = 21.
    21 is the minimum possible total cost so return it.
    
    Example 3:
    
    >>> minimumCost(sentence = "a", k = 5)
    >>> 0
    Explanation:
    The cost of the last row is not included in the total cost, and since there is only one row, return 0.
    """
def kthDistinct(arr: List[str], k: int) -> str:
    """
    A distinct string is a string that is present only once in an array.
    Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".
    Note that the strings are considered in the order in which they appear in the array.
    
    Example 1:
    
    >>> kthDistinct(arr = ["d","b","c","b","c","a"], k = 2)
    >>> "a"
    Explanation:
    The only distinct strings in arr are "d" and "a".
    "d" appears 1st, so it is the 1st distinct string.
    "a" appears 2nd, so it is the 2nd distinct string.
    Since k == 2, "a" is returned.
    
    Example 2:
    
    >>> kthDistinct(arr = ["aaa","aa","a"], k = 1)
    >>> "aaa"
    Explanation:
    All strings in arr are distinct, so the 1st string "aaa" is returned.
    
    Example 3:
    
    >>> kthDistinct(arr = ["a","b","a"], k = 3)
    >>> ""
    Explanation:
    The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
    """
def maxTwoEvents(events: List[List[int]]) -> int:
    """
    You are given a 0-indexed 2D integer array of events where events[i] = [startTimei, endTimei, valuei]. The ith event starts at startTimei and ends at endTimei, and if you attend this event, you will receive a value of valuei. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.
    Return this maximum sum.
    Note that the start time and end time is inclusive: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time t, the next event must start at or after t + 1.
    
    Example 1:
    
    
    >>> maxTwoEvents(events = [[1,3,2],[4,5,2],[2,4,3]])
    >>> 4
    Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
    
    Example 2:
    
    
    >>> maxTwoEvents(events = [[1,3,2],[4,5,2],[1,5,5]])
    >>> 5
    Explanation: Choose event 2 for a sum of 5.
    
    Example 3:
    
    
    >>> maxTwoEvents(events = [[1,5,3],[1,5,1],[6,6,5]])
    >>> 8
    Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8.
    """