ShwStone/humaneval-leetcode huggingface dataset:
def minCost(n: int, cost: List[List[int]]) -> int:
    """
    You are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting house i with color j + 1.
    The houses will look beautiful if they satisfy the following conditions:
    
    No two adjacent houses are painted the same color.
    Houses equidistant from the ends of the row are not painted the same color. For example, if n = 6, houses at positions (0, 5), (1, 4), and (2, 3) are considered equidistant.
    
    Return the minimum cost to paint the houses such that they look beautiful.
    
    Example 1:
    
    >>> minCost(n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]])
    >>> 9
    Explanation:
    The optimal painting sequence is [1, 2, 3, 2] with corresponding costs [3, 2, 1, 3]. This satisfies the following conditions:
    
    No adjacent houses have the same color.
    Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color (1 != 2).
    Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color (2 != 3).
    
    The minimum cost to paint the houses so that they look beautiful is 3 + 2 + 1 + 3 = 9.
    
    Example 2:
    
    >>> minCost(n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]])
    >>> 18
    Explanation:
    The optimal painting sequence is [1, 3, 2, 3, 1, 2] with corresponding costs [2, 8, 1, 2, 3, 2]. This satisfies the following conditions:
    
    No adjacent houses have the same color.
    Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color (1 != 2).
    Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color (3 != 1).
    Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color (2 != 3).
    
    The minimum cost to paint the houses so that they look beautiful is 2 + 8 + 1 + 2 + 3 + 2 = 18.
    """
def minUnlockedIndices(nums: List[int], locked: List[int]) -> int:
    """
    You are given an array nums consisting of integers between 1 and 3, and a binary array locked of the same size.
    We consider nums sortable if it can be sorted using adjacent swaps, where a swap between two indices i and i + 1 is allowed if nums[i] - nums[i + 1] == 1 and locked[i] == 0.
    In one operation, you can unlock any index i by setting locked[i] to 0.
    Return the minimum number of operations needed to make nums sortable. If it is not possible to make nums sortable, return -1.
    
    Example 1:
    
    >>> minUnlockedIndices(nums = [1,2,1,2,3,2], locked = [1,0,1,1,0,1])
    >>> 0
    Explanation:
    We can sort nums using the following swaps:
    
    swap indices 1 with 2
    swap indices 4 with 5
    
    So, there is no need to unlock any index.
    
    Example 2:
    
    >>> minUnlockedIndices(nums = [1,2,1,1,3,2,2], locked = [1,0,1,1,0,1,0])
    >>> 2
    Explanation:
    If we unlock indices 2 and 5, we can sort nums using the following swaps:
    
    swap indices 1 with 2
    swap indices 2 with 3
    swap indices 4 with 5
    swap indices 5 with 6
    
    
    Example 3:
    
    >>> minUnlockedIndices(nums = [1,2,1,2,3,2,1], locked = [0,0,0,0,0,0,0])
    >>> -1
    Explanation:
    Even if all indices are unlocked, it can be shown that nums is not sortable.
    """
def countPartitions(nums: List[int]) -> int:
    """
    You are given an integer array nums of length n.
    A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two non-empty subarrays such that:
    
    Left subarray contains indices [0, i].
    Right subarray contains indices [i + 1, n - 1].
    
    Return the number of partitions where the difference between the sum of the left and right subarrays is even.
    
    Example 1:
    
    >>> countPartitions(nums = [10,10,3,7,6])
    >>> 4
    Explanation:
    The 4 partitions are:
    
    [10], [10, 3, 7, 6] with a sum difference of 10 - 26 = -16, which is even.
    [10, 10], [3, 7, 6] with a sum difference of 20 - 16 = 4, which is even.
    [10, 10, 3], [7, 6] with a sum difference of 23 - 13 = 10, which is even.
    [10, 10, 3, 7], [6] with a sum difference of 30 - 6 = 24, which is even.
    
    
    Example 2:
    
    >>> countPartitions(nums = [1,2,2])
    >>> 0
    Explanation:
    No partition results in an even sum difference.
    
    Example 3:
    
    >>> countPartitions(nums = [2,4,6,8])
    >>> 3
    Explanation:
    All partitions result in an even sum difference.
    """
def countMentions(numberOfUsers: int, events: List[List[str]]) -> List[int]:
    """
    You are given an integer numberOfUsers representing the total number of users and an array events of size n x 3.
    Each events[i] can be either of the following two types:
    
    Message Event: ["MESSAGE", "timestampi", "mentions_stringi"]
    
    This event indicates that a set of users was mentioned in a message at timestampi.
    The mentions_stringi string can contain one of the following tokens:
    
    id: where  is an integer in range [0,numberOfUsers - 1]. There can be multiple ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.
    ALL: mentions all users.
    HERE: mentions all online users.
    
    
    
    
    Offline Event: ["OFFLINE", "timestampi", "idi"]
    
    This event indicates that the user idi had become offline at timestampi for 60 time units. The user will automatically be online again at time timestampi + 60.
    
    
    
    Return an array mentions where mentions[i] represents the number of mentions the user with id i has across all MESSAGE events.
    All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp.
    Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately.
    
    Example 1:
    
    >>> countMentions(numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]])
    >>> [2,2]
    Explanation:
    Initially, all users are online.
    At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
    At timestamp 11, id0 goes offline.
    At timestamp 71, id0 comes back online and "HERE" is mentioned. mentions = [2,2]
    
    Example 2:
    
    >>> countMentions(numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]])
    >>> [2,2]
    Explanation:
    Initially, all users are online.
    At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
    At timestamp 11, id0 goes offline.
    At timestamp 12, "ALL" is mentioned. This includes offline users, so both id0 and id1 are mentioned. mentions = [2,2]
    
    Example 3:
    
    >>> countMentions(numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]])
    >>> [0,1]
    Explanation:
    Initially, all users are online.
    At timestamp 10, id0 goes offline.
    At timestamp 12, "HERE" is mentioned. Because id0 is still offline, they will not be mentioned. mentions = [0,1]
    """
def maxFrequency(nums: List[int], k: int) -> int:
    """
    You are given an array nums of length n. You are also given an integer k.
    You perform the following operation on nums once:
    
    Select a subarray nums[i..j] where 0 <= i <= j <= n - 1.
    Select an integer x and add x to all the elements in nums[i..j].
    
    Find the maximum frequency of the value k after the operation.
    
    Example 1:
    
    >>> maxFrequency(nums = [1,2,3,4,5,6], k = 1)
    >>> 2
    Explanation:
    After adding -5 to nums[2..5], 1 has a frequency of 2 in [1, 2, -2, -1, 0, 1].
    
    Example 2:
    
    >>> maxFrequency(nums = [10,2,3,4,5,5,4,3,2,2], k = 10)
    >>> 4
    Explanation:
    After adding 8 to nums[1..9], 10 has a frequency of 4 in [10, 10, 11, 12, 13, 13, 12, 11, 10, 10].
    """
def supersequences(words: List[str]) -> List[List[int]]:
    """
    You are given an array of strings words. Find all shortest common supersequences (SCS) of words that are not permutations of each other.
    A shortest common supersequence is a string of minimum length that contains each string in words as a subsequence.
    Return a 2D array of integers freqs that represent all the SCSs. Each freqs[i] is an array of size 26, representing the frequency of each letter in the lowercase English alphabet for a single SCS. You may return the frequency arrays in any order.
    
    Example 1:
    
    >>> supersequences(words = ["ab","ba"])
    >>> [[1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
    Explanation:
    The two SCSs are "aba" and "bab". The output is the letter frequencies for each one.
    
    Example 2:
    
    >>> supersequences(words = ["aa","ac"])
    >>> [[2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
    Explanation:
    The two SCSs are "aac" and "aca". Since they are permutations of each other, keep only "aac".
    
    Example 3:
    
    >>> supersequences(words = ["aa","bb","cc"])
    >>> [[2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
    Explanation:
    "aabbcc" and all its permutations are SCSs.
    """
def permute(n: int) -> List[List[int]]:
    """
    Given an integer n, an alternating permutation is a permutation of the first n positive integers such that no two adjacent elements are both odd or both even.
    Return all such alternating permutations sorted in lexicographical order.
    
    Example 1:
    
    >>> permute(n = 4)
    >>> [[1,2,3,4],[1,4,3,2],[2,1,4,3],[2,3,4,1],[3,2,1,4],[3,4,1,2],[4,1,2,3],[4,3,2,1]]
    
    Example 2:
    
    >>> permute(n = 2)
    >>> [[1,2],[2,1]]
    
    Example 3:
    
    >>> permute(n = 3)
    >>> [[1,2,3],[3,2,1]]
    """
def findValidPair(s: str) -> str:
    """
    You are given a string s consisting only of digits. A valid pair is defined as two adjacent digits in s such that:
    
    The first digit is not equal to the second.
    Each digit in the pair appears in s exactly as many times as its numeric value.
    
    Return the first valid pair found in the string s when traversing from left to right. If no valid pair exists, return an empty string.
    
    Example 1:
    
    >>> findValidPair(s = "2523533")
    >>> "23"
    Explanation:
    Digit '2' appears 2 times and digit '3' appears 3 times. Each digit in the pair "23" appears in s exactly as many times as its numeric value. Hence, the output is "23".
    
    Example 2:
    
    >>> findValidPair(s = "221")
    >>> "21"
    Explanation:
    Digit '2' appears 2 times and digit '1' appears 1 time. Hence, the output is "21".
    
    Example 3:
    
    >>> findValidPair(s = "22")
    >>> ""
    Explanation:
    There are no valid adjacent pairs.
    """
def maxFreeTime(eventTime: int, k: int, startTime: List[int], endTime: List[int]) -> int:
    """
    You are given an integer eventTime denoting the duration of an event, where the event occurs from time t = 0 to time t = eventTime.
    You are also given two integer arrays startTime and endTime, each of length n. These represent the start and end time of n non-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]].
    You can reschedule at most k meetings by moving their start time while maintaining the same duration, to maximize the longest continuous period of free time during the event.
    The relative order of all the meetings should stay the same and they should remain non-overlapping.
    Return the maximum amount of free time possible after rearranging the meetings.
    Note that the meetings can not be rescheduled to a time outside the event.
    
    Example 1:
    
    >>> maxFreeTime(eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5])
    >>> 2
    Explanation:
    
    Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
    
    Example 2:
    
    >>> maxFreeTime(eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10])
    >>> 6
    Explanation:
    
    Reschedule the meeting at [2, 4] to [1, 3], leaving no meetings during the time [3, 9].
    
    Example 3:
    
    >>> maxFreeTime(eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5])
    >>> 0
    Explanation:
    There is no time during the event not occupied by meetings.
    """
def maxFreeTime(eventTime: int, startTime: List[int], endTime: List[int]) -> int:
    """
    You are given an integer eventTime denoting the duration of an event. You are also given two integer arrays startTime and endTime, each of length n.
    These represent the start and end times of n non-overlapping meetings that occur during the event between time t = 0 and time t = eventTime, where the ith meeting occurs during the time [startTime[i], endTime[i]].
    You can reschedule at most one meeting by moving its start time while maintaining the same duration, such that the meetings remain non-overlapping, to maximize the longest continuous period of free time during the event.
    Return the maximum amount of free time possible after rearranging the meetings.
    Note that the meetings can not be rescheduled to a time outside the event and they should remain non-overlapping.
    Note: In this version, it is valid for the relative ordering of the meetings to change after rescheduling one meeting.
    
    Example 1:
    
    >>> maxFreeTime(eventTime = 5, startTime = [1,3], endTime = [2,5])
    >>> 2
    Explanation:
    
    Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
    
    Example 2:
    
    >>> maxFreeTime(eventTime = 10, startTime = [0,7,9], endTime = [1,8,10])
    >>> 7
    Explanation:
    
    Reschedule the meeting at [0, 1] to [8, 9], leaving no meetings during the time [0, 7].
    
    Example 3:
    
    >>> maxFreeTime(eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10])
    >>> 6
    Explanation:
    
    Reschedule the meeting at [3, 4] to [8, 9], leaving no meetings during the time [1, 7].
    
    Example 4:
    
    >>> maxFreeTime(eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5])
    >>> 0
    Explanation:
    There is no time during the event not occupied by meetings.
    """
def minCostGoodCaption(caption: str) -> str:
    """
    You are given a string caption of length n. A good caption is a string where every character appears in groups of at least 3 consecutive occurrences.
    For example:
    
    "aaabbb" and "aaaaccc" are good captions.
    "aabbb" and "ccccd" are not good captions.
    
    You can perform the following operation any number of times:
    Choose an index i (where 0 <= i < n) and change the character at that index to either:
    
    The character immediately before it in the alphabet (if caption[i] != 'a').
    The character immediately after it in the alphabet (if caption[i] != 'z').
    
    Your task is to convert the given caption into a good caption using the minimum number of operations, and return it. If there are multiple possible good captions, return the lexicographically smallest one among them. If it is impossible to create a good caption, return an empty string "".
    
    Example 1:
    
    >>> minCostGoodCaption(caption = "cdcd")
    >>> "cccc"
    Explanation:
    It can be shown that the given caption cannot be transformed into a good caption with fewer than 2 operations. The possible good captions that can be created using exactly 2 operations are:
    
    "dddd": Change caption[0] and caption[2] to their next character 'd'.
    "cccc": Change caption[1] and caption[3] to their previous character 'c'.
    
    Since "cccc" is lexicographically smaller than "dddd", return "cccc".
    
    Example 2:
    
    >>> minCostGoodCaption(caption = "aca")
    >>> "aaa"
    Explanation:
    It can be proven that the given caption requires at least 2 operations to be transformed into a good caption. The only good caption that can be obtained with exactly 2 operations is as follows:
    
    Operation 1: Change caption[1] to 'b'. caption = "aba".
    Operation 2: Change caption[1] to 'a'. caption = "aaa".
    
    Thus, return "aaa".
    
    Example 3:
    
    >>> minCostGoodCaption(caption = "bc")
    >>> ""
    Explanation:
    It can be shown that the given caption cannot be converted to a good caption by using any number of operations.
    """
def maxDifference(s: str) -> int:
    """
    You are given a string s consisting of lowercase English letters. Your task is to find the maximum difference between the frequency of two characters in the string such that:
    
    One of the characters has an even frequency in the string.
    The other character has an odd frequency in the string.
    
    Return the maximum difference, calculated as the frequency of the character with an odd frequency minus the frequency of the character with an even frequency.
    
    Example 1:
    
    >>> maxDifference(s = "aaaaabbc")
    >>> 3
    Explanation:
    
    The character 'a' has an odd frequency of 5, and 'b' has an even frequency of 2.
    The maximum difference is 5 - 2 = 3.
    
    
    Example 2:
    
    >>> maxDifference(s = "abcabcab")
    >>> 1
    Explanation:
    
    The character 'a' has an odd frequency of 3, and 'c' has an even frequency of 2.
    The maximum difference is 3 - 2 = 1.
    """
def maxDistance(s: str, k: int) -> int:
    """
    You are given a string s consisting of the characters 'N', 'S', 'E', and 'W', where s[i] indicates movements in an infinite grid:
    
    'N' : Move north by 1 unit.
    'S' : Move south by 1 unit.
    'E' : Move east by 1 unit.
    'W' : Move west by 1 unit.
    
    Initially, you are at the origin (0, 0). You can change at most k characters to any of the four directions.
    Find the maximum Manhattan distance from the origin that can be achieved at any time while performing the movements in order.
    The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
    
    Example 1:
    
    >>> maxDistance(s = "NWSE", k = 1)
    >>> 3
    Explanation:
    Change s[2] from 'S' to 'N'. The string s becomes "NWNE".
    
    
    
    Movement
    Position (x, y)
    Manhattan Distance
    Maximum
    
    
    
    
    s[0] == 'N'
    (0, 1)
    0 + 1 = 1
    1
    
    
    s[1] == 'W'
    (-1, 1)
    1 + 1 = 2
    2
    
    
    s[2] == 'N'
    (-1, 2)
    1 + 2 = 3
    3
    
    
    s[3] == 'E'
    (0, 2)
    0 + 2 = 2
    3
    
    
    
    The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
    
    Example 2:
    
    >>> maxDistance(s = "NSWWEW", k = 3)
    >>> 6
    Explanation:
    Change s[1] from 'S' to 'N', and s[4] from 'E' to 'W'. The string s becomes "NNWWWW".
    The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
    """
def minimumIncrements(nums: List[int], target: List[int]) -> int:
    """
    You are given two arrays, nums and target.
    In a single operation, you may increment any element of nums by 1.
    Return the minimum number of operations required so that each element in target has at least one multiple in nums.
    
    Example 1:
    
    >>> minimumIncrements(nums = [1,2,3], target = [4])
    >>> 1
    Explanation:
    The minimum number of operations required to satisfy the condition is 1.
    
    Increment 3 to 4 with just one operation, making 4 a multiple of itself.
    
    
    Example 2:
    
    >>> minimumIncrements(nums = [8,4], target = [10,5])
    >>> 2
    Explanation:
    The minimum number of operations required to satisfy the condition is 2.
    
    Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10.
    
    
    Example 3:
    
    >>> minimumIncrements(nums = [7,9,10], target = [7])
    >>> 0
    Explanation:
    Target 7 already has a multiple in nums, so no additional operations are needed.
    """
def maxDifference(s: str, k: int) -> int:
    """
    You are given a string s and an integer k. Your task is to find the maximum difference between the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that:
    
    subs has a size of at least k.
    Character a has an odd frequency in subs.
    Character b has an even frequency in subs.
    
    Return the maximum difference.
    Note that subs can contain more than 2 distinct characters.
    
    Example 1:
    
    >>> maxDifference(s = "12233", k = 4)
    >>> -1
    Explanation:
    For the substring "12233", the frequency of '1' is 1 and the frequency of '3' is 2. The difference is 1 - 2 = -1.
    
    Example 2:
    
    >>> maxDifference(s = "1122211", k = 3)
    >>> 1
    Explanation:
    For the substring "11222", the frequency of '2' is 3 and the frequency of '1' is 2. The difference is 3 - 2 = 1.
    
    Example 3:
    
    >>> maxDifference(s = "110", k = 3)
    >>> -1
    """
def sortMatrix(grid: List[List[int]]) -> List[List[int]]:
    """
    You are given an n x n square matrix of integers grid. Return the matrix such that:
    
    The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
    The diagonals in the top-right triangle are sorted in non-decreasing order.
    
    
    Example 1:
    
    >>> sortMatrix(grid = [[1,7,3],[9,8,2],[4,5,6]])
    >>> [[8,2,3],[9,6,7],[4,5,1]]
    Explanation:
    
    The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:
    
    [1, 8, 6] becomes [8, 6, 1].
    [9, 5] and [4] remain unchanged.
    
    The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:
    
    [7, 2] becomes [2, 7].
    [3] remains unchanged.
    
    
    Example 2:
    
    >>> sortMatrix(grid = [[0,1],[1,2]])
    >>> [[2,1],[1,0]]
    Explanation:
    
    The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.
    
    Example 3:
    
    >>> sortMatrix(grid = [[1]])
    >>> [[1]]
    Explanation:
    Diagonals with exactly one element are already in order, so no changes are needed.
    """
def assignElements(groups: List[int], elements: List[int]) -> List[int]:
    """
    You are given an integer array groups, where groups[i] represents the size of the ith group. You are also given an integer array elements.
    Your task is to assign one element to each group based on the following rules:
    
    An element at index j can be assigned to a group i if groups[i] is divisible by elements[j].
    If there are multiple elements that can be assigned, assign the element with the smallest index j.
    If no element satisfies the condition for a group, assign -1 to that group.
    
    Return an integer array assigned, where assigned[i] is the index of the element chosen for group i, or -1 if no suitable element exists.
    Note: An element may be assigned to more than one group.
    
    Example 1:
    
    >>> assignElements(groups = [8,4,3,2,4], elements = [4,2])
    >>> [0,0,-1,1,0]
    Explanation:
    
    elements[0] = 4 is assigned to groups 0, 1, and 4.
    elements[1] = 2 is assigned to group 3.
    Group 2 cannot be assigned any element.
    
    
    Example 2:
    
    >>> assignElements(groups = [2,3,5,7], elements = [5,3,3])
    >>> [-1,1,0,-1]
    Explanation:
    
    elements[1] = 3 is assigned to group 1.
    elements[0] = 5 is assigned to group 2.
    Groups 0 and 3 cannot be assigned any element.
    
    
    Example 3:
    
    >>> assignElements(groups = [10,21,30,41], elements = [2,1])
    >>> [0,1,0,1]
    Explanation:
    elements[0] = 2 is assigned to the groups with even values, and elements[1] = 1 is assigned to the groups with odd values.
    """