task_id
stringlengths
3
79
prompt
stringlengths
255
3.9k
increment-submatrices-by-one
def rangeAddQueries(n: int, queries: List[List[int]]) -> List[List[int]]: """ You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes. You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you sho...
count-the-number-of-good-subarrays
def countGood(nums: List[int], k: int) -> int: """ Given an integer array nums and an integer k, return the number of good subarrays of nums. A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j]. A subarray is a contiguous non-empty sequence of elem...
difference-between-maximum-and-minimum-price-sum
def maxOutput(n: int, edges: List[List[int]], price: List[int]) -> int: """ There exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nod...
minimum-common-value
def getCommon(nums1: List[int], nums2: List[int]) -> int: """ Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1. Note that an integer is said to be common to nums1 and n...
minimum-operations-to-make-array-equal-ii
def minOperations(nums1: List[int], nums2: List[int], k: int) -> int: """ You are given two integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1: Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other w...
maximum-subsequence-score
def maxScore(nums1: List[int], nums2: List[int], k: int) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k. For chosen indices i0, i1, ..., ik - 1, your score is defined as: ...
check-if-point-is-reachable
def isReachable(targetX: int, targetY: int) -> bool: """ There exists an infinitely large grid. You are currently at point (1, 1), and you need to reach the point (targetX, targetY) using a finite number of steps. In one step, you can move from point (x, y) to any one of the following points: (x, y...
alternating-digit-sum
def alternateDigitSum(n: int) -> int: """ You are given a positive integer n. Each digit of n has a sign according to the following rules: The most significant digit is assigned a positive sign. Each other digit has an opposite sign to its adjacent digits. Return the sum of all digits with...
sort-the-students-by-their-kth-score
def sortTheStudents(score: List[List[int]], k: int) -> List[List[int]]: """ There is a class with m students and n exams. You are given a 0-indexed m x n integer matrix score, where each row represents one student and score[i][j] denotes the score the ith student got in the jth exam. The matrix score contains d...
apply-bitwise-operations-to-make-strings-equal
def makeStringsEqual(s: str, target: str) -> bool: """ You are given two 0-indexed binary strings s and target of the same length n. You can do the following operation on s any number of times: Choose two different indices i and j where 0 <= i, j < n. Simultaneously, replace s[i] with (s[i] OR s[j]...
minimum-cost-to-split-an-array
def minCost(nums: List[int], k: int) -> int: """ You are given an integer array nums and an integer k. Split the array into some number of non-empty subarrays. The cost of a split is the sum of the importance value of each subarray in the split. Let trimmed(subarray) be the version of the subarray where...
maximum-price-to-fill-a-bag
def maxPrice(items: List[List[int]], capacity: int) -> float: """ You are given a 2D integer array items where items[i] = [pricei, weighti] denotes the price and weight of the ith item, respectively. You are also given a positive integer capacity. Each item can be divided into two items with ratios part...
count-distinct-numbers-on-board
def distinctIntegers(n: int) -> int: """ You are given a positive integer n, that is initially placed on a board. Every day, for 109 days, you perform the following procedure: For each number x present on the board, find all numbers 1 <= i <= n such that x % i == 1. Then, place those numbers on the...
put-marbles-in-bags
def putMarbles(weights: List[int], k: int) -> int: """ You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k. Divide the marbles into the k bags according to the following rules: No bag is empty. If th...
count-increasing-quadruplets
def countQuadruplets(nums: List[int]) -> int: """ Given a 0-indexed integer array nums of size n containing all numbers from 1 to n, return the number of increasing quadruplets. A quadruplet (i, j, k, l) is increasing if: 0 <= i < j < k < l < n, and nums[i] < nums[k] < nums[j] < nums[l]. ...
separate-the-digits-in-an-array
def separateDigits(nums: List[int]) -> List[int]: """ Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums. To separate the digits of an integer is to get all the digits it has in the ...
maximum-number-of-integers-to-choose-from-a-range-i
def maxCount(banned: List[int], n: int, maxSum: int) -> int: """ You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules: The chosen integers have to be in the range [1, n]. Each integer can be chosen at most once. ...
maximize-win-from-two-segments
def maximizeWin(prizePositions: List[int], k: int) -> int: """ There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. Yo...
disconnect-path-in-a-binary-matrix-by-at-most-one-flip
def isPossibleToCutPath(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) that has the value 1. The matrix is disconnected if there is no path from (0, 0) to (m - 1, n - 1). You can...
maximum-number-of-integers-to-choose-from-a-range-ii
def maxCount(banned: List[int], n: int, maxSum: int) -> int: """ You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules: The chosen integers have to be in the range [1, n]. Each integer can be chosen at most once. ...
take-gifts-from-the-richest-pile
def pickGifts(gifts: List[int], k: int) -> int: """ You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following: Choose the pile with the maximum number of gifts. If there is more than one pile with the maximum number of gifts, choose any. ...
count-vowel-strings-in-ranges
def vowelStrings(words: List[str], queries: List[List[int]]) -> List[int]: """ You are given a 0-indexed array of strings words and a 2D array of integers queries. Each query queries[i] = [li, ri] asks us to find the number of strings present at the indices ranging from li to ri (both inclusive) of words th...
house-robber-iv
def minCapability(nums: List[int], k: int) -> int: """ There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he refuses to steal from adjacent homes. The capability of the robber is the maximum amount of...
rearranging-fruits
def minCost(basket1: List[int], basket2: List[int]) -> int: """ You have two fruit baskets containing n fruits each. You are given two 0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal. To do so, you can use the following operation as...
find-the-array-concatenation-value
def findTheArrayConcVal(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. The concatenation of two numbers is the number formed by concatenating their numerals. For example, the concatenation of 15, 49 is 1549. The concatenation value of nums is initially equal to ...
count-the-number-of-fair-pairs
def countFairPairs(nums: List[int], lower: int, upper: int) -> int: """ Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs. A pair (i, j) is fair if: 0 <= i < j < n, and lower <= nums[i] + nums[j] <= upper Example 1: ...
substring-xor-queries
def substringXorQueries(s: str, queries: List[List[int]]) -> List[List[int]]: """ You are given a binary string s, and a 2D integer array queries where queries[i] = [firsti, secondi]. For the ith query, find the shortest substring of s whose decimal value, val, yields secondi when bitwise XORed with firsti....
subsequence-with-the-minimum-score
def minimumScore(s: str, t: str) -> int: """ You are given two strings s and t. You are allowed to remove any number of characters from the string t. The score of the string is 0 if no characters are removed from the string t, otherwise: Let left be the minimum index among all removed character...
maximum-difference-by-remapping-a-digit
def minMaxDifference(num: int) -> int: """ You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit. Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num. Notes: When Bo...
minimum-score-by-changing-two-elements
def minimizeSum(nums: List[int]) -> int: """ You are given an integer array nums. The low score of nums is the minimum absolute difference between any two integers. The high score of nums is the maximum absolute difference between any two integers. The score of nums is the sum of the high and l...
minimum-impossible-or
def minImpossibleOR(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. We say that an integer x is expressible from nums if there exist some integers 0 <= index1 < index2 < ... < indexk < nums.length for which nums[index1] | nums[index2] | ... | nums[indexk] = x. In other words, an i...
handling-sum-queries-after-update
def handleQuery(nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]: """ You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries: For a query of type 1, queries[i] = [1, l, r]. Flip the values from 0 to 1 and from 1 t...
merge-two-2d-arrays-by-summing-values
def mergeArrays(nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]: """ You are given two 2D integer arrays nums1 and nums2. nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. nums2[i] = [idi, vali] indicate that the number with the id idi has...
minimum-operations-to-reduce-an-integer-to-0
def minOperations(n: int) -> int: """ You are given a positive integer n, you can do the following operation any number of times: Add or subtract a power of 2 from n. Return the minimum number of operations to make n equal to 0. A number x is power of 2 if x == 2i where i >= 0. Ex...
count-the-number-of-square-free-subsets
def squareFreeSubsets(nums: List[int]) -> int: """ You are given a positive integer 0-indexed array nums. A subset of the array nums is square-free if the product of its elements is a square-free integer. A square-free integer is an integer that is divisible by no square number other than 1. Return ...
find-the-string-with-lcp
def findTheString(lcp: List[List[int]]) -> str: """ We define the lcp matrix of any 0-indexed string word of n lowercase English letters as an n x n grid such that: lcp[i][j] is equal to the length of the longest common prefix between the substrings word[i,n-1] and word[j,n-1]. Given an n x n ...
left-and-right-sum-differences
def leftRightDifference(nums: List[int]) -> List[int]: """ You are given a 0-indexed integer array nums of size n. Define two arrays leftSum and rightSum where: leftSum[i] is the sum of elements to the left of the index i in the array nums. If there is no such element, leftSum[i] = 0. rightSum[...
find-the-divisibility-array-of-a-string
def divisibilityArray(word: str, m: int) -> List[int]: """ You are given a 0-indexed string word of length n consisting of digits, and a positive integer m. The divisibility array div of word is an integer array of length n such that: div[i] = 1 if the numeric value of word[0,...,i] is divisible by...
find-the-maximum-number-of-marked-indices
def maxNumOfMarkedIndices(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. Initially, all of the indices are unmarked. You are allowed to make this operation any number of times: Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and ...
minimum-time-to-visit-a-cell-in-a-grid
def minimumTime(grid: List[List[int]]) -> int: """ You are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater tha...
split-with-minimum-sum
def splitNum(num: int) -> int: """ Given a positive integer num, split it into two non-negative integers num1 and num2 such that: The concatenation of num1 and num2 is a permutation of num. In other words, the sum of the number of occurrences of each digit in num1 and num2 is equal to the...
count-total-number-of-colored-cells
def coloredCells(n: int) -> int: """ There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer n, indicating that you must do the following routine for n minutes: At the first minute, color any arbitrary unit cell blue. Every minute thereafter, ...
count-number-of-possible-root-nodes
def rootCount(edges: List[List[int]], guesses: List[List[int]], k: int) -> int: """ Alice has an undirected tree with n nodes labeled from 0 to n - 1. The tree is represented as 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 tree...
pass-the-pillow
def passThePillow(n: int, time: int) -> int: """ There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the ...
kth-largest-sum-in-a-binary-tree
# class TreeNode: # def __init__(val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def kthLargestLevelSum(root: Optional[TreeNode], k: int) -> int: """ You are given the root of a binary tree and a positive integer k. The ...
split-the-array-to-make-coprime-products
def findValidSplit(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums of length n. A split at an index i where 0 <= i <= n - 2 is called valid if the product of the first i + 1 elements and the product of the remaining elements are coprime. For example, if nums = [2, 3, 3], t...
number-of-ways-to-earn-points
def waysToReachTarget(target: int, types: List[List[int]]) -> int: """ There is a test that has n types of questions. You are given an integer target and a 0-indexed 2D integer array types where types[i] = [counti, marksi] indicates that there are counti questions of the ith type, and each one of them is worth ...
count-the-number-of-vowel-strings-in-range
def vowelStrings(words: List[str], left: int, right: int) -> int: """ You are given a 0-indexed array of string words and two integers left and right. A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'....
rearrange-array-to-maximize-prefix-score
def maxScore(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the given order). Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from ...
count-the-number-of-beautiful-subarrays
def beautifulSubarrays(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. In one operation, you can: Choose two different indices i and j such that 0 <= i, j < nums.length. Choose a non-negative integer k such that the kth bit (0-indexed) in the binary representation of nums...
minimum-time-to-complete-all-tasks
def findMinimumTime(tasks: List[List[int]]) -> int: """ There is a computer that can run an unlimited number of tasks at the same time. You are given a 2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the ith task should run for a total of durationi seconds (not necessarily conti...
distribute-money-to-maximum-children
def distMoney(money: int, children: int) -> int: """ You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: ...
maximize-greatness-of-an-array
def maximizeGreatness(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. You are allowed to permute nums into a new array perm of your choosing. We define the greatness of nums be the number of indices 0 <= i < nums.length for which perm[i] > nums[i]. Return the maximum possible ...
find-score-of-an-array-after-marking-all-elements
def findScore(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. Starting with score = 0, apply the following algorithm: Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. Add the value o...
minimum-time-to-repair-cars
def repairCars(ranks: List[int], cars: int) -> int: """ You are given an integer array ranks representing the ranks of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank r can repair n cars in r * n2 minutes. You are also given an integer cars representing the total number of car...
number-of-even-and-odd-bits
def evenOddBit(n: int) -> List[int]: """ You are given a positive integer n. Let even denote the number of even indices in the binary representation of n with value 1. Let odd denote the number of odd indices in the binary representation of n with value 1. Note that bits are indexed from right to le...
check-knight-tour-configuration
def checkValidGrid(grid: List[List[int]]) -> bool: """ There is a knight on an n x n chessboard. In a valid configuration, the knight starts at the top-left cell of the board and visits every cell on the board exactly once. You are given an n x n integer matrix grid consisting of distinct integers from the ...
the-number-of-beautiful-subsets
def beautifulSubsets(nums: List[int], k: int) -> int: """ You are given an array nums of positive integers and a positive integer k. A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k. Return the number of non-empty beautiful subsets of the array num...
smallest-missing-non-negative-integer-after-operations
def findSmallestInteger(nums: List[int], value: int) -> int: """ You are given a 0-indexed integer array nums and an integer value. In one operation, you can add or subtract value from any element of nums. For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] t...
make-the-prefix-sum-non-negative
def makePrefSumNonNegative(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. You can apply the following operation any number of times: Pick any element from nums and put it at the end of nums. The prefix sum array of nums is an array prefix of the same length as nums ...
k-items-with-the-maximum-sum
def kItemsWithMaximumSum(numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int: """ There is a bag that consists of items, each item has a number 1, 0, or -1 written on it. You are given four non-negative integers numOnes, numZeros, numNegOnes, and k. The bag initially contains: numOnes ...
prime-subtraction-operation
def primeSubOperation(nums: List[int]) -> bool: """ You are given a 0-indexed integer array nums of length n. You can perform the following operation as many times as you want: Pick an index i that you haven’t picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i...
minimum-operations-to-make-all-array-elements-equal
def minOperations(nums: List[int], queries: List[int]) -> List[int]: """ You are given an array nums consisting of positive integers. You are also given an integer array queries of size m. For the ith query, you want to make all of the elements of nums equal to queries[i]. You can perform the following oper...
collect-coins-in-a-tree
def collectTheCoins(coins: List[int], edges: List[List[int]]) -> int: """ There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given an integer n and 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 ...
minimum-time-to-eat-all-grains
def minimumTime(hens: List[int], grains: List[int]) -> int: """ There are n hens and m grains on a line. You are given the initial positions of the hens and the grains in two integer arrays hens and grains of size n and m respectively. Any hen can eat a grain if they are on the same position. The time taken...
form-smallest-number-from-two-digit-arrays
def minNumber(nums1: List[int], nums2: List[int]) -> int: """ Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array. Example 1: >>> minNumber(nums1 = [4,1,3], nums2 = [5,7]) >>> 15 Explanation: The number 15 conta...
find-the-substring-with-maximum-cost
def maximumCostSubstring(s: str, chars: str, vals: List[int]) -> int: """ You are given a string s, a string chars of distinct characters and an integer array vals of the same length as chars. The cost of the substring is the sum of the values of each character in the substring. The cost of an empty string ...
make-k-subarray-sums-equal
def makeSubKSumEqual(arr: List[int], k: int) -> int: """ You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element...
shortest-cycle-in-a-graph
def findShortestCycle(n: int, edges: List[List[int]]) -> int: """ There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1. The edges in the graph are represented by a given 2D integer array edges, where edges[i] = [ui, vi] denotes an edge between vertex ui and vertex vi. Ev...
find-the-longest-balanced-substring-of-a-binary-string
def findTheLongestBalancedSubstring(s: str) -> int: """ You are given a binary string s consisting only of zeroes and ones. A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is c...
convert-an-array-into-a-2d-array-with-conditions
def findMatrix(nums: List[int]) -> List[List[int]]: """ You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions: The 2D array should contain only the elements of the array nums. Each row in the 2D array contains distinct integers. The num...
mice-and-cheese
def miceAndCheese(reward1: List[int], reward2: List[int], k: int) -> int: """ There are two mice and n different types of cheese, each type of cheese should be eaten by exactly one mouse. A point of the cheese with index i (0-indexed) is: reward1[i] if the first mouse eats it. reward2[i] if the...
beautiful-pairs
def beautifulPair(nums1: List[int], nums2: List[int]) -> List[int]: """ You are given two 0-indexed integer arrays nums1 and nums2 of the same length. A pair of indices (i,j) is called beautiful if|nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is the smallest amongst all possible indices pairs where i < j. R...
prime-in-diagonal
def diagonalPrime(nums: List[List[int]]) -> int: """ You are given a 0-indexed two-dimensional integer array nums. Return the largest prime number that lies on at least one of the diagonals of nums. In case, no prime is present on any of the diagonals, return 0. Note that: An integer is prime i...
sum-of-distances
def distance(nums: List[int]) -> List[int]: """ You are given a 0-indexed integer array nums. There exists an array arr of length nums.length, where arr[i] is the sum of |i - j| over all j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i] to be 0. Return the array arr. Exam...
minimize-the-maximum-difference-of-pairs
def minimizeMax(nums: List[int], p: int) -> int: """ You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of e...
minimum-number-of-visited-cells-in-a-grid
def minimumVisitedCells(grid: List[List[int]]) -> int: """ You are given a 0-indexed m x n integer matrix grid. Your initial position is at the top-left cell (0, 0). Starting from the cell (i, j), you can move to one of the following cells: Cells (i, k) with j < k <= grid[i][j] + j (rightward movem...
count-the-number-of-k-free-subsets
def countTheNumOfKFreeSubsets(nums: List[int], k: int) -> int: """ You are given an integer array nums, which contains distinct elements and an integer k. A subset is called a k-Free subset if it contains no two elements with an absolute difference equal to k. Notice that the empty set is a k-Free subset. ...
find-the-width-of-columns-of-a-grid
def findColumnWidth(grid: List[List[int]]) -> List[int]: """ You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers. For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3. Return an intege...
find-the-score-of-all-prefixes-of-an-array
def findPrefixScore(nums: List[int]) -> List[int]: """ We define the conversion array conver of an array arr as follows: conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i. We also define the score of an array arr as the sum of the values o...
cousins-in-binary-tree-ii
# class TreeNode: # def __init__(val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def replaceValueInTree(root: Optional[TreeNode]) -> Optional[TreeNode]: """ Given the root of a binary tree, replace the value of each node in ...
row-with-maximum-ones
def rowAndMaximumOnes(mat: List[List[int]]) -> List[int]: """ Given a m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row. In case there are multiple rows that have the maximum count of ones, the row with the smallest ro...
find-the-maximum-divisibility-score
def maxDivScore(nums: List[int], divisors: List[int]) -> int: """ You are given two integer arrays nums and divisors. The divisibility score of divisors[i] is the number of indices j such that nums[j] is divisible by divisors[i]. Return the integer divisors[i] with the maximum divisibility score. If mul...
minimum-additions-to-make-valid-string
def addMinimum(word: str) -> int: """ Given a string word to which you can insert letters "a", "b" or "c" anywhere and any number of times, return the minimum number of letters that must be inserted so that word becomes valid. A string is called valid if it can be formed by concatenating the string "abc" se...
minimize-the-total-price-of-the-trips
def minimumTotalPrice(n: int, edges: List[List[int]], price: List[int], trips: List[List[int]]) -> int: """ There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there ...
color-the-triangle-red
def colorRed(n: int) -> List[List[int]]: """ You are given an integer n. Consider an equilateral triangle of side length n, broken up into n2 unit equilateral triangles. The triangle has n 1-indexed rows where the ith row has 2i - 1 unit equilateral triangles. The triangles in the ith row are also 1-indexed...
calculate-delayed-arrival-time
def findDelayedArrivalTime(arrivalTime: int, delayedTime: int) -> int: """ You are given a positive integer arrivalTime denoting the arrival time of a train in hours, and another positive integer delayedTime denoting the amount of delay in hours. Return the time when the train will arrive at the station. ...
sum-multiples
def sumOfMultiples(n: int) -> int: """ Given a positive integer n, find the sum of all integers in the range [1, n] inclusive that are divisible by 3, 5, or 7. Return an integer denoting the sum of all numbers in the given range satisfying the constraint. Example 1: >>> sumOfMultiples(n = ...
sliding-subarray-beauty
def getSubarrayBeauty(nums: List[int], k: int, x: int) -> List[int]: """ Given an integer array nums containing n integers, find the beauty of each subarray of size k. The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers. ...
minimum-number-of-operations-to-make-all-array-elements-equal-to-1
def minOperations(nums: List[int]) -> int: """ You are given a 0-indexed array nums consisiting of positive integers. You can do the following operation on the array any number of times: Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value. ...
find-maximal-uncovered-ranges
def findMaximalUncoveredRanges(n: int, ranges: List[List[int]]) -> List[List[int]]: """ You are given an integer n which is the length of a 0-indexed array nums, and a 0-indexed 2D-array ranges, which is a list of sub-ranges of nums (sub-ranges may overlap). Each row ranges[i] has exactly 2 cells: ...
maximum-sum-with-exactly-k-elements
def maximizeSum(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums and an integer k. Your task is to perform the following operation exactly k times in order to maximize your score: Select an element m from nums. Remove the selected element m from the array. Add a...
find-the-prefix-common-array-of-two-arrays
def findThePrefixCommonArray(A: List[int], B: List[int]) -> List[int]: """ You are given two 0-indexed integer permutations A and B of length n. A prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B. Retur...
maximum-number-of-fish-in-a-grid
def findMaxFish(grid: List[List[int]]) -> int: """ You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents: A land cell if grid[r][c] = 0, or A water cell containing grid[r][c] fish, if grid[r][c] > 0. A fisher can start at any water cell (r, c) and can do the follo...
make-array-empty
def countOperationsToEmptyArray(nums: List[int]) -> int: """ You are given an integer array nums containing distinct numbers, and you can perform the following operations until the array is empty: If the first element has the smallest value, remove it Otherwise, put the first element at the end of ...
determine-the-winner-of-a-bowling-game
def isWinner(player1: List[int], player2: List[int]) -> int: """ You are given two 0-indexed integer arrays player1 and player2, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling game consists of n turns, and the number of pins in each turn is exactl...
first-completely-painted-row-or-column
def firstCompleteIndex(arr: List[int], mat: List[List[int]]) -> int: """ You are given a 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n]. Go through each index i in arr starting from index 0 and paint the cell in mat containing ...
minimum-cost-of-a-path-with-special-roads
def minimumCost(start: List[int], target: List[int], specialRoads: List[List[int]]) -> int: """ You are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space. You are also given the array target where target = [targetX, targetY] represents your targe...
lexicographically-smallest-beautiful-string
def smallestBeautifulString(s: str, k: int) -> str: """ A string is beautiful if: It consists of the first k letters of the English lowercase alphabet. It does not contain any substring of length 2 or more which is a palindrome. You are given a beautiful string s of length n and a positive...
the-knights-tour
def tourOfKnight(m: int, n: int, r: int, c: int) -> List[List[int]]: """ Given two positive integers m and n which are the height and width of a 0-indexed 2D-array board, a pair of positive integers (r, c) which is the starting position of the knight on the board. Your task is to find an order of movements ...