task_id
stringlengths
3
79
prompt
stringlengths
255
3.9k
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
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 element that is strictly gre...
minimum-number-of-coins-to-be-added
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 minimum number of coins of a...
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
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..ri]. Return an array...
find-common-elements-between-two-arrays
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 : the number of indices i s...
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
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 equal to k. Return the l...
number-of-possible-sets-of-closing-branches
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 they are spending an excess...
count-tested-devices-after-test-operations
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 following test operations: ...
count-subarrays-where-max-element-appears-at-least-k-times
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 an array. Exam...
number-of-divisible-triplet-sums
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 = [3,3,4,7,8], d = 5) >>> ...
find-missing-and-repeated-values
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 repeating and missing numbers ...
divide-array-into-arrays-with-max-difference
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 any two elements in one ar...
minimum-cost-to-make-array-equalindromic
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 in the range [0, n - 1],...
apply-operations-to-maximize-frequency-score
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. The score of the final ar...
minimum-number-of-coins-for-fruits-ii
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 offer: If you purc...
count-the-number-of-incremovable-subarrays-i
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 of [5, 3, 4, 6, 7] becaus...
find-polygon-with-the-largest-perimeter
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 >= 3) positive real numbers...
count-the-number-of-incremovable-subarrays-ii
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 of [5, 3, 4, 6, 7] becaus...
find-number-of-coins-to-place-in-tree-nodes
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 between nodes ai and bi in the...
minimum-number-game
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: Every round, first Alice wi...
maximum-square-area-by-removing-fences-from-a-field
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. Horizontal fences are from the co...
minimum-cost-to-convert-string-i
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 and changed, and an inte...
minimum-cost-to-convert-string-ii
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 and changed, and an inte...
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
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 representation. For example,...
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
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 perform the following operatio...
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
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 x missing from nums such...
minimum-number-of-operations-to-make-array-xor-equal-to-k
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. Flipping a bit means cha...
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
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 area of the rectangle havin...
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
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 nums2 into a set s. ...
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
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 are also given a 0-indexed inte...
count-elements-with-maximum-frequency
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 that element in the arr...
find-beautiful-indices-in-the-given-array-i
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 that: 0 <= j <= ...
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
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 that: 0 <= j <= ...
maximum-number-of-intersections-on-the-chart
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 y-coordinate. We can...
divide-an-array-into-subarrays-with-minimum-cost-i
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 subarrays. Return the min...
find-if-array-can-be-sorted
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). Return true if you can s...
minimize-length-of-array-using-operations
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 i and j from nums, such...
divide-an-array-into-subarrays-with-minimum-cost-ii
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] is 3. You need to di...
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
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 . An additional street c...
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
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 . An additional street c...
maximum-number-of-removal-queries-that-can-be-processed-i
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 processing queries in the g...
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
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, ..., xk/2, xk, xk/2, ....
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
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] & nums[i + 1], where & re...
type-of-triangle
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 equal length. A triang...
find-the-number-of-ways-to-place-people-i
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, and there are no ot...
maximum-good-subarray-sum
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 if |nums[i] - nums[j]| =...
find-the-number-of-ways-to-place-people-ii
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 direction as negative x-axis...
ant-on-the-boundary
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 value of the current elem...
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
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 are adjacent if they sh...
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
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. Return the matrix answer...
number-of-subarrays-that-match-a-pattern-i
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 the following condition...
maximum-palindromes-after-operations
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, and y such that 0 <= i, j ...
number-of-subarrays-that-match-a-pattern-ii
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 the following condition...
maximum-number-of-operations-with-the-same-score-i
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 contains fewer than two...
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
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 delete them. Choose the ...
maximize-consecutive-elements-in-an-array-after-modification
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 that those elements are...
count-prefix-and-suffix-pairs-i
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 str2, and false otherwi...
find-the-length-of-the-longest-common-prefix
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 integer 12345, while 23...
most-frequent-prime
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, and north-east. Se...
count-prefix-and-suffix-pairs-ii
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 str2, and false otherwi...
split-the-array
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 should also contain distin...
find-the-largest-area-of-square-inside-two-rectangles
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_i] represent the bottom...
earliest-second-to-mark-indices-i
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. In each second, s, in o...
winner-of-the-linked-list-game
# class ListNode: # def __init__(val=0, next=None): # self.val = val # self.next = next class Solution: 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 e...
linked-list-frequency
# class ListNode: # def __init__(val=0, next=None): # self.val = val # self.next = next class Solution: 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 lengt...
minimum-operations-to-exceed-threshold-value-i
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 are greater than or equ...
minimum-operations-to-exceed-threshold-value-ii
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 and y from nums. Inse...
count-pairs-of-connectable-servers-in-a-weighted-tree-network
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 vertices ai and bi of we...
find-the-maximum-sum-of-node-values
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 and vi in the tree. You ...
distribute-elements-into-two-arrays-i
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 operation, append nums[...
count-submatrices-with-top-left-element-and-sum-less-than-k
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: >>> countSubmatrices(grid ...
minimum-operations-to-write-the-letter-y-on-a-grid
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 and ending at the center c...
distribute-elements-into-two-arrays-ii
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 the elements of nums be...
maximum-increasing-triplet-value
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: >>> maximumTripletValue(...
apple-redistribution-into-boxes
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. Return the minimum n...
maximize-happiness-of-selected-children
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 children in k turns. In eac...
shortest-uncommon-substring-in-an-array
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 in arr. If multiple suc...
maximum-strength-of-k-disjoint-subarrays
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{i+1} for all 1 <= i <= ...
match-alphanumerical-pattern-in-matrix-i
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 submatrix of board that mat...
find-the-sum-of-encrypted-integers
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 the sum of encrypted el...