task_id
stringlengths
3
79
prompt
stringlengths
255
3.93k
longest-binary-subsequence-less-than-or-equal-to-k
def longestSubsequence(s: str, k: int) -> int: """ You are given a binary string s and a positive integer k. Return the length of the longest subsequence of s that makes up a binary number less than or equal to k. Note: The subsequence can contain leading zeroes. The empty string is conside...
selling-pieces-of-wood
from typing import List def sellingWood(m: int, n: int, prices: List[List[int]]) -> int: """ You are given two integers m and n that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices, where prices[i] = [hi, wi, pricei] indicates you can sell a rectangula...
count-asterisks
def countAsterisks(s: str) -> int: """ You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth. Return the number of '*' in s, excluding the '*' between each pair of '|'. ...
count-unreachable-pairs-of-nodes-in-an-undirected-graph
from typing import List def countPairs(n: int, edges: List[List[int]]) -> int: """ You are given an integer n. There is an undirected graph with n nodes, 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 nodes ai an...
maximum-xor-after-operations
from typing import List def maximumXOR(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. In one operation, select any non-negative integer x and an index i, then update nums[i] to be equal to nums[i] AND (nums[i] XOR x). Note that AND is the bitwise AND operation and XOR is the bitw...
number-of-distinct-roll-sequences
def distinctSequences(n: int) -> int: """ You are given an integer n. You roll a fair 6-sided dice n times. Determine the total number of distinct sequences of rolls possible such that the following conditions are satisfied: The greatest common divisor of any adjacent values in the sequence is equal to...
check-if-matrix-is-x-matrix
from typing import List def checkXMatrix(grid: List[List[int]]) -> bool: """ A square matrix is said to be an X-Matrix if both of the following conditions hold: All the elements in the diagonals of the matrix are non-zero. All other elements are 0. Given a 2D integer array grid of size n x...
count-number-of-ways-to-place-houses
def countHousePlacements(n: int) -> int: """ There is a street with n * 2 plots, where there are n plots on each side of the street. The plots on each side are numbered from 1 to n. On each plot, a house can be placed. Return the number of ways houses can be placed such that no two houses are adjacent to ea...
maximum-score-of-spliced-array
from typing import List def maximumsSplicedArray(nums1: List[int], nums2: List[int]) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2, both of length n. You can choose two integers left and right where 0 <= left <= right < n and swap the subarray nums1[left...right] with the subarray n...
minimum-score-after-removals-on-a-tree
from typing import List def minimumScore(nums: List[int], edges: List[List[int]]) -> int: """ There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 0-indexed integer array nums of length n where nums[i] represents the value of the ith node. You are also ...
find-minimum-time-to-finish-all-jobs-ii
from typing import List def minimumTime(jobs: List[int], workers: List[int]) -> int: """ You are given two 0-indexed integer arrays jobs and workers of equal length, where jobs[i] is the amount of time needed to complete the ith job, and workers[j] is the amount of time the jth worker can work each day. Eac...
decode-the-message
def decodeMessage(key: str, message: str) -> str: """ You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows: Use the first appearance of all 26 lowercase English letters in key as the order of the substitut...
spiral-matrix-iv
from typing import List, Optional class ListNode: def __init__(val=0, next=None): self.val = val self.next = next def spiralMatrix(m: int, n: int, head: Optional[ListNode]) -> List[List[int]]: """ You are given two integers m and n, which represent the dimensions of a matrix. You are also given the ...
number-of-people-aware-of-a-secret
def peopleAwareOfSecret(n: int, delay: int, forget: int) -> int: """ On day 1, one person discovers a secret. You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer f...
number-of-increasing-paths-in-a-grid
from typing import List def countPaths(grid: List[List[int]]) -> int: """ You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions. Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell....
valid-palindrome-iv
def makePalindrome(s: str) -> bool: """ You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character. Return true if you can make s a palindrome after performing exactly one or two operations, or return false othe...
evaluate-boolean-binary-tree
from typing import Optional class TreeNode: def __init__(val=0, left=None, right=None): self.val = val self.left = left self.right = right def evaluateTree(root: Optional[TreeNode]) -> bool: """ You are given the root of a full binary tree with the following properties: Leaf nodes have eith...
the-latest-time-to-catch-a-bus
from typing import List def latestTimeCatchTheBus(buses: List[int], passengers: List[int], capacity: int) -> int: """ You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where...
minimum-sum-of-squared-difference
from typing import List def minSumSquareDiff(nums1: List[int], nums2: List[int], k1: int, k2: int) -> int: """ You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n. The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])2 for ea...
subarray-with-elements-greater-than-varying-threshold
from typing import List def validSubarraySize(nums: List[int], threshold: int) -> int: """ You are given an integer array nums and an integer threshold. Find any subarray of nums of length k such that every element in the subarray is greater than threshold / k. Return the size of any such subarray. If t...
minimum-amount-of-time-to-fill-cups
from typing import List def fillCups(amount: List[int]) -> int: """ You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up 2 cups with different types of water, or 1 cup of any type of water. You are given a 0-indexed integer array amount of length 3 whe...
move-pieces-to-obtain-a-string
def canChange(start: str, target: str) -> bool: """ You are given two strings start and target, both of length n. Each string consists only of the characters 'L', 'R', and '_' where: The characters 'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space direc...
count-the-number-of-ideal-arrays
def idealArrays(n: int, maxValue: int) -> int: """ You are given two integers n and maxValue, which are used to describe an ideal array. A 0-indexed integer array arr of length n is considered ideal if the following conditions hold: Every arr[i] is a value from 1 to maxValue, for 0 <= i < n. Ev...
minimum-adjacent-swaps-to-make-a-valid-array
from typing import List def minimumSwaps(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. Swaps of adjacent elements are able to be performed on nums. A valid array meets the following conditions: The largest element (any of the largest elements if there are multiple) ...
maximum-number-of-pairs-in-array
from typing import List def numberOfPairs(nums: List[int]) -> List[int]: """ You are given a 0-indexed integer array nums. In one operation, you may do the following: Choose two integers in nums that are equal. Remove both integers from nums, forming a pair. The operation is done on nums a...
max-sum-of-a-pair-with-equal-sum-of-digits
from typing import List def maximumSum(nums: List[int]) -> int: """ You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j]. Return the maximum value of nums[i] + nums[...
query-kth-smallest-trimmed-number
from typing import List def smallestTrimmedNumbers(nums: List[str], queries: List[List[int]]) -> List[int]: """ You are given a 0-indexed array of strings nums, where each string is of equal length and consists of only digits. You are also given a 0-indexed 2D integer array queries where queries[i] = [ki, t...
minimum-deletions-to-make-array-divisible
from typing import List def minOperations(nums: List[int], numsDivide: List[int]) -> int: """ You are given two positive integer arrays nums and numsDivide. You can delete any number of elements from nums. Return the minimum number of deletions such that the smallest element in nums divides all the elements...
finding-the-number-of-visible-mountains
from typing import List def visibleMountains(peaks: List[List[int]]) -> int: """ You are given a 0-indexed 2D integer array peaks where peaks[i] = [xi, yi] states that mountain i has a peak at coordinates (xi, yi). A mountain can be described as a right-angled isosceles triangle, with its base along the x-axis ...
best-poker-hand
from typing import Any, List def bestHand(ranks: List[int], suits: List[str]) -> str: """ You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i]. The following are the types of poker hands you can make from best to ...
number-of-zero-filled-subarrays
from typing import List def zeroFilledSubarray(nums: List[int]) -> int: """ Given an integer array nums, return the number of subarrays filled with 0. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: >>> zeroFilledSubarray(nums = [1,3,0,0,2,0,0,4]) ...
shortest-impossible-sequence-of-rolls
from typing import List def shortestSequence(rolls: List[int], k: int) -> int: """ You are given an integer array rolls of length n and an integer k. You roll a k sided dice numbered from 1 to k, n times, where the result of the ith roll is rolls[i]. Return the length of the shortest sequence of rolls so th...
first-letter-to-appear-twice
def repeatedCharacter(s: str) -> str: """ Given a string s consisting of lowercase English letters, return the first letter to appear twice. Note: A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b. s will contain at least one let...
equal-row-and-column-pairs
from typing import List def equalPairs(grid: List[List[int]]) -> int: """ Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal. A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal a...
number-of-excellent-pairs
from typing import List def countExcellentPairs(nums: List[int], k: int) -> int: """ You are given a 0-indexed positive integer array nums and a positive integer k. A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied: Both the numbers num1 and num2 exist in ...
maximum-number-of-books-you-can-take
from typing import List def maximumBooks(books: List[int]) -> int: """ You are given a 0-indexed integer array books of length n where books[i] denotes the number of books on the ith shelf of a bookshelf. You are going to take books from a contiguous section of the bookshelf spanning from l to r where 0 <= ...
make-array-zero-by-subtracting-equal-amounts
from typing import List def minimumOperations(nums: List[int]) -> int: """ You are given a non-negative integer array nums. In one operation, you must: Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums. Subtract x from every positive element in n...
maximum-number-of-groups-entering-a-competition
from typing import List def maximumGroups(grades: List[int]) -> int: """ You are given a positive integer array grades which represents the grades of students in a university. You would like to enter all these students into a competition in ordered non-empty groups, such that the ordering meets the following co...
find-closest-node-to-given-two-nodes
from typing import List def closestMeetingNode(edges: List[int], node1: int, node2: int) -> int: """ You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there...
longest-cycle-in-a-graph
from typing import List def longestCycle(edges: List[int]) -> int: """ You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node ...
minimum-costs-using-the-train-line
from typing import List def minimumCosts(regular: List[int], express: List[int], expressCost: int) -> List[int]: """ A train line going through a city has two routes, the regular route and the express route. Both routes go through the same n + 1 stops labeled from 0 to n. Initially, you start on the regular rou...
merge-similar-items
from typing import List def mergeSimilarItems(items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]: """ You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties: items[i] = [valuei, weighti] where valuei repres...
count-number-of-bad-pairs
from typing import List def countBadPairs(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair if i < j and j - i != nums[j] - nums[i]. Return the total number of bad pairs in nums. Example 1: >>> countBadPairs(nums = [4,1,3,3]) ...
task-scheduler-ii
from typing import List def taskSchedulerII(tasks: List[int], space: int) -> int: """ You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task. You are also given a positive integer space, which repr...
minimum-replacements-to-sort-the-array
from typing import List def minimumReplacement(nums: List[int]) -> int: """ You are given a 0-indexed integer array nums. In one operation you can replace any element of the array with any two elements that sum to it. For example, consider nums = [5,6,7]. In one operation, we can replace nums[1] with 2...
number-of-arithmetic-triplets
from typing import List def arithmeticTriplets(nums: List[int], diff: int) -> int: """ You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met: i < j < k, nums[j] - nums[i] == dif...
reachable-nodes-with-restrictions
from typing import List def reachableNodes(n: int, edges: List[List[int]], restricted: List[int]) -> int: """ There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge bet...
check-if-there-is-a-valid-partition-for-the-array
from typing import List def validPartition(nums: List[int]) -> bool: """ You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays. We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions: ...
longest-ideal-subsequence
def longestIdealString(s: str, k: int) -> int: """ You are given a string s consisting of lowercase letters and an integer k. We call a string t ideal if the following conditions are satisfied: t is a subsequence of the string s. The absolute difference in the alphabet order of every two adjacent l...
minimize-maximum-value-in-a-grid
from typing import List def minScore(grid: List[List[int]]) -> List[List[int]]: """ You are given an m x n integer matrix grid containing distinct positive integers. You have to replace each integer in the matrix with a positive integer satisfying the following conditions: The relative order of eve...
largest-local-values-in-a-matrix
from typing import List def largestLocal(grid: List[List[int]]) -> List[List[int]]: """ You are given an n x n integer matrix grid. Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that: maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i...
node-with-highest-edge-score
from typing import List def edgeScore(edges: List[int]) -> int: """ You are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge. The graph is represented by a given 0-indexed integer array edges of length n, where edges[i] indicates that there is a dire...
construct-smallest-number-from-di-string
def smallestNumber(pattern: str) -> str: """ You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing. A 0-indexed string num of length n + 1 is created using the following conditions: num consists of the digits '1' to '9', ...
count-special-integers
def countSpecialNumbers(n: int) -> int: """ We call a positive integer special if all of its digits are distinct. Given a positive integer n, return the number of special integers that belong to the interval [1, n]. Example 1: >>> countSpecialNumbers(n = 20) >>> 19 Explanation: All...
choose-edges-to-maximize-score-in-a-tree
from typing import List def maxScore(edges: List[List[int]]) -> int: """ You are given a weighted tree consisting of n nodes numbered from 0 to n - 1. The tree is rooted at node 0 and represented with a 2D array edges of size n where edges[i] = [pari, weighti] indicates that node pari is the parent of node ...
minimum-recolors-to-get-k-consecutive-black-blocks
def minimumRecolors(blocks: str, k: int) -> int: """ You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block. The characters 'W' and 'B' denote the colors white and black, respectively. You are also given an integer k, which is the d...
time-needed-to-rearrange-a-binary-string
def secondsToRemoveOccurrences(s: str) -> int: """ You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist. Return the number of seconds needed to complete this process. Example 1: ...
shifting-letters-ii
from typing import List def shiftingLetters(s: str, shifts: List[List[int]]) -> str: """ You are given a string s of lowercase English letters and a 2D integer array shifts where shifts[i] = [starti, endi, directioni]. For every i, shift the characters in s from the index starti to the index endi (inclusive) fo...
maximum-segment-sum-after-removals
from typing import List def maximumSegmentSum(nums: List[int], removeQueries: List[int]) -> List[int]: """ You are given two 0-indexed integer arrays nums and removeQueries, both of length n. For the ith query, the element in nums at the index removeQueries[i] is removed, splitting nums into different segments....
minimum-hours-of-training-to-win-a-competition
from typing import List def minNumberOfHours(initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int: """ You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively....
largest-palindromic-number
def largestPalindromic(num: str) -> str: """ You are given a string num consisting of digits only. Return the largest palindromic integer (in the form of a string) that can be formed using digits taken from num. It should not contain leading zeroes. Notes: You do not need to use all the digits ...
amount-of-time-for-binary-tree-to-be-infected
from typing import Optional class TreeNode: def __init__(val=0, left=None, right=None): self.val = val self.left = left self.right = right def amountOfTime(root: Optional[TreeNode], start: int) -> int: """ You are given the root of a binary tree with unique values, and an integer start. At m...
find-the-k-sum-of-an-array
from typing import List def kSum(nums: List[int], k: int) -> int: """ You are given an integer array nums and a positive integer k. You can choose any subsequence of the array and sum all of its elements together. We define the K-Sum of the array as the kth largest subsequence sum that can be obtained (not ...
median-of-a-row-wise-sorted-matrix
from typing import List def matrixMedian(grid: List[List[int]]) -> int: """ Given an m x n matrix grid containing an odd number of integers where each row is sorted in non-decreasing order, return the median of the matrix. You must solve the problem in less than O(m * n) time complexity. Example 1:...
longest-subsequence-with-limited-sum
from typing import List def answerQueries(nums: List[int], queries: List[int]) -> List[int]: """ You are given an integer array nums of length n, and an integer array queries of length m. Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such...
removing-stars-from-a-string
def removeStars(s: str) -> str: """ You are given a string s, which contains stars *. In one operation, you can: Choose a star in s. Remove the closest non-star character to its left, as well as remove the star itself. Return the string after all stars have been removed. Note: ...
minimum-amount-of-time-to-collect-garbage
from typing import List def garbageCollection(garbage: List[str], travel: List[int]) -> int: """ You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters 'M', 'P' and 'G' representing one unit of metal, ...
build-a-matrix-with-conditions
from typing import List def buildMatrix(k: int, rowConditions: List[List[int]], colConditions: List[List[int]]) -> List[List[int]]: """ You are given a positive integer k. You are also given: a 2D integer array rowConditions of size n where rowConditions[i] = [abovei, belowi], and a 2D integer arra...
count-strictly-increasing-subarrays
from typing import List def countSubarrays(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. Return the number of subarrays of nums that are in strictly increasing order. A subarray is a contiguous part of an array. Example 1: >>> countSubarrays(...
find-subarrays-with-equal-sum
from typing import List def findSubarrays(nums: List[int]) -> bool: """ Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices. Return true if these subarrays exist, and false otherwise. A ...
strictly-palindromic-number
def isStrictlyPalindromic(n: int) -> bool: """ An integer n is strictly palindromic if, for every base b between 2 and n - 2 (inclusive), the string representation of the integer n in base b is palindromic. Given an integer n, return true if n is strictly palindromic and false otherwise. A string is pal...
maximum-rows-covered-by-columns
from typing import List def maximumRows(matrix: List[List[int]], numSelect: int) -> int: """ You are given an m x n binary matrix matrix and an integer numSelect. Your goal is to select exactly numSelect distinct columns from matrix such that you cover as many rows as possible. A row is considered cover...
maximum-number-of-robots-within-budget
from typing import List def maximumRobots(chargeTimes: List[int], runningCosts: List[int], budget: int) -> int: """ You have n robots. You are given two 0-indexed integer arrays, chargeTimes and runningCosts, both of length n. The ith robot costs chargeTimes[i] units to charge and costs runningCosts[i] units to...
check-distances-between-same-letters
from typing import List def checkDistances(s: str, distance: List[int]) -> bool: """ You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26. Each letter in the alphabet ...
number-of-ways-to-reach-a-position-after-exactly-k-steps
def numberOfWays(startPos: int, endPos: int, k: int) -> int: """ You are given two positive integers startPos and endPos. Initially, you are standing at position startPos on an infinite number line. With one step, you can move either one position to the left, or one position to the right. Given a positive i...
longest-nice-subarray
from typing import Any, List def longestNiceSubarray(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0. Return the length of ...
meeting-rooms-iii
from typing import List def mostBooked(n: int, meetings: List[List[int]]) -> int: """ You are given an integer n. There are n rooms numbered from 0 to n - 1. You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [s...
minimum-time-to-kill-all-monsters
from typing import List def minimumTime(power: List[int]) -> int: """ You are given an integer array power where power[i] is the power of the ith monster. You start with 0 mana points, and each day you increase your mana points by gain where gain initially is equal to 1. Each day, after gaining gain man...
most-frequent-even-element
from typing import List def mostFrequentEven(nums: List[int]) -> int: """ Given an integer array nums, return the most frequent even element. If there is a tie, return the smallest one. If there is no such element, return -1. Example 1: >>> mostFrequentEven(nums = [0,1,2,2,4,4,1]) >>> ...
optimal-partition-of-string
def partitionString(s: str) -> int: """ Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once. Return the minimum number of substrings in such a partition. Note that each c...
divide-intervals-into-minimum-number-of-groups
from typing import List def minGroups(intervals: List[List[int]]) -> int: """ You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti]. You have to divide the intervals into one or more groups such that each interval is in exactly one ...
longest-increasing-subsequence-ii
from typing import List def lengthOfLIS(nums: List[int], k: int) -> int: """ You are given an integer array nums and an integer k. Find the longest subsequence of nums that meets the following requirements: The subsequence is strictly increasing and The difference between adjacent elements in t...
count-days-spent-together
def countDaysTogether(arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: """ Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from the dates arriveAlice to leaveAlice...
maximum-matching-of-players-with-trainers
from typing import List def matchPlayersAndTrainers(players: List[int], trainers: List[int]) -> int: """ You are given a 0-indexed integer array players, where players[i] represents the ability of the ith player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training ca...
smallest-subarrays-with-maximum-bitwise-or
from typing import List def smallestSubarrays(nums: List[int]) -> List[int]: """ You are given a 0-indexed array nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has...
minimum-money-required-before-transactions
from typing import List def minimumMoney(transactions: List[List[int]]) -> int: """ You are given a 0-indexed 2D integer array transactions, where transactions[i] = [costi, cashbacki]. The array describes transactions, where each transaction must be completed exactly once in some order. At any given moment,...
smallest-even-multiple
def smallestEvenMultiple(n: int) -> int: """ Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n. Example 1: >>> smallestEvenMultiple(n = 5) >>> 10 Explanation: The smallest multiple of both 5 and 2 is 10. Example 2: >>>...
length-of-the-longest-alphabetical-continuous-substring
def longestContinuousSubstring(s: str) -> int: """ An alphabetical continuous string is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string "abcdefghijklmnopqrstuvwxyz". For example, "abc" is an alphabetical continuous string, while "acb" and "z...
reverse-odd-levels-of-binary-tree
from typing import Optional class TreeNode: def __init__(val=0, left=None, right=None): self.val = val self.left = left self.right = right def reverseOddLevels(root: Optional[TreeNode]) -> Optional[TreeNode]: """ Given the root of a perfect binary tree, reverse the node values at each odd le...
sum-of-prefix-scores-of-strings
from typing import List def sumPrefixScores(words: List[str]) -> List[int]: """ You are given an array words of size n consisting of non-empty strings. We define the score of a string term as the number of strings words[i] such that term is a prefix of words[i]. For example, if words = ["a", "ab", ...
closest-fair-integer
def closestFair(n: int) -> int: """ You are given a positive integer n. We call an integer k fair if the number of even digits in k is equal to the number of odd digits in it. Return the smallest fair integer that is greater than or equal to n. Example 1: >>> closestFair(n = 2) >>>...
sort-the-people
from typing import List def sortPeople(names: List[str], heights: List[int]) -> List[str]: """ You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n. For each index i, names[i] and heights[i] denote the name and height of the i...
longest-subarray-with-maximum-bitwise-and
from typing import List def longestSubarray(nums: List[int]) -> int: """ You are given an integer array nums of size n. Consider a non-empty subarray from nums that has the maximum possible bitwise AND. In other words, let k be the maximum value of the bitwise AND of any subarray of nums. Then, onl...
find-all-good-indices
from typing import List def goodIndices(nums: List[int], k: int) -> List[int]: """ You are given a 0-indexed integer array nums of size n and a positive integer k. We call an index i in the range k <= i < n - k good if the following conditions are satisfied: The k elements that are just before the ...
number-of-good-paths
from typing import List def numberOfGoodPaths(vals: List[int], edges: List[List[int]]) -> int: """ There is a tree (i.e. a connected, undirected graph with no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. You are given a 0-indexed integer array vals of length n where vals[i...
merge-operations-to-turn-array-into-a-palindrome
from typing import List def minimumOperations(nums: List[int]) -> int: """ You are given an array nums consisting of positive integers. You can perform the following operation on the array any number of times: Choose any two adjacent elements and replace them with their sum. For examp...
remove-letter-to-equalize-frequency
def equalFrequency(word: str) -> bool: """ You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal. Return true if it is possible to remove one le...
bitwise-xor-of-all-pairings
from typing import List def xorAllNums(nums1: List[int], nums2: List[int]) -> int: """ You are given two 0-indexed arrays, nums1 and nums2, consisting of non-negative integers. Let there be another array, nums3, which contains the bitwise XOR of all pairings of integers between nums1 and nums2 (every integer in...
number-of-pairs-satisfying-inequality
from typing import List def numberOfPairs(nums1: List[int], nums2: List[int], diff: int) -> int: """ You are given two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs (i, j) such that: 0 <= i < j <= n - 1 and nums1[i] - nums1[j] <= nums2[i] - ...
number-of-common-factors
def commonFactors(a: int, b: int) -> int: """ Given two positive integers a and b, return the number of common factors of a and b. An integer x is a common factor of a and b if x divides both a and b. Example 1: >>> commonFactors(a = 12, b = 6) >>> 4 Explanation: The common factors...