ShwStone/humaneval-leetcode huggingface dataset:
def isHappy(n: int) -> bool:
    """
    Write an algorithm to determine if a number n is happy.
    A happy number is a number defined by the following process:
    
    Starting with any positive integer, replace the number by the sum of the squares of its digits.
    Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
    Those numbers for which this process ends in 1 are happy.
    
    Return true if n is a happy number, and false if not.
    
    Example 1:
    
    >>> isHappy(n = 19)
    >>> true
    Explanation:
    12 + 92 = 82
    82 + 22 = 68
    62 + 82 = 100
    12 + 02 + 02 = 1
    
    Example 2:
    
    >>> isHappy(n = 2)
    >>> false
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(head: Optional[ListNode], val: int) -> Optional[ListNode]:
    """
    Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,6,3,4,5,6], val = 6)
    >>> [1,2,3,4,5]
    
    Example 2:
    
    >>> __init__(head = [], val = 1)
    >>> []
    
    Example 3:
    
    >>> __init__(head = [7,7,7,7], val = 7)
    >>> []
    """
def countPrimes(n: int) -> int:
    """
    Given an integer n, return the number of prime numbers that are strictly less than n.
    
    Example 1:
    
    >>> countPrimes(n = 10)
    >>> 4
    Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
    
    Example 2:
    
    >>> countPrimes(n = 0)
    >>> 0
    
    Example 3:
    
    >>> countPrimes(n = 1)
    >>> 0
    """
def isIsomorphic(s: str, t: str) -> bool:
    """
    Given two strings s and t, determine if they are isomorphic.
    Two strings s and t are isomorphic if the characters in s can be replaced to get t.
    All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
    
    Example 1:
    
    >>> isIsomorphic(s = "egg", t = "add")
    >>> true
    Explanation:
    The strings s and t can be made identical by:
    
    Mapping 'e' to 'a'.
    Mapping 'g' to 'd'.
    
    
    Example 2:
    
    >>> isIsomorphic(s = "foo", t = "bar")
    >>> false
    Explanation:
    The strings s and t can not be made identical as 'o' needs to be mapped to both 'a' and 'r'.
    
    Example 3:
    
    >>> isIsomorphic(s = "paper", t = "title")
    >>> true
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given the head of a singly linked list, reverse the list, and return the reversed list.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5])
    >>> [5,4,3,2,1]
    
    Example 2:
    
    
    >>> __init__(head = [1,2])
    >>> [2,1]
    
    Example 3:
    
    >>> __init__(head = [])
    >>> []
    """
def canFinish(numCourses: int, prerequisites: List[List[int]]) -> bool:
    """
    There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
    
    For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
    
    Return true if you can finish all courses. Otherwise, return false.
    
    Example 1:
    
    >>> canFinish(numCourses = 2, prerequisites = [[1,0]])
    >>> true
    Explanation: There are a total of 2 courses to take.
    To take course 1 you should have finished course 0. So it is possible.
    
    Example 2:
    
    >>> canFinish(numCourses = 2, prerequisites = [[1,0],[0,1]])
    >>> false
    Explanation: There are a total of 2 courses to take.
    To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
    """
def minSubArrayLen(target: int, nums: List[int]) -> int:
    """
    Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
    
    Example 1:
    
    >>> minSubArrayLen(target = 7, nums = [2,3,1,2,4,3])
    >>> 2
    Explanation: The subarray [4,3] has the minimal length under the problem constraint.
    
    Example 2:
    
    >>> minSubArrayLen(target = 4, nums = [1,4,4])
    >>> 1
    
    Example 3:
    
    >>> minSubArrayLen(target = 11, nums = [1,1,1,1,1,1,1,1])
    >>> 0
    """
def findOrder(numCourses: int, prerequisites: List[List[int]]) -> List[int]:
    """
    There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
    
    For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
    
    Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
    
    Example 1:
    
    >>> findOrder(numCourses = 2, prerequisites = [[1,0]])
    >>> [0,1]
    Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
    
    Example 2:
    
    >>> findOrder(numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]])
    >>> [0,2,1,3]
    Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
    So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
    
    Example 3:
    
    >>> findOrder(numCourses = 1, prerequisites = [])
    >>> [0]
    """
def findWords(board: List[List[str]], words: List[str]) -> List[str]:
    """
    Given an m x n board of characters and a list of strings words, return all words on the board.
    Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
    
    Example 1:
    
    
    >>> findWords(board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"])
    >>> ["eat","oath"]
    
    Example 2:
    
    
    >>> findWords(board = [["a","b"],["c","d"]], words = ["abcb"])
    >>> []
    """
def rob(nums: List[int]) -> int:
    """
    You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
    Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
    
    Example 1:
    
    >>> rob(nums = [2,3,2])
    >>> 3
    Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
    
    Example 2:
    
    >>> rob(nums = [1,2,3,1])
    >>> 4
    Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
    Total amount you can rob = 1 + 3 = 4.
    
    Example 3:
    
    >>> rob(nums = [1,2,3])
    >>> 3
    """
def shortestPalindrome(s: str) -> str:
    """
    You are given a string s. You can convert s to a palindrome by adding characters in front of it.
    Return the shortest palindrome you can find by performing this transformation.
    
    Example 1:
    >>> shortestPalindrome(s = "aacecaaa")
    >>> "aaacecaaa"
    Example 2:
    >>> shortestPalindrome(s = "abcd")
    >>> "dcbabcd"
    """
def findKthLargest(nums: List[int], k: int) -> int:
    """
    Given an integer array nums and an integer k, return the kth largest element in the array.
    Note that it is the kth largest element in the sorted order, not the kth distinct element.
    Can you solve it without sorting?
    
    Example 1:
    >>> findKthLargest(nums = [3,2,1,5,6,4], k = 2)
    >>> 5
    Example 2:
    >>> findKthLargest(nums = [3,2,3,1,2,4,5,5,6], k = 4)
    >>> 4
    """
def combinationSum3(k: int, n: int) -> List[List[int]]:
    """
    Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
    
    Only numbers 1 through 9 are used.
    Each number is used at most once.
    
    Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
    
    Example 1:
    
    >>> combinationSum3(k = 3, n = 7)
    >>> [[1,2,4]]
    Explanation:
    1 + 2 + 4 = 7
    There are no other valid combinations.
    Example 2:
    
    >>> combinationSum3(k = 3, n = 9)
    >>> [[1,2,6],[1,3,5],[2,3,4]]
    Explanation:
    1 + 2 + 6 = 9
    1 + 3 + 5 = 9
    2 + 3 + 4 = 9
    There are no other valid combinations.
    
    Example 3:
    
    >>> combinationSum3(k = 4, n = 1)
    >>> []
    Explanation: There are no valid combinations.
    Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
    """
def containsDuplicate(nums: List[int]) -> bool:
    """
    Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
    
    Example 1:
    
    >>> containsDuplicate(nums = [1,2,3,1])
    >>> true
    Explanation:
    The element 1 occurs at the indices 0 and 3.
    
    Example 2:
    
    >>> containsDuplicate(nums = [1,2,3,4])
    >>> false
    Explanation:
    All elements are distinct.
    
    Example 3:
    
    >>> containsDuplicate(nums = [1,1,1,3,3,4,3,2,4,2])
    >>> true
    """
def getSkyline(buildings: List[List[int]]) -> List[List[int]]:
    """
    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively.
    The geometric information of each building is given in the array buildings where buildings[i] = [lefti, righti, heighti]:
    
    lefti is the x coordinate of the left edge of the ith building.
    righti is the x coordinate of the right edge of the ith building.
    heighti is the height of the ith building.
    
    You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
    The skyline should be represented as a list of "key points" sorted by their x-coordinate in the form [[x1,y1],[x2,y2],...]. Each key point is the left endpoint of some horizontal segment in the skyline except the last point in the list, which always has a y-coordinate 0 and is used to mark the skyline's termination where the rightmost building ends. Any ground between the leftmost and rightmost buildings should be part of the skyline's contour.
    Note: There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...,[2 3],[4 5],[7 5],[11 5],[12 7],...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...,[2 3],[4 5],[12 7],...]
    
    Example 1:
    
    
    >>> getSkyline(buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]])
    >>> [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
    Explanation:
    Figure A shows the buildings of the input.
    Figure B shows the skyline formed by those buildings. The red points in figure B represent the key points in the output list.
    
    Example 2:
    
    >>> getSkyline(buildings = [[0,2,3],[2,5,3]])
    >>> [[0,3],[5,0]]
    """
def containsNearbyDuplicate(nums: List[int], k: int) -> bool:
    """
    Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
    
    Example 1:
    
    >>> containsNearbyDuplicate(nums = [1,2,3,1], k = 3)
    >>> true
    
    Example 2:
    
    >>> containsNearbyDuplicate(nums = [1,0,1,1], k = 1)
    >>> true
    
    Example 3:
    
    >>> containsNearbyDuplicate(nums = [1,2,3,1,2,3], k = 2)
    >>> false
    """
def containsNearbyAlmostDuplicate(nums: List[int], indexDiff: int, valueDiff: int) -> bool:
    """
    You are given an integer array nums and two integers indexDiff and valueDiff.
    Find a pair of indices (i, j) such that:
    
    i != j,
    abs(i - j) <= indexDiff.
    abs(nums[i] - nums[j]) <= valueDiff, and
    
    Return true if such pair exists or false otherwise.
    
    Example 1:
    
    >>> containsNearbyAlmostDuplicate(nums = [1,2,3,1], indexDiff = 3, valueDiff = 0)
    >>> true
    Explanation: We can choose (i, j) = (0, 3).
    We satisfy the three conditions:
    i != j --> 0 != 3
    abs(i - j) <= indexDiff --> abs(0 - 3) <= 3
    abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
    
    Example 2:
    
    >>> containsNearbyAlmostDuplicate(nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3)
    >>> false
    Explanation: After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
    """
def maximalSquare(matrix: List[List[str]]) -> int:
    """
    Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
    
    Example 1:
    
    
    >>> maximalSquare(matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]])
    >>> 4
    
    Example 2:
    
    
    >>> maximalSquare(matrix = [["0","1"],["1","0"]])
    >>> 1
    
    Example 3:
    
    >>> maximalSquare(matrix = [["0"]])
    >>> 0
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def countNodes(root: Optional[TreeNode]) -> int:
    """
    Given the root of a complete binary tree, return the number of the nodes in the tree.
    According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
    Design an algorithm that runs in less than O(n) time complexity.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3,4,5,6])
    >>> 6
    
    Example 2:
    
    >>> __init__(root = [])
    >>> 0
    
    Example 3:
    
    >>> __init__(root = [1])
    >>> 1
    """
def computeArea(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
    """
    Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
    The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
    The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
    
    Example 1:
    
    
    >>> computeArea(ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2)
    >>> 45
    
    Example 2:
    
    >>> computeArea(ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2)
    >>> 16
    """
def calculate(s: str) -> int:
    """
    Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
    Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
    
    Example 1:
    
    >>> calculate(s = "1 + 1")
    >>> 2
    
    Example 2:
    
    >>> calculate(s = " 2-1 + 2 ")
    >>> 3
    
    Example 3:
    
    >>> calculate(s = "(1+(4+5+2)-3)+(6+8)")
    >>> 23
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(root: Optional[TreeNode]) -> Optional[TreeNode]:
    """
    Given the root of a binary tree, invert the tree, and return its root.
    
    Example 1:
    
    
    >>> __init__(root = [4,2,7,1,3,6,9])
    >>> [4,7,2,9,6,3,1]
    
    Example 2:
    
    
    >>> __init__(root = [2,1,3])
    >>> [2,3,1]
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    """
def calculate(s: str) -> int:
    """
    Given a string s which represents an expression, evaluate this expression and return its value.
    The integer division should truncate toward zero.
    You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].
    Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
    
    Example 1:
    >>> calculate(s = "3+2*2")
    >>> 7
    Example 2:
    >>> calculate(s = " 3/2 ")
    >>> 1
    Example 3:
    >>> calculate(s = " 3+5 / 2 ")
    >>> 5
    """
def summaryRanges(nums: List[int]) -> List[str]:
    """
    You are given a sorted unique integer array nums.
    A range [a,b] is the set of all integers from a to b (inclusive).
    Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
    Each range [a,b] in the list should be output as:
    
    "a->b" if a != b
    "a" if a == b
    
    
    Example 1:
    
    >>> summaryRanges(nums = [0,1,2,4,5,7])
    >>> ["0->2","4->5","7"]
    Explanation: The ranges are:
    [0,2] --> "0->2"
    [4,5] --> "4->5"
    [7,7] --> "7"
    
    Example 2:
    
    >>> summaryRanges(nums = [0,2,3,4,6,8,9])
    >>> ["0","2->4","6","8->9"]
    Explanation: The ranges are:
    [0,0] --> "0"
    [2,4] --> "2->4"
    [6,6] --> "6"
    [8,9] --> "8->9"
    """
def majorityElement(nums: List[int]) -> List[int]:
    """
    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
    
    Example 1:
    
    >>> majorityElement(nums = [3,2,3])
    >>> [3]
    
    Example 2:
    
    >>> majorityElement(nums = [1])
    >>> [1]
    
    Example 3:
    
    >>> majorityElement(nums = [1,2])
    >>> [1,2]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def kthSmallest(root: Optional[TreeNode], k: int) -> int:
    """
    Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
    
    Example 1:
    
    
    >>> __init__(root = [3,1,4,null,2], k = 1)
    >>> 1
    
    Example 2:
    
    
    >>> __init__(root = [5,3,6,2,4,null,null,1], k = 3)
    >>> 3
    """
def isPowerOfTwo(n: int) -> bool:
    """
    Given an integer n, return true if it is a power of two. Otherwise, return false.
    An integer n is a power of two, if there exists an integer x such that n == 2x.
    
    Example 1:
    
    >>> isPowerOfTwo(n = 1)
    >>> true
    Explanation: 20 = 1
    
    Example 2:
    
    >>> isPowerOfTwo(n = 16)
    >>> true
    Explanation: 24 = 16
    
    Example 3:
    
    >>> isPowerOfTwo(n = 3)
    >>> false
    """
def countDigitOne(n: int) -> int:
    """
    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
    
    Example 1:
    
    >>> countDigitOne(n = 13)
    >>> 6
    
    Example 2:
    
    >>> countDigitOne(n = 0)
    >>> 0
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isPalindrome(head: Optional[ListNode]) -> bool:
    """
    Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,2,1])
    >>> true
    
    Example 2:
    
    
    >>> __init__(head = [1,2])
    >>> false
    """
# class TreeNode:
#     def __init__(x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    """
    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
    According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
    
    Example 1:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1)
    >>> 3
    Explanation: The LCA of nodes 5 and 1 is 3.
    
    Example 2:
    
    
    >>> __init__(root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4)
    >>> 5
    Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
    
    Example 3:
    
    >>> __init__(root = [1,2], p = 1, q = 2)
    >>> 1
    """
def productExceptSelf(nums: List[int]) -> List[int]:
    """
    Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
    The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
    You must write an algorithm that runs in O(n) time and without using the division operation.
    
    Example 1:
    >>> productExceptSelf(nums = [1,2,3,4])
    >>> [24,12,8,6]
    Example 2:
    >>> productExceptSelf(nums = [-1,1,0,-3,3])
    >>> [0,0,9,0,0]
    """
def maxSlidingWindow(nums: List[int], k: int) -> List[int]:
    """
    You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
    Return the max sliding window.
    
    Example 1:
    
    >>> maxSlidingWindow(nums = [1,3,-1,-3,5,3,6,7], k = 3)
    >>> [3,3,5,5,6,7]
    Explanation:
    Window position                Max
    ---------------               -----
    [1  3  -1] -3  5  3  6  7       3
     1 [3  -1  -3] 5  3  6  7       3
     1  3 [-1  -3  5] 3  6  7       5
     1  3  -1 [-3  5  3] 6  7       5
     1  3  -1  -3 [5  3  6] 7       6
     1  3  -1  -3  5 [3  6  7]      7
    
    Example 2:
    
    >>> maxSlidingWindow(nums = [1], k = 1)
    >>> [1]
    """
def searchMatrix(matrix: List[List[int]], target: int) -> bool:
    """
    Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:
    
    Integers in each row are sorted in ascending from left to right.
    Integers in each column are sorted in ascending from top to bottom.
    
    
    Example 1:
    
    
    >>> searchMatrix(matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5)
    >>> true
    
    Example 2:
    
    
    >>> searchMatrix(matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20)
    >>> false
    """