ShwStone/humaneval-leetcode huggingface dataset:
def minimumTotal(triangle: List[List[int]]) -> int:
    """
    Given a triangle array, return the minimum path sum from top to bottom.
    For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
    
    Example 1:
    
    >>> minimumTotal(triangle = [[2],[3,4],[6,5,7],[4,1,8,3]])
    >>> 11
    Explanation: The triangle looks like:
       2
      3 4
     6 5 7
    4 1 8 3
    The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
    
    Example 2:
    
    >>> minimumTotal(triangle = [[-10]])
    >>> -10
    """
def maxProfit(prices: List[int]) -> int:
    """
    You are given an array prices where prices[i] is the price of a given stock on the ith day.
    You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
    Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
    
    Example 1:
    
    >>> maxProfit(prices = [7,1,5,3,6,4])
    >>> 5
    Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
    Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
    
    Example 2:
    
    >>> maxProfit(prices = [7,6,4,3,1])
    >>> 0
    Explanation: In this case, no transactions are done and the max profit = 0.
    """
def maxProfit(prices: List[int]) -> int:
    """
    You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
    On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
    Find and return the maximum profit you can achieve.
    
    Example 1:
    
    >>> maxProfit(prices = [7,1,5,3,6,4])
    >>> 7
    Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
    Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
    Total profit is 4 + 3 = 7.
    
    Example 2:
    
    >>> maxProfit(prices = [1,2,3,4,5])
    >>> 4
    Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
    Total profit is 4.
    
    Example 3:
    
    >>> maxProfit(prices = [7,6,4,3,1])
    >>> 0
    Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
    """
def maxProfit(prices: List[int]) -> int:
    """
    You are given an array prices where prices[i] is the price of a given stock on the ith day.
    Find the maximum profit you can achieve. You may complete at most two transactions.
    Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
    
    Example 1:
    
    >>> maxProfit(prices = [3,3,5,0,0,3,1,4])
    >>> 6
    Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
    Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
    Example 2:
    
    >>> maxProfit(prices = [1,2,3,4,5])
    >>> 4
    Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
    Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
    
    Example 3:
    
    >>> maxProfit(prices = [7,6,4,3,1])
    >>> 0
    Explanation: In this case, no transaction is done, i.e. max profit = 0.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxPathSum(root: Optional[TreeNode]) -> int:
    """
    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
    The path sum of a path is the sum of the node's values in the path.
    Given the root of a binary tree, return the maximum path sum of any non-empty path.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3])
    >>> 6
    Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
    
    Example 2:
    
    
    >>> __init__(root = [-10,9,20,null,null,15,7])
    >>> 42
    Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
    """
def isPalindrome(s: str) -> bool:
    """
    A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
    Given a string s, return true if it is a palindrome, or false otherwise.
    
    Example 1:
    
    >>> isPalindrome(s = "A man, a plan, a canal: Panama")
    >>> true
    Explanation: "amanaplanacanalpanama" is a palindrome.
    
    Example 2:
    
    >>> isPalindrome(s = "race a car")
    >>> false
    Explanation: "raceacar" is not a palindrome.
    
    Example 3:
    
    >>> isPalindrome(s = " ")
    >>> true
    Explanation: s is an empty string "" after removing non-alphanumeric characters.
    Since an empty string reads the same forward and backward, it is a palindrome.
    """
def ladderLength(beginWord: str, endWord: str, wordList: List[str]) -> int:
    """
    A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
    
    Every adjacent pair of words differs by a single letter.
    Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
    sk == endWord
    
    Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
    
    Example 1:
    
    >>> ladderLength(beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"])
    >>> 5
    Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
    
    Example 2:
    
    >>> ladderLength(beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"])
    >>> 0
    Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
    """
def longestConsecutive(nums: List[int]) -> int:
    """
    Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
    You must write an algorithm that runs in O(n) time.
    
    Example 1:
    
    >>> longestConsecutive(nums = [100,4,200,1,3,2])
    >>> 4
    Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
    
    Example 2:
    
    >>> longestConsecutive(nums = [0,3,7,2,5,8,4,6,0,1])
    >>> 9
    
    Example 3:
    
    >>> longestConsecutive(nums = [1,0,1,2])
    >>> 3
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumNumbers(root: Optional[TreeNode]) -> int:
    """
    You are given the root of a binary tree containing digits from 0 to 9 only.
    Each root-to-leaf path in the tree represents a number.
    
    For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
    
    Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
    A leaf node is a node with no children.
    
    Example 1:
    
    
    >>> __init__(root = [1,2,3])
    >>> 25
    Explanation:
    The root-to-leaf path 1->2 represents the number 12.
    The root-to-leaf path 1->3 represents the number 13.
    Therefore, sum = 12 + 13 = 25.
    
    Example 2:
    
    
    >>> __init__(root = [4,9,0,5,1])
    >>> 1026
    Explanation:
    The root-to-leaf path 4->9->5 represents the number 495.
    The root-to-leaf path 4->9->1 represents the number 491.
    The root-to-leaf path 4->0 represents the number 40.
    Therefore, sum = 495 + 491 + 40 = 1026.
    """
def solve(board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
    """
    You are given an m x n matrix board containing letters 'X' and 'O', capture regions that are surrounded:
    
    Connect: A cell is connected to adjacent cells horizontally or vertically.
    Region: To form a region connect every 'O' cell.
    Surround: The region is surrounded with 'X' cells if you can connect the region with 'X' cells and none of the region cells are on the edge of the board.
    
    To capture a surrounded region, replace all 'O's with 'X's in-place within the original board. You do not need to return anything.
    
    Example 1:
    
    >>> solve(board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]])
    >>> [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
    Explanation:
    
    In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.
    
    Example 2:
    
    >>> solve(board = [["X"]])
    >>> [["X"]]
    """
def partition(s: str) -> List[List[str]]:
    """
    Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
    
    Example 1:
    >>> partition(s = "aab")
    >>> [["a","a","b"],["aa","b"]]
    Example 2:
    >>> partition(s = "a")
    >>> [["a"]]
    """
def minCut(s: str) -> int:
    """
    Given a string s, partition s such that every substring of the partition is a palindrome.
    Return the minimum cuts needed for a palindrome partitioning of s.
    
    Example 1:
    
    >>> minCut(s = "aab")
    >>> 1
    Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
    
    Example 2:
    
    >>> minCut(s = "a")
    >>> 0
    
    Example 3:
    
    >>> minCut(s = "ab")
    >>> 1
    """
def canCompleteCircuit(gas: List[int], cost: List[int]) -> int:
    """
    There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
    You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
    Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique.
    
    Example 1:
    
    >>> canCompleteCircuit(gas = [1,2,3,4,5], cost = [3,4,5,1,2])
    >>> 3
    Explanation:
    Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
    Travel to station 4. Your tank = 4 - 1 + 5 = 8
    Travel to station 0. Your tank = 8 - 2 + 1 = 7
    Travel to station 1. Your tank = 7 - 3 + 2 = 6
    Travel to station 2. Your tank = 6 - 4 + 3 = 5
    Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
    Therefore, return 3 as the starting index.
    
    Example 2:
    
    >>> canCompleteCircuit(gas = [2,3,4], cost = [3,4,3])
    >>> -1
    Explanation:
    You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
    Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
    Travel to station 0. Your tank = 4 - 3 + 2 = 3
    Travel to station 1. Your tank = 3 - 3 + 3 = 3
    You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
    Therefore, you can't travel around the circuit once no matter where you start.
    """
def candy(ratings: List[int]) -> int:
    """
    There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
    You are giving candies to these children subjected to the following requirements:
    
    Each child must have at least one candy.
    Children with a higher rating get more candies than their neighbors.
    
    Return the minimum number of candies you need to have to distribute the candies to the children.
    
    Example 1:
    
    >>> candy(ratings = [1,0,2])
    >>> 5
    Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
    
    Example 2:
    
    >>> candy(ratings = [1,2,2])
    >>> 4
    Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
    The third child gets 1 candy because it satisfies the above two conditions.
    """
def singleNumber(nums: List[int]) -> int:
    """
    Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
    You must implement a solution with a linear runtime complexity and use only constant extra space.
    
    Example 1:
    
    >>> singleNumber(nums = [2,2,1])
    >>> 1
    
    Example 2:
    
    >>> singleNumber(nums = [4,1,2,1,2])
    >>> 4
    
    Example 3:
    
    >>> singleNumber(nums = [1])
    >>> 1
    """
def singleNumber(nums: List[int]) -> int:
    """
    Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it.
    You must implement a solution with a linear runtime complexity and use only constant extra space.
    
    Example 1:
    >>> singleNumber(nums = [2,2,3,2])
    >>> 3
    Example 2:
    >>> singleNumber(nums = [0,1,0,1,0,1,99])
    >>> 99
    """
def wordBreak(s: str, wordDict: List[str]) -> bool:
    """
    Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
    Note that the same word in the dictionary may be reused multiple times in the segmentation.
    
    Example 1:
    
    >>> wordBreak(s = "leetcode", wordDict = ["leet","code"])
    >>> true
    Explanation: Return true because "leetcode" can be segmented as "leet code".
    
    Example 2:
    
    >>> wordBreak(s = "applepenapple", wordDict = ["apple","pen"])
    >>> true
    Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
    Note that you are allowed to reuse a dictionary word.
    
    Example 3:
    
    >>> wordBreak(s = "catsandog", wordDict = ["cats","dog","sand","and","cat"])
    >>> false
    """
def wordBreak(s: str, wordDict: List[str]) -> List[str]:
    """
    Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.
    Note that the same word in the dictionary may be reused multiple times in the segmentation.
    
    Example 1:
    
    >>> wordBreak(s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"])
    >>> ["cats and dog","cat sand dog"]
    
    Example 2:
    
    >>> wordBreak(s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"])
    >>> ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
    Explanation: Note that you are allowed to reuse a dictionary word.
    
    Example 3:
    
    >>> wordBreak(s = "catsandog", wordDict = ["cats","dog","sand","and","cat"])
    >>> []
    """
# class ListNode:
#     def __init__(x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(head: Optional[ListNode]) -> bool:
    """
    Given head, the head of a linked list, determine if the linked list has a cycle in it.
    There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
    Return true if there is a cycle in the linked list. Otherwise, return false.
    
    Example 1:
    
    
    >>> __init__(head = [3,2,0,-4], pos = 1)
    >>> true
    Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
    
    Example 2:
    
    
    >>> __init__(head = [1,2], pos = 0)
    >>> true
    Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
    
    Example 3:
    
    
    >>> __init__(head = [1], pos = -1)
    >>> false
    Explanation: There is no cycle in the linked list.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reorderList(head: Optional[ListNode]) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
    """
    You are given the head of a singly linked-list. The list can be represented as:
    
    L0 → L1 → … → Ln - 1 → Ln
    
    Reorder the list to be on the following form:
    
    L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
    
    You may not modify the values in the list's nodes. Only nodes themselves may be changed.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4])
    >>> [1,4,2,3]
    
    Example 2:
    
    
    >>> __init__(head = [1,2,3,4,5])
    >>> [1,5,2,4,3]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def preorderTraversal(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary tree, return the preorder traversal of its nodes' values.
    
    Example 1:
    
    >>> __init__(root = [1,null,2,3])
    >>> [1,2,3]
    Explanation:
    
    
    Example 2:
    
    >>> __init__(root = [1,2,3,4,5,null,8,null,null,6,7,9])
    >>> [1,2,4,5,6,7,3,8,9]
    Explanation:
    
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    
    Example 4:
    
    >>> __init__(root = [1])
    >>> [1]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary tree, return the postorder traversal of its nodes' values.
    
    Example 1:
    
    >>> __init__(root = [1,null,2,3])
    >>> [3,2,1]
    Explanation:
    
    
    Example 2:
    
    >>> __init__(root = [1,2,3,4,5,null,8,null,null,6,7,9])
    >>> [4,6,7,5,2,9,8,3,1]
    Explanation:
    
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    
    Example 4:
    
    >>> __init__(root = [1])
    >>> [1]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def insertionSortList(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head.
    The steps of the insertion sort algorithm:
    
    Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
    At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
    It repeats until no input elements remain.
    
    The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.
    
    
    Example 1:
    
    
    >>> __init__(head = [4,2,1,3])
    >>> [1,2,3,4]
    
    Example 2:
    
    
    >>> __init__(head = [-1,5,3,4,0])
    >>> [-1,0,3,4,5]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given the head of a linked list, return the list after sorting it in ascending order.
    
    Example 1:
    
    
    >>> __init__(head = [4,2,1,3])
    >>> [1,2,3,4]
    
    Example 2:
    
    
    >>> __init__(head = [-1,5,3,4,0])
    >>> [-1,0,3,4,5]
    
    Example 3:
    
    >>> __init__(head = [])
    >>> []
    """
def maxPoints(points: List[List[int]]) -> int:
    """
    Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
    
    Example 1:
    
    
    >>> maxPoints(points = [[1,1],[2,2],[3,3]])
    >>> 3
    
    Example 2:
    
    
    >>> maxPoints(points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]])
    >>> 4
    """
def evalRPN(tokens: List[str]) -> int:
    """
    You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
    Evaluate the expression. Return an integer that represents the value of the expression.
    Note that:
    
    The valid operators are '+', '-', '*', and '/'.
    Each operand may be an integer or another expression.
    The division between two integers always truncates toward zero.
    There will not be any division by zero.
    The input represents a valid arithmetic expression in a reverse polish notation.
    The answer and all the intermediate calculations can be represented in a 32-bit integer.
    
    
    Example 1:
    
    >>> evalRPN(tokens = ["2","1","+","3","*"])
    >>> 9
    Explanation: ((2 + 1) * 3) = 9
    
    Example 2:
    
    >>> evalRPN(tokens = ["4","13","5","/","+"])
    >>> 6
    Explanation: (4 + (13 / 5)) = 6
    
    Example 3:
    
    >>> evalRPN(tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"])
    >>> 22
    Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
    = ((10 * (6 / (12 * -11))) + 17) + 5
    = ((10 * (6 / -132)) + 17) + 5
    = ((10 * 0) + 17) + 5
    = (0 + 17) + 5
    = 17 + 5
    = 22
    """
def reverseWords(s: str) -> str:
    """
    Given an input string s, reverse the order of the words.
    A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
    Return a string of the words in reverse order concatenated by a single space.
    Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
    
    Example 1:
    
    >>> reverseWords(s = "the sky is blue")
    >>> "blue is sky the"
    
    Example 2:
    
    >>> reverseWords(s = "  hello world  ")
    >>> "world hello"
    Explanation: Your reversed string should not contain leading or trailing spaces.
    
    Example 3:
    
    >>> reverseWords(s = "a good   example")
    >>> "example good a"
    Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
    """
def maxProduct(nums: List[int]) -> int:
    """
    Given an integer array nums, find a subarray that has the largest product, and return the product.
    The test cases are generated so that the answer will fit in a 32-bit integer.
    
    Example 1:
    
    >>> maxProduct(nums = [2,3,-2,4])
    >>> 6
    Explanation: [2,3] has the largest product 6.
    
    Example 2:
    
    >>> maxProduct(nums = [-2,0,-1])
    >>> 0
    Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
    """