ShwStone/humaneval-leetcode huggingface dataset:
def findRedundantConnection(edges: List[List[int]]) -> List[int]:
    """
    In this problem, a tree is an undirected graph that is connected and has no cycles.
    You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.
    Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.
    
    Example 1:
    
    
    >>> findRedundantConnection(edges = [[1,2],[1,3],[2,3]])
    >>> [2,3]
    
    Example 2:
    
    
    >>> findRedundantConnection(edges = [[1,2],[2,3],[3,4],[1,4],[1,5]])
    >>> [1,4]
    """
def findRedundantDirectedConnection(edges: List[List[int]]) -> List[int]:
    """
    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
    The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.
    The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi] that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.
    Return an edge that can be removed so that the resulting graph is a rooted tree of n nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
    
    Example 1:
    
    
    >>> findRedundantDirectedConnection(edges = [[1,2],[1,3],[2,3]])
    >>> [2,3]
    
    Example 2:
    
    
    >>> findRedundantDirectedConnection(edges = [[1,2],[2,3],[3,4],[4,1],[1,5]])
    >>> [4,1]
    """
def repeatedStringMatch(a: str, b: str) -> int:
    """
    Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1.
    Notice: string "abc" repeated 0 times is "", repeated 1 time is "abc" and repeated 2 times is "abcabc".
    
    Example 1:
    
    >>> repeatedStringMatch(a = "abcd", b = "cdabcdab")
    >>> 3
    Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.
    
    Example 2:
    
    >>> repeatedStringMatch(a = "a", b = "aa")
    >>> 2
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def longestUnivaluePath(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass through the root.
    The length of the path between two nodes is represented by the number of edges between them.
    
    Example 1:
    
    
    >>> __init__(root = [5,4,5,1,1,null,5])
    >>> 2
    Explanation: The shown image shows that the longest path of the same value (i.e. 5).
    
    Example 2:
    
    
    >>> __init__(root = [1,4,5,4,4,null,5])
    >>> 2
    Explanation: The shown image shows that the longest path of the same value (i.e. 4).
    """
def knightProbability(n: int, k: int, row: int, column: int) -> float:
    """
    On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).
    A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
    
    Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
    The knight continues moving until it has made exactly k moves or has moved off the chessboard.
    Return the probability that the knight remains on the board after it has stopped moving.
    
    Example 1:
    
    >>> knightProbability(n = 3, k = 2, row = 0, column = 0)
    >>> 0.06250
    Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
    From each of those positions, there are also two moves that will keep the knight on the board.
    The total probability the knight stays on the board is 0.0625.
    
    Example 2:
    
    >>> knightProbability(n = 1, k = 0, row = 0, column = 0)
    >>> 1.00000
    """
def maxSumOfThreeSubarrays(nums: List[int], k: int) -> List[int]:
    """
    Given an integer array nums and an integer k, find three non-overlapping subarrays of length k with maximum sum and return them.
    Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
    
    Example 1:
    
    >>> maxSumOfThreeSubarrays(nums = [1,2,1,2,6,7,5,1], k = 2)
    >>> [0,3,5]
    Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
    We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically smaller.
    
    Example 2:
    
    >>> maxSumOfThreeSubarrays(nums = [1,2,1,2,1,2,1,2,1], k = 2)
    >>> [0,2,4]
    """
def minStickers(stickers: List[str], target: str) -> int:
    """
    We are given n different types of stickers. Each sticker has a lowercase English word on it.
    You would like to spell out the given string target by cutting individual letters from your collection of stickers and rearranging them. You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
    Return the minimum number of stickers that you need to spell out target. If the task is impossible, return -1.
    Note: In all test cases, all words were chosen randomly from the 1000 most common US English words, and target was chosen as a concatenation of two random words.
    
    Example 1:
    
    >>> minStickers(stickers = ["with","example","science"], target = "thehat")
    >>> 3
    Explanation:
    We can use 2 "with" stickers, and 1 "example" sticker.
    After cutting and rearrange the letters of those stickers, we can form the target "thehat".
    Also, this is the minimum number of stickers necessary to form the target string.
    
    Example 2:
    
    >>> minStickers(stickers = ["notice","possible"], target = "basicbasic")
    >>> -1
    Explanation:
    We cannot form the target "basicbasic" from cutting letters from the given stickers.
    """
def topKFrequent(words: List[str], k: int) -> List[str]:
    """
    Given an array of strings words and an integer k, return the k most frequent strings.
    Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
    
    Example 1:
    
    >>> topKFrequent(words = ["i","love","leetcode","i","love","coding"], k = 2)
    >>> ["i","love"]
    Explanation: "i" and "love" are the two most frequent words.
    Note that "i" comes before "love" due to a lower alphabetical order.
    
    Example 2:
    
    >>> topKFrequent(words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4)
    >>> ["the","is","sunny","day"]
    Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
    """
def hasAlternatingBits(n: int) -> bool:
    """
    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
    
    Example 1:
    
    >>> hasAlternatingBits(n = 5)
    >>> true
    Explanation: The binary representation of 5 is: 101
    
    Example 2:
    
    >>> hasAlternatingBits(n = 7)
    >>> false
    Explanation: The binary representation of 7 is: 111.
    Example 3:
    
    >>> hasAlternatingBits(n = 11)
    >>> false
    Explanation: The binary representation of 11 is: 1011.
    """
def numDistinctIslands(grid: List[List[int]]) -> int:
    """
    You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
    An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.
    Return the number of distinct islands.
    
    Example 1:
    
    
    >>> numDistinctIslands(grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]])
    >>> 1
    
    Example 2:
    
    
    >>> numDistinctIslands(grid = [[1,1,0,1,1],[1,0,0,0,0],[0,0,0,0,1],[1,1,0,1,1]])
    >>> 3
    """
def maxAreaOfIsland(grid: List[List[int]]) -> int:
    """
    You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
    The area of an island is the number of cells with a value 1 in the island.
    Return the maximum area of an island in grid. If there is no island, return 0.
    
    Example 1:
    
    
    >>> maxAreaOfIsland(grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]])
    >>> 6
    Explanation: The answer is not 11, because the island must be connected 4-directionally.
    
    Example 2:
    
    >>> maxAreaOfIsland(grid = [[0,0,0,0,0,0,0,0]])
    >>> 0
    """
def countBinarySubstrings(s: str) -> int:
    """
    Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.
    Substrings that occur multiple times are counted the number of times they occur.
    
    Example 1:
    
    >>> countBinarySubstrings(s = "00110011")
    >>> 6
    Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
    Notice that some of these substrings repeat and are counted the number of times they occur.
    Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
    
    Example 2:
    
    >>> countBinarySubstrings(s = "10101")
    >>> 4
    Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
    """
def findShortestSubArray(nums: List[int]) -> int:
    """
    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
    Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
    
    Example 1:
    
    >>> findShortestSubArray(nums = [1,2,2,3,1])
    >>> 2
    Explanation:
    The input array has a degree of 2 because both elements 1 and 2 appear twice.
    Of the subarrays that have the same degree:
    [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
    The shortest length is 2. So return 2.
    
    Example 2:
    
    >>> findShortestSubArray(nums = [1,2,2,3,1,4,2])
    >>> 6
    Explanation:
    The degree is 3 because the element 2 is repeated 3 times.
    So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
    """
def canPartitionKSubsets(nums: List[int], k: int) -> bool:
    """
    Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
    
    Example 1:
    
    >>> canPartitionKSubsets(nums = [4,3,2,3,5,2,1], k = 4)
    >>> true
    Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
    
    Example 2:
    
    >>> canPartitionKSubsets(nums = [1,2,3,4], k = 3)
    >>> false
    """
def fallingSquares(positions: List[List[int]]) -> List[int]:
    """
    There are several squares being dropped onto the X-axis of a 2D plane.
    You are given a 2D integer array positions where positions[i] = [lefti, sideLengthi] represents the ith square with a side length of sideLengthi that is dropped with its left edge aligned with X-coordinate lefti.
    Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands on the top side of another square or on the X-axis. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.
    After each square is dropped, you must record the height of the current tallest stack of squares.
    Return an integer array ans where ans[i] represents the height described above after dropping the ith square.
    
    Example 1:
    
    
    >>> fallingSquares(positions = [[1,2],[2,3],[6,1]])
    >>> [2,5,5]
    Explanation:
    After the first drop, the tallest stack is square 1 with a height of 2.
    After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
    After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
    Thus, we return an answer of [2, 5, 5].
    
    Example 2:
    
    >>> fallingSquares(positions = [[100,100],[200,100]])
    >>> [100,100]
    Explanation:
    After the first drop, the tallest stack is square 1 with a height of 100.
    After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
    Thus, we return an answer of [100, 100].
    Note that square 2 only brushes the right side of square 1, which does not count as landing on it.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def searchBST(root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
    """
    You are given the root of a binary search tree (BST) and an integer val.
    Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
    
    Example 1:
    
    
    >>> __init__(root = [4,2,7,1,3], val = 2)
    >>> [2,1,3]
    
    Example 2:
    
    
    >>> __init__(root = [4,2,7,1,3], val = 5)
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoBST(root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
    """
    You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
    Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
    
    Example 1:
    
    
    >>> __init__(root = [4,2,7,1,3], val = 5)
    >>> [4,2,7,1,3,5]
    Explanation: Another accepted tree is:
    
    
    Example 2:
    
    >>> __init__(root = [40,20,60,10,30,50,70], val = 25)
    >>> [40,20,60,10,30,50,70,null,null,25]
    
    Example 3:
    
    >>> __init__(root = [4,2,7,1,3,null,null,null,null,null,null], val = 5)
    >>> [4,2,7,1,3,5]
    """
def search(nums: List[int], target: int) -> int:
    """
    Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
    You must write an algorithm with O(log n) runtime complexity.
    
    Example 1:
    
    >>> search(nums = [-1,0,3,5,9,12], target = 9)
    >>> 4
    Explanation: 9 exists in nums and its index is 4
    
    Example 2:
    
    >>> search(nums = [-1,0,3,5,9,12], target = 2)
    >>> -1
    Explanation: 2 does not exist in nums so return -1
    """
def toLowerCase(s: str) -> str:
    """
    Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
    
    Example 1:
    
    >>> toLowerCase(s = "Hello")
    >>> "hello"
    
    Example 2:
    
    >>> toLowerCase(s = "here")
    >>> "here"
    
    Example 3:
    
    >>> toLowerCase(s = "LOVELY")
    >>> "lovely"
    """
def numDistinctIslands2(grid: List[List[int]]) -> int:
    """
    You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
    An island is considered to be the same as another if they have the same shape, or have the same shape after rotation (90, 180, or 270 degrees only) or reflection (left/right direction or up/down direction).
    Return the number of distinct islands.
    
    Example 1:
    
    
    >>> numDistinctIslands2(grid = [[1,1,0,0,0],[1,0,0,0,0],[0,0,0,0,1],[0,0,0,1,1]])
    >>> 1
    Explanation: The two islands are considered the same because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.
    
    Example 2:
    
    
    >>> numDistinctIslands2(grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]])
    >>> 1
    """
def minimumDeleteSum(s1: str, s2: str) -> int:
    """
    Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.
    
    Example 1:
    
    >>> minimumDeleteSum(s1 = "sea", s2 = "eat")
    >>> 231
    Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
    Deleting "t" from "eat" adds 116 to the sum.
    At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
    
    Example 2:
    
    >>> minimumDeleteSum(s1 = "delete", s2 = "leet")
    >>> 403
    Explanation: Deleting "dee" from "delete" to turn the string into "let",
    adds 100[d] + 101[e] + 101[e] to the sum.
    Deleting "e" from "leet" adds 101[e] to the sum.
    At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
    If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
    """
def numSubarrayProductLessThanK(nums: List[int], k: int) -> int:
    """
    Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.
    
    Example 1:
    
    >>> numSubarrayProductLessThanK(nums = [10,5,2,6], k = 100)
    >>> 8
    Explanation: The 8 subarrays that have product less than 100 are:
    [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
    Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
    
    Example 2:
    
    >>> numSubarrayProductLessThanK(nums = [1,2,3], k = 0)
    >>> 0
    """
def maxProfit(prices: List[int], fee: int) -> int:
    """
    You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.
    Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
    Note:
    
    You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
    The transaction fee is only charged once for each stock purchase and sale.
    
    
    Example 1:
    
    >>> maxProfit(prices = [1,3,2,8,4,9], fee = 2)
    >>> 8
    Explanation: The maximum profit can be achieved by:
    - Buying at prices[0] = 1
    - Selling at prices[3] = 8
    - Buying at prices[4] = 4
    - Selling at prices[5] = 9
    The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
    
    Example 2:
    
    >>> maxProfit(prices = [1,3,7,5,10,3], fee = 3)
    >>> 6
    """
def isOneBitCharacter(bits: List[int]) -> bool:
    """
    We have two special characters:
    
    The first character can be represented by one bit 0.
    The second character can be represented by two bits (10 or 11).
    
    Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
    
    Example 1:
    
    >>> isOneBitCharacter(bits = [1,0,0])
    >>> true
    Explanation: The only way to decode it is two-bit character and one-bit character.
    So the last character is one-bit character.
    
    Example 2:
    
    >>> isOneBitCharacter(bits = [1,1,1,0])
    >>> false
    Explanation: The only way to decode it is two-bit character and two-bit character.
    So the last character is not one-bit character.
    """
def findLength(nums1: List[int], nums2: List[int]) -> int:
    """
    Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.
    
    Example 1:
    
    >>> findLength(nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7])
    >>> 3
    Explanation: The repeated subarray with maximum length is [3,2,1].
    
    Example 2:
    
    >>> findLength(nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0])
    >>> 5
    Explanation: The repeated subarray with maximum length is [0,0,0,0,0].
    """
def smallestDistancePair(nums: List[int], k: int) -> int:
    """
    The distance of a pair of integers a and b is defined as the absolute difference between a and b.
    Given an integer array nums and an integer k, return the kth smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.
    
    Example 1:
    
    >>> smallestDistancePair(nums = [1,3,1], k = 1)
    >>> 0
    Explanation: Here are all the pairs:
    (1,3) -> 2
    (1,1) -> 0
    (3,1) -> 2
    Then the 1st smallest distance pair is (1,1), and its distance is 0.
    
    Example 2:
    
    >>> smallestDistancePair(nums = [1,1,1], k = 2)
    >>> 0
    
    Example 3:
    
    >>> smallestDistancePair(nums = [1,6,1], k = 3)
    >>> 5
    """
def longestWord(words: List[str]) -> str:
    """
    Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words.
    If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.
    Note that the word should be built from left to right with each additional character being added to the end of a previous word.
    
    Example 1:
    
    >>> longestWord(words = ["w","wo","wor","worl","world"])
    >>> "world"
    Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
    
    Example 2:
    
    >>> longestWord(words = ["a","banana","app","appl","ap","apply","apple"])
    >>> "apple"
    Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
    """