ShwStone/humaneval-leetcode huggingface dataset:
def maxTaskAssign(tasks: List[int], workers: List[int], pills: int, strength: int) -> int:
    """
    You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the jth worker having workers[j] strength. Each worker can only be assigned to a single task and must have a strength greater than or equal to the task's strength requirement (i.e., workers[j] >= tasks[i]).
    Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, however, you may only give each worker at most one magical pill.
    Given the 0-indexed integer arrays tasks and workers and the integers pills and strength, return the maximum number of tasks that can be completed.
    
    Example 1:
    
    >>> maxTaskAssign(tasks = [3,2,1], workers = [0,3,3], pills = 1, strength = 1)
    >>> 3
    Explanation:
    We can assign the magical pill and tasks as follows:
    - Give the magical pill to worker 0.
    - Assign worker 0 to task 2 (0 + 1 >= 1)
    - Assign worker 1 to task 1 (3 >= 2)
    - Assign worker 2 to task 0 (3 >= 3)
    
    Example 2:
    
    >>> maxTaskAssign(tasks = [5,4], workers = [0,0,0], pills = 1, strength = 5)
    >>> 1
    Explanation:
    We can assign the magical pill and tasks as follows:
    - Give the magical pill to worker 0.
    - Assign worker 0 to task 0 (0 + 5 >= 5)
    
    Example 3:
    
    >>> maxTaskAssign(tasks = [10,15,30], workers = [0,10,10,10,10], pills = 3, strength = 10)
    >>> 2
    Explanation:
    We can assign the magical pills and tasks as follows:
    - Give the magical pill to worker 0 and worker 1.
    - Assign worker 0 to task 0 (0 + 10 >= 10)
    - Assign worker 1 to task 1 (10 + 10 >= 15)
    The last pill is not given because it will not make any worker strong enough for the last task.
    """
def timeRequiredToBuy(tickets: List[int], k: int) -> int:
    """
    There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.
    You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].
    Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
    Return the time taken for the person initially at position k (0-indexed) to finish buying tickets.
    
    Example 1:
    
    >>> timeRequiredToBuy(tickets = [2,3,2], k = 2)
    >>> 6
    Explanation:
    
    The queue starts as [2,3,2], where the kth person is underlined.
    After the person at the front has bought a ticket, the queue becomes [3,2,1] at 1 second.
    Continuing this process, the queue becomes [2,1,2] at 2 seconds.
    Continuing this process, the queue becomes [1,2,1] at 3 seconds.
    Continuing this process, the queue becomes [2,1] at 4 seconds. Note: the person at the front left the queue.
    Continuing this process, the queue becomes [1,1] at 5 seconds.
    Continuing this process, the queue becomes [1] at 6 seconds. The kth person has bought all their tickets, so return 6.
    
    
    Example 2:
    
    >>> timeRequiredToBuy(tickets = [5,1,1,1], k = 0)
    >>> 8
    Explanation:
    
    The queue starts as [5,1,1,1], where the kth person is underlined.
    After the person at the front has bought a ticket, the queue becomes [1,1,1,4] at 1 second.
    Continuing this process for 3 seconds, the queue becomes [4] at 4 seconds.
    Continuing this process for 4 seconds, the queue becomes [] at 8 seconds. The kth person has bought all their tickets, so return 8.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseEvenLengthGroups(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given the head of a linked list.
    The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,
    
    The 1st node is assigned to the first group.
    The 2nd and the 3rd nodes are assigned to the second group.
    The 4th, 5th, and 6th nodes are assigned to the third group, and so on.
    
    Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.
    Reverse the nodes in each group with an even length, and return the head of the modified linked list.
    
    Example 1:
    
    
    >>> __init__(head = [5,2,6,3,9,1,7,3,8,4])
    >>> [5,6,2,3,9,1,4,8,3,7]
    Explanation:
    - The length of the first group is 1, which is odd, hence no reversal occurs.
    - The length of the second group is 2, which is even, hence the nodes are reversed.
    - The length of the third group is 3, which is odd, hence no reversal occurs.
    - The length of the last group is 4, which is even, hence the nodes are reversed.
    
    Example 2:
    
    
    >>> __init__(head = [1,1,0,6])
    >>> [1,0,1,6]
    Explanation:
    - The length of the first group is 1. No reversal occurs.
    - The length of the second group is 2. The nodes are reversed.
    - The length of the last group is 1. No reversal occurs.
    
    Example 3:
    
    
    >>> __init__(head = [1,1,0,6,5])
    >>> [1,0,1,5,6]
    Explanation:
    - The length of the first group is 1. No reversal occurs.
    - The length of the second group is 2. The nodes are reversed.
    - The length of the last group is 2. The nodes are reversed.
    """
def decodeCiphertext(encodedText: str, rows: int) -> str:
    """
    A string originalText is encoded using a slanted transposition cipher to a string encodedText with the help of a matrix having a fixed number of rows rows.
    originalText is placed first in a top-left to bottom-right manner.
    
    The blue cells are filled first, followed by the red cells, then the yellow cells, and so on, until we reach the end of originalText. The arrow indicates the order in which the cells are filled. All empty cells are filled with ' '. The number of columns is chosen such that the rightmost column will not be empty after filling in originalText.
    encodedText is then formed by appending all characters of the matrix in a row-wise fashion.
    
    The characters in the blue cells are appended first to encodedText, then the red cells, and so on, and finally the yellow cells. The arrow indicates the order in which the cells are accessed.
    For example, if originalText = "cipher" and rows = 3, then we encode it in the following manner:
    
    The blue arrows depict how originalText is placed in the matrix, and the red arrows denote the order in which encodedText is formed. In the above example, encodedText = "ch ie pr".
    Given the encoded string encodedText and number of rows rows, return the original string originalText.
    Note: originalText does not have any trailing spaces ' '. The test cases are generated such that there is only one possible originalText.
    
    Example 1:
    
    >>> decodeCiphertext(encodedText = "ch   ie   pr", rows = 3)
    >>> "cipher"
    Explanation: This is the same example described in the problem description.
    
    Example 2:
    
    
    >>> decodeCiphertext(encodedText = "iveo    eed   l te   olc", rows = 4)
    >>> "i love leetcode"
    Explanation: The figure above denotes the matrix that was used to encode originalText.
    The blue arrows show how we can find originalText from encodedText.
    
    Example 3:
    
    
    >>> decodeCiphertext(encodedText = "coding", rows = 1)
    >>> "coding"
    Explanation: Since there is only 1 row, both originalText and encodedText are the same.
    """
def friendRequests(n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:
    """
    You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.
    You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xi, yi] means that person xi and person yi cannot become friends, either directly or indirectly through other people.
    Initially, no one is friends with each other. You are given a list of friend requests as a 0-indexed 2D integer array requests, where requests[j] = [uj, vj] is a friend request between person uj and person vj.
    A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order (i.e., requests[j] occurs before requests[j + 1]), and upon a successful request, uj and vj become direct friends for all future friend requests.
    Return a boolean array result, where each result[j] is true if the jth friend request is successful or false if it is not.
    Note: If uj and vj are already direct friends, the request is still successful.
    
    Example 1:
    
    >>> friendRequests(n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]])
    >>> [true,false]
    Explanation:
    Request 0: Person 0 and person 2 can be friends, so they become direct friends.
    Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).
    
    Example 2:
    
    >>> friendRequests(n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]])
    >>> [true,false]
    Explanation:
    Request 0: Person 1 and person 2 can be friends, so they become direct friends.
    Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).
    
    Example 3:
    
    >>> friendRequests(n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]])
    >>> [true,false,true,false]
    Explanation:
    Request 0: Person 0 and person 4 can be friends, so they become direct friends.
    Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.
    Request 2: Person 3 and person 1 can be friends, so they become direct friends.
    Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).
    """
def numberOfPaths(n: int, corridors: List[List[int]]) -> int:
    """
    A maze consists of n rooms numbered from 1 to n, and some rooms are connected by corridors. You are given a 2D integer array corridors where corridors[i] = [room1i, room2i] indicates that there is a corridor connecting room1i and room2i, allowing a person in the maze to go from room1i to room2i and vice versa.
    The designer of the maze wants to know how confusing the maze is. The confusion score of the maze is the number of different cycles of length 3.
    
    For example, 1 → 2 → 3 → 1 is a cycle of length 3, but 1 → 2 → 3 → 4 and 1 → 2 → 3 → 2 → 1 are not.
    
    Two cycles are considered to be different if one or more of the rooms visited in the first cycle is not in the second cycle.
    Return the confusion score of the maze.
    
    Example 1:
    
    
    >>> numberOfPaths(n = 5, corridors = [[1,2],[5,2],[4,1],[2,4],[3,1],[3,4]])
    >>> 2
    Explanation:
    One cycle of length 3 is 4 → 1 → 3 → 4, denoted in red.
    Note that this is the same cycle as 3 → 4 → 1 → 3 or 1 → 3 → 4 → 1 because the rooms are the same.
    Another cycle of length 3 is 1 → 2 → 4 → 1, denoted in blue.
    Thus, there are two different cycles of length 3.
    
    Example 2:
    
    
    >>> numberOfPaths(n = 4, corridors = [[1,2],[3,4]])
    >>> 0
    Explanation:
    There are no cycles of length 3.
    """
def maxDistance(colors: List[int]) -> int:
    """
    There are n houses evenly lined up on the street, and each house is beautifully painted. You are given a 0-indexed integer array colors of length n, where colors[i] represents the color of the ith house.
    Return the maximum distance between two houses with different colors.
    The distance between the ith and jth houses is abs(i - j), where abs(x) is the absolute value of x.
    
    Example 1:
    
    
    >>> maxDistance(colors = [1,1,1,6,1,1,1])
    >>> 3
    Explanation: In the above image, color 1 is blue, and color 6 is red.
    The furthest two houses with different colors are house 0 and house 3.
    House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.
    Note that houses 3 and 6 can also produce the optimal answer.
    
    Example 2:
    
    
    >>> maxDistance(colors = [1,8,3,8,3])
    >>> 4
    Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
    The furthest two houses with different colors are house 0 and house 4.
    House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
    
    Example 3:
    
    >>> maxDistance(colors = [0,1])
    >>> 1
    Explanation: The furthest two houses with different colors are house 0 and house 1.
    House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
    """
def wateringPlants(plants: List[int], capacity: int) -> int:
    """
    You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There is a river at x = -1 that you can refill your watering can at.
    Each plant needs a specific amount of water. You will water the plants in the following way:
    
    Water the plants in order from left to right.
    After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can.
    You cannot refill the watering can early.
    
    You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis.
    Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and an integer capacity representing the watering can capacity, return the number of steps needed to water all the plants.
    
    Example 1:
    
    >>> wateringPlants(plants = [2,2,3,3], capacity = 5)
    >>> 14
    Explanation: Start at the river with a full watering can:
    - Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.
    - Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.
    - Since you cannot completely water plant 2, walk back to the river to refill (2 steps).
    - Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.
    - Since you cannot completely water plant 3, walk back to the river to refill (3 steps).
    - Walk to plant 3 (4 steps) and water it.
    Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.
    
    Example 2:
    
    >>> wateringPlants(plants = [1,1,1,4,2,3], capacity = 4)
    >>> 30
    Explanation: Start at the river with a full watering can:
    - Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).
    - Water plant 3 (4 steps). Return to river (4 steps).
    - Water plant 4 (5 steps). Return to river (5 steps).
    - Water plant 5 (6 steps).
    Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.
    
    Example 3:
    
    >>> wateringPlants(plants = [7,7,7,7,7,7,7], capacity = 8)
    >>> 49
    Explanation: You have to refill before watering each plant.
    Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
    """
def kMirror(k: int, n: int) -> int:
    """
    A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.
    
    For example, 9 is a 2-mirror number. The representation of 9 in base-10 and base-2 are 9 and 1001 respectively, which read the same both forward and backward.
    On the contrary, 4 is not a 2-mirror number. The representation of 4 in base-2 is 100, which does not read the same both forward and backward.
    
    Given the base k and the number n, return the sum of the n smallest k-mirror numbers.
    
    Example 1:
    
    >>> kMirror(k = 2, n = 5)
    >>> 25
    Explanation:
    The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
      base-10    base-2
        1          1
        3          11
        5          101
        7          111
        9          1001
    Their sum = 1 + 3 + 5 + 7 + 9 = 25.
    
    Example 2:
    
    >>> kMirror(k = 3, n = 7)
    >>> 499
    Explanation:
    The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
      base-10    base-3
        1          1
        2          2
        4          11
        8          22
        121        11111
        151        12121
        212        21212
    Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
    
    Example 3:
    
    >>> kMirror(k = 7, n = 17)
    >>> 20379000
    Explanation: The 17 smallest 7-mirror numbers are:
    1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
    """
def numberOfSubstrings(s: str) -> int:
    """
    You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
    A substring is a contiguous non-empty sequence of characters within a string.
    
    Example 1:
    
    >>> numberOfSubstrings(s = "abcba")
    >>> 7
    Explanation:
    The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
    The substring of length 3 that starts and ends with the same letter is: "bcb".
    The substring of length 5 that starts and ends with the same letter is: "abcba".
    
    Example 2:
    
    >>> numberOfSubstrings(s = "abacad")
    >>> 9
    Explanation:
    The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d".
    The substrings of length 3 that start and end with the same letter are: "aba" and "aca".
    The substring of length 5 that starts and ends with the same letter is: "abaca".
    
    Example 3:
    
    >>> numberOfSubstrings(s = "a")
    >>> 1
    Explanation:
    The substring of length 1 that starts and ends with the same letter is: "a".
    """
def countWords(words1: List[str], words2: List[str]) -> int:
    """
    Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.
    
    Example 1:
    
    >>> countWords(words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"])
    >>> 2
    Explanation:
    - "leetcode" appears exactly once in each of the two arrays. We count this string.
    - "amazing" appears exactly once in each of the two arrays. We count this string.
    - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
    - "as" appears once in words1, but does not appear in words2. We do not count this string.
    Thus, there are 2 strings that appear exactly once in each of the two arrays.
    
    Example 2:
    
    >>> countWords(words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"])
    >>> 0
    Explanation: There are no strings that appear in each of the two arrays.
    
    Example 3:
    
    >>> countWords(words1 = ["a","ab"], words2 = ["a","a","a","ab"])
    >>> 1
    Explanation: The only string that appears exactly once in each of the two arrays is "ab".
    """
def minimumBuckets(hamsters: str) -> int:
    """
    You are given a 0-indexed string hamsters where hamsters[i] is either:
    
    'H' indicating that there is a hamster at index i, or
    '.' indicating that index i is empty.
    
    You will add some number of food buckets at the empty indices in order to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index i can be fed if you place a food bucket at index i - 1 and/or at index i + 1.
    Return the minimum number of food buckets you should place at empty indices to feed all the hamsters or -1 if it is impossible to feed all of them.
    
    Example 1:
    
    
    >>> minimumBuckets(hamsters = "H..H")
    >>> 2
    Explanation: We place two food buckets at indices 1 and 2.
    It can be shown that if we place only one food bucket, one of the hamsters will not be fed.
    
    Example 2:
    
    
    >>> minimumBuckets(hamsters = ".H.H.")
    >>> 1
    Explanation: We place one food bucket at index 2.
    
    Example 3:
    
    
    >>> minimumBuckets(hamsters = ".HHH.")
    >>> -1
    Explanation: If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.
    """
def minCost(startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
    """
    There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol).
    The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n.
    
    If the robot moves up or down into a cell whose row is r, then this move costs rowCosts[r].
    If the robot moves left or right into a cell whose column is c, then this move costs colCosts[c].
    
    Return the minimum total cost for this robot to return home.
    
    Example 1:
    
    
    >>> minCost(startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7])
    >>> 18
    Explanation: One optimal path is that:
    Starting from (1, 0)
    -> It goes down to (2, 0). This move costs rowCosts[2] = 3.
    -> It goes right to (2, 1). This move costs colCosts[1] = 2.
    -> It goes right to (2, 2). This move costs colCosts[2] = 6.
    -> It goes right to (2, 3). This move costs colCosts[3] = 7.
    The total cost is 3 + 2 + 6 + 7 = 18
    Example 2:
    
    >>> minCost(startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26])
    >>> 0
    Explanation: The robot is already at its home. Since no moves occur, the total cost is 0.
    """