ShwStone/humaneval-leetcode huggingface dataset:
def maxPoints(grid: List[List[int]], queries: List[int]) -> List[int]:
    """
    You are given an m x n integer matrix grid and an array queries of size k.
    Find an array answer of size k such that for each integer queries[i] you start in the top left cell of the matrix and repeat the following process:
    
    If queries[i] is strictly greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any adjacent cell in all 4 directions: up, down, left, and right.
    Otherwise, you do not get any points, and you end this process.
    
    After the process, answer[i] is the maximum number of points you can get. Note that for each query you are allowed to visit the same cell multiple times.
    Return the resulting array answer.
    
    Example 1:
    
    
    >>> maxPoints(grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2])
    >>> [5,8,1]
    Explanation: The diagrams above show which cells we visit to get points for each query.
    Example 2:
    
    
    >>> maxPoints(grid = [[5,2,1],[1,1,2]], queries = [3])
    >>> [0]
    Explanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.
    """
def subsequenceSumOr(nums: List[int]) -> int:
    """
    Given an integer array nums, return the value of the bitwise OR of the sum of all possible subsequences in the array.
    A subsequence is a sequence that can be derived from another sequence by removing zero or more elements without changing the order of the remaining elements.
    
    Example 1:
    
    >>> subsequenceSumOr(nums = [2,1,0,3])
    >>> 7
    Explanation: All possible subsequence sums that we can have are: 0, 1, 2, 3, 4, 5, 6.
    And we have 0 OR 1 OR 2 OR 3 OR 4 OR 5 OR 6 = 7, so we return 7.
    
    Example 2:
    
    >>> subsequenceSumOr(nums = [0,0,0])
    >>> 0
    Explanation: 0 is the only possible subsequence sum we can have, so we return 0.
    """
def similarPairs(words: List[str]) -> int:
    """
    You are given a 0-indexed string array words.
    Two strings are similar if they consist of the same characters.
    
    For example, "abca" and "cba" are similar since both consist of characters 'a', 'b', and 'c'.
    However, "abacba" and "bcfd" are not similar since they do not consist of the same characters.
    
    Return the number of pairs (i, j) such that 0 <= i < j <= word.length - 1 and the two strings words[i] and words[j] are similar.
    
    Example 1:
    
    >>> similarPairs(words = ["aba","aabb","abcd","bac","aabc"])
    >>> 2
    Explanation: There are 2 pairs that satisfy the conditions:
    - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'.
    - i = 3 and j = 4 : both words[3] and words[4] only consist of characters 'a', 'b', and 'c'.
    
    Example 2:
    
    >>> similarPairs(words = ["aabb","ab","ba"])
    >>> 3
    Explanation: There are 3 pairs that satisfy the conditions:
    - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'.
    - i = 0 and j = 2 : both words[0] and words[2] only consist of characters 'a' and 'b'.
    - i = 1 and j = 2 : both words[1] and words[2] only consist of characters 'a' and 'b'.
    
    Example 3:
    
    >>> similarPairs(words = ["nba","cba","dba"])
    >>> 0
    Explanation: Since there does not exist any pair that satisfies the conditions, we return 0.
    """
def smallestValue(n: int) -> int:
    """
    You are given a positive integer n.
    Continuously replace n with the sum of its prime factors.
    
    Note that if a prime factor divides n multiple times, it should be included in the sum as many times as it divides n.
    
    Return the smallest value n will take on.
    
    Example 1:
    
    >>> smallestValue(n = 15)
    >>> 5
    Explanation: Initially, n = 15.
    15 = 3 * 5, so replace n with 3 + 5 = 8.
    8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
    6 = 2 * 3, so replace n with 2 + 3 = 5.
    5 is the smallest value n will take on.
    
    Example 2:
    
    >>> smallestValue(n = 3)
    >>> 3
    Explanation: Initially, n = 3.
    3 is the smallest value n will take on.
    """
def isPossible(n: int, edges: List[List[int]]) -> bool:
    """
    There is an undirected graph consisting of n nodes numbered from 1 to n. You are given the integer n and a 2D array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi. The graph can be disconnected.
    You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.
    Return true if it is possible to make the degree of each node in the graph even, otherwise return false.
    The degree of a node is the number of edges connected to it.
    
    Example 1:
    
    
    >>> isPossible(n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]])
    >>> true
    Explanation: The above diagram shows a valid way of adding an edge.
    Every node in the resulting graph is connected to an even number of edges.
    
    Example 2:
    
    
    >>> isPossible(n = 4, edges = [[1,2],[3,4]])
    >>> true
    Explanation: The above diagram shows a valid way of adding two edges.
    Example 3:
    
    
    >>> isPossible(n = 4, edges = [[1,2],[1,3],[1,4]])
    >>> false
    Explanation: It is not possible to obtain a valid graph with adding at most 2 edges.
    """
def cycleLengthQueries(n: int, queries: List[List[int]]) -> List[int]:
    """
    You are given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value val in the range [1, 2n - 1 - 1] has two children where:
    
    The left node has the value 2 * val, and
    The right node has the value 2 * val + 1.
    
    You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, solve the following problem:
    
    Add an edge between the nodes with values ai and bi.
    Find the length of the cycle in the graph.
    Remove the added edge between nodes with values ai and bi.
    
    Note that:
    
    A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.
    The length of a cycle is the number of edges visited in the cycle.
    There could be multiple edges between two nodes in the tree after adding the edge of the query.
    
    Return an array answer of length m where answer[i] is the answer to the ith query.
    
    Example 1:
    
    
    >>> cycleLengthQueries(n = 3, queries = [[5,3],[4,7],[2,3]])
    >>> [4,5,3]
    Explanation: The diagrams above show the tree of 23 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
    - After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query.
    - After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query.
    - After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge.
    
    Example 2:
    
    
    >>> cycleLengthQueries(n = 2, queries = [[1,2]])
    >>> [2]
    Explanation: The diagram above shows the tree of 22 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
    - After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge.
    """
def isThereAPath(grid: List[List[int]]) -> bool:
    """
    You are given a 0-indexed m x n binary matrix grid. You can move from a cell (row, col) to any of the cells (row + 1, col) or (row, col + 1).
    Return true if there is a path from (0, 0) to (m - 1, n - 1) that visits an equal number of 0's and 1's. Otherwise return false.
    
    Example 1:
    
    
    >>> isThereAPath(grid = [[0,1,0,0],[0,1,0,0],[1,0,1,0]])
    >>> true
    Explanation: The path colored in blue in the above diagram is a valid path because we have 3 cells with a value of 1 and 3 with a value of 0. Since there is a valid path, we return true.
    
    Example 2:
    
    
    >>> isThereAPath(grid = [[1,1,0],[0,0,1],[1,0,0]])
    >>> false
    Explanation: There is no path in this grid with an equal number of 0's and 1's.
    """
def captureForts(forts: List[int]) -> int:
    """
    You are given a 0-indexed integer array forts of length n representing the positions of several forts. forts[i] can be -1, 0, or 1 where:
    
    -1 represents there is no fort at the ith position.
    0 indicates there is an enemy fort at the ith position.
    1 indicates the fort at the ith the position is under your command.
    
    Now you have decided to move your army from one of your forts at position i to an empty position j such that:
    
    0 <= i, j <= n - 1
    The army travels over enemy forts only. Formally, for all k where min(i,j) < k < max(i,j), forts[k] == 0.
    
    While moving the army, all the enemy forts that come in the way are captured.
    Return the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return 0.
    
    Example 1:
    
    >>> captureForts(forts = [1,0,0,-1,0,0,0,0,1])
    >>> 4
    Explanation:
    - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
    - Moving the army from position 8 to position 3 captures 4 enemy forts.
    Since 4 is the maximum number of enemy forts that can be captured, we return 4.
    
    Example 2:
    
    >>> captureForts(forts = [0,0,1,-1])
    >>> 0
    Explanation: Since no enemy fort can be captured, 0 is returned.
    """
def topStudents(positive_feedback: List[str], negative_feedback: List[str], report: List[str], student_id: List[int], k: int) -> List[int]:
    """
    You are given two string arrays positive_feedback and negative_feedback, containing the words denoting positive and negative feedback, respectively. Note that no word is both positive and negative.
    Initially every student has 0 points. Each positive word in a feedback report increases the points of a student by 3, whereas each negative word decreases the points by 1.
    You are given n feedback reports, represented by a 0-indexed string array report and a 0-indexed integer array student_id, where student_id[i] represents the ID of the student who has received the feedback report report[i]. The ID of each student is unique.
    Given an integer k, return the top k students after ranking them in non-increasing order by their points. In case more than one student has the same points, the one with the lower ID ranks higher.
    
    Example 1:
    
    >>> topStudents(positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2)
    >>> [1,2]
    Explanation:
    Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.
    
    Example 2:
    
    >>> topStudents(positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2)
    >>> [2,1]
    Explanation:
    - The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points.
    - The student with ID 2 has 1 positive feedback, so he has 3 points.
    Since student 2 has more points, [2,1] is returned.
    """
def minimizeSet(divisor1: int, divisor2: int, uniqueCnt1: int, uniqueCnt2: int) -> int:
    """
    We have two arrays arr1 and arr2 which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:
    
    arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1.
    arr2 contains uniqueCnt2 distinct positive integers, each of which is not divisible by divisor2.
    No integer is present in both arr1 and arr2.
    
    Given divisor1, divisor2, uniqueCnt1, and uniqueCnt2, return the minimum possible maximum integer that can be present in either array.
    
    Example 1:
    
    >>> minimizeSet(divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3)
    >>> 4
    Explanation:
    We can distribute the first 4 natural numbers into arr1 and arr2.
    arr1 = [1] and arr2 = [2,3,4].
    We can see that both arrays satisfy all the conditions.
    Since the maximum value is 4, we return it.
    
    Example 2:
    
    >>> minimizeSet(divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1)
    >>> 3
    Explanation:
    Here arr1 = [1,2], and arr2 = [3] satisfy all conditions.
    Since the maximum value is 3, we return it.
    Example 3:
    
    >>> minimizeSet(divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2)
    >>> 15
    Explanation:
    Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6].
    It can be shown that it is not possible to obtain a lower maximum satisfying all conditions.
    """
def closestTarget(words: List[str], target: str, startIndex: int) -> int:
    """
    You are given a 0-indexed circular string array words and a string target. A circular array means that the array's end connects to the array's beginning.
    
    Formally, the next element of words[i] is words[(i + 1) % n] and the previous element of words[i] is words[(i - 1 + n) % n], where n is the length of words.
    
    Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time.
    Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.
    
    Example 1:
    
    >>> closestTarget(words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1)
    >>> 1
    Explanation: We start from index 1 and can reach "hello" by
    - moving 3 units to the right to reach index 4.
    - moving 2 units to the left to reach index 4.
    - moving 4 units to the right to reach index 0.
    - moving 1 unit to the left to reach index 0.
    The shortest distance to reach "hello" is 1.
    
    Example 2:
    
    >>> closestTarget(words = ["a","b","leetcode"], target = "leetcode", startIndex = 0)
    >>> 1
    Explanation: We start from index 0 and can reach "leetcode" by
    - moving 2 units to the right to reach index 3.
    - moving 1 unit to the left to reach index 3.
    The shortest distance to reach "leetcode" is 1.
    Example 3:
    
    >>> closestTarget(words = ["i","eat","leetcode"], target = "ate", startIndex = 0)
    >>> -1
    Explanation: Since "ate" does not exist in words, we return -1.
    """
def takeCharacters(s: str, k: int) -> int:
    """
    You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s.
    Return the minimum number of minutes needed for you to take at least k of each character, or return -1 if it is not possible to take k of each character.
    
    Example 1:
    
    >>> takeCharacters(s = "aabaaaacaabc", k = 2)
    >>> 8
    Explanation:
    Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
    Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
    A total of 3 + 5 = 8 minutes is needed.
    It can be proven that 8 is the minimum number of minutes needed.
    
    Example 2:
    
    >>> takeCharacters(s = "a", k = 1)
    >>> -1
    Explanation: It is not possible to take one 'b' or 'c' so return -1.
    """
def maximumTastiness(price: List[int], k: int) -> int:
    """
    You are given an array of positive integers price where price[i] denotes the price of the ith candy and a positive integer k.
    The store sells baskets of k distinct candies. The tastiness of a candy basket is the smallest absolute difference of the prices of any two candies in the basket.
    Return the maximum tastiness of a candy basket.
    
    Example 1:
    
    >>> maximumTastiness(price = [13,5,1,8,21,2], k = 3)
    >>> 8
    Explanation: Choose the candies with the prices [13,5,21].
    The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8.
    It can be proven that 8 is the maximum tastiness that can be achieved.
    
    Example 2:
    
    >>> maximumTastiness(price = [1,3,1], k = 2)
    >>> 2
    Explanation: Choose the candies with the prices [1,3].
    The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2.
    It can be proven that 2 is the maximum tastiness that can be achieved.
    
    Example 3:
    
    >>> maximumTastiness(price = [7,7,7,7], k = 2)
    >>> 0
    Explanation: Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
    """
def countPartitions(nums: List[int], k: int) -> int:
    """
    You are given an array nums consisting of positive integers and an integer k.
    Partition the array into two ordered groups such that each element is in exactly one group. A partition is called great if the sum of elements of each group is greater than or equal to k.
    Return the number of distinct great partitions. Since the answer may be too large, return it modulo 109 + 7.
    Two partitions are considered distinct if some element nums[i] is in different groups in the two partitions.
    
    Example 1:
    
    >>> countPartitions(nums = [1,2,3,4], k = 4)
    >>> 6
    Explanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).
    
    Example 2:
    
    >>> countPartitions(nums = [3,3,3], k = 4)
    >>> 0
    Explanation: There are no great partitions for this array.
    
    Example 3:
    
    >>> countPartitions(nums = [6,6], k = 2)
    >>> 2
    Explanation: We can either put nums[0] in the first partition or in the second partition.
    The great partitions will be ([6], [6]) and ([6], [6]).
    """
def kBigIndices(nums: List[int], k: int) -> int:
    """
    You are given a 0-indexed integer array nums and a positive integer k.
    We call an index i k-big if the following conditions are satisfied:
    
    There exist at least k different indices idx1 such that idx1 < i and nums[idx1] < nums[i].
    There exist at least k different indices idx2 such that idx2 > i and nums[idx2] < nums[i].
    
    Return the number of k-big indices.
    
    Example 1:
    
    >>> kBigIndices(nums = [2,3,6,5,2,3], k = 2)
    >>> 2
    Explanation: There are only two 2-big indices in nums:
    - i = 2 --> There are two valid idx1: 0 and 1. There are three valid idx2: 2, 3, and 4.
    - i = 3 --> There are two valid idx1: 0 and 1. There are two valid idx2: 3 and 4.
    
    Example 2:
    
    >>> kBigIndices(nums = [1,1,1], k = 3)
    >>> 0
    Explanation: There are no 3-big indices in nums.
    """
def countDigits(num: int) -> int:
    """
    Given an integer num, return the number of digits in num that divide num.
    An integer val divides nums if nums % val == 0.
    
    Example 1:
    
    >>> countDigits(num = 7)
    >>> 1
    Explanation: 7 divides itself, hence the answer is 1.
    
    Example 2:
    
    >>> countDigits(num = 121)
    >>> 2
    Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
    
    Example 3:
    
    >>> countDigits(num = 1248)
    >>> 4
    Explanation: 1248 is divisible by all of its digits, hence the answer is 4.
    """
def distinctPrimeFactors(nums: List[int]) -> int:
    """
    Given an array of positive integers nums, return the number of distinct prime factors in the product of the elements of nums.
    Note that:
    
    A number greater than 1 is called prime if it is divisible by only 1 and itself.
    An integer val1 is a factor of another integer val2 if val2 / val1 is an integer.
    
    
    Example 1:
    
    >>> distinctPrimeFactors(nums = [2,4,3,7,10,6])
    >>> 4
    Explanation:
    The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 25 * 32 * 5 * 7.
    There are 4 distinct prime factors so we return 4.
    
    Example 2:
    
    >>> distinctPrimeFactors(nums = [2,4,8,16])
    >>> 1
    Explanation:
    The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 210.
    There is 1 distinct prime factor so we return 1.
    """
def minimumPartition(s: str, k: int) -> int:
    """
    You are given a string s consisting of digits from 1 to 9 and an integer k.
    A partition of a string s is called good if:
    
    Each digit of s is part of exactly one substring.
    The value of each substring is less than or equal to k.
    
    Return the minimum number of substrings in a good partition of s. If no good partition of s exists, return -1.
    Note that:
    
    The value of a string is its result when interpreted as an integer. For example, the value of "123" is 123 and the value of "1" is 1.
    A substring is a contiguous sequence of characters within a string.
    
    
    Example 1:
    
    >>> minimumPartition(s = "165462", k = 60)
    >>> 4
    Explanation: We can partition the string into substrings "16", "54", "6", and "2". Each substring has a value less than or equal to k = 60.
    It can be shown that we cannot partition the string into less than 4 substrings.
    
    Example 2:
    
    >>> minimumPartition(s = "238182", k = 5)
    >>> -1
    Explanation: There is no good partition for this string.
    """
def closestPrimes(left: int, right: int) -> List[int]:
    """
    Given two positive integers left and right, find the two integers num1 and num2 such that:
    
    left <= num1 < num2 <= right .
    Both num1 and num2 are prime numbers.
    num2 - num1 is the minimum amongst all other pairs satisfying the above conditions.
    
    Return the positive integer array ans = [num1, num2]. If there are multiple pairs satisfying these conditions, return the one with the smallest num1 value. If no such numbers exist, return [-1, -1].
    
    Example 1:
    
    >>> closestPrimes(left = 10, right = 19)
    >>> [11,13]
    Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
    The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
    Since 11 is smaller than 17, we return the first pair.
    
    Example 2:
    
    >>> closestPrimes(left = 4, right = 6)
    >>> [-1,-1]
    Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
    """