ShwStone/humaneval-leetcode huggingface dataset:
def maxSumSubmatrix(matrix: List[List[int]], k: int) -> int:
    """
    Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.
    It is guaranteed that there will be a rectangle with a sum no larger than k.
    
    Example 1:
    
    
    >>> maxSumSubmatrix(matrix = [[1,0,1],[0,-2,3]], k = 2)
    >>> 2
    Explanation: Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).
    
    Example 2:
    
    >>> maxSumSubmatrix(matrix = [[2,2,-1]], k = 3)
    >>> 3
    """
def canMeasureWater(x: int, y: int, target: int) -> bool:
    """
    You are given two jugs with capacities x liters and y liters. You have an infinite water supply. Return whether the total amount of water in both jugs may reach target using the following operations:
    
    Fill either jug completely with water.
    Completely empty either jug.
    Pour water from one jug into another until the receiving jug is full, or the transferring jug is empty.
    
    
    Example 1:
    
    >>> canMeasureWater(x = 3, y = 5, target = 4)
    >>> true
    Explanation:
    Follow these steps to reach a total of 4 liters:
    
    Fill the 5-liter jug (0, 5).
    Pour from the 5-liter jug into the 3-liter jug, leaving 2 liters (3, 2).
    Empty the 3-liter jug (0, 2).
    Transfer the 2 liters from the 5-liter jug to the 3-liter jug (2, 0).
    Fill the 5-liter jug again (2, 5).
    Pour from the 5-liter jug into the 3-liter jug until the 3-liter jug is full. This leaves 4 liters in the 5-liter jug (3, 4).
    Empty the 3-liter jug. Now, you have exactly 4 liters in the 5-liter jug (0, 4).
    
    Reference: The Die Hard example.
    
    Example 2:
    
    >>> canMeasureWater(x = 2, y = 6, target = 5)
    >>> false
    
    Example 3:
    
    >>> canMeasureWater(x = 1, y = 2, target = 3)
    >>> true
    Explanation: Fill both jugs. The total amount of water in both jugs is equal to 3 now.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findLeaves(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, collect a tree's nodes as if you were doing this:
    
    Collect all the leaf nodes.
    Remove all the leaf nodes.
    Repeat until the tree is empty.
    
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4,5])
    >>> [[4,5,3],[2],[1]]
    Explanation:
    [[3,5,4],[2],[1]] and [[3,4,5],[2],[1]] are also considered correct answers since per each level it does not matter the order on which elements are returned.
    
    Example 2:
    
    >>> __init__(root = [1])
    >>> [[1]]
    """
def isPerfectSquare(num: int) -> bool:
    """
    Given a positive integer num, return true if num is a perfect square or false otherwise.
    A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.
    You must not use any built-in library function, such as sqrt.
    
    Example 1:
    
    >>> isPerfectSquare(num = 16)
    >>> true
    Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
    
    Example 2:
    
    >>> isPerfectSquare(num = 14)
    >>> false
    Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
    """
def largestDivisibleSubset(nums: List[int]) -> List[int]:
    """
    Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:
    
    answer[i] % answer[j] == 0, or
    answer[j] % answer[i] == 0
    
    If there are multiple solutions, return any of them.
    
    Example 1:
    
    >>> largestDivisibleSubset(nums = [1,2,3])
    >>> [1,2]
    Explanation: [1,3] is also accepted.
    
    Example 2:
    
    >>> largestDivisibleSubset(nums = [1,2,4,8])
    >>> [1,2,4,8]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def plusOne(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given a non-negative integer represented as a linked list of digits, plus one to the integer.
    The digits are stored such that the most significant digit is at the head of the list.
    
    Example 1:
    >>> __init__(head = [1,2,3])
    >>> [1,2,4]
    Example 2:
    >>> __init__(head = [0])
    >>> [1]
    """
def getModifiedArray(length: int, updates: List[List[int]]) -> List[int]:
    """
    You are given an integer length and an array updates where updates[i] = [startIdxi, endIdxi, inci].
    You have an array arr of length length with all zeros, and you have some operation to apply on arr. In the ith operation, you should increment all the elements arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] by inci.
    Return arr after applying all the updates.
    
    Example 1:
    
    
    >>> getModifiedArray(length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]])
    >>> [-2,0,3,5,3]
    
    Example 2:
    
    >>> getModifiedArray(length = 10, updates = [[2,4,6],[5,6,8],[1,9,-4]])
    >>> [0,-4,2,2,2,4,4,-4,-4,-4]
    """
def getSum(a: int, b: int) -> int:
    """
    Given two integers a and b, return the sum of the two integers without using the operators + and -.
    
    Example 1:
    >>> getSum(a = 1, b = 2)
    >>> 3
    Example 2:
    >>> getSum(a = 2, b = 3)
    >>> 5
    """
def kSmallestPairs(nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
    """
    You are given two integer arrays nums1 and nums2 sorted in non-decreasing order and an integer k.
    Define a pair (u, v) which consists of one element from the first array and one element from the second array.
    Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.
    
    Example 1:
    
    >>> kSmallestPairs(nums1 = [1,7,11], nums2 = [2,4,6], k = 3)
    >>> [[1,2],[1,4],[1,6]]
    Explanation: The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
    
    Example 2:
    
    >>> kSmallestPairs(nums1 = [1,1,2], nums2 = [1,2,3], k = 2)
    >>> [[1,1],[1,1]]
    Explanation: The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
    """
def getMoneyAmount(n: int) -> int:
    """
    We are playing the Guessing Game. The game will work as follows:
    
    I pick a number between 1 and n.
    You guess a number.
    If you guess the right number, you win the game.
    If you guess the wrong number, then I will tell you whether the number I picked is higher or lower, and you will continue guessing.
    Every time you guess a wrong number x, you will pay x dollars. If you run out of money, you lose the game.
    
    Given a particular n, return the minimum amount of money you need to guarantee a win regardless of what number I pick.
    
    Example 1:
    
    
    >>> getMoneyAmount(n = 10)
    >>> 16
    Explanation: The winning strategy is as follows:
    - The range is [1,10]. Guess 7.
        - If this is my number, your total is $0. Otherwise, you pay $7.
        - If my number is higher, the range is [8,10]. Guess 9.
            - If this is my number, your total is $7. Otherwise, you pay $9.
            - If my number is higher, it must be 10. Guess 10. Your total is $7 + $9 = $16.
            - If my number is lower, it must be 8. Guess 8. Your total is $7 + $9 = $16.
        - If my number is lower, the range is [1,6]. Guess 3.
            - If this is my number, your total is $7. Otherwise, you pay $3.
            - If my number is higher, the range is [4,6]. Guess 5.
                - If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $5.
                - If my number is higher, it must be 6. Guess 6. Your total is $7 + $3 + $5 = $15.
                - If my number is lower, it must be 4. Guess 4. Your total is $7 + $3 + $5 = $15.
            - If my number is lower, the range is [1,2]. Guess 1.
                - If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $1.
                - If my number is higher, it must be 2. Guess 2. Your total is $7 + $3 + $1 = $11.
    The worst case in all these scenarios is that you pay $16. Hence, you only need $16 to guarantee a win.
    
    Example 2:
    
    >>> getMoneyAmount(n = 1)
    >>> 0
    Explanation: There is only one possible number, so you can guess 1 and not have to pay anything.
    
    Example 3:
    
    >>> getMoneyAmount(n = 2)
    >>> 1
    Explanation: There are two possible numbers, 1 and 2.
    - Guess 1.
        - If this is my number, your total is $0. Otherwise, you pay $1.
        - If my number is higher, it must be 2. Guess 2. Your total is $1.
    The worst case is that you pay $1.
    """
def wiggleMaxLength(nums: List[int]) -> int:
    """
    A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
    
    For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.
    In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.
    
    A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
    Given an integer array nums, return the length of the longest wiggle subsequence of nums.
    
    Example 1:
    
    >>> wiggleMaxLength(nums = [1,7,4,9,2,5])
    >>> 6
    Explanation: The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
    
    Example 2:
    
    >>> wiggleMaxLength(nums = [1,17,5,10,13,15,10,5,16,8])
    >>> 7
    Explanation: There are several subsequences that achieve this length.
    One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
    
    Example 3:
    
    >>> wiggleMaxLength(nums = [1,2,3,4,5,6,7,8,9])
    >>> 2
    """
def combinationSum4(nums: List[int], target: int) -> int:
    """
    Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
    The test cases are generated so that the answer can fit in a 32-bit integer.
    
    Example 1:
    
    >>> combinationSum4(nums = [1,2,3], target = 4)
    >>> 7
    Explanation:
    The possible combination ways are:
    (1, 1, 1, 1)
    (1, 1, 2)
    (1, 2, 1)
    (1, 3)
    (2, 1, 1)
    (2, 2)
    (3, 1)
    Note that different sequences are counted as different combinations.
    
    Example 2:
    
    >>> combinationSum4(nums = [9], target = 3)
    >>> 0
    """
def kthSmallest(matrix: List[List[int]], k: int) -> int:
    """
    Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element in the matrix.
    Note that it is the kth smallest element in the sorted order, not the kth distinct element.
    You must find a solution with a memory complexity better than O(n2).
    
    Example 1:
    
    >>> kthSmallest(matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8)
    >>> 13
    Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13
    
    Example 2:
    
    >>> kthSmallest(matrix = [[-5]], k = 1)
    >>> -5
    """
def canConstruct(ransomNote: str, magazine: str) -> bool:
    """
    Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
    Each letter in magazine can only be used once in ransomNote.
    
    Example 1:
    >>> canConstruct(ransomNote = "a", magazine = "b")
    >>> false
    Example 2:
    >>> canConstruct(ransomNote = "aa", magazine = "ab")
    >>> false
    Example 3:
    >>> canConstruct(ransomNote = "aa", magazine = "aab")
    >>> true
    """
def lexicalOrder(n: int) -> List[int]:
    """
    Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
    You must write an algorithm that runs in O(n) time and uses O(1) extra space.
    
    Example 1:
    >>> lexicalOrder(n = 13)
    >>> [1,10,11,12,13,2,3,4,5,6,7,8,9]
    Example 2:
    >>> lexicalOrder(n = 2)
    >>> [1,2]
    """
def firstUniqChar(s: str) -> int:
    """
    Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
    
    Example 1:
    
    >>> firstUniqChar(s = "leetcode")
    >>> 0
    Explanation:
    The character 'l' at index 0 is the first character that does not occur at any other index.
    
    Example 2:
    
    >>> firstUniqChar(s = "loveleetcode")
    >>> 2
    
    Example 3:
    
    >>> firstUniqChar(s = "aabb")
    >>> -1
    """
def lengthLongestPath(input: str) -> int:
    """
    Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:
    
    Here, we have dir as the only directory in the root. dir contains two subdirectories, subdir1 and subdir2. subdir1 contains a file file1.ext and subdirectory subsubdir1. subdir2 contains a subdirectory subsubdir2, which contains a file file2.ext.
    In text form, it looks like this (with ⟶ representing the tab character):
    
    dir
    ⟶ subdir1
    ⟶ ⟶ file1.ext
    ⟶ ⟶ subsubdir1
    ⟶ subdir2
    ⟶ ⟶ subsubdir2
    ⟶ ⟶ ⟶ file2.ext
    
    If we were to write this representation in code, it will look like this: "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". Note that the '\n' and '\t' are the new-line and tab characters.
    Every file and directory has a unique absolute path in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by '/'s. Using the above example, the absolute path to file2.ext is "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of letters, digits, and/or spaces. Each file name is of the form name.extension, where name and extension consist of letters, digits, and/or spaces.
    Given a string input representing the file system in the explained format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.
    Note that the testcases are generated such that the file system is valid and no file or directory name has length 0.
    
    Example 1:
    
    
    >>> lengthLongestPath(input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext")
    >>> 20
    Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
    
    Example 2:
    
    
    >>> lengthLongestPath(input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext")
    >>> 32
    Explanation: We have two files:
    "dir/subdir1/file1.ext" of length 21
    "dir/subdir2/subsubdir2/file2.ext" of length 32.
    We return 32 since it is the longest absolute path to a file.
    
    Example 3:
    
    >>> lengthLongestPath(input = "a")
    >>> 0
    Explanation: We do not have any files, just a single directory named "a".
    """
def findTheDifference(s: str, t: str) -> str:
    """
    You are given two strings s and t.
    String t is generated by random shuffling string s and then add one more letter at a random position.
    Return the letter that was added to t.
    
    Example 1:
    
    >>> findTheDifference(s = "abcd", t = "abcde")
    >>> "e"
    Explanation: 'e' is the letter that was added.
    
    Example 2:
    
    >>> findTheDifference(s = "", t = "y")
    >>> "y"
    """
def lastRemaining(n: int) -> int:
    """
    You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:
    
    Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
    Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.
    Keep repeating the steps again, alternating left to right and right to left, until a single number remains.
    
    Given the integer n, return the last number that remains in arr.
    
    Example 1:
    
    >>> lastRemaining(n = 9)
    >>> 6
    Explanation:
    arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    arr = [2, 4, 6, 8]
    arr = [2, 6]
    arr = [6]
    
    Example 2:
    
    >>> lastRemaining(n = 1)
    >>> 1
    """
def isRectangleCover(rectangles: List[List[int]]) -> bool:
    """
    Given an array rectangles where rectangles[i] = [xi, yi, ai, bi] represents an axis-aligned rectangle. The bottom-left point of the rectangle is (xi, yi) and the top-right point of it is (ai, bi).
    Return true if all the rectangles together form an exact cover of a rectangular region.
    
    Example 1:
    
    
    >>> isRectangleCover(rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]])
    >>> true
    Explanation: All 5 rectangles together form an exact cover of a rectangular region.
    
    Example 2:
    
    
    >>> isRectangleCover(rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]])
    >>> false
    Explanation: Because there is a gap between the two rectangular regions.
    
    Example 3:
    
    
    >>> isRectangleCover(rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]])
    >>> false
    Explanation: Because two of the rectangles overlap with each other.
    """
def isSubsequence(s: str, t: str) -> bool:
    """
    Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
    A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
    
    Example 1:
    >>> isSubsequence(s = "abc", t = "ahbgdc")
    >>> true
    Example 2:
    >>> isSubsequence(s = "axc", t = "ahbgdc")
    >>> false
    """
def validUtf8(data: List[int]) -> bool:
    """
    Given an integer array data representing the data, return whether it is a valid UTF-8 encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).
    A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
    
    For a 1-byte character, the first bit is a 0, followed by its Unicode code.
    For an n-bytes character, the first n bits are all one's, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.
    
    This is how the UTF-8 encoding would work:
    
         Number of Bytes   |        UTF-8 Octet Sequence
                           |              (binary)
       --------------------+-----------------------------------------
                1          |   0xxxxxxx
                2          |   110xxxxx 10xxxxxx
                3          |   1110xxxx 10xxxxxx 10xxxxxx
                4          |   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    
    x denotes a bit in the binary form of a byte that may be either 0 or 1.
    Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.
    
    Example 1:
    
    >>> validUtf8(data = [197,130,1])
    >>> true
    Explanation: data represents the octet sequence: 11000101 10000010 00000001.
    It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
    
    Example 2:
    
    >>> validUtf8(data = [235,140,4])
    >>> false
    Explanation: data represented the octet sequence: 11101011 10001100 00000100.
    The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
    The next byte is a continuation byte which starts with 10 and that's correct.
    But the second continuation byte does not start with 10, so it is invalid.
    """
def decodeString(s: str) -> str:
    """
    Given an encoded string, return its decoded string.
    The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
    You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].
    The test cases are generated so that the length of the output will never exceed 105.
    
    Example 1:
    
    >>> decodeString(s = "3[a]2[bc]")
    >>> "aaabcbc"
    
    Example 2:
    
    >>> decodeString(s = "3[a2[c]]")
    >>> "accaccacc"
    
    Example 3:
    
    >>> decodeString(s = "2[abc]3[cd]ef")
    >>> "abcabccdcdcdef"
    """
def longestSubstring(s: str, k: int) -> int:
    """
    Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.
    if no such substring exists, return 0.
    
    Example 1:
    
    >>> longestSubstring(s = "aaabb", k = 3)
    >>> 3
    Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
    
    Example 2:
    
    >>> longestSubstring(s = "ababbc", k = 2)
    >>> 5
    Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
    """
def maxRotateFunction(nums: List[int]) -> int:
    """
    You are given an integer array nums of length n.
    Assume arrk to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as follow:
    
    F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
    
    Return the maximum value of F(0), F(1), ..., F(n-1).
    The test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    >>> maxRotateFunction(nums = [4,3,2,6])
    >>> 26
    Explanation:
    F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
    F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
    F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
    F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
    So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
    
    Example 2:
    
    >>> maxRotateFunction(nums = [100])
    >>> 0
    """
def integerReplacement(n: int) -> int:
    """
    Given a positive integer n, you can apply one of the following operations:
    
    If n is even, replace n with n / 2.
    If n is odd, replace n with either n + 1 or n - 1.
    
    Return the minimum number of operations needed for n to become 1.
    
    Example 1:
    
    >>> integerReplacement(n = 8)
    >>> 3
    Explanation: 8 -> 4 -> 2 -> 1
    
    Example 2:
    
    >>> integerReplacement(n = 7)
    >>> 4
    Explanation: 7 -> 8 -> 4 -> 2 -> 1
    or 7 -> 6 -> 3 -> 2 -> 1
    
    Example 3:
    
    >>> integerReplacement(n = 4)
    >>> 2
    """