ShwStone/humaneval-leetcode huggingface dataset:
def kthSmallestSubarraySum(nums: List[int], k: int) -> int:
    """
    Given an integer array nums of length n and an integer k, return the kth smallest subarray sum.
    A subarray is defined as a non-empty contiguous sequence of elements in an array. A subarray sum is the sum of all elements in the subarray.
    
    Example 1:
    
    >>> kthSmallestSubarraySum(nums = [2,1,3], k = 4)
    >>> 3
    Explanation: The subarrays of [2,1,3] are:
    - [2] with sum 2
    - [1] with sum 1
    - [3] with sum 3
    - [2,1] with sum 3
    - [1,3] with sum 4
    - [2,1,3] with sum 6
    Ordering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.
    
    Example 2:
    
    >>> kthSmallestSubarraySum(nums = [3,3,5,5], k = 7)
    >>> 10
    Explanation: The subarrays of [3,3,5,5] are:
    - [3] with sum 3
    - [3] with sum 3
    - [5] with sum 5
    - [5] with sum 5
    - [3,3] with sum 6
    - [3,5] with sum 8
    - [5,5] with sum 10
    - [3,3,5], with sum 11
    - [3,5,5] with sum 13
    - [3,3,5,5] with sum 16
    Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.
    """
def buildArray(nums: List[int]) -> List[int]:
    """
    Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
    A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
    
    Example 1:
    
    >>> buildArray(nums = [0,2,1,5,3,4])
    >>> [0,1,2,4,5,3]
    Explanation: The array ans is built as follows:
    ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
        = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
        = [0,1,2,4,5,3]
    Example 2:
    
    >>> buildArray(nums = [5,0,1,2,3,4])
    >>> [4,5,0,1,2,3]
    Explanation: The array ans is built as follows:
    ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
        = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
        = [4,5,0,1,2,3]
    """
def eliminateMaximum(dist: List[int], speed: List[int]) -> int:
    """
    You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.
    The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.
    You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.
    You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.
    Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.
    
    Example 1:
    
    >>> eliminateMaximum(dist = [1,3,4], speed = [1,1,1])
    >>> 3
    Explanation:
    In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
    After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
    After a minute, the distances of the monsters are [X,X,2]. You eliminate the third monster.
    All 3 monsters can be eliminated.
    Example 2:
    
    >>> eliminateMaximum(dist = [1,1,2,3], speed = [1,1,1,1])
    >>> 1
    Explanation:
    In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
    After a minute, the distances of the monsters are [X,0,1,2], so you lose.
    You can only eliminate 1 monster.
    
    Example 3:
    
    >>> eliminateMaximum(dist = [3,2,4], speed = [5,3,2])
    >>> 1
    Explanation:
    In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
    After a minute, the distances of the monsters are [X,0,2], so you lose.
    You can only eliminate 1 monster.
    """
def countGoodNumbers(n: int) -> int:
    """
    A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
    
    For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.
    
    Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
    A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
    
    Example 1:
    
    >>> countGoodNumbers(n = 1)
    >>> 5
    Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
    
    Example 2:
    
    >>> countGoodNumbers(n = 4)
    >>> 400
    
    Example 3:
    
    >>> countGoodNumbers(n = 50)
    >>> 564908303
    """
def longestCommonSubpath(n: int, paths: List[List[int]]) -> int:
    """
    There is a country of n cities numbered from 0 to n - 1. In this country, there is a road connecting every pair of cities.
    There are m friends numbered from 0 to m - 1 who are traveling through the country. Each one of them will take a path consisting of some cities. Each path is represented by an integer array that contains the visited cities in order. The path may contain a city more than once, but the same city will not be listed consecutively.
    Given an integer n and a 2D integer array paths where paths[i] is an integer array representing the path of the ith friend, return the length of the longest common subpath that is shared by every friend's path, or 0 if there is no common subpath at all.
    A subpath of a path is a contiguous sequence of cities within that path.
    
    Example 1:
    
                           [2,3,4],
                           [4,0,1,2,3]]
    >>> longestCommonSubpath(n = 5, paths = [[0,1,2,3,4],)
    >>> 2
    Explanation: The longest common subpath is [2,3].
    
    Example 2:
    
    >>> longestCommonSubpath(n = 3, paths = [[0],[1],[2]])
    >>> 0
    Explanation: There is no common subpath shared by the three paths.
    
    Example 3:
    
                           [4,3,2,1,0]]
    >>> longestCommonSubpath(n = 5, paths = [[0,1,2,3,4],)
    >>> 1
    Explanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.
    """
def outerTrees(trees: List[List[int]]) -> List[float]:
    """
    You are given a 2D integer array trees where trees[i] = [xi, yi] represents the location of the ith tree in the garden.
    You are asked to fence the entire garden using the minimum length of rope possible. The garden is well-fenced only if all the trees are enclosed and the rope used forms a perfect circle. A tree is considered enclosed if it is inside or on the border of the circle.
    More formally, you must form a circle using the rope with a center (x, y) and radius r where all trees lie inside or on the circle and r is minimum.
    Return the center and radius of the circle as a length 3 array [x, y, r]. Answers within 10-5 of the actual answer will be accepted.
    
    Example 1:
    
    
    >>> outerTrees(trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]])
    >>> [2.00000,2.00000,2.00000]
    Explanation: The fence will have center = (2, 2) and radius = 2
    
    Example 2:
    
    
    >>> outerTrees(trees = [[1,2],[2,2],[4,2]])
    >>> [2.50000,2.00000,1.50000]
    Explanation: The fence will have center = (2.5, 2) and radius = 1.5
    """
def countTriples(n: int) -> int:
    """
    A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2.
    Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.
    
    Example 1:
    
    >>> countTriples(n = 5)
    >>> 2
    Explanation: The square triples are (3,4,5) and (4,3,5).
    
    Example 2:
    
    >>> countTriples(n = 10)
    >>> 4
    Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
    """
def nearestExit(maze: List[List[str]], entrance: List[int]) -> int:
    """
    You are given an m x n matrix maze (0-indexed) with empty cells (represented as '.') and walls (represented as '+'). You are also given the entrance of the maze, where entrance = [entrancerow, entrancecol] denotes the row and column of the cell you are initially standing at.
    In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance. An exit is defined as an empty cell that is at the border of the maze. The entrance does not count as an exit.
    Return the number of steps in the shortest path from the entrance to the nearest exit, or -1 if no such path exists.
    
    Example 1:
    
    
    >>> nearestExit(maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2])
    >>> 1
    Explanation: There are 3 exits in this maze at [1,0], [0,2], and [2,3].
    Initially, you are at the entrance cell [1,2].
    - You can reach [1,0] by moving 2 steps left.
    - You can reach [0,2] by moving 1 step up.
    It is impossible to reach [2,3] from the entrance.
    Thus, the nearest exit is [0,2], which is 1 step away.
    
    Example 2:
    
    
    >>> nearestExit(maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0])
    >>> 2
    Explanation: There is 1 exit in this maze at [1,2].
    [1,0] does not count as an exit since it is the entrance cell.
    Initially, you are at the entrance cell [1,0].
    - You can reach [1,2] by moving 2 steps right.
    Thus, the nearest exit is [1,2], which is 2 steps away.
    
    Example 3:
    
    
    >>> nearestExit(maze = [[".","+"]], entrance = [0,0])
    >>> -1
    Explanation: There are no exits in this maze.
    """
def sumGame(num: str) -> bool:
    """
    Alice and Bob take turns playing a game, with Alice starting first.
    You are given a string num of even length consisting of digits and '?' characters. On each turn, a player will do the following if there is still at least one '?' in num:
    
    Choose an index i where num[i] == '?'.
    Replace num[i] with any digit between '0' and '9'.
    
    The game ends when there are no more '?' characters in num.
    For Bob to win, the sum of the digits in the first half of num must be equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal.
    
    For example, if the game ended with num = "243801", then Bob wins because 2+4+3 = 8+0+1. If the game ended with num = "243803", then Alice wins because 2+4+3 != 8+0+3.
    
    Assuming Alice and Bob play optimally, return true if Alice will win and false if Bob will win.
    
    Example 1:
    
    >>> sumGame(num = "5023")
    >>> false
    Explanation: There are no moves to be made.
    The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 3.
    
    Example 2:
    
    >>> sumGame(num = "25??")
    >>> true
    Explanation: Alice can replace one of the '?'s with '9' and it will be impossible for Bob to make the sums equal.
    
    Example 3:
    
    >>> sumGame(num = "?3295???")
    >>> false
    Explanation: It can be proven that Bob will always win. One possible outcome is:
    - Alice replaces the first '?' with '9'. num = "93295???".
    - Bob replaces one of the '?' in the right half with '9'. num = "932959??".
    - Alice replaces one of the '?' in the right half with '2'. num = "9329592?".
    - Bob replaces the last '?' in the right half with '7'. num = "93295927".
    Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.
    """
def minCost(maxTime: int, edges: List[List[int]], passingFees: List[int]) -> int:
    """
    There is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges where edges[i] = [xi, yi, timei] denotes a road between cities xi and yi that takes timei minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.
    Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees of length n where passingFees[j] is the amount of dollars you must pay when you pass through city j.
    In the beginning, you are at city 0 and want to reach city n - 1 in maxTime minutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
    Given maxTime, edges, and passingFees, return the minimum cost to complete your journey, or -1 if you cannot complete it within maxTime minutes.
    
    Example 1:
    
    
    >>> minCost(maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3])
    >>> 11
    Explanation: The path to take is 0 -> 1 -> 2 -> 5, which takes 30 minutes and has $11 worth of passing fees.
    
    Example 2:
    
    
    >>> minCost(maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3])
    >>> 48
    Explanation: The path to take is 0 -> 3 -> 4 -> 5, which takes 26 minutes and has $48 worth of passing fees.
    You cannot take path 0 -> 1 -> 2 -> 5 since it would take too long.
    
    Example 3:
    
    >>> minCost(maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3])
    >>> -1
    Explanation: There is no way to reach city 5 from city 0 within 25 minutes.
    """
def getConcatenation(nums: List[int]) -> List[int]:
    """
    Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
    Specifically, ans is the concatenation of two nums arrays.
    Return the array ans.
    
    Example 1:
    
    >>> getConcatenation(nums = [1,2,1])
    >>> [1,2,1,1,2,1]
    Explanation: The array ans is formed as follows:
    - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
    - ans = [1,2,1,1,2,1]
    Example 2:
    
    >>> getConcatenation(nums = [1,3,2,1])
    >>> [1,3,2,1,1,3,2,1]
    Explanation: The array ans is formed as follows:
    - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
    - ans = [1,3,2,1,1,3,2,1]
    """
def countPalindromicSubsequence(s: str) -> int:
    """
    Given a string s, return the number of unique palindromes of length three that are a subsequence of s.
    Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
    A palindrome is a string that reads the same forwards and backwards.
    A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
    
    For example, "ace" is a subsequence of "abcde".
    
    
    Example 1:
    
    >>> countPalindromicSubsequence(s = "aabca")
    >>> 3
    Explanation: The 3 palindromic subsequences of length 3 are:
    - "aba" (subsequence of "aabca")
    - "aaa" (subsequence of "aabca")
    - "aca" (subsequence of "aabca")
    
    Example 2:
    
    >>> countPalindromicSubsequence(s = "adc")
    >>> 0
    Explanation: There are no palindromic subsequences of length 3 in "adc".
    
    Example 3:
    
    >>> countPalindromicSubsequence(s = "bbcbaba")
    >>> 4
    Explanation: The 4 palindromic subsequences of length 3 are:
    - "bbb" (subsequence of "bbcbaba")
    - "bcb" (subsequence of "bbcbaba")
    - "bab" (subsequence of "bbcbaba")
    - "aba" (subsequence of "bbcbaba")
    """
def colorTheGrid(m: int, n: int) -> int:
    """
    You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted.
    Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> colorTheGrid(m = 1, n = 1)
    >>> 3
    Explanation: The three possible colorings are shown in the image above.
    
    Example 2:
    
    
    >>> colorTheGrid(m = 1, n = 2)
    >>> 6
    Explanation: The six possible colorings are shown in the image above.
    
    Example 3:
    
    >>> colorTheGrid(m = 5, n = 5)
    >>> 580986
    """
def isDecomposable(s: str) -> bool:
    """
    A value-equal string is a string where all characters are the same.
    
    For example, "1111" and "33" are value-equal strings.
    In contrast, "123" is not a value-equal string.
    
    Given a digit string s, decompose the string into some number of consecutive value-equal substrings where exactly one substring has a length of 2 and the remaining substrings have a length of 3.
    Return true if you can decompose s according to the above rules. Otherwise, return false.
    A substring is a contiguous sequence of characters in a string.
    
    Example 1:
    
    >>> isDecomposable(s = "000111000")
    >>> false
    Explanation: s cannot be decomposed according to the rules because ["000", "111", "000"] does not have a substring of length 2.
    
    Example 2:
    
    >>> isDecomposable(s = "00011111222")
    >>> true
    Explanation: s can be decomposed into ["000", "111", "11", "222"].
    
    Example 3:
    
    >>> isDecomposable(s = "011100022233")
    >>> false
    Explanation: s cannot be decomposed according to the rules because of the first '0'.
    """
def canBeTypedWords(text: str, brokenLetters: str) -> int:
    """
    There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
    Given a string text of words separated by a single space (no leading or trailing spaces) and a string brokenLetters of all distinct letter keys that are broken, return the number of words in text you can fully type using this keyboard.
    
    Example 1:
    
    >>> canBeTypedWords(text = "hello world", brokenLetters = "ad")
    >>> 1
    Explanation: We cannot type "world" because the 'd' key is broken.
    
    Example 2:
    
    >>> canBeTypedWords(text = "leet code", brokenLetters = "lt")
    >>> 1
    Explanation: We cannot type "leet" because the 'l' and 't' keys are broken.
    
    Example 3:
    
    >>> canBeTypedWords(text = "leet code", brokenLetters = "e")
    >>> 0
    Explanation: We cannot type either word because the 'e' key is broken.
    """
def addRungs(rungs: List[int], dist: int) -> int:
    """
    You are given a strictly increasing integer array rungs that represents the height of rungs on a ladder. You are currently on the floor at height 0, and you want to reach the last rung.
    You are also given an integer dist. You can only climb to the next highest rung if the distance between where you are currently at (the floor or on a rung) and the next rung is at most dist. You are able to insert rungs at any positive integer height if a rung is not already there.
    Return the minimum number of rungs that must be added to the ladder in order for you to climb to the last rung.
    
    Example 1:
    
    >>> addRungs(rungs = [1,3,5,10], dist = 2)
    >>> 2
    Explanation:
    You currently cannot reach the last rung.
    Add rungs at heights 7 and 8 to climb this ladder.
    The ladder will now have rungs at [1,3,5,7,8,10].
    
    Example 2:
    
    >>> addRungs(rungs = [3,6,8,10], dist = 3)
    >>> 0
    Explanation:
    This ladder can be climbed without adding additional rungs.
    
    Example 3:
    
    >>> addRungs(rungs = [3,4,6,7], dist = 2)
    >>> 1
    Explanation:
    You currently cannot reach the first rung from the ground.
    Add a rung at height 1 to climb this ladder.
    The ladder will now have rungs at [1,3,4,6,7].
    """
def maxPoints(points: List[List[int]]) -> int:
    """
    You are given an m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.
    To gain points, you must pick one cell in each row. Picking the cell at coordinates (r, c) will add points[r][c] to your score.
    However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c1) and (r + 1, c2) will subtract abs(c1 - c2) from your score.
    Return the maximum number of points you can achieve.
    abs(x) is defined as:
    
    x for x >= 0.
    -x for x < 0.
    
    
    Example 1:
    
    
    >>> maxPoints(points = [[1,2,3],[1,5,1],[3,1,1]])
    >>> 9
    Explanation:
    The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
    You add 3 + 5 + 3 = 11 to your score.
    However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
    Your final score is 11 - 2 = 9.
    
    Example 2:
    
    
    >>> maxPoints(points = [[1,5],[2,3],[4,2]])
    >>> 11
    Explanation:
    The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
    You add 5 + 3 + 4 = 12 to your score.
    However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
    Your final score is 12 - 1 = 11.
    """
def maxGeneticDifference(parents: List[int], queries: List[List[int]]) -> List[int]:
    """
    There is a rooted tree consisting of n nodes numbered 0 to n - 1. Each node's number denotes its unique genetic value (i.e. the genetic value of node x is x). The genetic difference between two genetic values is defined as the bitwise-XOR of their values. You are given the integer array parents, where parents[i] is the parent for node i. If node x is the root of the tree, then parents[x] == -1.
    You are also given the array queries where queries[i] = [nodei, vali]. For each query i, find the maximum genetic difference between vali and pi, where pi is the genetic value of any node that is on the path between nodei and the root (including nodei and the root). More formally, you want to maximize vali XOR pi.
    Return an array ans where ans[i] is the answer to the ith query.
    
    Example 1:
    
    
    >>> maxGeneticDifference(parents = [-1,0,1,1], queries = [[0,2],[3,2],[2,5]])
    >>> [2,3,7]
    Explanation: The queries are processed as follows:
    - [0,2]: The node with the maximum genetic difference is 0, with a difference of 2 XOR 0 = 2.
    - [3,2]: The node with the maximum genetic difference is 1, with a difference of 2 XOR 1 = 3.
    - [2,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
    
    Example 2:
    
    
    >>> maxGeneticDifference(parents = [3,7,-1,2,0,7,0,2], queries = [[4,6],[1,15],[0,5]])
    >>> [6,14,7]
    Explanation: The queries are processed as follows:
    - [4,6]: The node with the maximum genetic difference is 0, with a difference of 6 XOR 0 = 6.
    - [1,15]: The node with the maximum genetic difference is 1, with a difference of 15 XOR 1 = 14.
    - [0,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
    """