ShwStone/humaneval-leetcode huggingface dataset:
def getLengthOfOptimalCompression(s: str, k: int) -> int:
    """
    Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".
    Notice that in this problem, we are not adding '1' after single characters.
    Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.
    Find the minimum length of the run-length encoded version of s after deleting at most k characters.
    
    Example 1:
    
    >>> getLengthOfOptimalCompression(s = "aaabcccd", k = 2)
    >>> 4
    Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
    Example 2:
    
    >>> getLengthOfOptimalCompression(s = "aabbaa", k = 2)
    >>> 2
    Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
    
    Example 3:
    
    >>> getLengthOfOptimalCompression(s = "aaaaaaaaaaa", k = 0)
    >>> 3
    Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
    """
def countGoodTriplets(arr: List[int], a: int, b: int, c: int) -> int:
    """
    Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.\r
    \r
    A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:\r
    \r
    \r
    	0 <= i < j < k < arr.length\r
    	|arr[i] - arr[j]| <= a\r
    	|arr[j] - arr[k]| <= b\r
    	|arr[i] - arr[k]| <= c\r
    \r
    \r
    Where |x| denotes the absolute value of x.\r
    \r
    Return the number of good triplets.\r
    \r
     \r
    Example 1:\r
    \r
    \r
    >>> countGoodTriplets(arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3\r)
    >>> 4\r
    Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].\r
    \r
    \r
    Example 2:\r
    \r
    \r
    >>> countGoodTriplets(arr = [1,1,2,2,3], a = 0, b = 0, c = 1\r)
    >>> 0\r
    Explanation: No triplet satisfies all conditions.\r
    \r
    \r
     \r
    """
def getWinner(arr: List[int], k: int) -> int:
    """
    Given an integer array arr of distinct integers and an integer k.
    A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds.
    Return the integer which will win the game.
    It is guaranteed that there will be a winner of the game.
    
    Example 1:
    
    >>> getWinner(arr = [2,1,3,5,4,6,7], k = 2)
    >>> 5
    Explanation: Let's see the rounds of the game:
    Round |       arr       | winner | win_count
      1   | [2,1,3,5,4,6,7] | 2      | 1
      2   | [2,3,5,4,6,7,1] | 3      | 1
      3   | [3,5,4,6,7,1,2] | 5      | 1
      4   | [5,4,6,7,1,2,3] | 5      | 2
    So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.
    
    Example 2:
    
    >>> getWinner(arr = [3,2,1], k = 10)
    >>> 3
    Explanation: 3 will win the first 10 rounds consecutively.
    """
def minSwaps(grid: List[List[int]]) -> int:
    """
    Given an n x n binary grid, in one step you can choose two adjacent rows of the grid and swap them.
    A grid is said to be valid if all the cells above the main diagonal are zeros.
    Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
    The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
    
    Example 1:
    
    
    >>> minSwaps(grid = [[0,0,1],[1,1,0],[1,0,0]])
    >>> 3
    
    Example 2:
    
    
    >>> minSwaps(grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]])
    >>> -1
    Explanation: All rows are similar, swaps have no effect on the grid.
    
    Example 3:
    
    
    >>> minSwaps(grid = [[1,0,0],[1,1,0],[1,1,1]])
    >>> 0
    """
def maxSum(nums1: List[int], nums2: List[int]) -> int:
    """
    You are given two sorted arrays of distinct integers nums1 and nums2.
    A valid path is defined as follows:
    
    Choose array nums1 or nums2 to traverse (from index-0).
    Traverse the current array from left to right.
    If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
    
    The score is defined as the sum of unique values in a valid path.
    Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> maxSum(nums1 = [2,4,5,8,10], nums2 = [4,6,8,9])
    >>> 30
    Explanation: Valid paths:
    [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10],  (starting from nums1)
    [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10]    (starting from nums2)
    The maximum is obtained with the path in green [2,4,6,8,10].
    
    Example 2:
    
    >>> maxSum(nums1 = [1,3,5,7,9], nums2 = [3,5,100])
    >>> 109
    Explanation: Maximum sum is obtained with the path [1,3,5,100].
    
    Example 3:
    
    >>> maxSum(nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10])
    >>> 40
    Explanation: There are no common elements between nums1 and nums2.
    Maximum sum is obtained with the path [6,7,8,9,10].
    """
def findKthPositive(arr: List[int], k: int) -> int:
    """
    Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.
    Return the kth positive integer that is missing from this array.
    
    Example 1:
    
    >>> findKthPositive(arr = [2,3,4,7,11], k = 5)
    >>> 9
    Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
    
    Example 2:
    
    >>> findKthPositive(arr = [1,2,3,4], k = 2)
    >>> 6
    Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
    """
def canConvertString(s: str, t: str, k: int) -> bool:
    """
    Given two strings s and t, your goal is to convert s into t in k moves or less.
    During the ith (1 <= i <= k) move you can:
    
    Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times.
    Do nothing.
    
    Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times.
    Remember that any index j can be picked at most once.
    Return true if it's possible to convert s into t in no more than k moves, otherwise return false.
    
    Example 1:
    
    >>> canConvertString(s = "input", t = "ouput", k = 9)
    >>> true
    Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.
    
    Example 2:
    
    >>> canConvertString(s = "abc", t = "bcd", k = 10)
    >>> false
    Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.
    
    Example 3:
    
    >>> canConvertString(s = "aab", t = "bbb", k = 27)
    >>> true
    Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.
    """
def minInsertions(s: str) -> int:
    """
    Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is balanced if:
    
    Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
    Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.
    
    In other words, we treat '(' as an opening parenthesis and '))' as a closing parenthesis.
    
    For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not balanced.
    
    You can insert the characters '(' and ')' at any position of the string to balance it if needed.
    Return the minimum number of insertions needed to make s balanced.
    
    Example 1:
    
    >>> minInsertions(s = "(()))")
    >>> 1
    Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to add one more ')' at the end of the string to be "(())))" which is balanced.
    
    Example 2:
    
    >>> minInsertions(s = "())")
    >>> 0
    Explanation: The string is already balanced.
    
    Example 3:
    
    >>> minInsertions(s = "))())(")
    >>> 3
    Explanation: Add '(' to match the first '))', Add '))' to match the last '('.
    """
def longestAwesome(s: str) -> int:
    """
    You are given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it a palindrome.
    Return the length of the maximum length awesome substring of s.
    
    Example 1:
    
    >>> longestAwesome(s = "3242415")
    >>> 5
    Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
    
    Example 2:
    
    >>> longestAwesome(s = "12345678")
    >>> 1
    
    Example 3:
    
    >>> longestAwesome(s = "213123")
    >>> 6
    Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
    """
def makeGood(s: str) -> str:
    """
    Given a string s of lower and upper case English letters.
    A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:
    
    0 <= i <= s.length - 2
    s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
    
    To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.
    Return the string after making it good. The answer is guaranteed to be unique under the given constraints.
    Notice that an empty string is also good.
    
    Example 1:
    
    >>> makeGood(s = "leEeetcode")
    >>> "leetcode"
    Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
    
    Example 2:
    
    >>> makeGood(s = "abBAcC")
    >>> ""
    Explanation: We have many possible scenarios, and all lead to the same answer. For example:
    "abBAcC" --> "aAcC" --> "cC" --> ""
    "abBAcC" --> "abBA" --> "aA" --> ""
    
    Example 3:
    
    >>> makeGood(s = "s")
    >>> "s"
    """
def findKthBit(n: int, k: int) -> str:
    """
    Given two positive integers n and k, the binary string Sn is formed as follows:
    
    S1 = "0"
    Si = Si - 1 + "1" + reverse(invert(Si - 1)) for i > 1
    
    Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
    For example, the first four strings in the above sequence are:
    
    S1 = "0"
    S2 = "011"
    S3 = "0111001"
    S4 = "011100110110001"
    
    Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
    
    Example 1:
    
    >>> findKthBit(n = 3, k = 1)
    >>> "0"
    Explanation: S3 is "0111001".
    The 1st bit is "0".
    
    Example 2:
    
    >>> findKthBit(n = 4, k = 11)
    >>> "1"
    Explanation: S4 is "011100110110001".
    The 11th bit is "1".
    """
def maxNonOverlapping(nums: List[int], target: int) -> int:
    """
    Given an array nums and an integer target, return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
    
    Example 1:
    
    >>> maxNonOverlapping(nums = [1,1,1,1,1], target = 2)
    >>> 2
    Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
    
    Example 2:
    
    >>> maxNonOverlapping(nums = [-1,3,5,1,4,2,-9], target = 6)
    >>> 2
    Explanation: There are 3 subarrays with sum equal to 6.
    ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
    """
def minCost(n: int, cuts: List[int]) -> int:
    """
    Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:
    
    Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.
    You should perform the cuts in order, you can change the order of the cuts as you wish.
    The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
    Return the minimum total cost of the cuts.
    
    Example 1:
    
    
    >>> minCost(n = 7, cuts = [1,3,4,5])
    >>> 16
    Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
    
    The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
    Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
    Example 2:
    
    >>> minCost(n = 9, cuts = [5,6,1,4,2])
    >>> 22
    Explanation: If you try the given cuts ordering the cost will be 25.
    There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
    """
def mostSimilar(n: int, roads: List[List[int]], names: List[str], targetPath: List[str]) -> List[int]:
    """
    We have n cities and m bi-directional roads where roads[i] = [ai, bi] connects city ai with city bi. Each city has a name consisting of exactly three upper-case English letters given in the string array names. Starting at any city x, you can reach any city y where y != x (i.e., the cities and the roads are forming an undirected connected graph).
    You will be given a string array targetPath. You should find a path in the graph of the same length and with the minimum edit distance to targetPath.
    You need to return the order of the nodes in the path with the minimum edit distance. The path should be of the same length of targetPath and should be valid (i.e., there should be a direct road between ans[i] and ans[i + 1]). If there are multiple answers return any one of them.
    The edit distance is defined as follows:
    
    
    Example 1:
    
    
    >>> mostSimilar(n = 5, roads = [[0,2],[0,3],[1,2],[1,3],[1,4],[2,4]], names = ["ATL","PEK","LAX","DXB","HND"], targetPath = ["ATL","DXB","HND","LAX"])
    >>> [0,2,4,2]
    Explanation: [0,2,4,2], [0,3,0,2] and [0,3,1,2] are accepted answers.
    [0,2,4,2] is equivalent to ["ATL","LAX","HND","LAX"] which has edit distance = 1 with targetPath.
    [0,3,0,2] is equivalent to ["ATL","DXB","ATL","LAX"] which has edit distance = 1 with targetPath.
    [0,3,1,2] is equivalent to ["ATL","DXB","PEK","LAX"] which has edit distance = 1 with targetPath.
    
    Example 2:
    
    
    >>> mostSimilar(n = 4, roads = [[1,0],[2,0],[3,0],[2,1],[3,1],[3,2]], names = ["ATL","PEK","LAX","DXB"], targetPath = ["ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX"])
    >>> [0,1,0,1,0,1,0,1]
    Explanation: Any path in this graph has edit distance = 8 with targetPath.
    
    Example 3:
    
    
    >>> mostSimilar(n = 6, roads = [[0,1],[1,2],[2,3],[3,4],[4,5]], names = ["ATL","PEK","LAX","ATL","DXB","HND"], targetPath = ["ATL","DXB","HND","DXB","ATL","LAX","PEK"])
    >>> [3,4,5,4,3,2,1]
    Explanation: [3,4,5,4,3,2,1] is the only path with edit distance = 0 with targetPath.
    It's equivalent to ["ATL","DXB","HND","DXB","ATL","LAX","PEK"]
    """
def threeConsecutiveOdds(arr: List[int]) -> bool:
    """
    Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
    
    Example 1:
    
    >>> threeConsecutiveOdds(arr = [2,6,4,1])
    >>> false
    Explanation: There are no three consecutive odds.
    
    Example 2:
    
    >>> threeConsecutiveOdds(arr = [1,2,34,3,4,5,7,23,12])
    >>> true
    Explanation: [5,7,23] are three consecutive odds.
    """
def minOperations(n: int) -> int:
    """
    You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e., 0 <= i < n).
    In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e., perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.
    Given an integer n, the length of the array, return the minimum number of operations needed to make all the elements of arr equal.
    
    Example 1:
    
    >>> minOperations(n = 3)
    >>> 2
    Explanation: arr = [1, 3, 5]
    First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
    In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
    
    Example 2:
    
    >>> minOperations(n = 6)
    >>> 9
    """
def maxDistance(position: List[int], m: int) -> int:
    """
    In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
    Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
    Given the integer array position and the integer m. Return the required force.
    
    Example 1:
    
    
    >>> maxDistance(position = [1,2,3,4,7], m = 3)
    >>> 3
    Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
    
    Example 2:
    
    >>> maxDistance(position = [5,4,3,2,1,1000000000], m = 2)
    >>> 999999999
    Explanation: We can use baskets 1 and 1000000000.
    """
def minDays(n: int) -> int:
    """
    There are n oranges in the kitchen and you decided to eat some of these oranges every day as follows:
    
    Eat one orange.
    If the number of remaining oranges n is divisible by 2 then you can eat n / 2 oranges.
    If the number of remaining oranges n is divisible by 3 then you can eat 2 * (n / 3) oranges.
    
    You can only choose one of the actions per day.
    Given the integer n, return the minimum number of days to eat n oranges.
    
    Example 1:
    
    >>> minDays(n = 10)
    >>> 4
    Explanation: You have 10 oranges.
    Day 1: Eat 1 orange,  10 - 1 = 9.
    Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
    Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
    Day 4: Eat the last orange  1 - 1  = 0.
    You need at least 4 days to eat the 10 oranges.
    
    Example 2:
    
    >>> minDays(n = 6)
    >>> 3
    Explanation: You have 6 oranges.
    Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
    Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
    Day 3: Eat the last orange  1 - 1  = 0.
    You need at least 3 days to eat the 6 oranges.
    """
def differByOne(dict: List[str]) -> bool:
    """
    Given a list of strings dict where all the strings are of the same length.
    Return true if there are 2 strings that only differ by 1 character in the same index, otherwise return false.
    
    Example 1:
    
    >>> differByOne(dict = ["abcd","acbd", "aacd"])
    >>> true
    Explanation: Strings "abcd" and "aacd" differ only by one character in the index 1.
    
    Example 2:
    
    >>> differByOne(dict = ["ab","cd","yz"])
    >>> false
    
    Example 3:
    
    >>> differByOne(dict = ["abcd","cccc","abyd","abab"])
    >>> true
    """
def thousandSeparator(n: int) -> str:
    """
    Given an integer n, add a dot (".") as the thousands separator and return it in string format.
    
    Example 1:
    
    >>> thousandSeparator(n = 987)
    >>> "987"
    
    Example 2:
    
    >>> thousandSeparator(n = 1234)
    >>> "1.234"
    """
def findSmallestSetOfVertices(n: int, edges: List[List[int]]) -> List[int]:
    """
    Given a directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.
    Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists.
    Notice that you can return the vertices in any order.
    
    Example 1:
    
    
    >>> findSmallestSetOfVertices(n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]])
    >>> [0,3]
    Explanation: It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].
    Example 2:
    
    
    >>> findSmallestSetOfVertices(n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]])
    >>> [0,2,3]
    Explanation: Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.
    """
def minOperations(nums: List[int]) -> int:
    """
    You are given an integer array nums. You have an integer array arr of the same length with all values set to 0 initially. You also have the following modify function:
    
    You want to use the modify function to convert arr to nums using the minimum number of calls.
    Return the minimum number of function calls to make nums from arr.
    The test cases are generated so that the answer fits in a 32-bit signed integer.
    
    Example 1:
    
    >>> minOperations(nums = [1,5])
    >>> 5
    Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
    Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
    Increment by 1 (both elements)  [0, 4] -> [1, 4] -> [1, 5] (2 operations).
    Total of operations: 1 + 2 + 2 = 5.
    
    Example 2:
    
    >>> minOperations(nums = [2,2])
    >>> 3
    Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
    Double all the elements: [1, 1] -> [2, 2] (1 operation).
    Total of operations: 2 + 1 = 3.
    
    Example 3:
    
    >>> minOperations(nums = [4,2,5])
    >>> 6
    Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
    """