task_id
stringlengths
3
79
prompt
stringlengths
255
3.93k
count-beautiful-substrings-ii
def beautifulSubstrings(s: str, k: int) -> int: """ You are given a string s and a positive integer k. Let vowels and consonants be the number of vowels and consonants in a string. A string is beautiful if: vowels == consonants. (vowels * consonants) % k == 0, in other terms the multiplicat...
number-of-divisible-substrings
def countDivisibleSubstrings(word: str) -> int: """ Each character of the English alphabet has been mapped to a digit as shown below. A string is divisible if the sum of the mapped values of its characters is divisible by its length. Given a string s, return the number of divisible substrings of s....
find-the-peaks
from typing import List def findPeaks(mountain: List[int]) -> List[int]: """ You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array. Return an array that consists of indices of peaks in the given array in any order. Notes: A peak is defined as an elem...
minimum-number-of-coins-to-be-added
from typing import List def minimumAddedCoins(coins: List[int], target: int) -> int: """ You are given a 0-indexed integer array coins, representing the values of the coins available, and an integer target. An integer x is obtainable if there exists a subsequence of coins that sums to x. Return the mini...
count-complete-substrings
def countCompleteSubstrings(word: str, k: int) -> int: """ You are given a string word and an integer k. A substring s of word is complete if: Each character in s occurs exactly k times. The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and...
number-of-same-end-substrings
from typing import List def sameEndSubstringCount(s: str, queries: List[List[int]]) -> List[int]: """ You are given a 0-indexed string s, and a 2D array of integers queries, where queries[i] = [li, ri] indicates a substring of s starting from the index li and ending at the index ri (both inclusive), i.e. s[li.....
find-common-elements-between-two-arrays
from typing import List def findIntersectionValues(nums1: List[int], nums2: List[int]) -> List[int]: """ You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values: answer1 : the number of indices i such that nums1[i] exists in nums2. answer2 : t...
remove-adjacent-almost-equal-characters
def removeAlmostEqualCharacters(word: str) -> int: """ You are given a 0-indexed string word. In one operation, you can pick any index i of word and change word[i] to any lowercase English letter. Return the minimum number of operations needed to remove all adjacent almost-equal characters from word. ...
length-of-longest-subarray-with-at-most-k-frequency
from typing import List def maxSubarrayLength(nums: List[int], k: int) -> int: """ You are given an integer array nums and an integer k. The frequency of an element x is the number of times it occurs in an array. An array is called good if the frequency of each element in this array is less than or equa...
number-of-possible-sets-of-closing-branches
from typing import List def numberOfSets(n: int, maxDistance: int, roads: List[List[int]]) -> int: """ There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads. The company has realized that the...
count-tested-devices-after-test-operations
from typing import List def countTestedDevices(batteryPercentages: List[int]) -> int: """ You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices. Your task is to test each device i in order from 0 to n - 1, by performing the follow...
count-subarrays-where-max-element-appears-at-least-k-times
from typing import List def countSubarrays(nums: List[int], k: int) -> int: """ You are given an integer array nums and a positive integer k. Return the number of subarrays where the maximum element of nums appears at least k times in that subarray. A subarray is a contiguous sequence of elements within...
number-of-divisible-triplet-sums
from typing import Any, List def divisibleTripletCount(nums: List[int], d: int) -> int: """ Given a 0-indexed integer array nums and an integer d, return the number of triplets (i, j, k) such that i < j < k and (nums[i] + nums[j] + nums[k]) % d == 0. Example 1: >>> divisibleTripletCount(nums =...
find-missing-and-repeated-values
from typing import List def findMissingAndRepeatedValues(grid: List[List[int]]) -> List[int]: """ You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeat...
divide-array-into-arrays-with-max-difference
from typing import List def divideArray(nums: List[int], k: int) -> List[List[int]]: """ You are given an integer array nums of size n where n is a multiple of 3 and a positive integer k. Divide the array nums into n / 3 arrays of size 3 satisfying the following condition: The difference between an...
minimum-cost-to-make-array-equalindromic
from typing import List def minimumCost(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums having length n. You are allowed to perform a special move any number of times (including zero) on nums. In one special move you perform the following steps in order: Choose an index i ...
apply-operations-to-maximize-frequency-score
from typing import List def maxFrequencyScore(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums and an integer k. You can perform the following operation on the array at most k times: Choose any index i from the array and increase or decrease nums[i] by 1. T...
minimum-number-of-coins-for-fruits-ii
from typing import List def minimumCoins(prices: List[int]) -> int: """ You are at a fruit market with different types of exotic fruits on display. You are given a 1-indexed array prices, where prices[i] denotes the number of coins needed to purchase the ith fruit. The fruit market has the following off...
count-the-number-of-incremovable-subarrays-i
from typing import List def incremovableSubarrayCount(nums: List[int]) -> int: """ You are given a 0-indexed array of positive integers nums. A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray o...
find-polygon-with-the-largest-perimeter
from typing import List def largestPerimeter(nums: List[int]) -> int: """ You are given an array of positive integers nums of length n. A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides. Conversely, if you have k (k >= ...
count-the-number-of-incremovable-subarrays-ii
from typing import List def incremovableSubarrayCount(nums: List[int]) -> int: """ You are given a 0-indexed array of positive integers nums. A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray o...
find-number-of-coins-to-place-in-tree-nodes
from typing import List def placedCoins(edges: List[List[int]], cost: List[int]) -> List[int]: """ You are given an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge betwee...
minimum-number-game
from typing import List def numberGame(nums: List[int]) -> List[int]: """ You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows: Eve...
maximum-square-area-by-removing-fences-from-a-field
from typing import List def maximizeSquareArea(m: int, n: int, hFences: List[int], vFences: List[int]) -> int: """ There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively. Horizonta...
minimum-cost-to-convert-string-i
from typing import List def minimumCost(source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: """ You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original ...
minimum-cost-to-convert-string-ii
from typing import List def minimumCost(source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: """ You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English characters. You are also given two 0-indexed string arrays original ...
most-expensive-item-that-can-not-be-bought
def mostExpensiveItem(primeOne: int, primeTwo: int) -> int: """ You are given two distinct prime numbers primeOne and primeTwo. Alice and Bob are visiting a market. The market has an infinite number of items, for any positive integer x there exists an item whose price is x. Alice wants to buy some items fro...
check-if-bitwise-or-has-trailing-zeros
from typing import List def hasTrailingZeros(nums: List[int]) -> bool: """ You are given an array of positive integers nums. You have to check if it is possible to select two or more elements in the array such that the bitwise OR of the selected elements has at least one trailing zero in its binary represen...
find-longest-special-substring-that-occurs-thrice-i
def maximumLength(s: str) -> int: """ You are given a string s that consists of lowercase English letters. A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special. Return the length of the ...
find-longest-special-substring-that-occurs-thrice-ii
def maximumLength(s: str) -> int: """ You are given a string s that consists of lowercase English letters. A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special. Return the length of the ...
palindrome-rearrangement-queries
from typing import List def canMakePalindromeQueries(s: str, queries: List[List[int]]) -> List[bool]: """ You are given a 0-indexed string s having an even length n. You are also given a 0-indexed 2D integer array, queries, where queries[i] = [ai, bi, ci, di]. For each query i, you are allowed to perfor...
number-of-self-divisible-permutations
def selfDivisiblePermutationCount(n: int) -> int: """ Given an integer n, return the number of permutations of the 1-indexed array nums = [1, 2, ..., n], such that it's self-divisible. A 1-indexed array a of length n is self-divisible if for every 1 <= i <= n, gcd(a[i], i) == 1. A permutation of an arra...
smallest-missing-integer-greater-than-sequential-prefix-sum
from typing import List def missingInteger(nums: List[int]) -> int: """ You are given a 0-indexed array of integers nums. A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential. Return the smallest integer ...
minimum-number-of-operations-to-make-array-xor-equal-to-k
from typing import List def minOperations(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: Choose any element of the array and flip a bit in its binary representation. ...
minimum-number-of-operations-to-make-x-and-y-equal
def minimumOperationsToMakeEqual(x: int, y: int) -> int: """ You are given two positive integers x and y. In one operation, you can do one of the four following operations: Divide x by 11 if x is a multiple of 11. Divide x by 5 if x is a multiple of 5. Decrement x by 1. Increment x by 1...
count-the-number-of-powerful-integers
def numberOfPowerfulInt(start: int, finish: int, limit: int, s: str) -> int: """ You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer. A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and ...
maximum-area-of-longest-diagonal-rectangle
from typing import List def areaOfMaxDiagonal(dimensions: List[List[int]]) -> int: """ You are given a 2D 0-indexed integer array dimensions. For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i. Return the are...
minimum-moves-to-capture-the-queen
def minMovesToCaptureTheQueen(a: int, b: int, c: int, d: int, e: int, f: int) -> int: """ There is a 1-indexed 8 x 8 chessboard containing 3 pieces. You are given 6 integers a, b, c, d, e, and f where: (a, b) denotes the position of the white rook. (c, d) denotes the position of the white bisho...
maximum-size-of-a-set-after-removals
from typing import List def maximumSetSize(nums1: List[int], nums2: List[int]) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2 of even length n. You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and ...
maximize-the-number-of-partitions-after-operations
def maxPartitionsAfterOperations(s: str, k: int) -> int: """ You are given a string s and an integer k. First, you are allowed to change at most one index in s to another lowercase English letter. After that, do the following partitioning operation until s is empty: Choose the longest prefix of...
maximum-subtree-of-the-same-color
from typing import Any, List def maximumSubtreeSize(edges: List[List[int]], colors: List[int]) -> int: """ You are given a 2D integer array edges representing a tree with n nodes, numbered from 0 to n - 1, rooted at node 0, where edges[i] = [ui, vi] means there is an edge between the nodes vi and ui. You ar...
count-elements-with-maximum-frequency
from typing import List def maxFrequencyElements(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. Return the total frequencies of elements in nums such that those elements all have the maximum frequency. The frequency of an element is the number of occurrences of...
find-beautiful-indices-in-the-given-array-i
from typing import List def beautifulIndices(s: str, a: str, b: str, k: int) -> List[int]: """ You are given a 0-indexed string s, a string a, a string b, and an integer k. An index i is beautiful if: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a There exists an index j such t...
maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k
def findMaximumNumber(k: int, x: int) -> int: """ You are given an integer k and an integer x. The price of a number num is calculated by the count of set bits at positions x, 2x, 3x, etc., in its binary representation, starting from the least significant bit. The following table contains examples of how price ...
find-beautiful-indices-in-the-given-array-ii
from typing import List def beautifulIndices(s: str, a: str, b: str, k: int) -> List[int]: """ You are given a 0-indexed string s, a string a, a string b, and an integer k. An index i is beautiful if: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a There exists an index j such t...
maximum-number-of-intersections-on-the-chart
from typing import List def maxIntersectionCount(y: List[int]) -> int: """ There is a line chart consisting of n points connected by line segments. You are given a 1-indexed integer array y. The kth point has coordinates (k, y[k]). There are no horizontal lines; that is, no two consecutive points have the same ...
divide-an-array-into-subarrays-with-minimum-cost-i
from typing import List def minimumCost(nums: List[int]) -> int: """ You are given an array of integers nums of length n. The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3. You need to divide nums into 3 disjoint contiguous subar...
find-if-array-can-be-sorted
from typing import List def canSortArray(nums: List[int]) -> bool: """ You are given a 0-indexed array of positive integers nums. In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero). ...
minimize-length-of-array-using-operations
from typing import List def minimumArrayLength(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums containing positive integers. Your task is to minimize the length of nums by performing the following operations any number of times (including zero): Select two distinct indices...
divide-an-array-into-subarrays-with-minimum-cost-ii
from typing import List def minimumCost(nums: List[int], k: int, dist: int) -> int: """ You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist. The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] ...
minimum-number-of-pushes-to-type-word-i
def minimumPushes(word: str) -> int: """ You are given a string word containing distinct lowercase English letters. Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"],...
count-the-number-of-houses-at-a-certain-distance-i
from typing import List def countOfPairs(n: int, x: int, y: int) -> List[int]: """ You are given three positive integers n, x, and y. In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 ...
minimum-number-of-pushes-to-type-word-ii
def minimumPushes(word: str) -> int: """ You are given a string word containing lowercase English letters. Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need ...
count-the-number-of-houses-at-a-certain-distance-ii
from typing import List def countOfPairs(n: int, x: int, y: int) -> List[int]: """ You are given three positive integers n, x, and y. In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 ...
maximum-number-of-removal-queries-that-can-be-processed-i
from typing import List def maximumProcessableQueries(nums: List[int], queries: List[int]) -> int: """ You are given a 0-indexed array nums and a 0-indexed array queries. You can do the following operation at the beginning at most once: Replace nums with a subsequence of nums. We start pro...
number-of-changing-keys
def countKeyChanges(s: str) -> int: """ You are given a 0-indexed string s typed by a user. Changing a key is defined as using a key different from the last used key. For example, s = "ab" has a change of a key while s = "bBBb" does not have any. Return the number of times the user had to change the key. ...
find-the-maximum-number-of-elements-in-subset
from typing import List def maximumLength(nums: List[int]) -> int: """ You are given an array of positive integers nums. You need to select a subset of nums which satisfies the following condition: You can place the selected elements in a 0-indexed array such that it follows the pattern: [x, x2, x4...
alice-and-bob-playing-flower-game
def flowerGame(n: int, m: int) -> int: """ Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are x flowers in the clockwise direction between Alice and Bob, and y flowers in the anti-clockwise direction between them. The game pr...
minimize-or-of-remaining-elements-using-operations
from typing import List def minOrAfterOperations(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums and an integer k. In one operation, you can pick any index i of nums such that 0 <= i < nums.length - 1 and replace nums[i] and nums[i + 1] with a single occurrence of nums[i] &...
type-of-triangle
from typing import List def triangleType(nums: List[int]) -> str: """ You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle. A triangle is called equilateral if it has all sides of equal length. A triangle is called isosceles if it has exactly two sides of eq...
find-the-number-of-ways-to-place-people-i
from typing import List def numberOfPairs(points: List[List[int]]) -> int: """ You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi]. Count the number of pairs of points (A, B), where A is on the upper left side of B,...
maximum-good-subarray-sum
from typing import List def maximumSubarraySum(nums: List[int], k: int) -> int: """ You are given an array nums of length n and a positive integer k. A subarray of nums is called good if the absolute difference between its first and last element is exactly k, in other words, the subarray nums[i..j] is good ...
find-the-number-of-ways-to-place-people-ii
from typing import List def numberOfPairs(points: List[List[int]]) -> int: """ You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi]. We define the right direction as positive x-axis (increasing x-coordinate) and the left dire...
ant-on-the-boundary
from typing import List def returnToBoundaryCount(nums: List[int]) -> int: """ An ant is on a boundary. It sometimes goes left and sometimes right. You are given an array of non-zero integers nums. The ant starts reading nums from the first element of it to its end. At each step, it moves according to the v...
minimum-time-to-revert-word-to-initial-state-i
def minimumTimeToInitialState(word: str, k: int) -> int: """ You are given a 0-indexed string word and an integer k. At every second, you must perform the following operations: Remove the first k characters of word. Add any k characters to the end of word. Note that you do not necessar...
find-the-grid-of-region-average
from typing import List def resultGrid(image: List[List[int]], threshold: int) -> List[List[int]]: """ You are given m x n grid image which represents a grayscale image, where image[i][j] represents a pixel with intensity in the range [0..255]. You are also given a non-negative integer threshold. Two pixels...
minimum-time-to-revert-word-to-initial-state-ii
def minimumTimeToInitialState(word: str, k: int) -> int: """ You are given a 0-indexed string word and an integer k. At every second, you must perform the following operations: Remove the first k characters of word. Add any k characters to the end of word. Note that you do not necessar...
count-numbers-with-unique-digits-ii
def numberCount(a: int, b: int) -> int: """ Given two positive integers a and b, return the count of numbers having unique digits in the range [a, b] (inclusive). Example 1: >>> numberCount(a = 1, b = 20) >>> 19 Explanation: All the numbers in the range [1, 20] have unique digits excep...
modify-the-matrix
from typing import List def modifiedMatrix(matrix: List[List[int]]) -> List[List[int]]: """ Given a 0-indexed m x n integer matrix matrix, create a new 0-indexed matrix called answer. Make answer equal to matrix, then replace each element with the value -1 with the maximum element in its respective column. ...
number-of-subarrays-that-match-a-pattern-i
from typing import List def countMatchingSubarrays(nums: List[int], pattern: List[int]) -> int: """ You are given a 0-indexed integer array nums of size n, and a 0-indexed integer array pattern of size m consisting of integers -1, 0, and 1. A subarray nums[i..j] of size m + 1 is said to match the pattern if...
maximum-palindromes-after-operations
from typing import List def maxPalindromesAfterOperations(words: List[str]) -> int: """ You are given a 0-indexed string array words having length n and containing 0-indexed strings. You are allowed to perform the following operation any number of times (including zero): Choose integers i, j, x, an...
number-of-subarrays-that-match-a-pattern-ii
from typing import List def countMatchingSubarrays(nums: List[int], pattern: List[int]) -> int: """ You are given a 0-indexed integer array nums of size n, and a 0-indexed integer array pattern of size m consisting of integers -1, 0, and 1. A subarray nums[i..j] of size m + 1 is said to match the pattern if...
maximum-number-of-operations-with-the-same-score-i
from typing import List def maxOperations(nums: List[int]) -> int: """ You are given an array of integers nums. Consider the following operation: Delete the first two elements nums and define the score of the operation as the sum of these two elements. You can perform this operation until nums...
apply-operations-to-make-string-empty
def lastNonEmptyString(s: str) -> str: """ You are given a string s. Consider performing the following operation until s becomes empty: For every alphabet character from 'a' to 'z', remove the first occurrence of that character in s (if it exists). For example, let initially s = "aabcbbca"...
maximum-number-of-operations-with-the-same-score-ii
from typing import List def maxOperations(nums: List[int]) -> int: """ Given an array of integers called nums, you can perform any of the following operation while nums contains at least 2 elements: Choose the first two elements of nums and delete them. Choose the last two elements of nums and dele...
maximize-consecutive-elements-in-an-array-after-modification
from typing import List def maxSelectedElements(nums: List[int]) -> int: """ You are given a 0-indexed array nums consisting of positive integers. Initially, you can increase the value of any element in the array by at most 1. After that, you need to select one or more elements from the final array such...
count-prefix-and-suffix-pairs-i
from typing import List def countPrefixSuffixPairs(words: List[str]) -> int: """ You are given a 0-indexed string array words. Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2: isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of...
find-the-length-of-the-longest-common-prefix
from typing import List def longestCommonPrefix(arr1: List[int], arr2: List[int]) -> int: """ You are given two arrays with positive integers arr1 and arr2. A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example, 123 is a prefix of the...
most-frequent-prime
from typing import List def mostFrequentPrime(mat: List[List[int]]) -> int: """ You are given a m x n 0-indexed 2D matrix mat. From every cell, you can create numbers in the following way: There could be at most 8 paths from the cells namely: east, south-east, south, south-west, west, north-west, north...
count-prefix-and-suffix-pairs-ii
from typing import List def countPrefixSuffixPairs(words: List[str]) -> int: """ You are given a 0-indexed string array words. Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2: isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of...
split-the-array
from typing import List def isPossibleToSplit(nums: List[int]) -> bool: """ You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that: nums1.length == nums2.length == nums.length / 2. nums1 should contain distinct elements. nums2 sh...
find-the-largest-area-of-square-inside-two-rectangles
from typing import List def largestSquareArea(bottomLeft: List[List[int]], topRight: List[List[int]]) -> int: """ There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays bottomLeft and topRight where bottomLeft[i] = [a_i, b_i] and topRight[i] = [c_i, d...
earliest-second-to-mark-indices-i
from typing import List def earliestSecondToMarkIndices(nums: List[int], changeIndices: List[int]) -> int: """ You are given two 1-indexed integer arrays, nums and, changeIndices, having lengths n and m, respectively. Initially, all indices in nums are unmarked. Your task is to mark all indices in nums. ...
winner-of-the-linked-list-game
from typing import Optional class ListNode: def __init__(val=0, next=None): self.val = val self.next = next def gameResult(head: Optional[ListNode]) -> str: """ You are given the head of a linked list of even length containing integers. Each odd-indexed node contains an odd integer and each even-ind...
linked-list-frequency
from typing import Optional class ListNode: def __init__(val=0, next=None): self.val = val self.next = next def frequenciesOfElements(head: Optional[ListNode]) -> Optional[ListNode]: """ Given the head of a linked list containing k distinct elements, return the head to a linked list of length k cont...
minimum-operations-to-exceed-threshold-value-i
from typing import List def minOperations(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums, and an integer k. In one operation, you can remove one occurrence of the smallest element of nums. Return the minimum number of operations needed so that all elements of the array...
minimum-operations-to-exceed-threshold-value-ii
from typing import List def minOperations(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums, and an integer k. You are allowed to perform some operations on nums, where in a single operation, you can: Select the two smallest integers x and y from nums. Remove x a...
count-pairs-of-connectable-servers-in-a-weighted-tree-network
from typing import List def countPairsOfConnectableServers(edges: List[List[int]], signalSpeed: int) -> List[int]: """ You are given an unrooted weighted tree with n vertices representing servers numbered from 0 to n - 1, an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional edge between ...
find-the-maximum-sum-of-node-values
from typing import List def maximumValueSum(nums: List[int], k: int, edges: List[List[int]]) -> int: """ There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 0-indexed 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui ...
distribute-elements-into-two-arrays-i
from typing import List def resultArray(nums: List[int]) -> List[int]: """ You are given a 1-indexed array of distinct integers nums of length n. You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations. In the first operation, append nums[1] to arr1. In the second...
count-submatrices-with-top-left-element-and-sum-less-than-k
from typing import List def countSubmatrices(grid: List[List[int]], k: int) -> int: """ You are given a 0-indexed integer matrix grid and an integer k. Return the number of submatrices that contain the top-left element of the grid, and have a sum less than or equal to k. Example 1: >>...
minimum-operations-to-write-the-letter-y-on-a-grid
from typing import List def minimumOperationsToWriteY(grid: List[List[int]]) -> int: """ You are given a 0-indexed n x n grid where n is odd, and grid[r][c] is 0, 1, or 2. We say that a cell belongs to the Letter Y if it belongs to one of the following: The diagonal starting at the top-left cell an...
distribute-elements-into-two-arrays-ii
from typing import List def resultArray(nums: List[int]) -> List[int]: """ You are given a 1-indexed array of integers nums of length n. We define a function greaterCount such that greaterCount(arr, val) returns the number of elements in arr that are strictly greater than val. You need to distribute all...
maximum-increasing-triplet-value
from typing import List def maximumTripletValue(nums: List[int]) -> int: """ Given an array nums, return the maximum value of a triplet (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. The value of a triplet (i, j, k) is nums[i] - nums[j] + nums[k]. Example 1: ...
apple-redistribution-into-boxes
from typing import List def minimumBoxes(apple: List[int], capacity: List[int]) -> int: """ You are given an array apple of size n and an array capacity of size m. There are n packs where the ith pack contains apple[i] apples. There are m boxes as well, and the ith box has a capacity of capacity[i] apples. ...
maximize-happiness-of-selected-children
from typing import List def maximumHappinessSum(happiness: List[int], k: int) -> int: """ You are given an array happiness of length n, and a positive integer k. There are n children standing in a queue, where the ith child has happiness value happiness[i]. You want to select k children from these n childre...
shortest-uncommon-substring-in-an-array
from typing import List def shortestSubstrings(arr: List[str]) -> List[str]: """ You are given an array arr of size n consisting of non-empty strings. Find a string array answer of size n such that: answer[i] is the shortest substring of arr[i] that does not occur as a substring in any other string...
maximum-strength-of-k-disjoint-subarrays
from typing import List def maximumStrength(nums: List[int], k: int) -> int: """ You are given an array of integers nums with length n, and a positive odd integer k. Select exactly k disjoint subarrays sub1, sub2, ..., subk from nums such that the last element of subi appears before the first element of sub...
match-alphanumerical-pattern-in-matrix-i
from typing import List def findPattern(board: List[List[int]], pattern: List[str]) -> List[int]: """ You are given a 2D integer matrix board and a 2D character matrix pattern. Where 0 <= board[r][c] <= 9 and each element of pattern is either a digit or a lowercase English letter. Your task is to find a sub...
find-the-sum-of-encrypted-integers
from typing import List def sumOfEncryptedInt(nums: List[int]) -> int: """ You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in x with the largest digit in x. For example, encrypt(523) = 555 and encrypt(213) = 333. Return...