ShwStone/humaneval-leetcode huggingface dataset:
def executeInstructions(n: int, startPos: List[int], s: str) -> List[int]:
    """
    There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol).
    You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).
    The robot can begin executing from any ith instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met:
    
    The next instruction will move the robot off the grid.
    There are no more instructions left to execute.
    
    Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the ith instruction in s.
    
    Example 1:
    
    
    >>> executeInstructions(n = 3, startPos = [0,1], s = "RRDDLU")
    >>> [1,5,4,3,1,0]
    Explanation: Starting from startPos and beginning execution from the ith instruction:
    - 0th: "RRDDLU". Only one instruction "R" can be executed before it moves off the grid.
    - 1st:  "RDDLU". All five instructions can be executed while it stays in the grid and ends at (1, 1).
    - 2nd:   "DDLU". All four instructions can be executed while it stays in the grid and ends at (1, 0).
    - 3rd:    "DLU". All three instructions can be executed while it stays in the grid and ends at (0, 0).
    - 4th:     "LU". Only one instruction "L" can be executed before it moves off the grid.
    - 5th:      "U". If moving up, it would move off the grid.
    
    Example 2:
    
    
    >>> executeInstructions(n = 2, startPos = [1,1], s = "LURD")
    >>> [4,1,0,0]
    Explanation:
    - 0th: "LURD".
    - 1st:  "URD".
    - 2nd:   "RD".
    - 3rd:    "D".
    
    Example 3:
    
    
    >>> executeInstructions(n = 1, startPos = [0,0], s = "LRUD")
    >>> [0,0,0,0]
    Explanation: No matter which instruction the robot begins execution from, it would move off the grid.
    """
def getDistances(arr: List[int]) -> List[int]:
    """
    You are given a 0-indexed array of n integers arr.
    The interval between two elements in arr is defined as the absolute difference between their indices. More formally, the interval between arr[i] and arr[j] is |i - j|.
    Return an array intervals of length n where intervals[i] is the sum of intervals between arr[i] and each element in arr with the same value as arr[i].
    Note: |x| is the absolute value of x.
    
    Example 1:
    
    >>> getDistances(arr = [2,1,3,1,2,3,3])
    >>> [4,2,7,2,4,4,5]
    Explanation:
    - Index 0: Another 2 is found at index 4. |0 - 4| = 4
    - Index 1: Another 1 is found at index 3. |1 - 3| = 2
    - Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7
    - Index 3: Another 1 is found at index 1. |3 - 1| = 2
    - Index 4: Another 2 is found at index 0. |4 - 0| = 4
    - Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4
    - Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5
    
    Example 2:
    
    >>> getDistances(arr = [10,5,10,10])
    >>> [5,0,3,4]
    Explanation:
    - Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5
    - Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
    - Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3
    - Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4
    """
def recoverArray(nums: List[int]) -> List[int]:
    """
    Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
    
    lower[i] = arr[i] - k, for every index i where 0 <= i < n
    higher[i] = arr[i] + k, for every index i where 0 <= i < n
    
    Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
    Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
    Note: The test cases are generated such that there exists at least one valid array arr.
    
    Example 1:
    
    >>> recoverArray(nums = [2,10,6,4,8,12])
    >>> [3,7,11]
    Explanation:
    If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
    Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
    Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
    
    Example 2:
    
    >>> recoverArray(nums = [1,1,3,3])
    >>> [2,2]
    Explanation:
    If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
    Combining lower and higher gives us [1,1,3,3], which is equal to nums.
    Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.
    This is invalid since k must be positive.
    
    Example 3:
    
    >>> recoverArray(nums = [5,435])
    >>> [220]
    Explanation:
    The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
    """
def minimumOperations(grid: List[List[int]]) -> int:
    """
    You are given a 0-indexed binary matrix grid. In one operation, you can flip any 1 in grid to be 0.
    A binary matrix is well-isolated if there is no 1 in the matrix that is 4-directionally connected (i.e., horizontal and vertical) to another 1.
    Return the minimum number of operations to make grid well-isolated.
    
    Example 1:
    
    
    >>> minimumOperations(grid = [[1,1,0],[0,1,1],[1,1,1]])
    >>> 3
    Explanation: Use 3 operations to change grid[0][1], grid[1][2], and grid[2][1] to 0.
    After, no more 1's are 4-directionally connected and grid is well-isolated.
    
    Example 2:
    
    
    >>> minimumOperations(grid = [[0,0,0],[0,0,0],[0,0,0]])
    >>> 0
    Explanation: There are no 1's in grid and it is well-isolated.
    No operations were done so return 0.
    
    Example 3:
    
    
    >>> minimumOperations(grid = [[0,1],[1,0]])
    >>> 0
    Explanation: None of the 1's are 4-directionally connected and grid is well-isolated.
    No operations were done so return 0.
    """
def checkString(s: str) -> bool:
    """
    Given a string s consisting of only the characters 'a' and 'b', return true if every 'a' appears before every 'b' in the string. Otherwise, return false.
    
    Example 1:
    
    >>> checkString(s = "aaabbb")
    >>> true
    Explanation:
    The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
    Hence, every 'a' appears before every 'b' and we return true.
    
    Example 2:
    
    >>> checkString(s = "abab")
    >>> false
    Explanation:
    There is an 'a' at index 2 and a 'b' at index 1.
    Hence, not every 'a' appears before every 'b' and we return false.
    
    Example 3:
    
    >>> checkString(s = "bbb")
    >>> true
    Explanation:
    There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
    """
def numberOfBeams(bank: List[str]) -> int:
    """
    Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.
    There is one laser beam between any two security devices if both conditions are met:
    
    The two devices are located on two different rows: r1 and r2, where r1 < r2.
    For each row i where r1 < i < r2, there are no security devices in the ith row.
    
    Laser beams are independent, i.e., one beam does not interfere nor join with another.
    Return the total number of laser beams in the bank.
    
    Example 1:
    
    
    >>> numberOfBeams(bank = ["011001","000000","010100","001000"])
    >>> 8
    Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
     * bank[0][1] -- bank[2][1]
     * bank[0][1] -- bank[2][3]
     * bank[0][2] -- bank[2][1]
     * bank[0][2] -- bank[2][3]
     * bank[0][5] -- bank[2][1]
     * bank[0][5] -- bank[2][3]
     * bank[2][1] -- bank[3][2]
     * bank[2][3] -- bank[3][2]
    Note that there is no beam between any device on the 0th row with any on the 3rd row.
    This is because the 2nd row contains security devices, which breaks the second condition.
    
    Example 2:
    
    
    >>> numberOfBeams(bank = ["000","111","000"])
    >>> 0
    Explanation: There does not exist two devices located on two different rows.
    """
def asteroidsDestroyed(mass: int, asteroids: List[int]) -> bool:
    """
    You are given an integer mass, which represents the original mass of a planet. You are further given an integer array asteroids, where asteroids[i] is the mass of the ith asteroid.
    You can arrange for the planet to collide with the asteroids in any arbitrary order. If the mass of the planet is greater than or equal to the mass of the asteroid, the asteroid is destroyed and the planet gains the mass of the asteroid. Otherwise, the planet is destroyed.
    Return true if all asteroids can be destroyed. Otherwise, return false.
    
    Example 1:
    
    >>> asteroidsDestroyed(mass = 10, asteroids = [3,9,19,5,21])
    >>> true
    Explanation: One way to order the asteroids is [9,19,5,3,21]:
    - The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19
    - The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38
    - The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43
    - The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46
    - The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67
    All asteroids are destroyed.
    
    Example 2:
    
    >>> asteroidsDestroyed(mass = 5, asteroids = [4,9,23,4])
    >>> false
    Explanation:
    The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
    After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
    This is less than 23, so a collision would not destroy the last asteroid.
    """
def maximumInvitations(favorite: List[int]) -> int:
    """
    A company is organizing a meeting and has a list of n employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees.
    The employees are numbered from 0 to n - 1. Each employee has a favorite person and they will attend the meeting only if they can sit next to their favorite person at the table. The favorite person of an employee is not themself.
    Given a 0-indexed integer array favorite, where favorite[i] denotes the favorite person of the ith employee, return the maximum number of employees that can be invited to the meeting.
    
    Example 1:
    
    
    >>> maximumInvitations(favorite = [2,2,1,2])
    >>> 3
    Explanation:
    The above figure shows how the company can invite employees 0, 1, and 2, and seat them at the round table.
    All employees cannot be invited because employee 2 cannot sit beside employees 0, 1, and 3, simultaneously.
    Note that the company can also invite employees 1, 2, and 3, and give them their desired seats.
    The maximum number of employees that can be invited to the meeting is 3.
    
    Example 2:
    
    >>> maximumInvitations(favorite = [1,2,0])
    >>> 3
    Explanation:
    Each employee is the favorite person of at least one other employee, and the only way the company can invite them is if they invite every employee.
    The seating arrangement will be the same as that in the figure given in example 1:
    - Employee 0 will sit between employees 2 and 1.
    - Employee 1 will sit between employees 0 and 2.
    - Employee 2 will sit between employees 1 and 0.
    The maximum number of employees that can be invited to the meeting is 3.
    
    Example 3:
    
    
    >>> maximumInvitations(favorite = [3,0,1,4,1])
    >>> 4
    Explanation:
    The above figure shows how the company will invite employees 0, 1, 3, and 4, and seat them at the round table.
    Employee 2 cannot be invited because the two spots next to their favorite employee 1 are taken.
    So the company leaves them out of the meeting.
    The maximum number of employees that can be invited to the meeting is 4.
    """
def removeOnes(grid: List[List[int]]) -> bool:
    """
    You are given an m x n binary matrix grid.
    In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).
    Return true if it is possible to remove all 1's from grid using any number of operations or false otherwise.
    
    Example 1:
    
    
    >>> removeOnes(grid = [[0,1,0],[1,0,1],[0,1,0]])
    >>> true
    Explanation: One possible way to remove all 1's from grid is to:
    - Flip the middle row
    - Flip the middle column
    
    Example 2:
    
    
    >>> removeOnes(grid = [[1,1,0],[0,0,0],[0,0,0]])
    >>> false
    Explanation: It is impossible to remove all 1's from grid.
    
    Example 3:
    
    
    >>> removeOnes(grid = [[0]])
    >>> true
    Explanation: There are no 1's in grid.
    """
def capitalizeTitle(title: str) -> str:
    """
    You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
    
    If the length of the word is 1 or 2 letters, change all letters to lowercase.
    Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
    
    Return the capitalized title.
    
    Example 1:
    
    >>> capitalizeTitle(title = "capiTalIze tHe titLe")
    >>> "Capitalize The Title"
    Explanation:
    Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
    
    Example 2:
    
    >>> capitalizeTitle(title = "First leTTeR of EACH Word")
    >>> "First Letter of Each Word"
    Explanation:
    The word "of" has length 2, so it is all lowercase.
    The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
    
    Example 3:
    
    >>> capitalizeTitle(title = "i lOve leetcode")
    >>> "i Love Leetcode"
    Explanation:
    The word "i" has length 1, so it is lowercase.
    The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def pairSum(head: Optional[ListNode]) -> int:
    """
    In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.
    
    For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.
    
    The twin sum is defined as the sum of a node and its twin.
    Given the head of a linked list with even length, return the maximum twin sum of the linked list.
    
    Example 1:
    
    
    >>> __init__(head = [5,4,2,1])
    >>> 6
    Explanation:
    Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
    There are no other nodes with twins in the linked list.
    Thus, the maximum twin sum of the linked list is 6.
    
    Example 2:
    
    
    >>> __init__(head = [4,2,2,3])
    >>> 7
    Explanation:
    The nodes with twins present in this linked list are:
    - Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
    - Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
    Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
    
    Example 3:
    
    
    >>> __init__(head = [1,100000])
    >>> 100001
    Explanation:
    There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
    """
def longestPalindrome(words: List[str]) -> int:
    """
    You are given an array of strings words. Each element of words consists of two lowercase English letters.
    Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected at most once.
    Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.
    A palindrome is a string that reads the same forward and backward.
    
    Example 1:
    
    >>> longestPalindrome(words = ["lc","cl","gg"])
    >>> 6
    Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
    Note that "clgglc" is another longest palindrome that can be created.
    
    Example 2:
    
    >>> longestPalindrome(words = ["ab","ty","yt","lc","cl","ab"])
    >>> 8
    Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
    Note that "lcyttycl" is another longest palindrome that can be created.
    
    Example 3:
    
    >>> longestPalindrome(words = ["cc","ll","xx"])
    >>> 2
    Explanation: One longest palindrome is "cc", of length 2.
    Note that "ll" is another longest palindrome that can be created, and so is "xx".
    """
def possibleToStamp(grid: List[List[int]], stampHeight: int, stampWidth: int) -> bool:
    """
    You are given an m x n binary matrix grid where each cell is either 0 (empty) or 1 (occupied).
    You are then given stamps of size stampHeight x stampWidth. We want to fit the stamps such that they follow the given restrictions and requirements:
    
    Cover all the empty cells.
    Do not cover any of the occupied cells.
    We can put as many stamps as we want.
    Stamps can overlap with each other.
    Stamps are not allowed to be rotated.
    Stamps must stay completely inside the grid.
    
    Return true if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return false.
    
    Example 1:
    
    
    >>> possibleToStamp(grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 4, stampWidth = 3)
    >>> true
    Explanation: We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.
    
    Example 2:
    
    
    >>> possibleToStamp(grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2)
    >>> false
    Explanation: There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.
    """
def checkValid(matrix: List[List[int]]) -> bool:
    """
    An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
    Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.
    
    Example 1:
    
    
    >>> checkValid(matrix = [[1,2,3],[3,1,2],[2,3,1]])
    >>> true
    Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
    Hence, we return true.
    
    Example 2:
    
    
    >>> checkValid(matrix = [[1,1,1],[1,2,3],[1,2,3]])
    >>> false
    Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
    Hence, we return false.
    """
def minSwaps(nums: List[int]) -> int:
    """
    A swap is defined as taking two distinct positions in an array and swapping the values in them.
    A circular array is defined as an array where we consider the first element and the last element to be adjacent.
    Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.
    
    Example 1:
    
    >>> minSwaps(nums = [0,1,0,1,1,0,0])
    >>> 1
    Explanation: Here are a few of the ways to group all the 1's together:
    [0,0,1,1,1,0,0] using 1 swap.
    [0,1,1,1,0,0,0] using 1 swap.
    [1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
    There is no way to group all 1's together with 0 swaps.
    Thus, the minimum number of swaps required is 1.
    
    Example 2:
    
    >>> minSwaps(nums = [0,1,1,1,0,0,1,1,0])
    >>> 2
    Explanation: Here are a few of the ways to group all the 1's together:
    [1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
    [1,1,1,1,1,0,0,0,0] using 2 swaps.
    There is no way to group all 1's together with 0 or 1 swaps.
    Thus, the minimum number of swaps required is 2.
    
    Example 3:
    
    >>> minSwaps(nums = [1,1,0,0,1])
    >>> 0
    Explanation: All the 1's are already grouped together due to the circular property of the array.
    Thus, the minimum number of swaps required is 0.
    """
def wordCount(startWords: List[str], targetWords: List[str]) -> int:
    """
    You are given two 0-indexed arrays of strings startWords and targetWords. Each string consists of lowercase English letters only.
    For each string in targetWords, check if it is possible to choose a string from startWords and perform a conversion operation on it to be equal to that from targetWords.
    The conversion operation is described in the following two steps:
    
    Append any lowercase letter that is not present in the string to its end.
    
    
    For example, if the string is "abc", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be "abcd".
    
    
    Rearrange the letters of the new string in any arbitrary order.
    
    For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.
    
    
    
    Return the number of strings in targetWords that can be obtained by performing the operations on any string of startWords.
    Note that you will only be verifying if the string in targetWords can be obtained from a string in startWords by performing the operations. The strings in startWords do not actually change during this process.
    
    Example 1:
    
    >>> wordCount(startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"])
    >>> 2
    Explanation:
    - In order to form targetWords[0] = "tack", we use startWords[1] = "act", append 'k' to it, and rearrange "actk" to "tack".
    - There is no string in startWords that can be used to obtain targetWords[1] = "act".
      Note that "act" does exist in startWords, but we must append one letter to the string before rearranging it.
    - In order to form targetWords[2] = "acti", we use startWords[1] = "act", append 'i' to it, and rearrange "acti" to "acti" itself.
    
    Example 2:
    
    >>> wordCount(startWords = ["ab","a"], targetWords = ["abc","abcd"])
    >>> 1
    Explanation:
    - In order to form targetWords[0] = "abc", we use startWords[0] = "ab", add 'c' to it, and rearrange it to "abc".
    - There is no string in startWords that can be used to obtain targetWords[1] = "abcd".
    """