ShwStone/humaneval-leetcode huggingface dataset:
def loudAndRich(richer: List[List[int]], quiet: List[int]) -> List[int]:
    """
    There is a group of n people labeled from 0 to n - 1 where each person has a different amount of money and a different level of quietness.
    You are given an array richer where richer[i] = [ai, bi] indicates that ai has more money than bi and an integer array quiet where quiet[i] is the quietness of the ith person. All the given data in richer are logically correct (i.e., the data will not lead you to a situation where x is richer than y and y is richer than x at the same time).
    Return an integer array answer where answer[x] = y if y is the least quiet person (that is, the person y with the smallest value of quiet[y]) among all people who definitely have equal to or more money than the person x.
    
    Example 1:
    
    >>> loudAndRich(richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0])
    >>> [5,5,2,5,4,5,6,7]
    Explanation:
    answer[0] = 5.
    Person 5 has more money than 3, which has more money than 1, which has more money than 0.
    The only person who is quieter (has lower quiet[x]) is person 7, but it is not clear if they have more money than person 0.
    answer[7] = 7.
    Among all people that definitely have equal to or more money than person 7 (which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x]) is person 7.
    The other answers can be filled out with similar reasoning.
    
    Example 2:
    
    >>> loudAndRich(richer = [], quiet = [0])
    >>> [0]
    """
def peakIndexInMountainArray(arr: List[int]) -> int:
    """
    You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease.
    Return the index of the peak element.
    Your task is to solve it in O(log(n)) time complexity.
    
    Example 1:
    
    >>> peakIndexInMountainArray(arr = [0,1,0])
    >>> 1
    
    Example 2:
    
    >>> peakIndexInMountainArray(arr = [0,2,1,0])
    >>> 1
    
    Example 3:
    
    >>> peakIndexInMountainArray(arr = [0,10,5,2])
    >>> 1
    """
def carFleet(target: int, position: List[int], speed: List[int]) -> int:
    """
    There are n cars at given miles away from the starting mile 0, traveling to reach the mile target.
    You are given two integer array position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour.
    A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.
    A car fleet is a car or cars driving next to each other. The speed of the car fleet is the minimum speed of any car in the fleet.
    If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet.
    Return the number of car fleets that will arrive at the destination.
    
    Example 1:
    
    >>> carFleet(target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3])
    >>> 3
    Explanation:
    
    The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target.
    The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.
    The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
    
    
    Example 2:
    
    >>> carFleet(target = 10, position = [3], speed = [3])
    >>> 1
    Explanation:
    There is only one car, hence there is only one fleet.
    Example 3:
    
    >>> carFleet(target = 100, position = [0,2,4], speed = [4,2,1])
    >>> 1
    Explanation:
    
    The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.
    Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
    """
def kSimilarity(s1: str, s2: str) -> int:
    """
    Strings s1 and s2 are k-similar (for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2.
    Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.
    
    Example 1:
    
    >>> kSimilarity(s1 = "ab", s2 = "ba")
    >>> 1
    Explanation: The two string are 1-similar because we can use one swap to change s1 to s2: "ab" --> "ba".
    
    Example 2:
    
    >>> kSimilarity(s1 = "abc", s2 = "bca")
    >>> 2
    Explanation: The two strings are 2-similar because we can use two swaps to change s1 to s2: "abc" --> "bac" --> "bca".
    """
def scoreOfParentheses(s: str) -> int:
    """
    Given a balanced parentheses string s, return the score of the string.
    The score of a balanced parentheses string is based on the following rule:
    
    "()" has score 1.
    AB has score A + B, where A and B are balanced parentheses strings.
    (A) has score 2 * A, where A is a balanced parentheses string.
    
    
    Example 1:
    
    >>> scoreOfParentheses(s = "()")
    >>> 1
    
    Example 2:
    
    >>> scoreOfParentheses(s = "(())")
    >>> 2
    
    Example 3:
    
    >>> scoreOfParentheses(s = "()()")
    >>> 2
    """
def mincostToHireWorkers(quality: List[int], wage: List[int], k: int) -> float:
    """
    There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.
    We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:
    
    Every worker in the paid group must be paid at least their minimum wage expectation.
    In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
    
    Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.
    
    Example 1:
    
    >>> mincostToHireWorkers(quality = [10,20,5], wage = [70,50,30], k = 2)
    >>> 105.00000
    Explanation: We pay 70 to 0th worker and 35 to 2nd worker.
    
    Example 2:
    
    >>> mincostToHireWorkers(quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3)
    >>> 30.66667
    Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
    """
def mirrorReflection(p: int, q: int) -> int:
    """
    There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
    The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
    Given the two integers p and q, return the number of the receptor that the ray meets first.
    The test cases are guaranteed so that the ray will meet a receptor eventually.
    
    Example 1:
    
    
    >>> mirrorReflection(p = 2, q = 1)
    >>> 2
    Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
    
    Example 2:
    
    >>> mirrorReflection(p = 3, q = 1)
    >>> 1
    """
def buddyStrings(s: str, goal: str) -> bool:
    """
    Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
    Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
    
    For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
    
    
    Example 1:
    
    >>> buddyStrings(s = "ab", goal = "ba")
    >>> true
    Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
    
    Example 2:
    
    >>> buddyStrings(s = "ab", goal = "ab")
    >>> false
    Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
    
    Example 3:
    
    >>> buddyStrings(s = "aa", goal = "aa")
    >>> true
    Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
    """
def lemonadeChange(bills: List[int]) -> bool:
    """
    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.
    Note that you do not have any change in hand at first.
    Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.
    
    Example 1:
    
    >>> lemonadeChange(bills = [5,5,5,10,20])
    >>> true
    Explanation:
    From the first 3 customers, we collect three $5 bills in order.
    From the fourth customer, we collect a $10 bill and give back a $5.
    From the fifth customer, we give a $10 bill and a $5 bill.
    Since all customers got correct change, we output true.
    
    Example 2:
    
    >>> lemonadeChange(bills = [5,5,10,10,20])
    >>> false
    Explanation:
    From the first two customers in order, we collect two $5 bills.
    For the next two customers in order, we collect a $10 bill and give back a $5 bill.
    For the last customer, we can not give the change of $15 back because we only have two $10 bills.
    Since not every customer received the correct change, the answer is false.
    """
def matrixScore(grid: List[List[int]]) -> int:
    """
    You are given an m x n binary matrix grid.
    A move consists of choosing any row or column and toggling each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).
    Every row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.
    Return the highest possible score after making any number of moves (including zero moves).
    
    Example 1:
    
    
    >>> matrixScore(grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]])
    >>> 39
    Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
    
    Example 2:
    
    >>> matrixScore(grid = [[0]])
    >>> 1
    """
def shortestSubarray(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.
    A subarray is a contiguous part of an array.
    
    Example 1:
    >>> shortestSubarray(nums = [1], k = 1)
    >>> 1
    Example 2:
    >>> shortestSubarray(nums = [1,2], k = 4)
    >>> -1
    Example 3:
    >>> shortestSubarray(nums = [2,-1,2], k = 3)
    >>> 3
    """
def shortestPathAllKeys(grid: List[str]) -> int:
    """
    You are given an m x n grid grid where:
    
    '.' is an empty cell.
    '#' is a wall.
    '@' is the starting point.
    Lowercase letters represent keys.
    Uppercase letters represent locks.
    
    You start at the starting point and one move consists of walking one space in one of the four cardinal directions. You cannot walk outside the grid, or walk into a wall.
    If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key.
    For some 1 <= k <= 6, there is exactly one lowercase and one uppercase letter of the first k letters of the English alphabet in the grid. This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet.
    Return the lowest number of moves to acquire all keys. If it is impossible, return -1.
    
    Example 1:
    
    
    >>> shortestPathAllKeys(grid = ["@.a..","###.#","b.A.B"])
    >>> 8
    Explanation: Note that the goal is to obtain all the keys not to open all the locks.
    
    Example 2:
    
    
    >>> shortestPathAllKeys(grid = ["@..aA","..B#.","....b"])
    >>> 6
    
    Example 3:
    
    
    >>> shortestPathAllKeys(grid = ["@Aa"])
    >>> -1
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def subtreeWithAllDeepest(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary tree, the depth of each node is the shortest distance to the root.
    Return the smallest subtree such that it contains all the deepest nodes in the original tree.
    A node is called the deepest if it has the largest depth possible among any node in the entire tree.
    The subtree of a node is a tree consisting of that node, plus the set of all descendants of that node.
    
    Example 1:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4])
    >>> [2,7,4]
    Explanation: We return the node with value 2, colored in yellow in the diagram.
    The nodes coloured in blue are the deepest nodes of the tree.
    Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
    
    Example 2:
    
    >>> __init__(root = [1])
    >>> [1]
    Explanation: The root is the deepest node in the tree.
    
    Example 3:
    
    >>> __init__(root = [0,1,3,null,2])
    >>> [2]
    Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
    """
def primePalindrome(n: int) -> int:
    """
    Given an integer n, return the smallest prime palindrome greater than or equal to n.
    An integer is prime if it has exactly two divisors: 1 and itself. Note that 1 is not a prime number.
    
    For example, 2, 3, 5, 7, 11, and 13 are all primes.
    
    An integer is a palindrome if it reads the same from left to right as it does from right to left.
    
    For example, 101 and 12321 are palindromes.
    
    The test cases are generated so that the answer always exists and is in the range [2, 2 * 108].
    
    Example 1:
    >>> primePalindrome(n = 6)
    >>> 7
    Example 2:
    >>> primePalindrome(n = 8)
    >>> 11
    Example 3:
    >>> primePalindrome(n = 13)
    >>> 101
    """
def transpose(matrix: List[List[int]]) -> List[List[int]]:
    """
    Given a 2D integer array matrix, return the transpose of matrix.
    The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
    
    
    Example 1:
    
    >>> transpose(matrix = [[1,2,3],[4,5,6],[7,8,9]])
    >>> [[1,4,7],[2,5,8],[3,6,9]]
    
    Example 2:
    
    >>> transpose(matrix = [[1,2,3],[4,5,6]])
    >>> [[1,4],[2,5],[3,6]]
    """
def binaryGap(n: int) -> int:
    """
    Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.
    Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.
    
    Example 1:
    
    >>> binaryGap(n = 22)
    >>> 2
    Explanation: 22 in binary is "10110".
    The first adjacent pair of 1's is "10110" with a distance of 2.
    The second adjacent pair of 1's is "10110" with a distance of 1.
    The answer is the largest of these two distances, which is 2.
    Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
    
    Example 2:
    
    >>> binaryGap(n = 8)
    >>> 0
    Explanation: 8 in binary is "1000".
    There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
    
    Example 3:
    
    >>> binaryGap(n = 5)
    >>> 2
    Explanation: 5 in binary is "101".
    """
def reorderedPowerOf2(n: int) -> bool:
    """
    You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
    Return true if and only if we can do this so that the resulting number is a power of two.
    
    Example 1:
    
    >>> reorderedPowerOf2(n = 1)
    >>> true
    
    Example 2:
    
    >>> reorderedPowerOf2(n = 10)
    >>> false
    """
def advantageCount(nums1: List[int], nums2: List[int]) -> List[int]:
    """
    You are given two integer arrays nums1 and nums2 both of the same length. The advantage of nums1 with respect to nums2 is the number of indices i for which nums1[i] > nums2[i].
    Return any permutation of nums1 that maximizes its advantage with respect to nums2.
    
    Example 1:
    >>> advantageCount(nums1 = [2,7,11,15], nums2 = [1,10,4,11])
    >>> [2,11,7,15]
    Example 2:
    >>> advantageCount(nums1 = [12,24,8,32], nums2 = [13,25,32,11])
    >>> [24,32,8,12]
    """
def minRefuelStops(target: int, startFuel: int, stations: List[List[int]]) -> int:
    """
    A car travels from a starting position to a destination which is target miles east of the starting position.
    There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starting position and has fueli liters of gas.
    The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
    Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.
    Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
    
    Example 1:
    
    >>> minRefuelStops(target = 1, startFuel = 1, stations = [])
    >>> 0
    Explanation: We can reach the target without refueling.
    
    Example 2:
    
    >>> minRefuelStops(target = 100, startFuel = 1, stations = [[10,100]])
    >>> -1
    Explanation: We can not reach the target (or even the first gas station).
    
    Example 3:
    
    >>> minRefuelStops(target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]])
    >>> 2
    Explanation: We start with 10 liters of fuel.
    We drive to position 10, expending 10 liters of fuel.  We refuel from 0 liters to 60 liters of gas.
    Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
    and refuel from 10 liters to 50 liters of gas.  We then drive to and reach the target.
    We made 2 refueling stops along the way, so we return 2.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def leafSimilar(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    """
    Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
    
    For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
    Two binary trees are considered leaf-similar if their leaf value sequence is the same.
    Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
    
    Example 1:
    
    
    >>> __init__(root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8])
    >>> true
    
    Example 2:
    
    
    >>> __init__(root1 = [1,2,3], root2 = [1,3,2])
    >>> false
    """
def lenLongestFibSubseq(arr: List[int]) -> int:
    """
    A sequence x1, x2, ..., xn is Fibonacci-like if:
    
    n >= 3
    xi + xi+1 == xi+2 for all i + 2 <= n
    
    Given a strictly increasing array arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence of arr. If one does not exist, return 0.
    A subsequence is derived from another sequence arr by deleting any number of elements (including none) from arr, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].
    
    Example 1:
    
    >>> lenLongestFibSubseq(arr = [1,2,3,4,5,6,7,8])
    >>> 5
    Explanation: The longest subsequence that is fibonacci-like: [1,2,3,5,8].
    Example 2:
    
    >>> lenLongestFibSubseq(arr = [1,3,7,11,12,14,18])
    >>> 3
    Explanation: The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18].
    """
def robotSim(commands: List[int], obstacles: List[List[int]]) -> int:
    """
    A robot on an infinite XY-plane starts at point (0, 0) facing north. The robot receives an array of integers commands, which represents a sequence of moves that it needs to execute. There are only three possible types of instructions the robot can receive:
    
    -2: Turn left 90 degrees.
    -1: Turn right 90 degrees.
    1 <= k <= 9: Move forward k units, one unit at a time.
    
    Some of the grid squares are obstacles. The ith obstacle is at grid point obstacles[i] = (xi, yi). If the robot runs into an obstacle, it will stay in its current location (on the block adjacent to the obstacle) and move onto the next command.
    Return the maximum squared Euclidean distance that the robot reaches at any point in its path (i.e. if the distance is 5, return 25).
    Note:
    
    There can be an obstacle at (0, 0). If this happens, the robot will ignore the obstacle until it has moved off the origin. However, it will be unable to return to (0, 0) due to the obstacle.
    North means +Y direction.
    East means +X direction.
    South means -Y direction.
    West means -X direction.
    
    
    Example 1:
    
    >>> robotSim(commands = [4,-1,3], obstacles = [])
    >>> 25
    Explanation:
    The robot starts at (0, 0):
    
    Move north 4 units to (0, 4).
    Turn right.
    Move east 3 units to (3, 4).
    
    The furthest point the robot ever gets from the origin is (3, 4), which squared is 32 + 42 = 25 units away.
    
    Example 2:
    
    >>> robotSim(commands = [4,-1,4,-2,4], obstacles = [[2,4]])
    >>> 65
    Explanation:
    The robot starts at (0, 0):
    
    Move north 4 units to (0, 4).
    Turn right.
    Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).
    Turn left.
    Move north 4 units to (1, 8).
    
    The furthest point the robot ever gets from the origin is (1, 8), which squared is 12 + 82 = 65 units away.
    
    Example 3:
    
    >>> robotSim(commands = [6,-1,-1,6], obstacles = [[0,0]])
    >>> 36
    Explanation:
    The robot starts at (0, 0):
    
    Move north 6 units to (0, 6).
    Turn right.
    Turn right.
    Move south 5 units and get blocked by the obstacle at (0,0), robot is at (0, 1).
    
    The furthest point the robot ever gets from the origin is (0, 6), which squared is 62 = 36 units away.
    """
def minEatingSpeed(piles: List[int], h: int) -> int:
    """
    Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
    Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
    Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
    Return the minimum integer k such that she can eat all the bananas within h hours.
    
    Example 1:
    
    >>> minEatingSpeed(piles = [3,6,7,11], h = 8)
    >>> 4
    
    Example 2:
    
    >>> minEatingSpeed(piles = [30,11,23,4,20], h = 5)
    >>> 30
    
    Example 3:
    
    >>> minEatingSpeed(piles = [30,11,23,4,20], h = 6)
    >>> 23
    """