id
int64
1
2.39k
title
stringlengths
3
79
title_slug
stringlengths
3
79
question_content
stringlengths
38
1.55k
βŒ€
tag
stringlengths
4
122
βŒ€
level
stringclasses
3 values
success_rate
float64
14.2
95.5
total_submission
int64
186
13.2M
total_accepted
int64
153
6.4M
question_likes
int64
4
31.2k
question_dislikes
int64
0
11.5k
question_hints
stringlengths
19
3.98k
βŒ€
similar_question_ids
stringlengths
1
51
βŒ€
2,304
Cells in a Range on an Excel Sheet
cells-in-a-range-on-an-excel-sheet
A cell (r, c) of an excel sheet is represented as a string "<col><row>" where: You are given a string sΒ inΒ the format "<col1><row1>:<col2><row2>", where <col1> represents the column c1, <row1> represents the row r1, <col2> represents the column c2, and <row2> represents the row r2, such that r1 <= r2 and c1 <= c2. Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented asΒ strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.
String
Easy
85.7
24,272
20,792
134
30
From the given string, find the corresponding rows and columns. Iterate through the columns in ascending order and for each column, iterate through the rows in ascending order to obtain the required cells in sorted order.
168,171,1094
2,305
Append K Integers With Minimal Sum
append-k-integers-with-minimal-sum
You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum. Return the sum of the k integers appended to nums.
Array,Math,Greedy,Sorting
Medium
22.9
66,897
15,315
316
223
The k smallest numbers that do not appear in nums will result in the minimum sum. Recall that the sum of the first n positive numbers is equal to n * (n+1) / 2. Initialize the answer as the sum of 1 to k. Then, adjust the answer depending on the values in nums.
402,448,1646
2,306
Create Binary Tree From Descriptions
create-binary-tree-from-descriptions
You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore, Construct the binary tree described by descriptions and return its root. The test cases will be generated such that the binary tree is valid.
Array,Hash Table,Tree,Depth-First Search,Breadth-First Search,Binary Tree
Medium
70.3
19,891
13,986
334
7
Could you represent and store the descriptions more efficiently? Could you find the root node? The node that is not a child in any of the descriptions is the root node.
109,1820
2,307
Replace Non-Coprime Numbers in Array
replace-non-coprime-numbers-in-array
You are given an array of integers nums. Perform the following steps: Return the final modified array. It can be shown that replacing adjacent non-coprime numbers in any arbitrary order will lead to the same result. The test cases are generated such that the values in the final array are less than or equal to 108. Two values x and y are non-coprime if GCD(x, y) > 1 where GCD(x, y) is the Greatest Common Divisor of x and y.
Array,Math,Stack,Number Theory
Hard
35
20,150
7,044
221
6
Notice that the order of merging two numbers into their LCM does not matter so we can greedily merge elements to its left if possible. If a new value is formed, we should recursively check if it can be merged with the value to its left. To simulate the merge efficiently, we can maintain a stack that stores processed elements. When we iterate through the array, we only compare with the top of the stack (which is the value to its left).
1320,2129
2,308
Divide Array Into Equal Pairs
divide-array-into-equal-pairs
You are given an integer array nums consisting of 2 * n integers. You need to divide nums into n pairs such that: Return true if nums can be divided into n pairs, otherwise return false.
Array,Hash Table,Bit Manipulation,Counting
Easy
77.6
25,323
19,656
159
5
For any number x in the range [1, 500], count the number of elements in nums whose values are equal to x. The elements with equal value can be divided completely into pairs if and only if their count is even.
1741
2,309
Maximize Number of Subsequences in a String
maximize-number-of-subsequences-in-a-string
You are given a 0-indexed string text and another 0-indexed string pattern of length 2, both of which consist of only lowercase English letters. You can add either pattern[0] or pattern[1] anywhere in text exactly once. Note that the character can be added even at the beginning or at the end of text. Return the maximum number of times pattern can occur as a subsequence of the modified text. A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
String,Greedy,Prefix Sum
Medium
30.7
33,828
10,383
215
12
Find the optimal position to add pattern[0] so that the number of subsequences is maximized. Similarly, find the optimal position to add pattern[1]. For each of the above cases, count the number of times the pattern occurs as a subsequence in text. The larger count is the required answer.
1250
2,310
Minimum Operations to Halve Array Sum
minimum-operations-to-halve-array-sum
You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.) Return the minimum number of operations to reduce the sum of nums by at least half.
Array,Greedy,Heap (Priority Queue)
Medium
43.3
25,848
11,199
200
9
It is always optimal to halve the largest element. What data structure allows for an efficient query of the maximum element? Use a heap or priority queue to maintain the current elements.
2094
2,311
Minimum White Tiles After Covering With Carpets
minimum-white-tiles-after-covering-with-carpets
You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor: You are also given numCarpets and carpetLen. You have numCarpets black carpets, each of length carpetLen tiles. Cover the tiles with the given carpets such that the number of white tiles still visible is minimum. Carpets may overlap one another. Return the minimum number of white tiles still visible.
String,Dynamic Programming,Prefix Sum
Hard
31.2
19,948
6,228
257
11
Can you think of a DP solution? Let DP[i][j] denote the minimum number of white tiles still visible from indices i to floor.length-1 after covering with at most j carpets. The transition will be whether to put down the carpet at position i (if possible), or not.
72
2,312
Most Frequent Number Following Key In an Array
most-frequent-number-following-key-in-an-array
You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums. For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that: Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique.
Array,Hash Table,Counting
Easy
60.6
27,172
16,467
129
66
Count the number of times each target value follows the key in the array. Choose the target with the maximum count and return it.
1741
2,313
Longest Winning Streak
longest-winning-streak
null
Database
Hard
50.2
1,239
622
22
1
null
null
2,314
Remove All Ones With Row and Column Flips II
remove-all-ones-with-row-and-column-flips-ii
null
Array,Bit Manipulation,Breadth-First Search,Matrix
Medium
68.9
1,498
1,032
30
1
With the given constraints, could a brute force solution pass? What would a brute force solution look like? We can try every single possibility of choosing to do an operation on a cell with a 1 or choosing to ignore it.
73,1409,2268
2,315
The Change in Global Rankings
the-change-in-global-rankings
null
Database
Medium
65.6
1,604
1,052
9
5
null
null
2,316
Count Hills and Valleys in an Array
count-hills-and-valleys-in-an-array
You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j]. Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index. Return the number of hills and valleys in nums.
Array
Easy
56.3
30,741
17,316
184
44
For each index, could you find the closest non-equal neighbors? Ensure that adjacent indices that are part of the same hill or valley are not double-counted.
162,932,1519
2,317
Count Collisions on a Road
count-collisions-on-a-road
There are n cars on an infinitely long road. The cars are numbered from 0 to n - 1 from left to right and each car is present at a unique point. You are given a 0-indexed string directions of length n. directions[i] can be either 'L', 'R', or 'S' denoting whether the ith car is moving towards the left, towards the right, or staying at its current point respectively. Each moving car has the same speed. The number of collisions can be calculated as follows: After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion. Return the total number of collisions that will happen on the road.
String,Stack
Medium
39.8
31,592
12,566
257
150
In what circumstances does a moving car not collide with another car? If we disregard the moving cars that do not collide with another car, what does each moving car contribute to the answer? Will stationary cars contribute towards the answer?
735,883,1627,1902
2,318
Maximum Points in an Archery Competition
maximum-points-in-an-archery-competition
Alice and Bob are opponents in an archery competition. The competition has set the following rules: For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points. You are given the integer numArrows and an integer array aliceArrows of size 12, which represents the number of arrows Alice shot on each scoring section from 0 to 11. Now, Bob wants to maximize the total number of points he can obtain. Return the array bobArrows which represents the number of arrows Bob shot on each scoring section from 0 to 11. The sum of the values in bobArrows should equal numArrows. If there are multiple ways for Bob to earn the maximum total points, return any one of them.
Array,Bit Manipulation,Recursion,Enumeration
Medium
47.2
17,571
8,300
243
21
To obtain points for some certain section x, what is the minimum number of arrows Bob must shoot? Given the small number of sections, can we brute force which sections Bob wants to win? For every set of sections Bob wants to win, check if we have the required amount of arrows. If we do, it is a valid selection.
2130
2,319
Longest Substring of One Repeating Character
longest-substring-of-one-repeating-character
You are given a 0-indexed string s. You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indices queryIndices of length k, both of which are used to describe k queries. The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i]. Return an array lengths of length k where lengths[i] is the length of the longest substring of s consisting of only one repeating character after the ith query is performed.
Array,String,Segment Tree,Ordered Set
Hard
28.5
6,683
1,906
97
66
Use a segment tree to perform fast point updates and range queries. We need each segment tree node to store the length of the longest substring of that segment consisting of only 1 repeating character. We will also have each segment tree node store the leftmost and rightmost character of the segment, the max length of a prefix substring consisting of only 1 repeating character, and the max length of a suffix substring consisting of only 1 repeating character. Use this information to properly merge the two segment tree nodes together.
56,424,1542,1772
2,320
Find All K-Distant Indices in an Array
find-all-k-distant-indices-in-an-array
You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key. Return a list of all k-distant indices sorted in increasing order.
Array
Easy
64.4
32,898
21,198
177
26
For every occurrence of key in nums, find all indices within distance k from it. Use a hash table to remove duplicate indices.
1,243
2,321
Minimum Weighted Subgraph With the Required Paths
minimum-weighted-subgraph-with-the-required-paths
You are given an integer n denoting the number of nodes of a weighted directed graph. The nodes are numbered from 0 to n - 1. You are also given a 2D integer array edges where edges[i] = [fromi, toi, weighti] denotes that there exists a directed edge from fromi to toi with weight weighti. Lastly, you are given three distinct integers src1, src2, and dest denoting three distinct nodes of the graph. Return the minimum weight of a subgraph of the graph such that it is possible to reach dest from both src1 and src2 via a set of edges of this subgraph. In case such a subgraph does not exist, return -1. A subgraph is a graph whose vertices and edges are subsets of the original graph. The weight of a subgraph is the sum of weights of its constituent edges.
Graph,Shortest Path
Hard
35.1
16,637
5,834
369
11
Consider what the paths from src1 to dest and src2 to dest would look like in the optimal solution. It can be shown that in an optimal solution, the two paths from src1 and src2 will coincide at one node, and the remaining part to dest will be the same for both paths. Now consider how to find the node where the paths will coincide. How can algorithms for finding the shortest path between two nodes help us?
1485
2,322
Number of Ways to Build Sturdy Brick Wall
number-of-ways-to-build-sturdy-brick-wall
null
Array,Dynamic Programming,Bit Manipulation,Bitmask
Medium
57
1,531
873
39
14
A row of bricks can be represented uniquely by the points where two bricks are joined together. For a given row of bricks, how many configurations of bricks could you have put below this row such that the wall is sturdy? Use dynamic programming to store the number of possible sturdy walls with a given height and configuration of bricks on the top row.
554,821
2,323
Minimum Bit Flips to Convert Number
minimum-bit-flips-to-convert-number
A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0. Given two integers start and goal, return the minimum number of bit flips to convert start to goal.
Bit Manipulation
Easy
81.1
17,915
14,525
111
2
If the value of a bit in start and goal differ, then we need to flip that bit. Consider using the XOR operation to determine which bits need a bit flip.
1441
2,324
Find Triangular Sum of an Array
find-triangular-sum-of-an-array
You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive). The triangular sum of nums is the value of the only element present in nums after the following process terminates: Return the triangular sum of nums.
Array,Math,Simulation,Combinatorics
Medium
78.9
18,583
14,664
172
6
Try simulating the entire process. To reduce space, use a temporary array to update nums in every step instead of creating a new array at each step.
119
2,325
Number of Ways to Select Buildings
number-of-ways-to-select-buildings
You are given a 0-indexed binary string s which represents the types of buildings along a street where: As a city official, you would like to select 3 buildings for random inspection. However, to ensure variety, no two consecutive buildings out of the selected buildings can be of the same type. Return the number of valid ways to select 3 buildings.
String,Dynamic Programming,Prefix Sum
Medium
44.7
21,770
9,730
323
11
There are only 2 valid patterns: β€˜101’ and β€˜010’. Think about how we can construct these 2 patterns from smaller patterns. Count the number of subsequences of the form β€˜01’ or β€˜10’ first. Let n01[i] be the number of β€˜01’ subsequences that exist in the prefix of s up to the ith building. How can you compute n01[i]? Let n0[i] and n1[i] be the number of β€˜0’s and β€˜1’s that exists in the prefix of s up to i respectively. Then n01[i] = n01[i – 1] if s[i] == β€˜0’, otherwise n01[i] = n01[i – 1] + n0[i – 1]. The same logic applies to building the n10 array and subsequently the n101 and n010 arrays for the number of β€˜101’ and β€˜010β€˜ subsequences.
null
2,326
Sum of Scores of Built Strings
sum-of-scores-of-built-strings
You are building a string s of length n one character at a time, prepending each new character to the front of the string. The strings are labeled from 1 to n, where the string with length i is labeled si. The score of si is the length of the longest common prefix between si and sn (Note that s == sn). Given the final string s, return the sum of the score of every si.
String,Binary Search,Rolling Hash,Suffix Array,String Matching,Hash Function
Hard
32.9
9,479
3,120
110
146
Each s_i is a suffix of the string s, so consider algorithms that can determine the longest prefix that is also a suffix. Could you use the Z array from the Z algorithm to find the score of each s_i?
1508
2,327
Largest Number After Digit Swaps by Parity
largest-number-after-digit-swaps-by-parity
You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits). Return the largest possible value of num after any number of swaps.
Sorting,Heap (Priority Queue)
Easy
56.4
27,114
15,301
134
139
The bigger digit should appear first (more to the left) because it contributes more to the value of the number. Get all the even digits, as well as odd digits. Sort them separately. Reconstruct the number by giving the earlier digits the highest available digit of the same parity.
748,941,958,1308,2271
2,328
Minimize Result by Adding Parentheses to Expression
minimize-result-by-adding-parentheses-to-expression
You are given a 0-indexed string expression of the form "<num1>+<num2>" where <num1> and <num2> represent positive integers. Add a pair of parentheses to expression such that after the addition of parentheses, expression is a valid mathematical expression and evaluates to the smallest possible value. The left parenthesis must be added to the left of '+' and the right parenthesis must be added to the right of '+'. Return expression after adding a pair of parentheses such that expression evaluates to the smallest possible value. If there are multiple answers that yield the same result, return any of them. The input has been generated such that the original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
String,Enumeration
Medium
63.6
14,828
9,438
102
210
The maximum length of expression is very low. We can try every possible spot to place the parentheses. Every possibility of expression is of the form a * (b + c) * d where a, b, c, d represent integers. Note the edge cases where a and/or d do not exist, in which case use 1 instead of them.
224,241,640
2,329
Maximum Product After K Increments
maximum-product-after-k-increments
You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1. Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. Note that you should maximize the product before taking the modulo.
Array,Greedy,Heap (Priority Queue)
Medium
39
32,222
12,569
248
23
If you can increment only once, which number should you increment? We should always prioritize the smallest number. What kind of data structure could we use? Use a min heap to hold all the numbers. Each time we do an operation, replace the top of the heap x by x + 1.
209,982,1938
2,330
Maximum Total Beauty of the Gardens
maximum-total-beauty-of-the-gardens
Alice is a caretaker of n gardens and she wants to plant flowers to maximize the total beauty of all her gardens. You are given a 0-indexed integer array flowers of size n, where flowers[i] is the number of flowers already planted in the ith garden. Flowers that are already planted cannot be removed. You are then given another integer newFlowers, which is the maximum number of flowers that Alice can additionally plant. You are also given the integers target, full, and partial. A garden is considered complete if it has at least target flowers. The total beauty of the gardens is then determined as the sum of the following: Return the maximum total beauty that Alice can obtain after planting at most newFlowers flowers.
Array,Two Pointers,Binary Search,Greedy,Sorting
Hard
25.7
10,388
2,667
186
20
Say we choose k gardens to be complete, is there an optimal way of choosing which gardens to plant more flowers to achieve this? For a given k, we should greedily fill-up the k gardens with the most flowers planted already. This gives us the most remaining flowers to fill up the other gardens. After sorting flowers, we can thus try every possible k and what is left is to find the highest minimum flowers we can obtain by planting the remaining flowers in the other gardens. To find the highest minimum in the other gardens, we can use binary search to find the most optimal way of planting.
410
2,335
Finding the Topic of Each Post
finding-the-topic-of-each-post
null
Database
Hard
39.4
923
364
10
6
null
1625
2,336
The Number of Users That Are Eligible for Discount
the-number-of-users-that-are-eligible-for-discount
null
Database
Easy
47.8
1,722
823
9
11
null
177,2371
2,345
Minimum Number of Operations to Convert Time
minimum-number-of-operations-to-convert-time
You are given two strings current and correct representing two 24-hour times. 24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59. In one operation you can increase the time current by 1, 5, 15, or 60 minutes. You can perform this operation any number of times. Return the minimum number of operations needed to convert current to correct.
String,Greedy
Easy
61.8
27,654
17,096
133
17
Convert the times to minutes. Use the operation with the biggest value possible at each step.
322
2,354
Minimum Health to Beat Game
minimum-health-to-beat-game
null
Array,Greedy,Prefix Sum
Medium
58.7
2,054
1,206
30
12
When should you use your armor ability? It is always optimal to use your armor ability on the level where you take the most amount of damage.
174,2049
2,369
Maximum Sum Score of Array
maximum-sum-score-of-array
null
Array,Prefix Sum
Medium
67
1,031
691
21
6
How can we use precalculation to efficiently calculate the average difference at an index? Create a prefix and/or suffix sum array.
560,724,2102
2,370
Users With Two Purchases Within Seven Days
users-with-two-purchases-within-seven-days
null
Database
Medium
47.9
900
431
5
3
null
1852
2,371
The Users That Are Eligible for Discount
the-users-that-are-eligible-for-discount
null
Database
Easy
49.3
603
297
4
2
null
2336
2,376
Number of Times a Driver Was a Passenger
number-of-times-a-driver-was-a-passenger
null
null
Medium
82.3
186
153
6
1
null
1779,1785,1795
2,383
Add Two Integers
add-two-integers
null
Math
Easy
93.5
6,800
6,356
58
256
null
null
2,384
Root Equals Sum of Children
root-equals-sum-of-children
You are given the root of a binary tree that consists of exactly 3 nodes: the root, its left child, and its right child. Return true if the value of the root is equal to the sum of the values of its two children, or false otherwise.
Tree,Binary Tree
Easy
91.4
4,698
4,294
56
103
null
null
2,385
Count Positions on Street With Required Brightness
count-positions-on-street-with-required-brightness
null
null
Medium
74.6
394
294
9
2
How can we find the brightness at every position on the street? We can use a hash table to store the change in brightness from the previous position to the current position.
370,2075