task_id
stringlengths
3
79
prompt
stringlengths
255
3.9k
find-the-distinct-difference-array
def distinctDifferenceArray(nums: List[int]) -> List[int]: """ You are given a 0-indexed array nums of length n. The distinct difference array of nums is an array diff of length n such that diff[i] is equal to the number of distinct elements in the suffix nums[i + 1, ..., n - 1] subtracted from the number o...
number-of-adjacent-elements-with-the-same-color
def colorTheArray(n: int, queries: List[List[int]]) -> List[int]: """ You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori]. For the ith query: Set colors...
make-costs-of-paths-equal-in-a-binary-tree
def minIncrements(n: int, cost: List[int]) -> int: """ You are given an integer n representing the number of nodes in a perfect binary tree consisting of nodes numbered from 1 to n. The root of the tree is node 1 and each node i in the tree has two children where the left child is the node 2 * i and the right c...
number-of-senior-citizens
def countSeniors(details: List[str]) -> int: """ You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that: The first ten characters consist of the phone number of passengers. ...
sum-in-a-matrix
def matrixSum(nums: List[List[int]]) -> int: """ You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty: From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter w...
maximum-or
def maximumOr(nums: List[int], k: int) -> int: """ You are given a 0-indexed integer array nums of length n and an integer k. In an operation, you can choose an element and multiply it by 2. Return the maximum possible value of nums[0] | nums[1] | ... | nums[n - 1] that can be obtained after applying the op...
power-of-heroes
def sumOfPower(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums representing the strength of some heroes. The power of a group of heroes is defined as follows: Let i0, i1, ... ,ik be the indices of the heroes in a group. Then, the power of this group is max(nums[i0], nums[i1], ...
find-the-losers-of-the-circular-game
def circularGameLosers(n: int, k: int) -> List[int]: """ There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise fro...
neighboring-bitwise-xor
def doesValidArrayExist(derived: List[int]) -> bool: """ A 0-indexed array derived with length n is derived by computing the bitwise XOR (⊕) of adjacent values in a binary array original of length n. Specifically, for each index i in the range [0, n - 1]: If i = n - 1, then derived[i] = original[i]...
maximum-number-of-moves-in-a-grid
def maxMoves(grid: List[List[int]]) -> int: """ You are given a 0-indexed m x n matrix grid consisting of positive integers. You can start at any cell in the first column of the matrix, and traverse the grid in the following way: From a cell (row, col), you can move to any of the cells: (row - 1, c...
count-the-number-of-complete-components
def countCompleteComponents(n: int, edges: List[List[int]]) -> int: """ You are given an integer n. There is an undirected graph with n vertices, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting vertices ai and bi....
minimum-string-length-after-removing-substrings
def minLength(s: str) -> int: """ You are given a string s consisting only of uppercase English letters. You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s. Return the minimum possible length of the resulting s...
lexicographically-smallest-palindrome
def makeSmallestPalindrome(s: str) -> str: """ You are given a string s consisting of lowercase English letters, and you are allowed to perform operations on it. In one operation, you can replace a character in s with another lowercase English letter. Your task is to make s a palindrome with the minimum num...
find-the-punishment-number-of-an-integer
def punishmentNumber(n: int) -> int: """ Given a positive integer n, return the punishment number of n. The punishment number of n is defined as the sum of the squares of all integers i such that: 1 <= i <= n The decimal representation of i * i can be partitioned into contiguous substrings such...
modify-graph-edge-weights
def modifiedGraphEdges(n: int, edges: List[List[int]], source: int, destination: int, target: int) -> List[List[int]]: """ You are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is an edge bet...
minimum-operations-to-make-numbers-non-positive
def minOperations(nums: List[int], x: int, y: int) -> int: """ You are given a 0-indexed integer array nums and two integers x and y. In one operation, you must choose an index i such that 0 <= i < nums.length and perform the following: Decrement nums[i] by x. Decrement values by y at all indices e...
buy-two-chocolates
def buyChoco(prices: List[int], money: int) -> int: """ You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money. You must buy exactly two chocolates in such a way that you still h...
extra-characters-in-a-string
def minExtraChar(s: str, dictionary: List[str]) -> int: """ You are given a 0-indexed string s and a dictionary of words dictionary. You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary. There may be some extra characters in s which are not present in...
maximum-strength-of-a-group
def maxStrength(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums representing the score of students in an exam. The teacher would like to form one non-empty group of students with maximal strength, where the strength of a group of students of indices i0, i1, i2, ... , ik is defined as n...
greatest-common-divisor-traversal
def canTraverseAllPairs(nums: List[int]) -> bool: """ You are given a 0-indexed integer array nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor. Your task is to d...
remove-trailing-zeros-from-a-string
def removeTrailingZeros(num: str) -> str: """ Given a positive integer num represented as a string, return the integer num without trailing zeros as a string. Example 1: >>> removeTrailingZeros(num = "51230100") >>> "512301" Explanation: Integer "51230100" has 2 trailing zeros, we remo...
difference-of-number-of-distinct-values-on-diagonals
def differenceOfDistinctValues(grid: List[List[int]]) -> List[List[int]]: """ Given a 2D grid of size m x n, you should find the matrix answer of size m x n. The cell answer[r][c] is calculated by looking at the diagonal values of the cell grid[r][c]: Let leftAbove[r][c] be the number of distinct v...
minimum-cost-to-make-all-characters-equal
def minimumCost(s: str) -> int: """ You are given a 0-indexed binary string s of length n on which you can apply two types of operations: Choose an index i and invert all characters from index 0 to index i (both inclusive), with a cost of i + 1 Choose an index i and invert all characters from index...
maximum-strictly-increasing-cells-in-a-matrix
def maxIncreasingCells(mat: List[List[int]]) -> int: """ Given a 1-indexed m x n integer matrix mat, you can select any cell in the matrix as your starting cell. From the starting cell, you can move to any other cell in the same row or column, but only if the value of the destination cell is strictly greate...
find-shortest-path-with-k-hops
def shortestPathWithHops(n: int, edges: List[List[int]], s: int, d: int, k: int) -> int: """ You are given a positive integer n which is the number of nodes of a 0-indexed undirected weighted connected graph and a 0-indexed 2D array edges where edges[i] = [ui, vi, wi] indicates that there is an edge between nod...
minimize-string-length
def minimizedStringLength(s: str) -> int: """ Given a string s, you have two types of operation: Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists). Choose an index i in the string, and let c be the character in...
semi-ordered-permutation
def semiOrderedPermutation(nums: List[int]) -> int: """ You are given a 0-indexed permutation of n integers nums. A permutation is called semi-ordered if the first number equals 1 and the last number equals n. You can perform the below operation as many times as you want until you make nums a semi-ordered p...
sum-of-matrix-after-queries
def matrixSumQueries(n: int, queries: List[List[int]]) -> int: """ You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali]. Initially, there is a 0-indexed n x n matrix filled with 0's. For each query, you must apply one of the following changes: if type...
count-of-integers
def count(num1: str, num2: str, min_sum: int, max_sum: int) -> int: """ You are given two numeric strings num1 and num2 and two integers max_sum and min_sum. We denote an integer x to be good if: num1 <= x <= num2 min_sum <= digit_sum(x) <= max_sum. Return the number of good integers. Sinc...
check-if-the-number-is-fascinating
def isFascinating(n: int) -> bool: """ You are given an integer n that consists of exactly 3 digits. We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0's: Concatenate n with the numb...
find-the-longest-semi-repetitive-substring
def longestSemiRepetitiveSubstring(s: str) -> int: """ You are given a digit string s that consists of digits from 0 to 9. A string is called semi-repetitive if there is at most one adjacent pair of the same digit. For example, "0010", "002020", "0123", "2002", and "54944" are semi-repetitive while the foll...
movement-of-robots
def sumDistance(nums: List[int], s: str, d: int) -> int: """ Some robots are standing on an infinite number line with their initial coordinates given by a 0-indexed integer array nums and will start moving once given the command to move. The robots will move a unit distance each second. You are given a stri...
find-a-good-subset-of-the-matrix
def goodSubsetofBinaryMatrix(grid: List[List[int]]) -> List[int]: """ You are given a 0-indexed m x n binary matrix grid. Let us call a non-empty subset of rows good if the sum of each column of the subset is at most half of the length of the subset. More formally, if the length of the chosen subset of ...
neither-minimum-nor-maximum
def findNonMinOrMax(nums: List[int]) -> int: """ Given an integer array nums containing distinct positive integers, find and return any number from the array that is neither the minimum nor the maximum value in the array, or -1 if there is no such number. Return the selected integer. Example 1: ...
lexicographically-smallest-string-after-substring-operation
def smallestString(s: str) -> str: """ Given a string s consisting of lowercase English letters. Perform the following operation: Select any non-empty substring then replace every letter of the substring with the preceding letter of the English alphabet. For example, 'b' is converted to 'a', and 'a' is...
collecting-chocolates
def minCost(nums: List[int], x: int) -> int: """ You are given a 0-indexed integer array nums of size n representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index i is nums[i]. Each chocolate is of a different type, and initially, the chocolate at the index i is ...
maximum-sum-queries
def maximumSumQueries(nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]: """ You are given two 0-indexed integer arrays nums1 and nums2, each of length n, and a 1-indexed 2D array queries where queries[i] = [xi, yi]. For the ith query, find the maximum value of nums1[j] + nums2[j] a...
find-the-closest-marked-node
def minimumDistance(n: int, edges: List[List[int]], s: int, marked: List[int]) -> int: """ You are given a positive integer n which is the number of nodes of a 0-indexed directed weighted graph and a 0-indexed 2D array edges where edges[i] = [ui, vi, wi] indicates that there is an edge from node ui to node vi w...
total-distance-traveled
def distanceTraveled(mainTank: int, additionalTank: int) -> int: """ A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters. The truck has a mileage of 10 km p...
find-the-value-of-the-partition
def findValueOfPartition(nums: List[int]) -> int: """ You are given a positive integer array nums. Partition nums into two arrays, nums1 and nums2, such that: Each element of the array nums belongs to either the array nums1 or the array nums2. Both arrays are non-empty. The value of the par...
special-permutations
def specialPerm(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums containing n distinct positive integers. A permutation of nums is called special if: For all indexes 0 <= i < n - 1, either nums[i] % nums[i+1] == 0 or nums[i+1] % nums[i] == 0. Return the total number of...
painting-the-walls
def paintWalls(cost: List[int], time: List[int]) -> int: """ You are given two 0-indexed integer arrays, cost and time, of size n representing the costs and the time taken to paint n different walls respectively. There are two painters available: A paid painter that paints the ith wall in time[i] units...
count-substrings-without-repeating-character
def numberOfSpecialSubstrings(s: str) -> int: """ You are given a string s consisting only of lowercase English letters. We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character). Your task is to count the number of spec...
find-maximum-number-of-string-pairs
def maximumNumberOfStringPairs(words: List[str]) -> int: """ You are given a 0-indexed array words consisting of distinct strings. The string words[i] can be paired with the string words[j] if: The string words[i] is equal to the reversed string of words[j]. 0 <= i < j < words.length. ...
construct-the-longest-new-string
def longestString(x: int, y: int, z: int) -> int: """ You are given three integers x, y, and z. You have x strings equal to "AA", y strings equal to "BB", and z strings equal to "AB". You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. Thi...
decremental-string-concatenation
def minimizeConcatenatedLength(words: List[str]) -> int: """ You are given a 0-indexed array words containing n strings. Let's define a join operation join(x, y) between two strings x and y as concatenating them into xy. However, if the last character of x is equal to the first character of y, one of them i...
count-zero-request-servers
def countServers(n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]: """ You are given an integer n denoting the total number of servers and a 2D 0-indexed integer array logs, where logs[i] = [server_id, time] denotes that the server with id server_id received a request at time time. Yo...
number-of-beautiful-pairs
def countBeautifulPairs(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. A pair of indices i, j where 0 <= i < j < nums.length is called beautiful if the first digit of nums[i] and the last digit of nums[j] are coprime. Return the total number of beautiful pairs in nums. Two in...
minimum-operations-to-make-the-integer-zero
def makeTheIntegerZero(num1: int, num2: int) -> int: """ You are given two integers num1 and num2. In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1. Return the integer denoting the minimum number of operations needed to make num1 equal to 0. If it is i...
ways-to-split-array-into-good-subarrays
def numberOfGoodSubarraySplits(nums: List[int]) -> int: """ You are given a binary array nums. A subarray of an array is good if it contains exactly one element with the value 1. Return an integer denoting the number of ways to split the array nums into good subarrays. As the number may be too large, re...
robot-collisions
def survivedRobotsHealths(positions: List[int], healths: List[int], directions: str) -> List[int]: """ There are n 1-indexed robots, each having a position on a line, health, and movement direction. You are given 0-indexed integer arrays positions, healths, and a string directions (directions[i] is either '...
longest-even-odd-subarray-with-threshold
def longestAlternatingSubarray(nums: List[int], threshold: int) -> int: """ You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: ...
prime-pairs-with-target-sum
def findPrimePairs(n: int) -> List[List[int]]: """ You are given an integer n. We say that two integers x and y form a prime number pair if: 1 <= x <= y <= n x + y == n x and y are prime numbers Return the 2D sorted list of prime number pairs [xi, yi]. The list should be sorted in incr...
continuous-subarrays
def continuousSubarrays(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total...
sum-of-imbalance-numbers-of-all-subarrays
def sumImbalanceNumbers(nums: List[int]) -> int: """ The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: 0 <= i < n - 1, and sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted ...
is-array-a-preorder-of-some-binary-tree
def isPreorder(nodes: List[List[int]]) -> bool: """ Given a 0-indexed integer 2D array nodes, your task is to determine if the given array represents the preorder traversal of some binary tree. For each index i, nodes[i] = [id, parentId], where id is the id of the node at the index i and parentId is the id ...
longest-alternating-subarray
def alternatingSubarray(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if: m is greater than 1. s1 = s0 + 1. The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1,...
relocate-marbles
def relocateMarbles(nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]: """ You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length. Throughout moveFrom.length step...
partition-string-into-minimum-beautiful-substrings
def minimumBeautifulSubstrings(s: str) -> int: """ Given a binary string s, partition the string into one or more substrings such that each substring is beautiful. A string is beautiful if: It doesn't contain leading zeros. It's the binary representation of a number that is a power of 5. ...
number-of-black-blocks
def countBlackBlocks(m: int, n: int, coordinates: List[List[int]]) -> List[int]: """ You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates ...
find-the-maximum-achievable-number
def theMaximumAchievableX(num: int, t: int) -> int: """ Given two integers, num and t. A number x is achievable if it can become equal to num after applying the following operation at most t times: Increase or decrease x by 1, and simultaneously increase or decrease num by 1. Return the maximu...
maximum-number-of-jumps-to-reach-the-last-index
def maximumJumps(nums: List[int], target: int) -> int: """ You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: 0 <= i < j < n -target <= nums[j] - nums[i] <= target ...
longest-non-decreasing-subarray-from-two-arrays
def maxNonDecreasingLength(nums1: List[int], nums2: List[int]) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i...
apply-operations-to-make-all-array-elements-equal-to-zero
def checkArray(nums: List[int], k: int) -> bool: """ 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 subarray of size k from the array and decrease all its elements by 1. Return true i...
height-of-special-binary-tree
# class TreeNode: # def __init__(val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def heightOfTree(root: Optional[TreeNode]) -> int: """ You are given a root, which is the root of a special binary tree with n nodes. The nodes...
sum-of-squares-of-special-elements
def sumOfSquares(nums: List[int]) -> int: """ You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums. Example 1: >>> sumOfSquares(nums = [1,2,3...
maximum-beauty-of-an-array-after-applying-operation
def maximumBeauty(nums: List[int], k: int) -> int: """ You are given a 0-indexed array nums and a non-negative integer k. In one operation, you can do the following: Choose an index i that hasn't been chosen before from the range [0, nums.length - 1]. Replace nums[i] with any integer from the r...
minimum-index-of-a-valid-split
def minimumIndex(nums: List[int]) -> int: """ An element x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x. You are given a 0-indexed integer array nums of length n with one dominant element. You can split nums at an index i into two arrays nums[0,...
length-of-the-longest-valid-substring
def longestValidSubstring(word: str, forbidden: List[str]) -> int: """ You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a con...
number-of-unique-categories
# class CategoryHandler: # def haveSameCategory(a: int, b: int) -> bool: # pass class Solution: def numberOfCategories(n: int, categoryHandler: Optional['CategoryHandler']) -> int: """ You are given an integer n and an object categoryHandler of class CategoryHandler. There are n elements, nu...
check-if-array-is-good
def isGood(nums: List[int]) -> bool: """ You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For examp...
sort-vowels-in-a-string
def sortVowels(s: str) -> str: """ Given a 0-indexed string s, permute s to get a new string t such that: All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. The vowels must be sorted in the no...
visit-array-positions-to-maximize-score
def maxScore(nums: List[int], x: int) -> int: """ You are given a 0-indexed integer array nums and a positive integer x. You are initially at position 0 in the array and you can visit other positions according to the following rules: If you are currently in position i, then you can move to any posi...
ways-to-express-an-integer-as-sum-of-powers
def numberOfWays(n: int, x: int) -> int: """ Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Since the resul...
split-strings-by-separator
def splitWordsBySeparator(words: List[str], separator: str) -> List[str]: """ Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes separat...
largest-element-in-an-array-after-merge-operations
def maxArrayValue(nums: List[int]) -> int: """ You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1...
maximum-number-of-groups-with-increasing-length
def maxIncreasingGroups(usageLimits: List[int]) -> int: """ You are given a 0-indexed array usageLimits of length n. Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the foll...
count-paths-that-can-form-a-palindrome-in-a-tree
def countPalindromePaths(parent: List[int], s: str) -> int: """ You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Si...
count-nodes-that-are-great-enough
# class TreeNode: # def __init__(val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def countGreatEnoughNodes(root: Optional[TreeNode], k: int) -> int: """ You are given a root to a binary tree and an integer k. A node of this ...
number-of-employees-who-met-the-target
def numberOfEmployeesWhoMetTarget(hours: List[int], target: int) -> int: """ There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company. The company requires each employee to work for at least target hours. You are given a 0-indexed array o...
count-complete-subarrays-in-an-array
def countCompleteSubarrays(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: The number of distinct elements in the subarray is equal to the number of distinct elements in the wh...
shortest-string-that-contains-three-strings
def minimumString(a: str, b: str, c: str) -> str: """ Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings. If there are multiple such strings, return the lexicographically smallest one. Return a string denoting the answe...
count-stepping-numbers-in-range
def countSteppingNumbers(low: str, high: str) -> int: """ Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1. ...
find-the-k-th-lucky-number
def kthLuckyNumber(k: int) -> str: """ We know that 4 and 7 are lucky digits. Also, a number is called lucky if it contains only lucky digits. You are given an integer k, return the kth lucky number represented as a string. Example 1: >>> kthLuckyNumber(k = 4) >>> "47" Explanation:...
account-balance-after-rounded-purchase
def accountBalanceAfterPurchase(purchaseAmount: int) -> int: """ Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars, in other words, its price. When making the purchase, first the purchaseAmou...
insert-greatest-common-divisors-in-linked-list
# class ListNode: # def __init__(val=0, next=None): # self.val = val # self.next = next class Solution: def insertGreatestCommonDivisors(head: Optional[ListNode]) -> Optional[ListNode]: """ Given the head of a linked list head, in which each node contains an integer value. Between ev...
minimum-seconds-to-equalize-a-circular-array
def minimumSeconds(nums: List[int]) -> int: """ You are given a 0-indexed array nums containing n integers. At each second, you perform the following operation on the array: For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n]. ...
minimum-time-to-make-array-sum-at-most-x
def minimumTime(nums1: List[int], nums2: List[int], x: int) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation: ...
faulty-keyboard
def finalString(s: str) -> str: """ Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Retur...
check-if-it-is-possible-to-split-array
def canSplitArray(nums: List[int], m: int) -> bool: """ You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n arrays of size 1 by performing a series of steps. An array is called good if: The length of the array is one, or Th...
find-the-safest-path-in-a-grid
def maximumSafenessFactor(grid: List[List[int]]) -> int: """ You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: A cell containing a thief if grid[r][c] = 1 An empty cell if grid[r][c] = 0 You are initially positioned at cell (0, 0). In one move, you can move t...
maximum-elegance-of-a-k-length-subsequence
def findMaximumElegance(items: List[List[int]], k: int) -> int: """ You are given a 0-indexed 2D integer array items of length n and an integer k. items[i] = [profiti, categoryi], where profiti and categoryi denote the profit and category of the ith item respectively. Let's define the elegance of a subs...
minimum-time-takes-to-reach-destination-without-drowning
def minimumSeconds(land: List[List[str]]) -> int: """ You are given an n * m 0-indexed grid of string land. Right now, you are standing at the cell that contains "S", and you want to get to the cell containing "D". There are three other types of cells in this land: ".": These cells are empty. "X": ...
max-pair-sum-in-an-array
def maxSum(nums: List[int]) -> int: """ You are given an integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the largest digit in both numbers is equal. For example, 2373 is made up of three distinct digits: 2, 3, and 7, where 7 is the largest among them. Return...
double-a-number-represented-as-a-linked-list
# class ListNode: # def __init__(val=0, next=None): # self.val = val # self.next = next class Solution: def doubleIt(head: Optional[ListNode]) -> Optional[ListNode]: """ You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Retu...
minimum-absolute-difference-between-elements-with-constraint
def minAbsoluteDifference(nums: List[int], x: int) -> int: """ You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and a...
minimum-relative-loss-after-buying-chocolates
def minimumRelativeLosses(prices: List[int], queries: List[List[int]]) -> List[int]: """ You are given an integer array prices, which shows the chocolate prices and a 2D integer array queries, where queries[i] = [ki, mi]. Alice and Bob went to buy some chocolates, and Alice suggested a way to pay for them, ...
count-pairs-whose-sum-is-less-than-target
def countPairs(nums: List[int], target: int) -> int: """ Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target. Example 1: >>> countPairs(nums = [-1,1,2,3,1], target = 2) >>> 3 Expla...
make-string-a-subsequence-using-cyclic-increments
def canMakeSubsequence(str1: str, str2: str) -> bool: """ You are given two 0-indexed strings str1 and str2. In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z'...
sorting-three-groups
def minimumOperations(nums: List[int]) -> int: """ You are given an integer array nums. Each element in nums is 1, 2 or 3. In each operation, you can remove an element from nums. Return the minimum number of operations to make nums non-decreasing. Example 1: >>> minimumOperations(nums = [2,1,3...